source: trunk/lib/statistics/AveragerPair.h @ 510

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

fixed nan return in AveragerPair::ccc()

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