Changeset 489
- Timestamp:
- Jan 4, 2006, 8:42:00 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/Makefile.am
r320 r489 3 3 ## $Id$ 4 4 5 all-local: 6 doxygen doxygen.config 5 all-local: doxygen-local Statistics 7 6 8 7 clean-local: … … 10 9 11 10 distclean-local: clean-local 11 12 doxygen-local: 13 doxygen doxygen.config 14 15 Statistics: Statistics.tex 16 latex2html Statistics.tex -
trunk/lib/statistics/Averager.h
r484 r489 16 16 /// Class to calculate simple (first and second moments) averages. 17 17 /// 18 /// @see Averager Weighted18 /// @see Averager AveragerPair AveragerPairWeighted 19 19 /// 20 20 class Averager -
trunk/lib/statistics/AveragerPair.h
r487 r489 15 15 /// 16 16 /// Class for taking care of mean and covariance of two variables. 17 /// 18 /// @see Averager AveragerPair AveragerPairWeighted 17 19 /// 18 20 class AveragerPair -
trunk/lib/statistics/AveragerPairWeighted.h
r476 r489 17 17 /// a weighted manner. 18 18 /// 19 /// <a href="../Statistics/index.html">Weighted Statistics document</a> 20 /// 21 /// If nothing else stated, each function fulfills the 22 /// following:<br> <ul><li>Setting a weight to zero corresponds to 23 /// removing the data point from the dataset.</li><li> Setting all 24 /// weights to unity, the yields the same result as from 25 /// corresponding function in AveragerPair.</li><li> Rescaling weights 26 /// does not change the performance of the object.</li></ul> 27 /// 28 /// @see Averager AveragerPair AveragerPairWeighted 29 /// 19 30 class AveragerPairWeighted 20 31 { … … 28 39 /// 29 40 /// Adding a pair of data points with value \a x and \a y, and 30 /// their weights. 41 /// their weights. If either of the weights are zero the addition 42 /// is ignored 31 43 /// 32 44 void add(const double x, const double y, … … 34 46 35 47 /// 36 /// Adding vectors48 /// Adding pair of data and corresponding weight in vectors 37 49 /// 38 50 void add(const gslapi::vector& x, … … 42 54 43 55 /// 44 /// @return Pearson correlation coefficient. 56 /// @brief Pearson correlation coefficient. 57 /// 58 /// @return \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sqrt{\sum 59 /// w_xw_y (y-m_y)^2\sum w_xw_y (y-m_y)^2}} \f$ where m is 60 /// calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$ 45 61 /// 46 62 inline double correlation(void) const 47 { return (wxy_/w_ - x_.mean()*y_.mean() )/ sqrt(x_.variance()*y_.variance()); }63 { return covariance() / ( x_.std()*y_.std() ); } 48 64 49 65 /// 50 /// @reset 66 /// \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sum w_xw_y} \f$ where m 67 /// is calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$ 68 /// 69 inline double covariance(void) const { return sum_xy_centered()/sum_w(); } 70 71 /// 72 /// reset everything to zero 51 73 /// 52 74 inline void reset(void) { x_.reset(); y_.reset(); wxy_=0; w_=0; } 53 75 76 /// 77 /// @return \f$ \sum w_xw_y \f$ 78 /// 79 inline double sum_w(void) const { return w_; } 80 81 /// 82 /// @return \f$ \sum w_xw_yxy \f$ 83 /// 84 inline double sum_xy(void) const { return wxy_; } 85 86 /// 87 /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as 88 /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$ 89 /// 90 inline double sum_xy_centered(void) const 91 { return sum_xy() - x_.sum_wx()*y_.mean(); } 92 93 /// 94 /// @note the weights are calculated as \f$ w = w_x * w_y \f$ 95 /// 96 /// @return AveragerWeighted for x 97 /// 98 inline const AveragerWeighted& x_averager(void) const { return x_; } 99 100 /// 101 /// @note the weights are calculated as \f$ w = w_x * w_y \f$ 102 /// 103 /// @return AveragerWeighted for y 104 /// 105 inline const AveragerWeighted& y_averager(void) const { return y_; } 106 54 107 private: 55 AveragerWeighted x_; 56 AveragerWeighted y_; 108 AveragerWeighted x_; // weighted averager with w = w_x*w_y 109 AveragerWeighted y_; // weighted averager with w = w_x*w_y 57 110 double wxy_; 58 111 double w_; -
trunk/lib/statistics/AveragerWeighted.h
r486 r489 120 120 121 121 /// 122 /// \f$ \sum w_ix_i \f$ 123 /// 124 /// @return weighted sum of x 125 /// 126 inline double sum_wx(void) const 127 { return wx_.sum_x(); } 128 129 /// 122 130 /// @return \f$ \sum_i w_i (x_i-m)^2\f$ 123 131 /// … … 148 156 149 157 private: 150 ///151 /// \f$ \sum w_ix_i \f$ @return weighted sum of x152 ///153 inline double sum_wx(void) const154 { return wx_.sum_x(); }155 156 158 inline double sum_ww(void) const 157 159 { return w_.sum_xx(); } -
trunk/lib/statistics/LinearWeighted.cc
r429 r489 3 3 #include <c++_tools/statistics/LinearWeighted.h> 4 4 5 #include <c++_tools/statistics/AveragerPair .h>5 #include <c++_tools/statistics/AveragerPairWeighted.h> 6 6 #include <c++_tools/gslapi/vector.h> 7 7 … … 18 18 const gslapi::vector& w) 19 19 { 20 double m_x = w*x /w.sum(); 21 double m_y = w*y /w.sum(); 20 // AveragerPairWeighted requires 2 weights but works only on the 21 // product wx*wy, so we can send in w and a dummie to get what we 22 // want. 23 gslapi::vector dummie(w.size(),1); 24 AveragerPairWeighted ap; 25 ap.add(x,y,w,dummie); 26 27 double m_x = ap.x_averager().mean(); 28 double m_y = ap.y_averager().mean(); 22 29 23 double sxy = 0; 24 for (size_t i=0; i<x.size(); i++) 25 sxy += w(i)*(x(i)-m_x)*(y(i)-m_y); 30 double sxy = ap.sum_xy_centered(); 26 31 27 double sxx = 0; 28 for (size_t i=0; i<x.size(); i++) 29 sxx += w(i)*(x(i)-m_x)*(x(i)-m_x); 30 31 double syy = 0; 32 for (size_t i=0; i<y.size(); i++) 33 syy += w(i)*(y(i)-m_y)*(y(i)-m_y); 32 double sxx = ap.x_averager().sum_xx_centered(); 33 double syy = ap.y_averager().sum_xx_centered(); 34 34 35 35 // estimating the noise level. see attached document for motivation -
trunk/test/regression_test.cc
r430 r489 48 48 double y_predicted_err=0; 49 49 linear_w.predict(1990,y_predicted,y_predicted_err); 50 if (y_predicted!=12.8){ 51 *error << "regression_LinearWeighted: cannot reproduce fit." << std::endl; 50 if (fabs(y_predicted-12.8)>0.001){ 51 *error << "error: cannot reproduce fit." << std::endl; 52 *error << "predicted value: " << y_predicted << " expected 12.8" 53 << std::endl; 52 54 ok=false; 53 55 }
Note: See TracChangeset
for help on using the changeset viewer.