source: trunk/c++_tools/statistics/AveragerPair.h @ 593

Last change on this file since 593 was 593, checked in by Markus Ringnér, 15 years ago

Fixed std includes to compile with g++ 4.1.

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