1 | // $Id: WeightedAverager.h 220 2004-12-30 22:31:34Z 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 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) |
---|
41 | {w_.add(w); wx_.add(w*d); wwx_+=w*w*d;} |
---|
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 deviation is calculated as \f$ \frac{\sum w_i^2}{(\sum |
---|
71 | /// w_i)^2-\sum w_i^2}\frac{\sum w_i(x_i-m)^2}{\sum w_i} \f$ |
---|
72 | /// |
---|
73 | /// @return standard deviation of mean |
---|
74 | /// |
---|
75 | inline double standard_error(void) const |
---|
76 | { return sqrt(squared_sum()/((sum_w()*sum_w())-squared_sum()) |
---|
77 | *squared_error()/sum_w()); } |
---|
78 | |
---|
79 | /// |
---|
80 | /// Calculating the squared sum |
---|
81 | /// |
---|
82 | /// @return \f$ \sum w_i^2(x_i-m)^2 \f$ |
---|
83 | /// |
---|
84 | inline double squared_sum(void) const |
---|
85 | { return (sum_wwxx()-2*mean()*wwx_+ |
---|
86 | mean()*mean()*sum_ww()) ; } |
---|
87 | |
---|
88 | /// |
---|
89 | /// Calculating the sum of weights: \f$ \sum |
---|
90 | /// w_i \f$ @return sum of weights |
---|
91 | /// |
---|
92 | inline double sum_w(void) const |
---|
93 | { return w_.sum_x(); } |
---|
94 | |
---|
95 | /// |
---|
96 | /// \f$ \sum w_ix_i \f$ @return weighted sum of x |
---|
97 | /// |
---|
98 | inline double sum_wx(void) const |
---|
99 | { return wx_.sum_x(); } |
---|
100 | |
---|
101 | |
---|
102 | private: |
---|
103 | inline double sum_ww(void) const |
---|
104 | { return w_.sum_xsqr(); } |
---|
105 | /// |
---|
106 | /// @return \f$ \sum w_i^2x_i^2 \f$ |
---|
107 | /// |
---|
108 | inline double sum_wwxx(void) const |
---|
109 | { return wx_.sum_xsqr(); } |
---|
110 | |
---|
111 | /// |
---|
112 | /// @return \f$ \sum w_i^2x_i \f$ |
---|
113 | /// |
---|
114 | inline double sum_wwx(void) const |
---|
115 | { return wwx_; } |
---|
116 | |
---|
117 | /// |
---|
118 | /// operator to add a WeightedAverager |
---|
119 | /// |
---|
120 | inline WeightedAverager operator+=(WeightedAverager& a) |
---|
121 | { wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); return *this; } |
---|
122 | |
---|
123 | Averager w_; |
---|
124 | Averager wx_; |
---|
125 | double wwx_; |
---|
126 | |
---|
127 | inline Averager wx(void) const {return wx_;} |
---|
128 | inline Averager w(void) const {return w_;} |
---|
129 | |
---|
130 | |
---|
131 | }; |
---|
132 | |
---|
133 | /// |
---|
134 | /// The WeightedAverager output operator |
---|
135 | /// |
---|
136 | std::ostream& operator<<(std::ostream& s,const WeightedAverager&); |
---|
137 | |
---|
138 | }} // of namespace statistics and namespace theplu |
---|
139 | |
---|
140 | #endif |
---|