Changeset 489


Ignore:
Timestamp:
Jan 4, 2006, 8:42:00 PM (17 years ago)
Author:
Peter
Message:

added functions to AveragerPairWeighted?. Not tested yet.

Location:
trunk
Files:
7 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/doc/Makefile.am

    r320 r489  
    33## $Id$
    44
    5 all-local:
    6   doxygen doxygen.config
     5all-local: doxygen-local Statistics
    76
    87clean-local:
     
    109
    1110distclean-local: clean-local
     11
     12doxygen-local:
     13  doxygen doxygen.config
     14
     15Statistics: Statistics.tex
     16  latex2html Statistics.tex
  • trunk/lib/statistics/Averager.h

    r484 r489  
    1616  /// Class to calculate simple (first and second moments) averages.
    1717  ///
    18   /// @see AveragerWeighted
     18  /// @see Averager AveragerPair AveragerPairWeighted
    1919  ///
    2020  class Averager
  • trunk/lib/statistics/AveragerPair.h

    r487 r489  
    1515  ///
    1616  /// Class for taking care of mean and covariance of two variables.
     17  ///
     18  /// @see Averager AveragerPair AveragerPairWeighted
    1719  ///
    1820  class AveragerPair
  • trunk/lib/statistics/AveragerPairWeighted.h

    r476 r489  
    1717  /// a weighted manner.
    1818  ///
     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  ///
    1930  class AveragerPairWeighted
    2031  {
     
    2839    ///
    2940    /// 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
    3143    ///
    3244    void  add(const double x, const double y,
     
    3446
    3547    ///
    36     /// Adding vectors
     48    /// Adding pair of data and corresponding weight in vectors
    3749    ///
    3850    void add(const gslapi::vector& x,
     
    4254
    4355    ///
    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$
    4561    ///
    4662    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() ); }
    4864 
    4965    ///
    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
    5173    ///
    5274    inline void reset(void) { x_.reset(); y_.reset(); wxy_=0; w_=0; }
    5375
     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
    54107  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
    57110    double wxy_;
    58111    double w_;
  • trunk/lib/statistics/AveragerWeighted.h

    r486 r489  
    120120
    121121    ///
     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    ///
    122130    /// @return \f$ \sum_i w_i (x_i-m)^2\f$
    123131    ///
     
    148156
    149157  private:
    150     ///
    151     /// \f$ \sum w_ix_i \f$ @return weighted sum of x
    152     ///
    153     inline double sum_wx(void)  const
    154     { return wx_.sum_x(); }
    155 
    156158    inline double sum_ww(void)  const
    157159    { return w_.sum_xx(); }
  • trunk/lib/statistics/LinearWeighted.cc

    r429 r489  
    33#include <c++_tools/statistics/LinearWeighted.h>
    44
    5 #include <c++_tools/statistics/AveragerPair.h>
     5#include <c++_tools/statistics/AveragerPairWeighted.h>
    66#include <c++_tools/gslapi/vector.h>
    77
     
    1818                           const gslapi::vector& w)
    1919  {
    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();
    2229   
    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();
    2631
    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();
    3434
    3535    // estimating the noise level. see attached document for motivation
  • trunk/test/regression_test.cc

    r430 r489  
    4848  double y_predicted_err=0;
    4949  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;
    5254    ok=false;
    5355  }
Note: See TracChangeset for help on using the changeset viewer.