1 | // $Id: Averager.h 197 2004-10-27 19:04:22Z jari $ |
---|
2 | |
---|
3 | #ifndef _theplu_cpptools_averager_ |
---|
4 | #define _theplu_cpptools_averager_ |
---|
5 | |
---|
6 | #include <cmath> |
---|
7 | #include "vector.h" |
---|
8 | |
---|
9 | namespace theplu{ |
---|
10 | namespace statistics{ |
---|
11 | class ostream; |
---|
12 | |
---|
13 | /// |
---|
14 | /// Class to calulate simple (first and second moments) averages. |
---|
15 | /// |
---|
16 | /// @see WeightedAverager |
---|
17 | /// |
---|
18 | class Averager |
---|
19 | { |
---|
20 | public: |
---|
21 | |
---|
22 | /// |
---|
23 | /// Default constructor |
---|
24 | /// |
---|
25 | Averager(void); |
---|
26 | |
---|
27 | /// |
---|
28 | /// Constructor taking sum of \a x, sum of squared x, \a xx, and |
---|
29 | /// number of samples \a n. |
---|
30 | /// |
---|
31 | Averager(const double x, const double xx, const long n); |
---|
32 | |
---|
33 | /// |
---|
34 | /// Copy constructor |
---|
35 | /// |
---|
36 | Averager(const Averager&); |
---|
37 | |
---|
38 | /// |
---|
39 | /// Adding \a n (default=1) number of data point(s) with value \a d. |
---|
40 | /// |
---|
41 | inline void add(const double d,const long n=1) |
---|
42 | {n_+=n; x_+=n*d; xx_+=n*d*d;} |
---|
43 | |
---|
44 | /// |
---|
45 | /// @return Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$ |
---|
46 | /// |
---|
47 | inline double mean(void) const { return n_ ? x_/n_ : 0; } |
---|
48 | |
---|
49 | /// |
---|
50 | /// @return Mean of squared values \f$ \frac{1}{n}\sum x_i^2 \f$. |
---|
51 | /// |
---|
52 | inline double mean_sqr(void) const { return n_ ? xx_/n_ : 0; } |
---|
53 | |
---|
54 | /// |
---|
55 | /// @return Number of data points |
---|
56 | /// |
---|
57 | inline long n(void) const { return n_; } |
---|
58 | |
---|
59 | /// |
---|
60 | /// Rescales the object, \f$ \forall x_i \rightarrow a*x_i\f$, \f$ |
---|
61 | /// \forall x_i^2 \rightarrow a^2*x_i^2 \f$ |
---|
62 | /// |
---|
63 | inline void rescale(double a) { x_*=a; xx_*=a*a; } |
---|
64 | |
---|
65 | /// |
---|
66 | /// Resets everything to zero |
---|
67 | /// |
---|
68 | inline void reset(void) { n_=0; x_=xx_=0.0;} |
---|
69 | |
---|
70 | /// |
---|
71 | /// The standard deviation is defined as the square root of the |
---|
72 | /// variance. |
---|
73 | /// |
---|
74 | /// @return The standard deviation, root of the variance(). |
---|
75 | /// |
---|
76 | inline double std(void) const { return sqrt(variance()); } |
---|
77 | |
---|
78 | /// |
---|
79 | /// @return Standard error, i.e. standard deviation of the mean |
---|
80 | /// \f$ \sqrt{variance()/n} \f$ |
---|
81 | /// |
---|
82 | inline double standard_error(void) const { return sqrt(variance()/n_); } |
---|
83 | |
---|
84 | /// |
---|
85 | /// @return The sum of x |
---|
86 | /// |
---|
87 | inline double sum_x(void) const { return x_; } |
---|
88 | |
---|
89 | /// |
---|
90 | /// @return The sum of squares |
---|
91 | /// |
---|
92 | inline double sum_xsqr(void) const { return xx_; } |
---|
93 | |
---|
94 | /// |
---|
95 | /// The variance is calculated using the \f$ (n-1) \f$ correction, |
---|
96 | /// which means it is the best unbiased estimator of the variance |
---|
97 | /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2\f$, where \f$m\f$ is the |
---|
98 | /// mean. |
---|
99 | /// |
---|
100 | /// @return The variance |
---|
101 | /// |
---|
102 | inline double variance(void) const |
---|
103 | { return (n_>1) ? (xx_-x_*x_/n_)/(n_-1) : 0; } |
---|
104 | |
---|
105 | /// |
---|
106 | /// The assignment operator |
---|
107 | /// |
---|
108 | inline const Averager& operator=(const Averager& a) |
---|
109 | { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; } |
---|
110 | |
---|
111 | /// |
---|
112 | /// Operator to add another Averager |
---|
113 | /// |
---|
114 | const Averager& operator+=(const Averager&); |
---|
115 | |
---|
116 | private: |
---|
117 | long n_; |
---|
118 | double x_, xx_; |
---|
119 | }; |
---|
120 | |
---|
121 | }} // of namespace statistics and namespace theplu |
---|
122 | |
---|
123 | #endif |
---|