1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
#include "fixed_bin_histogram.h"
#include "auto_histogram.h"
#include <library/cpp/histogram/adaptive/protos/histo.pb.h>
#include <util/generic/algorithm.h>
#include <util/generic/yexception.h>
#include <util/generic/ymath.h>
#include <util/string/printf.h>
namespace NKiwiAggr {
TFixedBinHistogram::TFixedBinHistogram(size_t intervals, ui64 id, size_t trainingSetSize)
: TrainingSetSize(trainingSetSize)
, IsInitialized(false)
, IsEmpty(true)
, Id(id)
, Sum(0.0)
, Freqs(0)
, ReserveFreqs(0)
, BinRange(0.0)
, Intervals(intervals)
, BaseIndex(intervals / 2)
{
}
TFixedBinHistogram::TFixedBinHistogram(const THistogram& histo, size_t defaultIntervals, ui64 defaultId, size_t trainingSetSize)
: TrainingSetSize(trainingSetSize)
, IsInitialized(false)
, IsEmpty(true)
, Id(defaultId)
, Sum(0.0)
, Freqs(0)
, ReserveFreqs(0)
, BinRange(0.0)
, Intervals(defaultIntervals)
, BaseIndex(defaultIntervals / 2)
{
FromProto(histo);
}
TFixedBinHistogram::TFixedBinHistogram(IHistogram* histo, size_t defaultIntervals, ui64 defaultId, size_t trainingSetSize)
: TrainingSetSize(trainingSetSize)
, IsInitialized(false)
, IsEmpty(true)
, Id(defaultId)
, Sum(0.0)
, Freqs(0)
, ReserveFreqs(0)
, BinRange(0.0)
, Intervals(defaultIntervals)
, BaseIndex(defaultIntervals / 2)
{
TFixedBinHistogram* fixedBinHisto = dynamic_cast<TFixedBinHistogram*>(histo);
if (!fixedBinHisto) {
FromIHistogram(histo);
return;
}
fixedBinHisto->Initialize();
TrainingSetSize = fixedBinHisto->TrainingSetSize;
IsInitialized = fixedBinHisto->IsInitialized;
IsEmpty = fixedBinHisto->IsEmpty;
Id = fixedBinHisto->Id;
MinValue = fixedBinHisto->MinValue;
MaxValue = fixedBinHisto->MaxValue;
Sum = fixedBinHisto->Sum;
Freqs.assign(fixedBinHisto->Freqs.begin(), fixedBinHisto->Freqs.end());
ReserveFreqs.assign(fixedBinHisto->ReserveFreqs.begin(), fixedBinHisto->ReserveFreqs.end());
ReferencePoint = fixedBinHisto->ReferencePoint;
BinRange = fixedBinHisto->BinRange;
Intervals = fixedBinHisto->Intervals;
FirstUsedBin = fixedBinHisto->FirstUsedBin;
LastUsedBin = fixedBinHisto->LastUsedBin;
BaseIndex = fixedBinHisto->BaseIndex;
}
void TFixedBinHistogram::Clear() {
TrainingSet.Destroy();
IsInitialized = false;
IsEmpty = true;
Sum = 0.0;
Freqs.clear();
ReserveFreqs.clear();
BinRange = 0.0;
}
void TFixedBinHistogram::Add(const THistoRec& rec) {
if (!rec.HasId() || rec.GetId() == Id) {
Add(rec.GetValue(), rec.GetWeight());
}
}
void TFixedBinHistogram::Add(double value, double weight) {
if (!IsValidFloat(value) || !IsValidFloat(weight)) {
ythrow yexception() << Sprintf("Histogram id %lu: bad value %f weight %f", Id, value, weight);
}
if (weight <= 0.0) {
return; // all zero-weighted values should be skipped because they don't affect the distribution, negative weights are forbidden
}
Sum += weight;
if (!IsInitialized) {
if (!TrainingSet) {
TrainingSet.Reset(new TVector<TWeightedValue>(0));
}
TrainingSet->push_back(TWeightedValue(value, weight));
if (TrainingSet->size() >= TrainingSetSize) {
Initialize();
}
return;
}
i32 bin = CalcBin(value);
if (bin < 0 || bin >= (i32)Freqs.size() || (BinRange == 0.0 && value != ReferencePoint)) {
Shrink(Min(value, MinValue), Max(value, MaxValue));
Freqs[CalcBin(value)] += weight;
} else {
MinValue = Min(value, MinValue);
MaxValue = Max(value, MaxValue);
FirstUsedBin = Min(FirstUsedBin, bin);
LastUsedBin = Max(LastUsedBin, bin);
Freqs[bin] += weight;
}
}
void TFixedBinHistogram::Merge(const THistogram& /*histo*/, double /*multiplier*/) {
ythrow yexception() << "Method is not implemented for TFixedBinHistogram";
}
void TFixedBinHistogram::Merge(const TVector<THistogram>& histogramsToMerge) {
TVector<IHistogramPtr> parsedHistogramsToMerge;
for (size_t i = 0; i < histogramsToMerge.size(); ++i) {
parsedHistogramsToMerge.push_back(IHistogramPtr(new TAutoHistogram(histogramsToMerge[i], Intervals, Id)));
}
Merge(parsedHistogramsToMerge);
}
void TFixedBinHistogram::Merge(TVector<IHistogramPtr> histogramsToMerge) {
TVector<IHistogramPtr> histogramsToMergeRepacked(0);
TVector<TFixedBinHistogram*> histograms(0);
// put current histogram to the vector of histograms to merge and clear self
if (!Empty()) {
histogramsToMergeRepacked.push_back(IHistogramPtr(new TFixedBinHistogram(this, Intervals, Id, TrainingSetSize)));
histograms.push_back(dynamic_cast<TFixedBinHistogram*>(histogramsToMergeRepacked.back().Get()));
}
Clear();
for (size_t i = 0; i < histogramsToMerge.size(); ++i) {
if (!histogramsToMerge[i] || histogramsToMerge[i]->Empty()) {
continue;
}
TFixedBinHistogram* fixedBinHisto = dynamic_cast<TFixedBinHistogram*>(histogramsToMerge[i].Get());
if (fixedBinHisto) {
fixedBinHisto->Initialize();
histogramsToMergeRepacked.push_back(histogramsToMerge[i]);
} else {
histogramsToMergeRepacked.push_back(IHistogramPtr(new TFixedBinHistogram(histogramsToMerge[i].Get(), Intervals, Id, TrainingSetSize))); // Convert histograms that are not of TFixedBinHistogram type
}
histograms.push_back(dynamic_cast<TFixedBinHistogram*>(histogramsToMergeRepacked.back().Get()));
}
if (histograms.size() == 0) {
return;
}
double minValue = histograms[0]->MinValue;
double maxValue = histograms[0]->MaxValue;
Sum = histograms[0]->Sum;
for (size_t i = 1; i < histograms.size(); ++i) {
minValue = Min(minValue, histograms[i]->MinValue);
maxValue = Max(maxValue, histograms[i]->MaxValue);
Sum += histograms[i]->Sum;
}
SetFrame(minValue, maxValue, true);
if (BinRange == 0.0) {
Freqs[BaseIndex] = Sum;
return;
}
for (size_t histoIndex = 0; histoIndex < histograms.size(); ++histoIndex) {
TFixedBinHistogram* histo = histograms[histoIndex];
for (i32 bin = histo->FirstUsedBin; bin <= histo->LastUsedBin; ++bin) {
double binStart = histo->BinStart(bin);
double binEnd = histo->BinEnd(bin);
double freq = histo->Freqs[bin];
if (binStart == binEnd) {
Freqs[CalcBin(binStart)] += freq;
continue;
}
size_t firstCross = CalcBin(binStart);
size_t lastCross = CalcBin(binEnd);
for (size_t i = firstCross; i <= lastCross; ++i) {
double mergedBinStart = BinStart(i);
double mergedBinEnd = BinEnd(i);
double crossStart = Max(mergedBinStart, binStart);
double crossEnd = Min(mergedBinEnd, binEnd);
if (binStart == binEnd) {
}
Freqs[i] += freq * (crossEnd - crossStart) / (binEnd - binStart);
}
}
}
}
void TFixedBinHistogram::Multiply(double factor) {
if (!IsValidFloat(factor) || factor <= 0) {
ythrow yexception() << "Not valid factor in IHistogram::Multiply(): " << factor;
}
if (!IsInitialized) {
Initialize();
}
Sum *= factor;
for (i32 i = FirstUsedBin; i <= LastUsedBin; ++i) {
Freqs[i] *= factor;
}
}
void TFixedBinHistogram::FromProto(const THistogram& histo) {
if (histo.HasType() && histo.GetType() != HT_FIXED_BIN_HISTOGRAM) {
ythrow yexception() << "Attempt to parse TFixedBinHistogram from THistogram protobuf record of wrong type = " << (ui32)histo.GetType();
}
TrainingSet.Destroy();
IsInitialized = false;
Sum = 0.0;
Id = histo.GetId();
size_t intervals = histo.FreqSize();
if (intervals == 0) {
IsEmpty = true;
return;
}
Intervals = intervals;
TrainingSetSize = Intervals;
BaseIndex = Intervals / 2;
if (!IsValidFloat(histo.GetMinValue()) || !IsValidFloat(histo.GetMaxValue()) || !IsValidFloat(histo.GetBinRange())) {
ythrow yexception() << Sprintf("FromProto in histogram id %lu: skip bad histo with minvalue %f maxvalue %f binrange %f", Id, histo.GetMinValue(), histo.GetMaxValue(), histo.GetBinRange());
}
double minValue = histo.GetMinValue();
double binRange = histo.GetBinRange();
double maxValue = histo.HasMaxValue() ? histo.GetMaxValue() : minValue + binRange * Intervals;
SetFrame(minValue, maxValue, true);
BinRange = binRange;
for (i32 i = FirstUsedBin; i <= LastUsedBin; ++i) {
Freqs[i] = histo.GetFreq(i - BaseIndex);
if (!IsValidFloat(Freqs[i])) {
ythrow yexception() << Sprintf("FromProto in histogram id %lu: bad value %f", Id, Freqs[i]);
}
Sum += Freqs[i];
}
}
void TFixedBinHistogram::ToProto(THistogram& histo) {
histo.Clear();
if (!IsInitialized) {
Initialize();
}
histo.SetType(HT_FIXED_BIN_HISTOGRAM);
histo.SetId(Id);
if (IsEmpty) {
return;
}
if (FirstUsedBin < (i32)BaseIndex || (LastUsedBin - FirstUsedBin + 1) > (i32)Intervals) {
Shrink(MinValue, MaxValue);
}
histo.SetMinValue(MinValue);
histo.SetMaxValue(MaxValue);
histo.SetBinRange(BinRange);
for (ui32 i = BaseIndex; i < BaseIndex + Intervals; ++i) {
histo.AddFreq(Freqs[i]);
}
}
void TFixedBinHistogram::SetId(ui64 id) {
Id = id;
}
ui64 TFixedBinHistogram::GetId() {
return Id;
}
bool TFixedBinHistogram::Empty() {
if (!IsInitialized) {
Initialize();
}
return IsEmpty;
}
double TFixedBinHistogram::GetMinValue() {
if (!IsInitialized) {
Initialize();
}
return MinValue;
}
double TFixedBinHistogram::GetMaxValue() {
if (!IsInitialized) {
Initialize();
}
return MaxValue;
}
double TFixedBinHistogram::GetSum() {
return Sum;
}
double TFixedBinHistogram::GetSumInRange(double leftBound, double rightBound) {
if (!IsInitialized) {
Initialize();
}
if (leftBound > rightBound) {
return 0.0;
}
return GetSumAboveBound(leftBound) + GetSumBelowBound(rightBound) - Sum;
}
double TFixedBinHistogram::GetSumAboveBound(double bound) {
if (!IsInitialized) {
Initialize();
}
if (IsEmpty) {
return 0.0;
}
if (BinRange == 0.0) { // special case - all values added to histogram are the same
return (bound <= ReferencePoint) ? Sum : 0.0;
}
i32 bin = CalcBin(bound);
if (bin < FirstUsedBin) {
return Sum;
}
if (bin > LastUsedBin) {
return 0.0;
}
double binStart = BinStart(bin);
double binEnd = BinEnd(bin);
double result = (bound < binStart) ? Freqs[bin] : Freqs[bin] * (binEnd - bound) / (binEnd - binStart);
for (i32 i = bin + 1; i <= LastUsedBin; ++i) {
result += Freqs[i];
}
return result;
}
double TFixedBinHistogram::GetSumBelowBound(double bound) {
if (!IsInitialized) {
Initialize();
}
if (IsEmpty) {
return 0.0;
}
if (BinRange == 0.0) { // special case - all values added to histogram are the same
return (bound > ReferencePoint) ? Sum : 0.0;
}
i32 bin = CalcBin(bound);
if (bin < FirstUsedBin) {
return 0.0;
}
if (bin > LastUsedBin) {
return Sum;
}
double binStart = BinStart(bin);
double binEnd = BinEnd(bin);
double result = (bound > binEnd) ? Freqs[bin] : Freqs[bin] * (bound - binStart) / (binEnd - binStart);
for (i32 i = bin - 1; i >= FirstUsedBin; --i) {
result += Freqs[i];
}
return result;
}
double TFixedBinHistogram::CalcUpperBound(double sum) {
if (!IsInitialized) {
Initialize();
}
if (sum == 0.0) {
return MinValue;
}
if (IsEmpty) {
return MaxValue;
}
i32 currentBin = FirstUsedBin;
double gatheredSum = 0.0;
while (gatheredSum < sum && currentBin <= LastUsedBin) {
gatheredSum += Freqs[currentBin];
++currentBin;
}
--currentBin;
if ((gatheredSum <= sum && currentBin == LastUsedBin) || (Freqs[currentBin] == 0)) {
return MaxValue;
}
double binStart = BinStart(currentBin);
double binEnd = BinEnd(currentBin);
return binEnd - (binEnd - binStart) * (gatheredSum - sum) / Freqs[currentBin];
}
double TFixedBinHistogram::CalcLowerBound(double sum) {
if (!IsInitialized) {
Initialize();
}
if (sum == 0.0) {
return MaxValue;
}
if (IsEmpty) {
return MinValue;
}
i32 currentBin = LastUsedBin;
double gatheredSum = 0.0;
while (gatheredSum < sum && currentBin >= FirstUsedBin) {
gatheredSum += Freqs[currentBin];
--currentBin;
}
++currentBin;
if ((gatheredSum <= sum && currentBin == FirstUsedBin) || (Freqs[currentBin] == 0)) {
return MinValue;
}
double binStart = BinStart(currentBin);
double binEnd = BinEnd(currentBin);
return binStart + (binEnd - binStart) * (gatheredSum - sum) / Freqs[currentBin];
}
double TFixedBinHistogram::CalcUpperBoundSafe(double sum) {
if (!Empty()) {
sum = Max(Freqs[FirstUsedBin], sum);
}
return CalcUpperBound(sum);
}
double TFixedBinHistogram::CalcLowerBoundSafe(double sum) {
if (!Empty()) {
sum = Max(Freqs[LastUsedBin], sum);
}
return CalcLowerBound(sum);
}
double TFixedBinHistogram::CalcBinRange(double referencePoint, double maxValue) {
return (maxValue - referencePoint) / ((double)Intervals - 0.02);
}
void TFixedBinHistogram::SetFrame(double minValue, double maxValue, bool clear) {
MinValue = minValue;
MaxValue = maxValue;
ReferencePoint = MinValue;
BinRange = CalcBinRange(ReferencePoint, MaxValue);
FirstUsedBin = BaseIndex;
LastUsedBin = (BinRange == 0.0) ? BaseIndex : BaseIndex + Intervals - 1;
if (clear) {
Freqs.assign(2 * Intervals, 0.0);
ReserveFreqs.assign(2 * Intervals, 0.0);
IsEmpty = false;
IsInitialized = true;
}
}
void TFixedBinHistogram::FromIHistogram(IHistogram* histo) {
if (!histo) {
ythrow yexception() << "Attempt to create TFixedBinFistogram from a NULL pointer";
}
Id = histo->GetId();
if (histo->Empty()) {
IsInitialized = false;
IsEmpty = true;
return;
}
SetFrame(histo->GetMinValue(), histo->GetMaxValue(), true);
Sum = histo->GetSum();
if (BinRange == 0.0) {
Freqs[BaseIndex] = Sum;
return;
}
for (i32 i = FirstUsedBin; i <= LastUsedBin; ++i) {
Freqs[i] = histo->GetSumInRange(BinStart(i), BinEnd(i));
}
return;
}
void TFixedBinHistogram::Initialize() {
if (IsInitialized) {
return;
}
if (!TrainingSet || TrainingSet->size() == 0) {
IsEmpty = true;
return;
}
SetFrame(MinElement(TrainingSet->begin(), TrainingSet->end(), CompareWeightedValue)->first,
MaxElement(TrainingSet->begin(), TrainingSet->end(), CompareWeightedValue)->first, true);
for (TVector<TWeightedValue>::const_iterator it = TrainingSet->begin(); it != TrainingSet->end(); ++it) {
Freqs[CalcBin(it->first)] += it->second;
}
TrainingSet.Destroy();
}
i32 TFixedBinHistogram::CalcBin(double value) {
return (BinRange == 0.0) ? BaseIndex : static_cast<i32>(BaseIndex + (value - ReferencePoint) / BinRange);
}
double TFixedBinHistogram::CalcDensity(double value) {
i32 bin = CalcBin(value);
if (bin < 0 || bin >= (i32)Freqs.size() || BinRange == 0.0 || GetSum() == 0) {
return 0.0;
}
return Freqs[bin] / GetSum() / BinRange;
}
double TFixedBinHistogram::BinStart(i32 i) {
return Max(ReferencePoint + (i - BaseIndex) * BinRange, MinValue);
}
double TFixedBinHistogram::BinEnd(i32 i) {
return Min(ReferencePoint + (i + 1 - BaseIndex) * BinRange, MaxValue);
}
void TFixedBinHistogram::Shrink(double newReferencePoint, double newMaxValue) {
Y_VERIFY(newReferencePoint < newMaxValue, "Invalid Shrink()");
memset(&(ReserveFreqs[0]), 0, ReserveFreqs.size() * sizeof(double));
double newBinRange = CalcBinRange(newReferencePoint, newMaxValue);
for (i32 i = FirstUsedBin; i <= LastUsedBin; ++i) {
double binStart = BinStart(i);
double binEnd = BinEnd(i);
double freq = Freqs[i];
i32 firstCross = static_cast<i32>(BaseIndex + (binStart - newReferencePoint) / newBinRange);
i32 lastCross = static_cast<i32>(BaseIndex + (binEnd - newReferencePoint) / newBinRange);
for (i32 j = firstCross; j <= lastCross; ++j) {
double newBinStart = newReferencePoint + (j - BaseIndex) * newBinRange;
double newBinEnd = newReferencePoint + (j + 1 - BaseIndex) * newBinRange;
double crossStart = Max(newBinStart, binStart);
double crossEnd = Min(newBinEnd, binEnd);
ReserveFreqs[j] += (binStart == binEnd) ? freq : freq * (crossEnd - crossStart) / (binEnd - binStart);
}
}
Freqs.swap(ReserveFreqs);
SetFrame(newReferencePoint, newMaxValue, false);
}
}
|