source: trunk/src/AveragerPair.h @ 219

Last change on this file since 219 was 219, checked in by Peter, 18 years ago

sum_xy_centered(void) added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1// $Id: AveragerPair.h 219 2004-12-30 22:30:17Z peter $
2
3#ifndef _theplu_cpptools_averagerpair_
4#define _theplu_cpptools_averagerpair_
5
6#include <cmath>
7#include <utility>
8
9#include "Averager.h"
10
11namespace theplu{
12
13  class gslapi::vector;
14
15namespace statistics{
16  ///
17  /// Class for taking care of mean and covariance of two variables.
18  ///
19  class AveragerPair
20  {
21  public:
22
23    ///
24    /// Default constructor
25    ///
26    AveragerPair(void);
27   
28    ///
29    /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and
30    /// number of pair of values \a n
31    ///
32    AveragerPair(const double x, const double xx, const double y, 
33                 const double yy, const double xy, const long);
34
35    ///
36    /// Copy constructor
37    ///
38    AveragerPair(const AveragerPair&);
39   
40    ///
41    /// Adding \a n pairs of data points with value \a x and \a y.
42    ///
43    inline void add(const double x, const double y, const long n=1)
44      { x_.add(x,n); y_.add(y,n), xy_ += n*x*y; }
45
46    ///
47    /// \f$\frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
48    /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}}\f$
49    ///
50    /// @return Pearson correlation.
51    ///
52    inline double correlation(void) const 
53      { return ((x_.std()>0 && y_.std()>0) ? 
54                (covariance() / (x_.std()*y_.std()) ) : 0); }
55 
56    ///
57    /// The covariance is calculated using the \f$ (n-1) \f$
58    /// correction, which means it is the best unbiased estimator of
59    /// the covariance \f$ \frac{1}{N-1}\sum_i (x_i-m_x)(y_i-m_y)\f$,
60    /// where \f$m\f$ is the mean.
61    ///
62    /// @return The covariance.
63    ///
64    inline double covariance(void) const 
65      { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / (n()-1): 0; }
66 
67    ///
68    /// @return The mean of xy.
69    ///
70    inline double mean_xy(void) const { return xy_/n(); }
71
72    ///
73    /// @return Average squared difference between x and y \f$
74    /// \frac{1}{N} \sum (x-y)^2 \f$
75    ///
76    inline double mse(void) { return x_.mean_sqr()+y_.mean_sqr()-2*mean_xy(); }
77
78    ///
79    /// @return The number of pair of data points.
80    ///
81    inline long n(void) const { return x_.n(); }
82
83    ///
84    /// Resets everything to zero
85    ///
86    inline void reset(void) { x_.reset(); y_.reset(); xy_=0.0; }
87
88    ///
89    /// @return The sum of xy.
90    ///
91    inline double sum_xy(void) const { return xy_; }
92
93    ///
94    /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y)\f$
95    ///
96    inline double sum_xy_centered(void) const {return xy_-x_.sum_x()*y_.mean();}
97
98    ///
99    /// @return A const refencer to the averager object for x.
100    ///
101    inline const Averager& x_averager(void) const { return x_; }
102
103    ///
104    /// @return A const reference to the averager object for y
105    ///
106    inline const Averager& y_averager(void) const { return y_; }
107
108    ///
109    /// The assigment operator
110    ///
111    inline const AveragerPair& operator=(const AveragerPair& a)
112      { x_=a.x_; y_=a.y_; xy_=a.xy_; return *this; }
113
114    ///
115    /// Operator to add another Averager
116    ///
117    const AveragerPair& operator+=(const AveragerPair&);
118
119  private:
120    Averager x_;
121    Averager y_;
122    double  xy_;
123
124  };
125
126}} // of namespace statistics and namespace theplu
127
128#endif
Note: See TracBrowser for help on using the repository browser.