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

Last change on this file since 95 was 95, checked in by Peter, 19 years ago

• Property svn:eol-style set to native
• Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line
1// $Id: WeightedAverager.h 95 2004-06-09 16:01:43Z 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 cpptools{
11  ///
12  /// Averager class with weights.
13  ///
14  class WeightedAverager
15  {
16  public:
17
18    ///
19    /// Default constructor
20    ///
21    WeightedAverager(void);
22
23    ///
24    /// Constructor taking the data point, i.e. the value and its
25    /// weight (default = 1)
26    ///
27    WeightedAverager(const double, const double=1);
28
29    ///
30    /// Copy constructor
31    ///
32    WeightedAverager(const WeightedAverager&);
33
34    ///
35    /// adding a data point d, with weight w (default is 1)
36    ///
37    inline void add(const double d,const double w=1)
39
40    ///
41    /// Calculating the average according to: \f$\frac{\sum 42 /// w_ix_i}{\sum w_i}\f$ @return average
43    ///
44    inline double average(void) const {return mean();}
45
46    ///
47    ///Calculating the mean according to: \f$\frac{\sum 48 ///w_ix_i}{\sum w_i} \f$ @return average
49    ///
50    inline double mean(void) const { return w_.sum_x() ?
51                                       wx_.sum_x()/w_.sum_x() : 0; }
52
53    ///
54    /// Calculating the squared error according to: \f$\frac{\sum 55 /// w_i^2(x_i-m)^2}{(\sum w_i)^2} \f$ @return squared error
56    ///
57    inline double squared_error(void) const
58    { return (wx_.sum_xsqr()-2*mean()*wwx_+
59              mean()*mean()*w_.sum_xsqr()) / (w_.sum_x()*w_.sum_x()); }
60
61    ///
62    /// Calculating the sum of weights: \f$\sum 63 /// w_i \f$ @return sum of weights
64    ///
65    inline double sum_w(void) const
66    { return w_.sum_x(); }
67
68    ///
69    /// \f$\sum w_i^2 \f$ @return sum of squared weights
70    ///
71    inline double sum_ww(void)  const
72    { return w_.sum_xsqr(); }
73
74    ///
75    /// \f$\sum w_ix_i \f$ @return weighted sum of x
76    ///
77    inline double sum_wx(void)  const
78    { return w_.sum_xsqr(); }
79
80    ///
81    ///  @return \f$\sum w_i^2x_i^2 \f$
82    ///
83    inline double sum_wwxx(void)  const
84    { return wx_.sum_xsqr(); }
85
86    ///
87    ///  @return \f$\sum w_i^2x_i \f$
88    ///
89    inline double sum_wwx(void) const
90    { return wwx_; }
91
92    ///
93    /// operator to add a double (with weight 1) to the object
94    ///
95    inline WeightedAverager operator+=(double d)
96    { add(d); return *this; }
97
98    ///
99    /// operator to add a WeightedAverager
100    ///
101    inline WeightedAverager operator+=(WeightedAverager a)
102    { wx_+=a.wx(); w_+=a.w(); return *this; }
103
104
105  private:
106    Averager w_;
107    Averager wx_;
108    double wwx_;
109
110    inline Averager wx(void) const {return wx_;}
111    inline Averager w(void) const {return w_;}
112
113
114  };
115
116
117}} // of namespace cpptools and namespace theplu
118
119#endif
Note: See TracBrowser for help on using the repository browser.