1 | // $Id: Averager.h 95 2004-06-09 16:01:43Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_cpptools_averager_ |
---|
4 | #define _theplu_cpptools_averager_ |
---|
5 | |
---|
6 | #include <cmath> |
---|
7 | |
---|
8 | namespace theplu{ |
---|
9 | namespace cpptools{ |
---|
10 | class ostream; |
---|
11 | /// |
---|
12 | /// Modified version of the Averager class written by Patrik Eden, |
---|
13 | /// Dept. Theoretical Physic, Lund University, Sweden |
---|
14 | /// |
---|
15 | class Averager |
---|
16 | { |
---|
17 | public: |
---|
18 | |
---|
19 | /// |
---|
20 | /// Default constructor |
---|
21 | /// |
---|
22 | Averager(void); |
---|
23 | |
---|
24 | /// |
---|
25 | /// Constructor taking the first value. |
---|
26 | /// |
---|
27 | Averager(double); |
---|
28 | |
---|
29 | /// |
---|
30 | /// Constructor taking sum of x, sum of squared x and number of |
---|
31 | /// samples. |
---|
32 | /// |
---|
33 | Averager(double, double, long); |
---|
34 | |
---|
35 | /// |
---|
36 | /// Copy constructor |
---|
37 | /// |
---|
38 | Averager(const Averager&); |
---|
39 | |
---|
40 | /// |
---|
41 | /// adding a data point d, with weight w (default is 1) |
---|
42 | /// |
---|
43 | inline void add(const double d,const double w=1) |
---|
44 | {n_+=w; x_+=d*w; xx_+=d*d*w;} |
---|
45 | |
---|
46 | /// |
---|
47 | /// @return average |
---|
48 | /// |
---|
49 | inline double average(void) const {return mean();} |
---|
50 | |
---|
51 | /// |
---|
52 | /// @return second moment (mean of squares) |
---|
53 | /// |
---|
54 | inline double average_sqr(void) const {return mean_sqr();} |
---|
55 | |
---|
56 | /// |
---|
57 | /// @return average |
---|
58 | /// |
---|
59 | inline double mean(void) const { return n_ ? x_/n_ : 0; } |
---|
60 | |
---|
61 | /// |
---|
62 | /// @return second moment (mean of squares) |
---|
63 | /// |
---|
64 | inline double mean_sqr(void) const { return n_ ? xx_/n_ : 0; } |
---|
65 | |
---|
66 | /// |
---|
67 | /// @return number of data points |
---|
68 | /// |
---|
69 | inline double n(void) const { return n_; } |
---|
70 | |
---|
71 | /// |
---|
72 | /// resets everything to zero |
---|
73 | /// |
---|
74 | inline void reset(void) { n_=x_=xx_=0;} |
---|
75 | |
---|
76 | /// |
---|
77 | /// @return standard deviation |
---|
78 | /// |
---|
79 | inline double standard_deviation(void) const { return sqrt(variance()); } |
---|
80 | |
---|
81 | /// |
---|
82 | /// @return standard error, i.e. standard deviation of the mean |
---|
83 | /// |
---|
84 | inline double standard_error(void) const { return sqrt(variance()/n_); } |
---|
85 | |
---|
86 | /// |
---|
87 | /// @return the sum of x |
---|
88 | /// |
---|
89 | inline double sum_x(void) const { return x_; } |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return the sum of squares |
---|
93 | /// |
---|
94 | inline double sum_xsqr(void) const { return xx_; } |
---|
95 | |
---|
96 | /// |
---|
97 | ///The variance is calculated using the (n-1) correction so the |
---|
98 | ///expectation value is unbiased @return variance of mean |
---|
99 | /// |
---|
100 | inline double variance(void) const |
---|
101 | { return (n_>1) ? (xx_-x_*x_/n_)/(n_-1) : 0; } |
---|
102 | |
---|
103 | /// |
---|
104 | /// equality operator |
---|
105 | /// |
---|
106 | inline Averager operator=(const Averager& a) |
---|
107 | { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; } |
---|
108 | |
---|
109 | /// |
---|
110 | /// operator to add a double to the object |
---|
111 | /// |
---|
112 | inline Averager operator+=(double d) |
---|
113 | { add(d); return *this; } |
---|
114 | |
---|
115 | /// |
---|
116 | /// operator to add a "one" |
---|
117 | /// |
---|
118 | inline Averager operator++(void) |
---|
119 | { add(1); return *this; } |
---|
120 | |
---|
121 | /// |
---|
122 | /// Operator to rescale the object |
---|
123 | /// |
---|
124 | inline Averager operator*=(double d) |
---|
125 | { x_*=d; xx_*=d*d; return *this; } |
---|
126 | |
---|
127 | /// |
---|
128 | /// Operator to rescale the object |
---|
129 | /// |
---|
130 | inline Averager operator/=(double d) |
---|
131 | { return (*this)*=(1/d); } |
---|
132 | |
---|
133 | /// |
---|
134 | /// operator to add another Averager |
---|
135 | /// |
---|
136 | const Averager& operator+=(const Averager&); |
---|
137 | |
---|
138 | /// |
---|
139 | /// operator to multiply with an Averager. See code for details. |
---|
140 | /// |
---|
141 | const Averager& operator*=(const Averager&); |
---|
142 | |
---|
143 | /// |
---|
144 | /// operator to multiply with an Averager. See code for details. |
---|
145 | /// |
---|
146 | const Averager& operator/=(const Averager&); |
---|
147 | |
---|
148 | private: |
---|
149 | double n_; |
---|
150 | double x_, xx_; |
---|
151 | }; |
---|
152 | |
---|
153 | Averager operator*(double,const Averager&); |
---|
154 | |
---|
155 | /// |
---|
156 | /// The output operator |
---|
157 | /// |
---|
158 | std::ostream& operator<<( std::ostream&, const Averager&); |
---|
159 | |
---|
160 | |
---|
161 | }} // of namespace cpptools and namespace theplu |
---|
162 | |
---|
163 | #endif |
---|