# source:trunk/src/WeightedAverager.h@232

Last change on this file since 232 was 232, checked in by Peter, 17 years ago

doc corrected

• Property svn:eol-style set to native
• Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line
1// $Id: WeightedAverager.h 232 2005-02-21 14:51:18Z peter$
2
3#ifndef _theplu_cpptools_weighted_averager_
4#define _theplu_cpptools_weighted_averager_
5
6//#include <cmath>
7#include "Averager.h"
8
9namespace theplu{
10namespace statistics{
11  ///
12  /// Class to calulate simple (first and second moments) averages
13  /// with weights.
14  ///
15  /// @see Averager
16  ///
17  class WeightedAverager
18  {
19  public:
20
21    ///
22    /// Default constructor
23    ///
24    WeightedAverager(void);
25
26    ///
27    /// Constructor taking the data point, i.e. the value and its
28    /// weight (default = 1)
29    ///
30    WeightedAverager(const double, const double=1);
31
32    ///
33    /// Copy constructor
34    ///
35    WeightedAverager(const WeightedAverager&);
36
37    ///
38    /// adding a data point d, with weight w (default is 1)
39    ///
40    inline void add(const double d,const double w=1)
42
43    ///
44    /// Calculating the weighted mean
45    ///
46    /// @return \f$\frac{\sum w_ix_i}{\sum w_i} \f$
47    ///
48    inline double mean(void) const { return sum_w() ?
49                                       sum_wx()/sum_w() : 0; }
50
51    ///
52    /// rescale object, i.e. each data point is rescaled
53    /// \f$x = a * x \f$
54    ///
55    inline void rescale(double a) { wx_.rescale(a); wwx_*=a; }
56
57    ///
58    /// resets everything to zero
59    ///
60    inline void reset(void) { wx_.reset(); w_.reset(); wwx_=0;}
61
62    ///
63    /// Calculating the squared error according to: \f$\frac{\sum 64 /// w_i^2(x_i-m)^2}{(\sum w_i)^2} \f$ @return squared error
65    ///
66    inline double squared_error(void) const
67    { return (squared_sum()) / (sum_w()*sum_w()); }
68
69    ///
70    /// The standard error is calculated as \f$\sqrt{\frac{\sum 71 /// w_i^2}{(\sum w_i)^2-\sum w_i^2}\frac{\sum w_i(x_i-m)^2}{\sum 72 /// w_i}} \f$
73    ///
74    /// @return standard deviation of mean
75    ///
76    inline double standard_error(void)  const
77    { return sqrt(squared_sum()/((sum_w()*sum_w())-squared_sum())
78              *squared_error()/sum_w()); }
79
80    ///
81    /// Calculating the squared sum
82    ///
83    /// @return \f$\sum w_i^2(x_i-m)^2 \f$
84    ///
85    inline double squared_sum(void) const
86    { return (sum_wwxx()-2*mean()*wwx_+
87              mean()*mean()*sum_ww()) ; }
88
89    ///
90    /// Calculating the sum of weights: \f$\sum 91 /// w_i \f$ @return sum of weights
92    ///
93    inline double sum_w(void) const
94    { return w_.sum_x(); }
95
96    ///
97    /// \f$\sum w_ix_i \f$ @return weighted sum of x
98    ///
99    inline double sum_wx(void)  const
100    { return wx_.sum_x(); }
101
102
103  private:
104    inline double sum_ww(void)  const
105    { return w_.sum_xsqr(); }
106    ///
107    ///  @return \f$\sum w_i^2x_i^2 \f$
108    ///
109    inline double sum_wwxx(void)  const
110    { return wx_.sum_xsqr(); }
111
112    ///
113    ///  @return \f$\sum w_i^2x_i \f$
114    ///
115    inline double sum_wwx(void) const
116    { return wwx_; }
117
118    ///
119    /// operator to add a WeightedAverager
120    ///
121    inline WeightedAverager operator+=(WeightedAverager& a)
122    { wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); return *this; }
123
124    Averager w_;
125    Averager wx_;
126    double wwx_;
127
128    inline Averager wx(void) const {return wx_;}
129    inline Averager w(void) const {return w_;}
130
131
132  };
133
134///
135/// The WeightedAverager output operator
136///
137std::ostream& operator<<(std::ostream& s,const WeightedAverager&);
138
139}} // of namespace statistics and namespace theplu
140
141#endif
Note: See TracBrowser for help on using the repository browser.