Changeset 397


Ignore:
Timestamp:
Oct 13, 2005, 2:27:14 PM (16 years ago)
Author:
Jari Häkkinen
Message:

Fixed issues in AveragerWeighted?, added todo items.

Location:
trunk/lib/statistics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/statistics/AveragerWeighted.cc

    r295 r397  
    1212
    1313  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)
    2015  {
    2116  }
    2217
    2318  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())
    2623  {
     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;
    2730  }
    2831
  • trunk/lib/statistics/AveragerWeighted.h

    r295 r397  
    66#include <c++_tools/statistics/Averager.h>
    77
     8#include <cmath>
    89#include <ostream>
    910
     
    1718  /// @see Averager
    1819  ///
     20  /// @todo Check the implementation. Add link to peters
     21  /// stats_weighted.tex document. Make Averager and Averager*
     22  /// interface more coherent
     23  ///
    1924  class AveragerWeighted
    2025  {
     
    2732
    2833    ///
    29     /// Constructor taking the data point, i.e. the value and its
    30     /// weight (default = 1)
    31     ///
    32     AveragerWeighted(const double, const double=1);
    33 
    34     ///
    3534    /// Copy constructor
    3635    ///
     
    4140    ///
    4241    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; }
    4443
    4544    ///
     
    5554    /// \f$ x = a * x \f$
    5655    ///
    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; }
    5857
    5958    ///
    6059    /// resets everything to zero
    6160    ///
    62     inline void reset(void) { wx_.reset(); w_.reset(); wwx_=0;}
     61    inline void reset(void) { wx_.reset(); w_.reset(); wwx_=0; wxx_=0; }
    6362
    6463    ///
    6564    /// Calculating the squared error according to: \f$ \frac{\sum
    66     /// w_i^2(x_i-m)^2}{(\sum w_i)^2} \f$ @return squared error
     65    /// w_i^2(x_i-m)^2}{(\sum w_i)^2} \f$
     66    ///
     67    /// @return squared error
    6768    ///
    6869    inline double squared_error(void) const
     
    7778    ///
    7879    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()); }
    8190
    8291    ///
     
    8695    ///
    8796    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()) ; }
    9098
    9199    ///
     
    102110    { return wx_.sum_x(); }
    103111
     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_i-m)^2\f$
     124    ///
     125    inline double sum_xx_centered(void) const
     126    { return sum_wxx() - mean()*mean()*sum_w(); }
    104127
    105128  private:
     
    119142
    120143    ///
     144    ///  @return \f$ \sum w_i x_i^2 \f$
     145    ///
     146    inline double sum_wxx(void) const { return wxx_; }
     147
     148    ///
    121149    /// operator to add a AveragerWeighted
    122150    ///
    123     inline AveragerWeighted operator+=(AveragerWeighted& a)
    124     { wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); return *this; }
     151    AveragerWeighted operator+=(const AveragerWeighted&);
    125152   
    126153    Averager w_;
    127154    Averager wx_;
    128155    double wwx_;
     156    double wxx_;
    129157   
    130158    inline Averager wx(void) const {return wx_;}
Note: See TracChangeset for help on using the changeset viewer.