1 | // $Id: WeightedAverager.h 101 2004-06-10 15:34:39Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_cpptools_weighted_averager_ |
---|
4 | #define _theplu_cpptools_weighted_averager_ |
---|
5 | |
---|
6 | //#include <cmath> |
---|
7 | #include "Averager.h" |
---|
8 | |
---|
9 | namespace theplu{ |
---|
10 | namespace 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) |
---|
38 | {w_.add(w); wx_.add(w*d); wwx_+=w*w*d;} |
---|
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 sum_w() ? |
---|
51 | sum_wx()/sum_w() : 0; } |
---|
52 | |
---|
53 | /// |
---|
54 | /// resets everything to zero |
---|
55 | /// |
---|
56 | inline void reset(void) { wx_.reset(); w_.reset(); wwx_=0;} |
---|
57 | |
---|
58 | /// |
---|
59 | /// Calculating the squared error according to: \f$ \frac{\sum |
---|
60 | /// w_i^2(x_i-m)^2}{(\sum w_i)^2} \f$ @return squared error |
---|
61 | /// |
---|
62 | inline double squared_error(void) const |
---|
63 | { return (sum_wwxx()-2*mean()*wwx_+ |
---|
64 | mean()*mean()*sum_ww()) / (sum_w()*sum_w()); } |
---|
65 | |
---|
66 | /// |
---|
67 | /// Calculating the sum of weights: \f$ \sum |
---|
68 | /// w_i \f$ @return sum of weights |
---|
69 | /// |
---|
70 | inline double sum_w(void) const |
---|
71 | { return w_.sum_x(); } |
---|
72 | |
---|
73 | /// |
---|
74 | /// \f$ \sum w_i^2 \f$ @return sum of squared weights |
---|
75 | /// |
---|
76 | inline double sum_ww(void) const |
---|
77 | { return w_.sum_xsqr(); } |
---|
78 | |
---|
79 | /// |
---|
80 | /// \f$ \sum w_ix_i \f$ @return weighted sum of x |
---|
81 | /// |
---|
82 | inline double sum_wx(void) const |
---|
83 | { return w_.sum_xsqr(); } |
---|
84 | |
---|
85 | /// |
---|
86 | /// @return \f$ \sum w_i^2x_i^2 \f$ |
---|
87 | /// |
---|
88 | inline double sum_wwxx(void) const |
---|
89 | { return wx_.sum_xsqr(); } |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return \f$ \sum w_i^2x_i \f$ |
---|
93 | /// |
---|
94 | inline double sum_wwx(void) const |
---|
95 | { return wwx_; } |
---|
96 | |
---|
97 | /// |
---|
98 | /// operator to add a double (with weight 1) to the object |
---|
99 | /// |
---|
100 | inline WeightedAverager operator+=(double d) |
---|
101 | { add(d); return *this; } |
---|
102 | |
---|
103 | /// |
---|
104 | /// operator to add a WeightedAverager |
---|
105 | /// |
---|
106 | inline WeightedAverager operator+=(WeightedAverager& a) |
---|
107 | { wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); return *this; } |
---|
108 | |
---|
109 | /// |
---|
110 | /// operator to rescale object, i.e. each data point is rescaled |
---|
111 | /// \f$ x = d * x \f$ |
---|
112 | /// |
---|
113 | inline WeightedAverager operator*=(double d) |
---|
114 | { wx_*=d; wwx_*=d; return *this; } |
---|
115 | |
---|
116 | |
---|
117 | private: |
---|
118 | Averager w_; |
---|
119 | Averager wx_; |
---|
120 | double wwx_; |
---|
121 | |
---|
122 | inline Averager wx(void) const {return wx_;} |
---|
123 | inline Averager w(void) const {return w_;} |
---|
124 | |
---|
125 | |
---|
126 | }; |
---|
127 | |
---|
128 | |
---|
129 | }} // of namespace cpptools and namespace theplu |
---|
130 | |
---|
131 | #endif |
---|