-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlineStatistics.cpp
100 lines (86 loc) · 3.28 KB
/
OnlineStatistics.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
#include <sstream>
#include "OnlineStatistics.hpp"
//==============================================================================
// OnlineStatistics constructor; initializes internal state by calling reset()
//==============================================================================
OnlineStatistics::OnlineStatistics()
{
// Reset (initialize) all internal state
reset();
}
//==============================================================================
// OnlineStatistics destructor; does nothing
//==============================================================================
OnlineStatistics::~OnlineStatistics()
{
}
//==============================================================================
// Resets (initializes) internal state
//==============================================================================
void OnlineStatistics::reset()
{
sample_count = 0;
mean = 0.0;
varianceSource = 0.0;
maximum = 0.0;
minimum = 0.0;
}
//==============================================================================
// Recalculates internal state, taking the provided unitless sample into account
//==============================================================================
void OnlineStatistics::update(double sample)
{
// We've been given a new sample so increment the sample counter
++sample_count;
// Update statistics
// We need to retain the old mean for the variance calculation below
double last_mean = mean;
// Update the mean
mean += (sample - mean) / sample_count;
// Update the variance
varianceSource += (sample - mean) * (sample - last_mean);
// If this sample is the only sample we've ever received then it must be
// both the smallest and largest sample ever received
if (sample_count == 1)
{
maximum = sample;
minimum = sample;
}
else
{
// Is this sample larger than any sample yet received?
if (sample > maximum)
{
maximum = sample;
}
// Is this sample smaller than any sample yet received?
if (sample < minimum)
{
minimum = sample;
}
}
}
//==============================================================================
// Prints internal state to the out string. If units are known, units can be
// provided in the units string to be included in the output
//==============================================================================
void
OnlineStatistics::toString(std::string& out, const std::string& units) const
{
std::ostringstream ostr;
ostr << "Samples " << sample_count << "\n"
<< "Mean " << mean << " " << units << "\n"
<< "Stddev " << getStandardDeviation() << " " << units << "\n"
<< "Min " << minimum << " " << units << "\n"
<< "Max " << maximum << " " << units << "\n";
out = ostr.str();
}
//==============================================================================
// Textual output of OnlineStatistics internal state
//==============================================================================
std::ostream& operator<<(std::ostream& os, const OnlineStatistics& obj)
{
std::string out;
obj.toString(out);
return os << out;
}