-
Notifications
You must be signed in to change notification settings - Fork 0
/
libEDM_histogram.h
445 lines (347 loc) · 14.1 KB
/
libEDM_histogram.h
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
#pragma once
#include "libEDM_library.h"
//
// class Histogram
//
struct DataType
{
long bin;
size_t numSamples;
double pdf;
double cdf;
double inverseCDF() {return 1.0 - cdf;}
};
typedef vector<DataType> HistogramData;
template <class Type>
class Histogram {
public:
const string title;
const string columnLabel;
const string defaultFilename;
size_t count() const {return _count;}
Type sum() const {return _sum;}
Type sumOfSquares() const {return _sumOfSquares;}
Type high() const {return _low + _histogram.size() * interval;}
Type interval() const {return _interval;}
Type max() const {return _max;}
Type min() const {return _min;}
double mean() const {return _count == 0 ? 0.0 : _sum / _count;}
double meanSquare() const {return _count == 0 ? 0.0 : _sumOfSquares / _count;}
double stdDev() const {return sqrt(variance());}
double variance() const {return meanSquare() - sqr(mean());}
// constructors
Histogram (const string &title, const string &columnLabel, Type interval, size_t maxBins, const string &defaultFilename)
: title(title), columnLabel(columnLabel), _defaultInterval(interval), _maxBins(maxBins), defaultFilename(defaultFilename) {reset();}
Type getPercentile (double percentile) const;
void reset ();
// print methods
void print (ostream &file, const Type startFrom = most_negative<Type>(), const Type stopAt = numeric_limits<Type>::infinity(), const bool suppressEmptyBins = false) const;
void print (const char *fileName = NULL, const Type startFrom = most_negative<Type>(), const Type stopAt = numeric_limits<Type>::infinity(), const bool suppressEmptyBins = false) const;
void print (const string &fileName, const Type startFrom = most_negative<Type>(), const Type stopAt = numeric_limits<Type>::infinity(), const bool suppressEmptyBins = false) const {print(fileName.c_str(), startFrom, stopAt, suppressEmptyBins);}
void print (const fs::path &file, const Type startFrom = most_negative<Type>(), const Type stopAt = numeric_limits<Type>::infinity(), const bool suppressEmptyBins = false) const {print(file.string(), startFrom, stopAt, suppressEmptyBins);}
void print ( const Type startFrom , const Type stopAt = numeric_limits<Type>::infinity(), const bool suppressEmptyBins = false) const {print(NULL , startFrom, stopAt, suppressEmptyBins);}
// pure virtual functions
// these stop the Histogram class being instantiate directly
virtual void update(const Type sample) = 0;
protected:
const Type _defaultInterval;
size_t _count, _maxBins, _positiveInfinities, _negativeInfinities;
Type _interval, _low, _max, _min;
double _sum, _sumOfSquares;
vector<size_t> _histogram;
long which_bin (const Type sample) const;
Type binCentre (const long bin) const;
HistogramData get_data (const bool suppressEmptyBins = false) const;
void update_histogram (const Type sample);
virtual void print_footer(ostream &file) const {};
};
template <class Type>
void Histogram<Type>::update(const Type sample)
{
_count++;
if (sample < _min)
_min = sample;
if (sample > _max)
_max = sample;
_sum += static_cast<double>(sample);
_sumOfSquares += sqr(static_cast<double>(sample));
}
template <class Type>
HistogramData Histogram<Type>::get_data (const bool suppressEmptyBins) const
{
HistogramData data;
if ( _negativeInfinities > 0 )
{
const double value = divide(_negativeInfinities, _count);
DataType binData;
binData.bin = numeric_limits<long>::min();
binData.numSamples = _negativeInfinities;
binData.pdf = value;
binData.cdf = value;
data.push_back(binData);
}
size_t cumulative = _negativeInfinities;
long bin = 0;
// skip bins with no hits
while ( _histogram[bin] == 0 )
bin++;
if ( !suppressEmptyBins && _negativeInfinities == 0 && !(numeric_limits<Type>::is_modulo && binCentre(bin) == numeric_limits<Type>::min()) )
{
// store bin before first bin to have hits unless Type is wraparound and bin centre of lowest bin with hits is equal to minimum value
DataType binData;
binData.bin = bin - 1;
binData.numSamples = 0;
binData.pdf = 0.0;
binData.cdf = 0.0;
data.push_back(binData);
}
do
{
cumulative += _histogram[bin];
if ( !suppressEmptyBins || _histogram[bin] > 0 )
{
DataType binData;
binData.bin = bin;
binData.numSamples = _histogram[bin];
binData.pdf = divide(_histogram[bin], _count);
binData.cdf = divide(cumulative, _count);
data.push_back(binData);
}
bin++;
} while (cumulative + _positiveInfinities < _count);
if ( _positiveInfinities > 0 )
{
DataType binData;
binData.bin = numeric_limits<long>::max();
binData.numSamples = _positiveInfinities;
binData.pdf = divide(_positiveInfinities, _count);
binData.cdf = 1.0;
data.push_back(binData);
}
assert ( cumulative + _positiveInfinities == _count );
return data;
}
template <class Type>
Type Histogram<Type>::getPercentile (double percentile) const
{
percentile *= 0.01;
percentile = std::min(percentile, 1.0);
percentile = std::max(percentile, 0.0);
HistogramData data = this->get_data(true);
for (size_t i=0; i < data.size(); i++)
if ( data[i].cdf > percentile )
return binCentre(data[i].bin);
// should only get to this point if percentile = 100.0
assert ( percentile == 1.0 );
// percentile not found, so return last bin value
return binCentre(data.back().bin);
}
template <class Type>
void Histogram<Type>::print(ostream &file, const Type startFrom, const Type stopAt, const bool suppressEmptyBins) const
{
const size_t numColumns = 5;
if ( ! file )
// file not open
error("File not open");
file << title.c_str() << endl;
if ( _count == 0 )
{
file << "No data" << endl;
return;
}
HistogramData data = this->get_data(suppressEmptyBins);
file << "Number of samples";
for (size_t i=1; i<numColumns; i++)
file << ", " << _count;
file << endl;
file << "Mean Value";
for (size_t i=1; i<numColumns; i++)
file << ", " << scientific << mean();
file << endl;
file << "Std. Dev.";
for (size_t i=1; i<numColumns; i++)
file << ", " << scientific << stdDev();
file << endl;
file << endl;
this->print_footer(file);
// print colums labels
file << ", Num Samples, PDF, CDF, 1 - CDF" << endl;
for (size_t i = 1; i < numColumns; i++)
file << ", " << columnLabel;
file << endl;
const int startBin = which_bin(startFrom);
const int stopBin = which_bin(stopAt);
// print entries from startFrom value up to first data entry
if ( startFrom > most_negative<Type>() )
{
for (int bin = startBin; bin < data[0].bin; bin++)
{
file << setw(8) << fixed << setprecision(2) << binCentre(bin) << ",";
file << setw(8) << fixed << 0 << ", ";
file << setw(9) << setprecision(4) << 0.0 << ",";
file << setw(9) << setprecision(4) << 0.0 << ",";
file << setw(9) << setprecision(4) << 1.0 << endl;
}
}
for (size_t i=0; i<data.size(); i++)
{
// don't print values where x is less than startFrom value
if ( data[i].bin < startBin || data[i].bin > stopBin )
continue;
if ( data[i].bin == numeric_limits<long>::min() )
file << " -1.#INF,";
else if ( data[i].bin == numeric_limits<long>::max() )
file << " 1.#INF,";
else
file << setw(8) << fixed << setprecision(2) << binCentre(data[i].bin) << ",";
file << setw(9) << fixed << data[i].numSamples << ",";
file << setw(9) << setprecision(4) << data[i].pdf << ",";
file << setw(9) << setprecision(4) << data[i].cdf << ",";
file << setw(9) << setprecision(4) << data[i].inverseCDF() << endl;
}
file << endl;
file.flush();
}
template <class Type>
void Histogram<Type>::print (const char *inputFilename, Type startFrom, Type stopAt, const bool suppressEmptyBins) const
{
const char *filename = inputFilename == NULL ? defaultFilename.c_str() : inputFilename;
ofstream file(filename);
if ( ! file )
// Cannot open file
error(string("Cannot open file ") + filename);
print(file, startFrom, stopAt, suppressEmptyBins);
file.close();
}
template <class Type>
void Histogram<Type>::reset()
{
_count = 0;
_interval = _defaultInterval;
_low = 0;
_max = most_negative<Type>();
_min = numeric_limits<Type>::max();
_sum = 0.0;
_sumOfSquares = 0.0;
_histogram.clear();
_positiveInfinities = 0;
_negativeInfinities = 0;
// check if file with defaultFilename can be used
if ( defaultFilename != "" )
{
ofstream file(defaultFilename.c_str());
if ( ! file )
// Cannot open file
error(string("Cannot open file ") + defaultFilename.c_str());
else
file.close();
}
}
template <class Type>
Type Histogram<Type>::binCentre (const long bin) const
{
if ( bin == numeric_limits<long>::min() )
return most_negative<Type>();
if ( bin == numeric_limits<long>::max() )
return numeric_limits<Type>::infinity();
return _low + bin * _interval;
}
template <class Type>
long Histogram<Type>::which_bin (const Type sample) const
{
if ( numeric_limits<Type>::has_infinity )
{
// Test whether sample is +ve or -ve infinity
if ( sample == numeric_limits<Type>::infinity() )
return numeric_limits<long>::max();
if ( sample == -numeric_limits<Type>::infinity() )
return numeric_limits<long>::min();
}
return static_cast<long>( floor( (static_cast<double>(sample) - _low) / _interval ));
}
template <class Type>
void Histogram<Type>::update_histogram(const Type sample)
{
if ( numeric_limits<Type>::has_infinity )
{
// Test whether sample is +ve or -ve infinity
// Don't include these values directly in the histogram
if ( sample == numeric_limits<Type>::infinity() )
{
_positiveInfinities++;
return;
}
if ( sample == -numeric_limits<Type>::infinity() )
{
_negativeInfinities++;
return;
}
}
if ( _count == 1 )
// set _low equal to quantised sample value
_low = floor(divide(sample, _interval)) * _interval;
//_low = ceil(divide(sample, _interval)) * _interval;
long bin = which_bin(sample);
if (bin < 0)
{
// insert bins at lower end of histogram
_histogram.insert(_histogram.begin(), abs(bin), 0);
// recalibrate _low
_low = floor(divide(sample, _interval)) * _interval;
//_low = ceil(divide(sample, _interval)) * _interval;
bin = 0;
}
else
if ( static_cast<size_t>(bin) >= _histogram.size() )
_histogram.resize(bin+1, 0);
_histogram[bin]++;
if ( _histogram.size() > _maxBins )
{
if ( _histogram.size() % 2 == 1 )
// make number of bins an even number
_histogram.resize(_histogram.size() + 1, 0);
// merge bins to reduce size
for (size_t i=0; i<_histogram.size()/2; i++)
_histogram[i] = _histogram[2*i] + _histogram[2*i+1];
_histogram.resize(_histogram.size()/2);
_interval *= 2;
}
}
//
// class LinearHistogram
//
template <class Type>
class LinearHistogram : public Histogram<Type> {
public:
void update(const Type sample);
LinearHistogram(const string &title, const string &columnLabel, Type interval, size_t maxBins = uMAX, const string &defaultFilename = "") : Histogram<Type>(title, columnLabel, interval, maxBins, defaultFilename) {}
LinearHistogram(const string &title, const string &columnLabel, Type interval, const string &defaultFilename, size_t maxBins = uMAX) : Histogram<Type>(title, columnLabel, interval, maxBins, defaultFilename) {}
LinearHistogram(const string &title, const string &columnLabel, Type interval, const fs::path &defaultFile, size_t maxBins = uMAX) : Histogram<Type>(title, columnLabel, interval, maxBins, defaultFile.string()) {}
};
template <class Type>
void LinearHistogram<Type>::update(const Type sample)
{
Histogram<Type>::update(sample);
update_histogram(sample);
}
//
// class LogHistogram
//
class LogHistogram : public Histogram<double> {
public:
enum LogType {B, dB, dBm, dBW};
void update(const double sample);
void reset ();
LogHistogram(const string &title, const string &columnLabel, double interval, size_t maxBins = uMAX, LogType logType = dB, const string &defaultFilename = "") : Histogram<double>(title, columnLabel, interval, maxBins, defaultFilename), _logType(logType) {reset();}
LogHistogram(const string &title, const string &columnLabel, double interval, const string &defaultFilename, size_t maxBins = uMAX, LogType logType = dB) : Histogram<double>(title, columnLabel, interval, maxBins, defaultFilename), _logType(logType) {reset();}
LogHistogram(const string &title, const string &columnLabel, double interval, const fs::path &defaultFile, size_t maxBins = uMAX, LogType logType = dB) : Histogram<double>(title, columnLabel, interval, maxBins, defaultFile.string()), _logType(logType) {reset();}
private:
const LogType _logType;
double _dBSum, _dBSumOfSquares;
double dBMean() const {return _count == 0 ? 0.0 : _dBSum / _count;}
double dBMeanSquare() const {return _count == 0 ? 0.0 : _dBSumOfSquares / _count;}
double dBStdDev() const {return sqrt(dBVariance());}
double dBVariance() const {return dBMeanSquare() - sqr(dBMean());}
string dBType() const;
void print_footer(ostream &file) const;
};