# source:trunk/src/AveragerPair.h@252

Last change on this file since 252 was 252, checked in by Peter, 17 years ago

corrected calculation of ccc

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