Plaquette
 
Loading...
Searching...
No Matches
MovingStats.h
1/*
2 * MovingStats.h
3 *
4 * Computes floating-point mean and variance statistics over time using an exponential moving average.
5 * This class computes the following statistics: mean, variance and standard deviation.
6 *
7 * (c) 2014 Sofian Audry -- info(@)sofianaudry(.)com
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#ifndef MOVINGSTATS_H_
24#define MOVINGSTATS_H_
25
26#include "MovingAverage.h"
27
28namespace pq {
29
31public:
32 // Moving average over values (ie. mean).
33 MovingAverage _avg;
34
35 // Moving average of variance.
36 float _mean2;
37
40
43
44 virtual ~MovingStats() {}
45
48
50 void timeWindow(float seconds);
51
53 float timeWindow() const { return _avg.timeWindow(); }
54
56 bool timeWindowIsInfinite() const { return _avg.timeWindowIsInfinite(); }
57
58 // Keep this commented out to prevent confusion with MovingFilter::cutoff().
59 //
60 // /// Changes the smoothing window cutoff frequency (expressed in Hz).
61 // void cutoff(float hz);
62
63 // /// Returns the smoothing window cutoff frequency (expressed in Hz).
64 // float cutoff() const { return _avg.cutoff(); }
65
67 virtual void reset();
68
70 virtual float update(float value, float sampleRate=1);
71
73 virtual float mean() const { return _avg.constGet(); }
74
76 virtual float var() const { return (_mean2 - sq(mean())); }
77
79 virtual float stdDev() const;
80
82 virtual float normalize(float value) const;
83
85 virtual float normalize(float value, float mean, float stdDev) const;
86
93 virtual bool isOutlier(float value, float nStdDev=1.5f) const;
94
101 virtual bool isLowOutlier(float value, float nStdDev=1.5f) const;
102
109 virtual bool isHighOutlier(float value, float nStdDev=1.5f) const;
110
113 virtual float stddev() const { return stdDev(); }
114};
115
116} // namespace pq
117
118#endif /* MOVINGSTATS_H_ */
An exponential moving average class.
Definition MovingAverage.h:35
bool timeWindowIsInfinite() const
Returns true if time window is infinite.
Definition MovingAverage.cpp:64
void timeWindow(float seconds)
Changes the smoothing window (expressed in seconds).
Definition MovingAverage.cpp:48
void infiniteTimeWindow()
Sets to "infinite" smoothing window.
Definition MovingAverage.cpp:44
Definition MovingStats.h:30
virtual float normalize(float value) const
Returns the normalized value according N(0, 1).
Definition MovingStats.cpp:73
virtual float stdDev() const
Returns the standard deviation of the samples.
Definition MovingStats.cpp:63
virtual bool isOutlier(float value, float nStdDev=1.5f) const
Returns true if the value is considered an outlier.
Definition MovingStats.cpp:88
bool timeWindowIsInfinite() const
Returns true if time window is infinite.
Definition MovingStats.h:56
virtual bool isHighOutlier(float value, float nStdDev=1.5f) const
Returns true if the value is considered a high outlier (above average).
Definition MovingStats.cpp:97
float timeWindow() const
Returns the smoothing window (expressed in seconds).
Definition MovingStats.h:53
virtual void reset()
Resets the statistics.
Definition MovingStats.cpp:44
void infiniteTimeWindow()
Sets to "infinite" smoothing window.
Definition MovingStats.h:47
virtual float update(float value, float sampleRate=1)
Adds a value to the statistics (returns the mean).
Definition MovingStats.cpp:49
virtual float var() const
Returns an exponential moving variance of the samples.
Definition MovingStats.h:76
MovingStats()
Default constructor (infinite time window).
Definition MovingStats.cpp:32
virtual float mean() const
Returns an exponential moving average of the samples.
Definition MovingStats.h:73
virtual bool isLowOutlier(float value, float nStdDev=1.5f) const
Returns true if the value is considered a low outlier (below average).
Definition MovingStats.cpp:93
virtual float stddev() const
Definition MovingStats.h:113