1 | #ifndef _theplu_yat_statistics_averager_ |
---|
2 | #define _theplu_yat_statistics_averager_ |
---|
3 | |
---|
4 | // $Id: Averager.h 1487 2008-09-10 08:41:36Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 3 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "yat/utility/iterator_traits.h" |
---|
28 | |
---|
29 | #include <cmath> |
---|
30 | |
---|
31 | namespace theplu{ |
---|
32 | namespace yat{ |
---|
33 | namespace statistics{ |
---|
34 | |
---|
35 | class ostream; |
---|
36 | |
---|
37 | /// |
---|
38 | /// @brief Class to calculate simple (first and second moments) averages. |
---|
39 | /// |
---|
40 | /// @see AveragerWeighted AveragerPair AveragerPairWeighted |
---|
41 | /// |
---|
42 | class Averager |
---|
43 | { |
---|
44 | public: |
---|
45 | |
---|
46 | /// |
---|
47 | /// Default constructor |
---|
48 | /// |
---|
49 | Averager(void); |
---|
50 | |
---|
51 | /// |
---|
52 | /// Constructor taking sum of \a x, sum of squared x, \a xx, and |
---|
53 | /// number of samples \a n. |
---|
54 | /// |
---|
55 | Averager(double x, double xx, long n); |
---|
56 | |
---|
57 | /// |
---|
58 | /// Copy constructor |
---|
59 | /// |
---|
60 | Averager(const Averager& a); |
---|
61 | |
---|
62 | /// |
---|
63 | /// Adding \a n (default=1) number of data point(s) with value \a d. |
---|
64 | /// |
---|
65 | void add(double d, long n=1); |
---|
66 | |
---|
67 | /** |
---|
68 | @brief Coeffient of variation |
---|
69 | |
---|
70 | Coeffient of variation (cv) is defined as ratio between the |
---|
71 | standard deviation and the mean: \f$ \frac{\sigma}{\mu} \f$. |
---|
72 | |
---|
73 | @return standard deviation divided by mean. |
---|
74 | */ |
---|
75 | double cv(void) const; |
---|
76 | |
---|
77 | /// |
---|
78 | /// @return %Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$ |
---|
79 | /// |
---|
80 | double mean(void) const; |
---|
81 | |
---|
82 | /// |
---|
83 | /// @return Number of data points |
---|
84 | /// |
---|
85 | long n(void) const; |
---|
86 | |
---|
87 | /// |
---|
88 | /// @brief Rescales the object |
---|
89 | /// |
---|
90 | /// \f$ \forall x_i \rightarrow a*x_i \f$, |
---|
91 | /// \f$ \forall x_i^2 \rightarrow a^2*x_i^2 \f$ |
---|
92 | /// |
---|
93 | void rescale(double a); |
---|
94 | |
---|
95 | /// |
---|
96 | /// @return Standard error, i.e. standard deviation of the mean |
---|
97 | /// \f$ \sqrt{variance()/n} \f$ |
---|
98 | /// |
---|
99 | double standard_error(void) const; |
---|
100 | |
---|
101 | /// |
---|
102 | /// @brief The standard deviation is defined as the square root of |
---|
103 | /// the variance. |
---|
104 | /// |
---|
105 | /// @return The standard deviation, root of the variance(). |
---|
106 | /// |
---|
107 | double std(void) const; |
---|
108 | |
---|
109 | /// |
---|
110 | /// @brief The standard deviation is defined as the square root of |
---|
111 | /// the variance. |
---|
112 | /// |
---|
113 | /// @return Standard deviation around \a m, root of the variance(m). |
---|
114 | /// |
---|
115 | double std(double m) const; |
---|
116 | |
---|
117 | /// |
---|
118 | /// @return The sum of x |
---|
119 | /// |
---|
120 | double sum_x(void) const; |
---|
121 | |
---|
122 | /// |
---|
123 | /// @return The sum of squares |
---|
124 | /// |
---|
125 | double sum_xx(void) const; |
---|
126 | |
---|
127 | /// |
---|
128 | /// @return \f$ \sum_i (x_i-m)^2 \f$ |
---|
129 | /// |
---|
130 | double sum_xx_centered(void) const; |
---|
131 | |
---|
132 | /// |
---|
133 | /// @brief The variance with know mean |
---|
134 | /// |
---|
135 | /// The variance is calculated as |
---|
136 | /// \f$ \frac{1}{n}\sum (x_i-m)^2 \f$. |
---|
137 | /// |
---|
138 | /// @return Variance when the mean is known to be \a m. |
---|
139 | /// |
---|
140 | double variance(double m) const; |
---|
141 | |
---|
142 | /** |
---|
143 | \brief The estimated variance |
---|
144 | |
---|
145 | The variance is calculated as \f$ \frac{1}{N}\sum_i |
---|
146 | (x_i-m)^2 \f$, where \f$ m \f$ is the mean. |
---|
147 | |
---|
148 | \return Estimation of variance |
---|
149 | */ |
---|
150 | double variance(void) const; |
---|
151 | |
---|
152 | /// |
---|
153 | /// The variance is calculated using the \f$ (n-1) \f$ correction, |
---|
154 | /// which means it is the best unbiased estimator of the variance |
---|
155 | /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2 \f$, where \f$ m \f$ is the |
---|
156 | /// mean. |
---|
157 | /// |
---|
158 | /// @return unbiased estimation of variance |
---|
159 | /// |
---|
160 | double variance_unbiased(void) const; |
---|
161 | |
---|
162 | /// |
---|
163 | /// @brief Reset everything to zero |
---|
164 | /// |
---|
165 | void reset(void); |
---|
166 | |
---|
167 | /// |
---|
168 | /// @brief The assignment operator |
---|
169 | /// |
---|
170 | const Averager& operator=(const Averager&); |
---|
171 | |
---|
172 | /// |
---|
173 | /// Operator to add another Averager |
---|
174 | /// |
---|
175 | const Averager& operator+=(const Averager&); |
---|
176 | |
---|
177 | private: |
---|
178 | long n_; |
---|
179 | double x_, xx_; |
---|
180 | }; |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | \brief adding a range of values to Averager \a a |
---|
185 | */ |
---|
186 | template <typename Iter> |
---|
187 | void add(Averager& a, Iter first, Iter last) |
---|
188 | { |
---|
189 | utility::check_iterator_is_unweighted(first); |
---|
190 | for ( ; first != last; ++first) |
---|
191 | a.add(*first); |
---|
192 | } |
---|
193 | |
---|
194 | }}} // of namespace statistics, yat, and theplu |
---|
195 | |
---|
196 | #endif |
---|