-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistics.h
executable file
·113 lines (88 loc) · 1.84 KB
/
statistics.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
#ifndef _STATISTICS_H
#define _STATISTICS_H
#include <cmath>
#include <climits>
/**
@author Tian-Li Yu
*/
#ifndef INF
#define INF (1e10)
#endif
class Statistics {
public:
Statistics () {
reset ();
}
void reset () {
precision = 1e-6;
min = INF;
second_min = INF;
max = -INF;
second_max = -INF;
sum = 0.0;
variance = 0.0;
number = 0;
status = true;
}
void record (double value) {
if (status == false)
return;
number++;
sum += value;
variance += value * value;
if (min > value + precision) {
second_min = min;
min = value;
}
if (max < value - precision) {
second_max = max;
max = value;
}
}
/** get the number of samples */
long int getNumber () {
return number;
}
/** get mean */
double getMean () {
return sum / number;
}
/** get variance */
double getVariance () {
double mean = getMean ();
return variance / number - mean * mean;
}
/** get standard deviation */
double getStdev () {
return::sqrt (getVariance ());
}
double getMin () {
return min;
}
double getMax () {
return max;
}
double getSecondMax () {
return second_max;
}
double getSecondMin () {
return second_min;
}
void turnOn () {
status = true;
}
void turnOff () {
status = false;
}
private:
double precision;
double min;
double max;
double second_min;
double second_max;
double sum;
double variance;
long int number;
bool status;
};
#endif