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

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

file structure modifications. NOTE, this revision is not working, please wait for the next...

  • 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 295 2005-04-29 09:15:58Z 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{
13
14  class gslapi::vector;
15
16namespace statistics{
17  ///
18  /// Class for taking care of mean and covariance of two variables.
19  ///
20  class AveragerPair
21  {
22  public:
23
24    ///
25    /// Default constructor
26    ///
27    AveragerPair(void);
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    AveragerPair(const double x, const double xx, const double y, 
34                 const double yy, const double xy, const long);
35
36    ///
37    /// Copy constructor
38    ///
39    AveragerPair(const AveragerPair&);
40   
41    ///
42    /// Adding \a n pairs of data points with value \a x and \a y.
43    ///
44    inline void add(const double x, const double y, const long n=1)
45      { x_.add(x,n); y_.add(y,n), xy_ += n*x*y; }
46
47    ///
48    /// \f$\frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
49    /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}}\f$
50    ///
51    /// @return Concordence correlation coefficient.
52    ///
53    inline double ccc(void) const 
54      { return ( (x_.variance() || y_.variance() || x_.mean()-y_.mean() ) ? 
55                 ((2*covariance()) / 
56                  ((x_.variance()+y_.variance()) +
57                   (x_.mean()-y_.mean())*(x_.mean()-y_.mean()))) : 0); }
58 
59    ///
60    /// \f$\frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
61    /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}}\f$
62    ///
63    /// @return Pearson correlation coefficient.
64    ///
65    inline double correlation(void) const 
66      { return ((x_.std()>0 && y_.std()>0) ? 
67                (covariance() / (x_.std()*y_.std()) ) : 0); }
68 
69    ///
70    /// The covariance is calculated using the \f$ (n-1) \f$
71    /// correction, which means it is the best unbiased estimator of
72    /// the covariance \f$ \frac{1}{N-1}\sum_i (x_i-m_x)(y_i-m_y)\f$,
73    /// where \f$m\f$ is the mean.
74    ///
75    /// @return The covariance.
76    ///
77    inline double covariance(void) const 
78      { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / (n()-1): 0; }
79 
80    ///
81    /// @return The mean of xy.
82    ///
83    inline double mean_xy(void) const { return xy_/n(); }
84
85    ///
86    /// @return Average squared deviation between x and y \f$
87    /// \frac{1}{N} \sum (x-y)^2 \f$
88    ///
89    inline double msd() const {return x_.mean_sqr()+y_.mean_sqr()-2*mean_xy();}
90
91    ///
92    /// @return The number of pair of data points.
93    ///
94    inline long n(void) const { return x_.n(); }
95
96    ///
97    /// Resets everything to zero
98    ///
99    inline void reset(void) { x_.reset(); y_.reset(); xy_=0.0; }
100
101    ///
102    /// @return The sum of xy.
103    ///
104    inline double sum_xy(void) const { return xy_; }
105
106    ///
107    /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y)\f$
108    ///
109    inline double sum_xy_centered(void) const {return xy_-x_.sum_x()*y_.mean();}
110
111    ///
112    /// @return A const refencer to the averager object for x.
113    ///
114    inline const Averager& x_averager(void) const { return x_; }
115
116    ///
117    /// @return A const reference to the averager object for y
118    ///
119    inline const Averager& y_averager(void) const { return y_; }
120
121    ///
122    /// The assigment operator
123    ///
124    inline const AveragerPair& operator=(const AveragerPair& a)
125      { x_=a.x_; y_=a.y_; xy_=a.xy_; return *this; }
126
127    ///
128    /// Operator to add another Averager
129    ///
130    const AveragerPair& operator+=(const AveragerPair&);
131
132  private:
133    Averager x_;
134    Averager y_;
135    double  xy_;
136
137  };
138
139}} // of namespace statistics and namespace theplu
140
141#endif
Note: See TracBrowser for help on using the repository browser.