1 | #ifndef _theplu_yat_statistics_averagerweighted_ |
---|
2 | #define _theplu_yat_statistics_averagerweighted_ |
---|
3 | |
---|
4 | // $Id: AveragerWeighted.h 683 2006-10-11 22:20:36Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) The authors contributing to this file. |
---|
8 | |
---|
9 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "Averager.h" |
---|
28 | |
---|
29 | #include <cmath> |
---|
30 | |
---|
31 | namespace theplu{ |
---|
32 | namespace yat{ |
---|
33 | namespace statistics{ |
---|
34 | |
---|
35 | /// |
---|
36 | /// @brief Class to calulate averages with weights. |
---|
37 | /// |
---|
38 | /// There are several different reasons why a statistical analysis |
---|
39 | /// needs to adjust for weighting. In the litterature reasons are |
---|
40 | /// mainly divided into two kinds of weights - probablity weights |
---|
41 | /// and analytical weights. 1) Analytical weights are appropriate |
---|
42 | /// for scientific experiments where some measurements are known to |
---|
43 | /// be more precise than others. The larger weight a measurement has |
---|
44 | /// the more precise is is assumed to be, or more formally the |
---|
45 | /// weight is proportional to the reciprocal variance |
---|
46 | /// \f$ \sigma_i^2 = \frac{\sigma^2}{w_i} \f$. 2) Probability weights |
---|
47 | /// are used for the situation when calculating averages over a |
---|
48 | /// distribution \f$ f \f$ , but sampling from a distribution \f$ f' |
---|
49 | /// \f$. Compensating for this discrepancy averages of observables |
---|
50 | /// are taken to be \f$ \sum \frac{f}{f'}X \f$ For further discussion: |
---|
51 | /// <a href="Statistics/index.html">Weighted Statistics document</a><br> |
---|
52 | /// |
---|
53 | /// If nothing else stated, each function fulfills the |
---|
54 | /// following:<br> <ul><li>Setting a weight to zero corresponds to |
---|
55 | /// removing the data point from the dataset.</li><li> Setting all |
---|
56 | /// weights to unity, the yields the same result as from |
---|
57 | /// corresponding function in Averager.</li><li> Rescaling weights |
---|
58 | /// does not change the performance of the object.</li></ul> |
---|
59 | /// |
---|
60 | /// @see Averager AveragerPair AveragerPairWeighted |
---|
61 | /// |
---|
62 | class AveragerWeighted |
---|
63 | { |
---|
64 | public: |
---|
65 | |
---|
66 | /// |
---|
67 | /// Default constructor |
---|
68 | /// |
---|
69 | inline AveragerWeighted(void) |
---|
70 | : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0) {} |
---|
71 | |
---|
72 | /// |
---|
73 | /// Copy constructor |
---|
74 | /// |
---|
75 | inline AveragerWeighted(const AveragerWeighted& a) |
---|
76 | : w_(Averager(a.sum_w(),a.sum_ww(),1)), |
---|
77 | wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), wwx_(a.sum_wwx()), |
---|
78 | wxx_(a.sum_wxx()) {} |
---|
79 | |
---|
80 | /// |
---|
81 | /// adding a data point d, with weight w (default is 1) |
---|
82 | /// |
---|
83 | inline void add(const double d,const double w=1) |
---|
84 | { if(w==0) return; w_.add(w); wx_.add(w*d); wwx_+=w*w*d; wxx_+=w*d*d; } |
---|
85 | |
---|
86 | /// |
---|
87 | /// Adding each value in an array \a x and corresponding value in |
---|
88 | /// weight array \a w. |
---|
89 | /// |
---|
90 | /// The requirements for the types T1 and T2 of the arrays \a x |
---|
91 | /// and \a w are: operator[] returning an element and function |
---|
92 | /// size() returning the number of elements. |
---|
93 | /// |
---|
94 | template <typename T1, typename T2> |
---|
95 | void add_values(const T1& x, const T2& w); |
---|
96 | |
---|
97 | /// |
---|
98 | /// Calculating the weighted mean |
---|
99 | /// |
---|
100 | /// @return \f$ \frac{\sum w_ix_i}{\sum w_i} \f$ |
---|
101 | /// |
---|
102 | inline double mean(void) const { return sum_w() ? |
---|
103 | sum_wx()/sum_w() : 0; } |
---|
104 | |
---|
105 | /// |
---|
106 | /// @brief Weighted version of number of data points. If all |
---|
107 | /// weights are equal, the unweighted version is identical to the |
---|
108 | /// non-weighted version. Adding a data point with zero weight |
---|
109 | /// does not change n(). The calculated value is always smaller |
---|
110 | /// than the actual number of data points added to object. |
---|
111 | /// |
---|
112 | /// @return \f$ \frac{\left(\sum w_i\right)^2}{\sum w_i^2} \f$ |
---|
113 | /// |
---|
114 | inline double n(void) const { return sum_w()*sum_w()/sum_ww(); } |
---|
115 | |
---|
116 | /// |
---|
117 | /// rescale object, i.e. each data point is rescaled |
---|
118 | /// \f$ x = a * x \f$ |
---|
119 | /// |
---|
120 | inline void rescale(double a) { wx_.rescale(a); wwx_*=a; wxx_*=a*a; } |
---|
121 | |
---|
122 | /// |
---|
123 | /// resets everything to zero |
---|
124 | /// |
---|
125 | inline void reset(void) { wx_.reset(); w_.reset(); wwx_=0; wxx_=0; } |
---|
126 | |
---|
127 | /// |
---|
128 | /// The standard deviation is defined as the square root of the |
---|
129 | /// variance(). |
---|
130 | /// |
---|
131 | /// @return The standard deviation, root of the variance(). |
---|
132 | /// |
---|
133 | inline double std(void) const { return sqrt(variance()); } |
---|
134 | |
---|
135 | /// |
---|
136 | /// Calculates standard deviation of the mean(). Variance from the |
---|
137 | /// weights are here neglected. This is true when the weight is |
---|
138 | /// known before the measurement. In case this is not a good |
---|
139 | /// approximation, use bootstrapping to estimate the error. |
---|
140 | /// |
---|
141 | /// @return \f$ \frac{\sum w^2}{\left(\sum w\right)^3}\sum w(x-m)^2 \f$ |
---|
142 | /// where \f$ m \f$ is the mean() |
---|
143 | /// |
---|
144 | inline double standard_error(void) const |
---|
145 | { return sqrt(sum_ww()/(sum_w()*sum_w()*sum_w()) * |
---|
146 | sum_xx_centered()); } |
---|
147 | |
---|
148 | /// |
---|
149 | /// Calculating the sum of weights |
---|
150 | /// |
---|
151 | /// @return \f$ \sum w_i \f$ |
---|
152 | /// |
---|
153 | inline double sum_w(void) const |
---|
154 | { return w_.sum_x(); } |
---|
155 | |
---|
156 | /// |
---|
157 | /// @return \f$ \sum w_i^2 \f$ |
---|
158 | /// |
---|
159 | inline double sum_ww(void) const |
---|
160 | { return w_.sum_xx(); } |
---|
161 | |
---|
162 | /// |
---|
163 | /// \f$ \sum w_ix_i \f$ |
---|
164 | /// |
---|
165 | /// @return weighted sum of x |
---|
166 | /// |
---|
167 | inline double sum_wx(void) const |
---|
168 | { return wx_.sum_x(); } |
---|
169 | |
---|
170 | /// |
---|
171 | /// @return \f$ \sum_i w_i (x_i-m)^2\f$ |
---|
172 | /// |
---|
173 | inline double sum_xx_centered(void) const |
---|
174 | { return sum_wxx() - mean()*mean()*sum_w(); } |
---|
175 | |
---|
176 | /// |
---|
177 | /// The variance is calculated as \f$ \frac{\sum w_i (x_i - m)^2 |
---|
178 | /// }{\sum w_i} \f$, where \a m is the known mean. |
---|
179 | /// |
---|
180 | /// @return Variance when the mean is known to be \a m. |
---|
181 | /// |
---|
182 | inline double variance(const double m) const |
---|
183 | { return (sum_wxx()-2*m*sum_wx())/sum_w()+m*m; } |
---|
184 | |
---|
185 | /// |
---|
186 | /// The variance is calculated as \f$ \frac{\sum w_i (x_i - m)^2 |
---|
187 | /// }{\sum w_i} \f$, where \a m is the mean(). Here the weight are |
---|
188 | /// interpreted as probability weights. For analytical weights the |
---|
189 | /// variance has no meaning as each data point has its own |
---|
190 | /// variance. |
---|
191 | /// |
---|
192 | /// @return The variance. |
---|
193 | /// |
---|
194 | inline double variance(void) const |
---|
195 | { return sum_xx_centered()/sum_w(); } |
---|
196 | |
---|
197 | |
---|
198 | private: |
---|
199 | /// |
---|
200 | /// @return \f$ \sum w_i^2x_i^2 \f$ |
---|
201 | /// |
---|
202 | inline double sum_wwxx(void) const |
---|
203 | { return wx_.sum_xx(); } |
---|
204 | |
---|
205 | /// |
---|
206 | /// @return \f$ \sum w_i^2x_i \f$ |
---|
207 | /// |
---|
208 | inline double sum_wwx(void) const |
---|
209 | { return wwx_; } |
---|
210 | |
---|
211 | /// |
---|
212 | /// @return \f$ \sum w_i x_i^2 \f$ |
---|
213 | /// |
---|
214 | inline double sum_wxx(void) const { return wxx_; } |
---|
215 | |
---|
216 | /// |
---|
217 | /// operator to add a AveragerWeighted |
---|
218 | /// |
---|
219 | AveragerWeighted operator+=(const AveragerWeighted&); |
---|
220 | |
---|
221 | Averager w_; |
---|
222 | Averager wx_; |
---|
223 | double wwx_; |
---|
224 | double wxx_; |
---|
225 | |
---|
226 | inline Averager wx(void) const {return wx_;} |
---|
227 | inline Averager w(void) const {return w_;} |
---|
228 | |
---|
229 | |
---|
230 | }; |
---|
231 | |
---|
232 | // Template implementations |
---|
233 | template <typename T1, typename T2> |
---|
234 | void AveragerWeighted::add_values(const T1& x, const T2& w) |
---|
235 | { |
---|
236 | assert(x.size()==w.size()); |
---|
237 | for (size_t i=0; i<x.size(); i++) |
---|
238 | add(x[i],w[i]); |
---|
239 | } |
---|
240 | |
---|
241 | /// |
---|
242 | /// The AveragerWeighted output operator |
---|
243 | /// |
---|
244 | ///std::ostream& operator<<(std::ostream& s,const AveragerWeighted&); |
---|
245 | |
---|
246 | }}} // of namespace statistics, yat, and theplu |
---|
247 | |
---|
248 | #endif |
---|