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

Last change on this file since 496 was 496, checked in by Peter, 16 years ago

implemented AveragerPair::msd()

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