source: trunk/src/AveragerPair.h @ 230

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

Interface changed. mse changed to be msd and ccc statistics added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1// $Id: AveragerPair.h 230 2005-02-21 14:48:15Z 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 Concordence correlation coefficient.
51    ///
52    inline double ccc(void) const 
53      { return ( (x_.variance()>0 || y_.variance()>0 || msd()) ? 
54                 (covariance() / (x_.variance()+y_.variance()+msd()) ) : 0); }
55 
56    ///
57    /// \f$\frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
58    /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}}\f$
59    ///
60    /// @return Pearson correlation coefficient.
61    ///
62    inline double correlation(void) const 
63      { return ((x_.std()>0 && y_.std()>0) ? 
64                (covariance() / (x_.std()*y_.std()) ) : 0); }
65 
66    ///
67    /// The covariance is calculated using the \f$ (n-1) \f$
68    /// correction, which means it is the best unbiased estimator of
69    /// the covariance \f$ \frac{1}{N-1}\sum_i (x_i-m_x)(y_i-m_y)\f$,
70    /// where \f$m\f$ is the mean.
71    ///
72    /// @return The covariance.
73    ///
74    inline double covariance(void) const 
75      { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / (n()-1): 0; }
76 
77    ///
78    /// @return The mean of xy.
79    ///
80    inline double mean_xy(void) const { return xy_/n(); }
81
82    ///
83    /// @return Average squared deviation between x and y \f$
84    /// \frac{1}{N} \sum (x-y)^2 \f$
85    ///
86    inline double msd() const {return x_.mean_sqr()+y_.mean_sqr()-2*mean_xy();}
87
88    ///
89    /// @return The number of pair of data points.
90    ///
91    inline long n(void) const { return x_.n(); }
92
93    ///
94    /// Resets everything to zero
95    ///
96    inline void reset(void) { x_.reset(); y_.reset(); xy_=0.0; }
97
98    ///
99    /// @return The sum of xy.
100    ///
101    inline double sum_xy(void) const { return xy_; }
102
103    ///
104    /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y)\f$
105    ///
106    inline double sum_xy_centered(void) const {return xy_-x_.sum_x()*y_.mean();}
107
108    ///
109    /// @return A const refencer to the averager object for x.
110    ///
111    inline const Averager& x_averager(void) const { return x_; }
112
113    ///
114    /// @return A const reference to the averager object for y
115    ///
116    inline const Averager& y_averager(void) const { return y_; }
117
118    ///
119    /// The assigment operator
120    ///
121    inline const AveragerPair& operator=(const AveragerPair& a)
122      { x_=a.x_; y_=a.y_; xy_=a.xy_; return *this; }
123
124    ///
125    /// Operator to add another Averager
126    ///
127    const AveragerPair& operator+=(const AveragerPair&);
128
129  private:
130    Averager x_;
131    Averager y_;
132    double  xy_;
133
134  };
135
136}} // of namespace statistics and namespace theplu
137
138#endif
Note: See TracBrowser for help on using the repository browser.