source: trunk/yat/statistics/AveragerWeighted.h @ 756

Last change on this file since 756 was 756, checked in by Jari Häkkinen, 17 years ago

Added missing #include

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1#ifndef _theplu_yat_statistics_averagerweighted_
2#define _theplu_yat_statistics_averagerweighted_
3
4// $Id: AveragerWeighted.h 756 2007-02-19 18:14:23Z 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 <cassert>
30#include <cmath>
31
32namespace theplu{
33namespace yat{
34namespace statistics{
35
36  ///
37  /// @brief Class to calulate averages with weights.
38  ///
39  /// There are several different reasons why a statistical analysis
40  /// needs to adjust for weighting. In the litterature reasons are
41  /// mainly divided into two kinds of weights - probablity weights
42  /// and analytical weights. 1) Analytical weights are appropriate
43  /// for scientific experiments where some measurements are known to
44  /// be more precise than others. The larger weight a measurement has
45  /// the more precise is is assumed to be, or more formally the
46  /// weight is proportional to the reciprocal variance
47  /// \f$ \sigma_i^2 = \frac{\sigma^2}{w_i} \f$. 2) Probability weights
48  /// are used for the situation when calculating averages over a
49  /// distribution \f$ f \f$ , but sampling from a distribution \f$ f'
50  /// \f$. Compensating for this discrepancy averages of observables
51  /// are taken to be \f$ \sum \frac{f}{f'}X \f$ For further discussion:
52  /// <a href="Statistics/index.html">Weighted Statistics document</a><br>
53  ///
54  /// If nothing else stated, each function fulfills the
55  /// following:<br> <ul><li>Setting a weight to zero corresponds to
56  /// removing the data point from the dataset.</li><li> Setting all
57  /// weights to unity, the yields the same result as from
58  /// corresponding function in Averager.</li><li> Rescaling weights
59  /// does not change the performance of the object.</li></ul>
60  ///
61  /// @see Averager AveragerPair AveragerPairWeighted
62  ///
63  class AveragerWeighted
64  {
65  public:
66
67    ///
68    /// @brief The default constructor
69    ///
70    AveragerWeighted(void);
71
72    ///
73    /// @brief The copy constructor
74    ///
75    AveragerWeighted(const AveragerWeighted&);
76
77    ///
78    /// Adding a data point \a d, with weight \a w (default is 1)
79    ///
80    void  add(const double d,const double w=1);
81
82    ///
83    /// Adding each value in an array \a x and corresponding value in
84    /// weight array \a w.
85    ///
86    /// The requirements for the types T1 and T2 of the arrays \a x
87    /// and \a w are: operator[] returning an element and function
88    /// size() returning the number of elements.
89    ///
90    template <typename T1, typename T2>
91    void add_values(const T1& x, const T2& w);
92
93    ///
94    /// @brief Calculate the weighted mean
95    ///
96    /// @return \f$ \frac{\sum w_ix_i}{\sum w_i} \f$
97    ///
98    double mean(void) const;
99
100    ///
101    /// @brief Weighted version of number of data points.
102    ///
103    /// If all
104    /// weights are equal, the unweighted version is identical to the
105    /// non-weighted version. Adding a data point with zero weight
106    /// does not change n(). The calculated value is always smaller
107    /// than the actual number of data points added to object.
108    ///
109    /// @return \f$ \frac{\left(\sum w_i\right)^2}{\sum w_i^2} \f$
110    ///
111    double n(void) const;
112
113    ///
114    /// @brief Rescale object.
115    ///
116    /// Each data point is rescaled as \f$ x = a * x \f$
117    ///
118    void rescale(double a);
119
120    ///
121    /// @brief Reset everything to zero.
122    ///
123    void reset(void);
124
125    ///
126    /// @brief The standard deviation is defined as the square root of
127    /// the variance().
128    ///
129    /// @return The standard deviation, root of the variance().
130    ///
131    double std(void) const;
132
133    ///
134    /// @brief Calculates standard deviation of the mean().
135    ///
136    /// 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    double standard_error(void) const;
145
146    ///
147    /// Calculating the sum of weights 
148    ///
149    /// @return \f$ \sum w_i \f$
150    ///
151    double sum_w(void) const;
152
153    ///
154    /// @return \f$ \sum w_i^2 \f$
155    ///
156    double sum_ww(void) const;
157
158    ///
159    /// \f$ \sum w_ix_i \f$
160    ///
161    /// @return weighted sum of x
162    ///
163    double sum_wx(void) const;
164
165    ///
166    /// @return \f$ \sum_i w_i (x_i-m)^2\f$
167    ///
168    double sum_xx_centered(void) const;
169
170    /**
171       The variance is calculated as \f$ \frac{\sum w_i (x_i - m)^2
172       }{\sum w_i} \f$, where \a m is the known mean.
173       
174       @return Variance when the mean is known to be \a m.
175    */
176    double variance(const double m) const;
177
178    /**
179       The variance is calculated as \f$ \frac{\sum w_i (x_i - m)^2
180       }{\sum w_i} \f$, where \a m is the mean(). Here the weight are
181       interpreted as probability weights. For analytical weights the
182       variance has no meaning as each data point has its own
183       variance.
184       
185       @return The variance.
186    */
187    double variance(void) const;
188
189
190  private:
191    ///
192    ///  @return \f$ \sum w_i^2x_i \f$
193    ///
194    double sum_wwx(void) const;
195
196    ///
197    ///  @return \f$ \sum w_i^2x_i^2 \f$
198    ///
199    double sum_wwxx(void) const;
200   
201    ///
202    ///  @return \f$ \sum w_i x_i^2 \f$
203    ///
204    double sum_wxx(void) const;
205
206    const Averager& wx(void) const;
207    const Averager& w(void) const;
208
209    ///
210    /// operator to add a AveragerWeighted
211    ///
212    const AveragerWeighted& operator+=(const AveragerWeighted&);
213   
214    Averager w_;
215    Averager wx_;
216    double wwx_;
217    double wxx_;
218  };
219
220  // Template implementations
221  template <typename T1, typename T2>
222  void AveragerWeighted::add_values(const T1& x, const T2& w)
223  {
224    assert(x.size()==w.size());
225    for (size_t i=0; i<x.size(); i++) 
226      add(x[i],w[i]);
227  }
228
229///
230/// The AveragerWeighted output operator
231///
232///std::ostream& operator<<(std::ostream& s,const AveragerWeighted&);
233
234}}} // of namespace statistics, yat, and theplu
235
236#endif
Note: See TracBrowser for help on using the repository browser.