source:trunk/src/AveragerPair.h@197

Last change on this file since 197 was 197, checked in by Jari Häkkinen, 18 years ago

Cleanup interfaces for Averager classes and move stuff into statsitics
namespace.

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