-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStatistics.cpp
122 lines (97 loc) · 2.84 KB
/
Statistics.cpp
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
//============================================================================
// Name : Statistics.cpp
// Author : Arslan Saleem
//============================================================================
#include <iostream>
#include <cmath>
#include <ctime>
#include <boost/circular_buffer.hpp>
using namespace std;
class RunningStatsCalculator {
private:
long int count = 0; // long in case of window size not specified
float runningMean = 0.f;
float dSquaredValue = 0.f;
public:
RunningStatsCalculator() {
}
void update(float newValue) {
count++;
float meanDifferential = (newValue - runningMean) / count;
float newMean = runningMean + meanDifferential;
float dSquaredIncrement = (newValue - newMean) * (newValue - runningMean);
dSquaredValue += dSquaredIncrement;
runningMean = newMean;
}
float mean() {
return validate()? runningMean: 0.f;
}
float populationVariance() {
return dSquaredValue / count;
}
float populationStdev() {
return sqrt(populationVariance());
}
float sampleVariance() {
return count > 1 ? dSquaredValue / (count - 1) : 0;
}
float sampleStdev() {
return sqrt(sampleVariance());
}
bool validate() {
return count==0? false: true;
}
};
class RunningWindowStatsCalculator {
private:
long int count = 0; // long in case of window size not specified
float runningMean = 0.f;
float dSquaredValue = 0.f;
float windowSize = 0.f;
boost::circular_buffer<float> circularBuffer;
public:
RunningWindowStatsCalculator(int winSize) {
circularBuffer = boost::circular_buffer<float>(windowSize);
windowSize = winSize;
}
void update(float newValue) {
if (count!=windowSize){count++;}
float poppedValue = circularBuffer.front();
float meanDifferential = (newValue - poppedValue) / count;
float newMean = runningMean + meanDifferential;
float dSquaredIncrement = ((newValue - poppedValue)*(newValue - newMean + poppedValue - runningMean));
dSquaredValue += dSquaredIncrement;
runningMean = newMean;
circularBuffer.push_back(newValue);
}
float mean() {
return validate()? runningMean: 0.f;
}
float populationVariance() {
return dSquaredValue / count;
}
float populationStdev() {
return sqrt(populationVariance());
}
float sampleVariance() {
return count > 1 ? dSquaredValue / (count - 1) : 0;
}
float sampleStdev() {
return sqrt(sampleVariance());
}
bool validate() {
return count==0? false: true;
}
};
int main() {
RunningStatsCalculator running = RunningStatsCalculator();
running.update(1);
running.update(2);
running.update(3);
cout<<"running mean = "<< running.mean()<<"\n";
cout<<"running pop variance = "<< running.populationVariance()<<"\n";
cout<<"running pop stdev = "<< running.populationStdev()<<"\n";
cout<<"running sample variance = "<< running.sampleVariance()<<"\n";
cout<<"running sample stdev = "<< running.sampleStdev()<<"\n";
return 0;
}