MLIB
Loading...
Searching...
No Matches
statpars.h
Go to the documentation of this file.
1
6#pragma once
7
8#if __has_include("defs.h")
9#include "defs.h"
10#endif
11
12#include <deque>
13
14namespace mlib {
15
18{
19public:
20 statpars (int nmax = 0);
21 statpars (std::vector<double> vec);
22
23 void add (double val);
24 void add (double* vals, int count);
25 void add (std::vector<double> v);
26 void clear ();
27 int count ();
28 double average ();
29 double stdev ();
30 double mad ();
31 double variance ();
32 double skewness ();
33 double kurtosis ();
34
35private:
36 void calculate ();
37 std::deque<double> values;
38 int nmax;
39 bool calc;
40 double sum, adev, var, sdev, skew, kurt;
41};
42
44inline void statpars::add (double val)
45{
46 if (nmax && values.size () == nmax)
47 {
48 sum -= values.front ();
49 values.pop_front ();
50 }
51 values.push_back (val);
52 sum += val;
53 calc = false;
54}
55
57inline void statpars::add (double* vals, int count)
58{
59 for (int i = 0; i < count; i++)
60 add (vals[i]);
61}
62
64inline void statpars::add (std::vector<double> vals)
65{
66 for (double v : vals)
67 add (v);
68}
69
71inline double statpars::average ()
72{
73 if (!values.empty ())
74 return sum / values.size ();
75 else
76 return 0.;
77}
78
80inline double statpars::variance ()
81{
82 if (!calc)
83 calculate ();
84 return var;
85}
86
88inline double statpars::stdev ()
89{
90 if (!calc)
91 calculate ();
92 return sdev;
93}
94
96inline double statpars::mad ()
97{
98 if (!calc)
99 calculate ();
100 return adev;
101}
102
104inline double statpars::skewness ()
105{
106 if (!calc)
107 calculate ();
108 return skew;
109}
110
112inline double statpars::kurtosis ()
113{
114 if (!calc)
115 calculate ();
116 return kurt;
117}
118
120inline int statpars::count ()
121{
122 return (int)values.size ();
123}
124
126inline void statpars::clear ()
127{
128 values.clear ();
129 calc = false;
130 sum = 0.;
131}
132
133} // namespace mlib
Calculator for statistical parameters of a distribution.
Definition statpars.h:18
void clear()
Calculator reset.
Definition statpars.h:126
double variance()
Return distribution's variance (2nd order moment)
Definition statpars.h:80
double average()
Return distribution's average (first order moment)
Definition statpars.h:71
double skewness()
Return skewness (3rd order moment)
Definition statpars.h:104
double mad()
Return average absolute deviation also called "mean absolute deviation" (MAD)
Definition statpars.h:96
void add(double val)
Add a value to the calculator.
Definition statpars.h:44
double stdev()
Return standard deviation (square root of variance)
Definition statpars.h:88
int count()
Return number of samples.
Definition statpars.h:120
double kurtosis()
Return kurtosis (4th order moment)
Definition statpars.h:112