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

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

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpair_
2#define _theplu_yat_statistics_averagerpair_
3
4// $Id: AveragerPair.h 1487 2008-09-10 08:41:36Z jari $
5
6/*
7  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
10
11  This file is part of the yat library, http://dev.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 3 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with yat. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "Averager.h"
28
29#include "yat/utility/iterator_traits.h"
30
31#include <cmath>
32#include <utility>
33
34namespace theplu{
35namespace yat{
36namespace statistics{
37
38  ///
39  /// @brief Class for taking care of mean and covariance of two variables.
40  ///
41  /// @see Averager AveragerWeighted AveragerPairWeighted
42  ///
43  class AveragerPair
44  {
45  public:
46
47    ///
48    /// @brief The default constructor
49    ///
50    AveragerPair(void);
51
52    ///
53    /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and
54    /// number of pair of values \a n
55    ///
56    //AveragerPair(const double x, const double xx, const double y,
57    //                    const double yy, const double xy, const unsigned long n)
58    //  : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy) {}
59
60    ///
61    /// The copy constructor
62    ///
63    AveragerPair(const AveragerPair&);
64
65    ///
66    /// Adding \a n pairs of data points with value \a x and \a y.
67    ///
68    void add(const double x, const double y, const long n=1);
69
70    /**
71       \brief Concordence correlation coefficient.
72
73       \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sum_i
74       (x_i-m_x)^2+\sum_i (y_i-m_y)^2 + n(m_x-m_y)^2} \f$
75       
76    */
77    double ccc(void) const;
78
79    /**
80       \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
81       (x_i-m_x)^2\sum_i (y_i-m_y)^2}} \f$
82       
83       \return %Pearson correlation coefficient.
84    */
85    double correlation(void) const;
86 
87    ///
88    /// Calculating covariance using
89    /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y) \f$,
90    /// where \f$ m \f$ is the mean.
91    ///
92    /// @return The covariance.
93    ///
94    double covariance(void) const;
95 
96    ///
97    /// @return The mean of xy.
98    ///
99    double mean_xy(void) const;
100
101    /**
102       \return Average squared deviation between x and y \f$
103       \frac{1}{N} \sum (x-y)^2 \f$
104    */
105    double msd(void) const;
106
107    ///
108    /// @return The number of pair of data points.
109    ///
110    long n(void) const;
111
112    ///
113    /// @brief Reset everything to zero
114    ///
115    void reset(void);
116
117    ///
118    /// @return The sum of xy.
119    ///
120    double sum_xy(void) const;
121
122    /**
123       \return Sum of squared deviation between x and y \f$
124       \sum (x-y)^2 \f$
125    */
126    double sum_squared_deviation(void) const;
127
128    ///
129    /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y) \f$
130    ///
131    double sum_xy_centered(void) const;
132
133    ///
134    /// @return A const refencer to the averager object for x.
135    ///
136    const Averager& x_averager(void) const;
137
138    ///
139    /// @return A const reference to the averager object for y
140    ///
141    const Averager& y_averager(void) const;
142
143    ///
144    /// @brief The assigment operator
145    ///
146    const AveragerPair& operator=(const AveragerPair& a);
147
148    ///
149    /// Operator to add another Averager
150    ///
151    const AveragerPair& operator+=(const AveragerPair&);
152
153  private:
154    Averager x_;
155    Averager y_;
156    double  xy_;
157
158  };
159
160  /**
161     \brief adding data from two ranges to AveragerPair \a ap
162   */
163  template <class Iter1, class Iter2>
164  void add(AveragerPair& ap, Iter1 first1, Iter1 last1, Iter2 first2)
165  {
166    utility::check_iterator_is_unweighted(first1);
167    utility::check_iterator_is_unweighted(first2);
168    for ( ; first1 != last1; ++first1, ++first2)
169      ap.add(*first1, *first2);
170  }
171
172
173}}} // of namespace statistics, yat, and theplu
174
175#endif
Note: See TracBrowser for help on using the repository browser.