-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathIntStatistics.h
More file actions
112 lines (93 loc) · 3.78 KB
/
IntStatistics.h
File metadata and controls
112 lines (93 loc) · 3.78 KB
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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Statistics Library
Author: Scott Daniels
Website: http://provideyourown.com
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
This library uses some 'tricks' in order to perform statistical
analysis without storing each data point - thus making it micro-
friendly. To save even more memory there are two versions - a floating
point and an integer version. The integer version saves about 200
bytes of SRAM if you don't require floating point in your code.
In order to avoid storing individual data points, a running total
is stored instead. When creating the Statistics object, the sample
size is chosen. When data overruns this sample size, older values are
sloughed off the total. The way this is done is by sloughing off the
average of all the points collected.
In a similar way, the variance is stored (from which std deviation
is calculated). Normally, the variance is the sum of the squares of
the difference between the data points and the mean. Since the mean
is not yet known, the square of the actual data point is used instead.
Then, when it is time to calculate the variance, the mean squared times
the number of data points is subtracted from the variance total. This
turns out to be the true variance by virtue of associative principle.
*/
#ifndef IntStatistics_h
#define IntStatistics_h
#include <limits.h>
#define sqr(x) ((x)*(x))
class IntStatistics {
public:
IntStatistics(int numSamples) : mNumSamples(numSamples), mCurrNumSamples(0), mTotal(0), mRefVariance(0), mMin(INT_MAX), mMax(INT_MIN) {}
void setNewSampleSize(int numSamples)
{
mNumSamples = numSamples;
reset();
}
void reset()
{
mCurrNumSamples = 0;
mTotal = 0;
mRefVariance = 0;
mMin = INT_MAX;
mMax = INT_MIN;
}
void addData(long val)
{
if (mCurrNumSamples >= mNumSamples) // reduce samples total by one sample value
{
mTotal = (mTotal * (mNumSamples - 1)) / mNumSamples;
mRefVariance = mRefVariance * (mNumSamples - 1) / mNumSamples;
}
else
mCurrNumSamples++; // increment the current number
mTotal += val;
mRefVariance += val*val; // add the square of val (referenced to zero)
if (val > mMax)
mMax = val;
if (val < mMin)
mMin = val;
}
long mean() const { return mTotal / mCurrNumSamples; }
long variance() const { return (mRefVariance - (mCurrNumSamples * sqr(mean()))) / mCurrNumSamples; }
// long stdDeviation() const { return sqrt(variance()); }
long maxVal() const { return mMax; }
long minVal() const { return mMin; }
long total() const { return mTotal; }
unsigned int samples() const { return mCurrNumSamples; }
unsigned int sampleSize() const { return mNumSamples; }
protected:
unsigned int mNumSamples;
unsigned int mCurrNumSamples;
long mTotal;
unsigned long mRefVariance; // not true variance - it must be calculated
int mMin;
int mMax;
};
#endif // #define IntStatistics_h