source: trunk/yat/statistics/AveragerPair.h @ 703

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

Addresses #65 and #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpair_
2#define _theplu_yat_statistics_averagerpair_
3
4// $Id: AveragerPair.h 703 2006-12-18 00:47:44Z jari $
5
6/*
7  Copyright (C) The authors contributing to this file.
8
9  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "Averager.h"
28
29#include <cassert>
30#include <cmath>
31#include <utility>
32
33namespace theplu{
34namespace yat{
35namespace statistics{
36
37  ///
38  /// Class for taking care of mean and covariance of two variables.
39  ///
40  /// @see Averager AveragerWeighted AveragerPairWeighted
41  ///
42  class AveragerPair
43  {
44  public:
45
46    ///
47    /// @brief The default constructor
48    ///
49    AveragerPair(void);
50
51    ///
52    /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and
53    /// number of pair of values \a n
54    ///
55    //inline AveragerPair(const double x, const double xx, const double y,
56    //                    const double yy, const double xy, const unsigned long n)
57    //  : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy) {}
58
59    ///
60    /// The copy constructor
61    ///
62    AveragerPair(const AveragerPair&);
63
64    ///
65    /// Adding \a n pairs of data points with value \a x and \a y.
66    ///
67    void add(const double x, const double y, const unsigned long n=1);
68
69    ///
70    /// Adding pairs of arrays with data points \a x and \a y.
71    ///
72    /// The requirements for the types T1 and T2 of the arrays \a x
73    /// and \a y are: operator[] returning an element and function
74    /// size() returning the number of elements.
75    ///   
76    template <typename T1, typename T2>
77    void add_values(const T1& x, const T2& y);
78
79    ///
80    /// \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sum_i
81    /// (x_i-m_x)^2+\sum_i (y_i-m_y)^2 + n(m_x-m_y)^2} \f$
82    ///
83    /// In case of a zero denominator - zero is returned.
84    ///
85    /// @return Concordence correlation coefficient.
86    ///
87    inline double ccc(void) const 
88      { return ( (x_.variance() && y_.variance() && x_.mean()-y_.mean() ) ? 
89                 ((2*covariance()) / 
90                  ((x_.variance()+y_.variance()) +
91                   (x_.mean()-y_.mean())*(x_.mean()-y_.mean()))) : 0); }
92 
93    ///
94    /// \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
95    /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}} \f$
96    ///
97    /// @return Pearson correlation coefficient.
98    ///
99    inline double correlation(void) const 
100      { return ((x_.std()>0 && y_.std()>0) ? 
101                (covariance() / (x_.std()*y_.std()) ) : 0); }
102 
103    ///
104    /// Calculating covariance using
105    /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y) \f$,
106    /// where \f$ m \f$ is the mean.
107    ///
108    /// @return The covariance.
109    ///
110    inline double covariance(void) const 
111      { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / n(): 0; }
112 
113    ///
114    /// @return The mean of xy.
115    ///
116    inline double mean_xy(void) const { return xy_/n(); }
117
118    ///
119    /// @return Average squared deviation between x and y \f$
120    /// \frac{1}{N} \sum (x-y)^2 \f$
121    ///
122    inline double msd() const 
123    {return ( x_averager().sum_xx()+y_averager().sum_xx()-2*sum_xy() )/n();}
124
125    ///
126    /// @return The number of pair of data points.
127    ///
128    inline unsigned long n(void) const { return x_.n(); }
129
130    ///
131    /// @brief Reset everything to zero
132    ///
133    void reset(void);
134
135    ///
136    /// @return The sum of xy.
137    ///
138    inline double sum_xy(void) const { return xy_; }
139
140    ///
141    /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y) \f$
142    ///
143    inline double sum_xy_centered(void) const {return xy_-x_.sum_x()*y_.mean();}
144
145    ///
146    /// @return A const refencer to the averager object for x.
147    ///
148    inline const Averager& x_averager(void) const { return x_; }
149
150    ///
151    /// @return A const reference to the averager object for y
152    ///
153    inline const Averager& y_averager(void) const { return y_; }
154
155    ///
156    /// @brief The assigment operator
157    ///
158    const AveragerPair& operator=(const AveragerPair& a);
159
160    ///
161    /// Operator to add another Averager
162    ///
163    const AveragerPair& operator+=(const AveragerPair&);
164
165  private:
166    Averager x_;
167    Averager y_;
168    double  xy_;
169
170  };
171
172  // Template implementations
173  template <typename T1, typename T2>
174  void AveragerPair::add_values(const T1& x, const T2& y)
175  {
176    assert(x.size()==y.size());
177    for (size_t i=0; i<x.size(); i++) 
178      add(x[i],y[i]);
179  }
180
181
182}}} // of namespace statistics, yat, and theplu
183
184#endif
Note: See TracBrowser for help on using the repository browser.