Changeset 397
 Timestamp:
 Oct 13, 2005, 2:27:14 PM (16 years ago)
 Location:
 trunk/lib/statistics
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/lib/statistics/AveragerWeighted.cc
r295 r397 12 12 13 13 AveragerWeighted::AveragerWeighted(void) 14 : w_(Averager()), wx_(Averager()), wwx_(0) 15 { 16 } 17 18 AveragerWeighted::AveragerWeighted(const double d, const double w) 19 : w_(Averager(w,w*w,1)), wx_(Averager(w*d,w*w*d*d,1)), wwx_(w*w*d) 14 : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0) 20 15 { 21 16 } 22 17 23 18 AveragerWeighted::AveragerWeighted(const AveragerWeighted& a) 24 : w_(Averager(a.sum_w(), a.sum_ww(),1)), 25 wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), wwx_(a.sum_wwx()) 19 : w_(Averager(a.sum_w(),a.sum_ww(),1)), 20 wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), 21 wwx_(a.sum_wwx()), 22 wxx_(a.sum_wxx()) 26 23 { 24 } 25 26 AveragerWeighted AveragerWeighted::operator+=(const AveragerWeighted& a) 27 { 28 wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); wxx_+=a.sum_wxx(); 29 return *this; 27 30 } 28 31 
trunk/lib/statistics/AveragerWeighted.h
r295 r397 6 6 #include <c++_tools/statistics/Averager.h> 7 7 8 #include <cmath> 8 9 #include <ostream> 9 10 … … 17 18 /// @see Averager 18 19 /// 20 /// @todo Check the implementation. Add link to peters 21 /// stats_weighted.tex document. Make Averager and Averager* 22 /// interface more coherent 23 /// 19 24 class AveragerWeighted 20 25 { … … 27 32 28 33 /// 29 /// Constructor taking the data point, i.e. the value and its30 /// weight (default = 1)31 ///32 AveragerWeighted(const double, const double=1);33 34 ///35 34 /// Copy constructor 36 35 /// … … 41 40 /// 42 41 inline void add(const double d,const double w=1) 43 { w_.add(w); wx_.add(w*d); wwx_+=w*w*d;}42 { w_.add(w); wx_.add(w*d); wwx_+=w*w*d; wxx_+=w*d*d; } 44 43 45 44 /// … … 55 54 /// \f$ x = a * x \f$ 56 55 /// 57 inline void rescale(double a) { wx_.rescale(a); wwx_*=a; }56 inline void rescale(double a) { wx_.rescale(a); wwx_*=a; wxx_*=a*a; } 58 57 59 58 /// 60 59 /// resets everything to zero 61 60 /// 62 inline void reset(void) { wx_.reset(); w_.reset(); wwx_=0; }61 inline void reset(void) { wx_.reset(); w_.reset(); wwx_=0; wxx_=0; } 63 62 64 63 /// 65 64 /// Calculating the squared error according to: \f$ \frac{\sum 66 /// w_i^2(x_im)^2}{(\sum w_i)^2} \f$ @return squared error 65 /// w_i^2(x_im)^2}{(\sum w_i)^2} \f$ 66 /// 67 /// @return squared error 67 68 /// 68 69 inline double squared_error(void) const … … 77 78 /// 78 79 inline double standard_error(void) const 79 { return sqrt(squared_sum()/((sum_w()*sum_w())squared_sum()) 80 *squared_error()/sum_w()); } 80 { return sqrt(sum_ww()/((sum_w()*sum_w())sum_ww()) * 81 sum_xx_centered()/sum_w()); } 82 83 /// 84 /// The standard deviation is defined as the square root of the 85 /// variance. 86 /// 87 /// @return The standard deviation, root of the variance(). 88 /// 89 inline double std(void) const { return sqrt(variance()); } 81 90 82 91 /// … … 86 95 /// 87 96 inline double squared_sum(void) const 88 { return (sum_wwxx()2*mean()*wwx_+ 89 mean()*mean()*sum_ww()) ; } 97 { return (sum_wwxx()2*mean()*wwx_+ mean()*mean()*sum_ww()) ; } 90 98 91 99 /// … … 102 110 { return wx_.sum_x(); } 103 111 112 /// 113 /// The variance is calculated as \f$ \frac{\sum w_i}{\left(\sum 114 /// w_i\right)^2  \sum w_i^2} \sum w_i (x_i  m)^2 \f$. This 115 /// variance is the same as the one implemented in GSL. 116 /// 117 /// @return The variance. 118 /// 119 inline double variance(void) const 120 { return sum_ww() / (sum_w()*sum_w()sum_ww()) * sum_xx_centered(); } 121 122 /// 123 /// @return \f$ \sum_i w_i (x_im)^2\f$ 124 /// 125 inline double sum_xx_centered(void) const 126 { return sum_wxx()  mean()*mean()*sum_w(); } 104 127 105 128 private: … … 119 142 120 143 /// 144 /// @return \f$ \sum w_i x_i^2 \f$ 145 /// 146 inline double sum_wxx(void) const { return wxx_; } 147 148 /// 121 149 /// operator to add a AveragerWeighted 122 150 /// 123 inline AveragerWeighted operator+=(AveragerWeighted& a) 124 { wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); return *this; } 151 AveragerWeighted operator+=(const AveragerWeighted&); 125 152 126 153 Averager w_; 127 154 Averager wx_; 128 155 double wwx_; 156 double wxx_; 129 157 130 158 inline Averager wx(void) const {return wx_;}
Note: See TracChangeset
for help on using the changeset viewer.