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

Last change on this file since 912 was 912, checked in by Peter, 14 years ago

Made add function for AveragerPair? no longer accept weighted iterators. refs #246

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpair_
2#define _theplu_yat_statistics_averagerpair_
3
4// $Id: AveragerPair.h 912 2007-09-29 00:53:06Z peter $
5
6/*
7  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
9  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
10
11  This file is part of the yat library, http://trac.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26  02111-1307, USA.
27*/
28
29#include "yat/utility/IteratorTraits.h"
30
31#include "Averager.h"
32
33#include "yat/utility/IteratorTraits.h"
34
35#include <cmath>
36#include <utility>
37
38namespace theplu{
39namespace yat{
40namespace statistics{
41
42  ///
43  /// @brief Class for taking care of mean and covariance of two variables.
44  ///
45  /// @see Averager AveragerWeighted AveragerPairWeighted
46  ///
47  class AveragerPair
48  {
49  public:
50
51    ///
52    /// @brief The default constructor
53    ///
54    AveragerPair(void);
55
56    ///
57    /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and
58    /// number of pair of values \a n
59    ///
60    //AveragerPair(const double x, const double xx, const double y,
61    //                    const double yy, const double xy, const unsigned long n)
62    //  : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy) {}
63
64    ///
65    /// The copy constructor
66    ///
67    AveragerPair(const AveragerPair&);
68
69    ///
70    /// Adding \a n pairs of data points with value \a x and \a y.
71    ///
72    void add(const double x, const double y, const unsigned long n=1);
73
74    ///
75    /// Adding pairs of arrays with data points \a x and \a y.
76    ///
77    /// The requirements for the types T1 and T2 of the arrays \a x
78    /// and \a y are: operator[] returning an element and function
79    /// size() returning the number of elements.
80    ///   
81    template <typename T1, typename T2>
82    void add_values(const T1& x, const T2& y);
83
84    ///
85    /// \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sum_i
86    /// (x_i-m_x)^2+\sum_i (y_i-m_y)^2 + n(m_x-m_y)^2} \f$
87    ///
88    /// In case of a zero denominator - zero is returned.
89    ///
90    /// @return Concordence correlation coefficient.
91    ///
92    double ccc(void) const;
93
94    ///
95    /// \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
96    /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}} \f$
97    ///
98    /// @return Pearson correlation coefficient.
99    ///
100    double correlation(void) const;
101 
102    ///
103    /// Calculating covariance using
104    /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y) \f$,
105    /// where \f$ m \f$ is the mean.
106    ///
107    /// @return The covariance.
108    ///
109    double covariance(void) const;
110 
111    ///
112    /// @return The mean of xy.
113    ///
114    double mean_xy(void) const;
115
116    ///
117    /// @return Average squared deviation between x and y \f$
118    /// \frac{1}{N} \sum (x-y)^2 \f$
119    ///
120    double msd(void) const;
121
122    ///
123    /// @return The number of pair of data points.
124    ///
125    unsigned long n(void) const;
126
127    ///
128    /// @brief Reset everything to zero
129    ///
130    void reset(void);
131
132    ///
133    /// @return The sum of xy.
134    ///
135    double sum_xy(void) const;
136
137    ///
138    /// @return Sum of squared deviation between x and y \f$
139    /// \sum (x-y)^2 \f$
140    ///
141    double sum_squared_deviation(void) const;
142
143    ///
144    /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y) \f$
145    ///
146    double sum_xy_centered(void) const;
147
148    ///
149    /// @return A const refencer to the averager object for x.
150    ///
151    const Averager& x_averager(void) const;
152
153    ///
154    /// @return A const reference to the averager object for y
155    ///
156    const Averager& y_averager(void) const;
157
158    ///
159    /// @brief The assigment operator
160    ///
161    const AveragerPair& operator=(const AveragerPair& a);
162
163    ///
164    /// Operator to add another Averager
165    ///
166    const AveragerPair& operator+=(const AveragerPair&);
167
168  private:
169    Averager x_;
170    Averager y_;
171    double  xy_;
172
173  };
174
175  /**
176     \brief adding a ranges of values to AveragerPair \a ap
177   */
178  template <class Iter1, class Iter2>
179  void add(AveragerPair& ap, Iter1 first1, Iter1 last1, Iter2 first2)
180  {
181    add(ap, first1, last1, first2, 
182        typename utility::weighted_iterator_traits<Iter1>::type(),
183        typename utility::weighted_iterator_traits<Iter2>::type());
184  }
185
186  // unweighted impl. (weighted version is not implemented and should
187  // not compile)
188  template <typename Iter1, typename Iter2>
189  void add(AveragerPair& ap, Iter1 first1, Iter1 last1, Iter2 first2, 
190           utility::unweighted_type type1, utility::unweighted_type type2)
191  {
192    for ( ; first1 != last1; ++first1, ++first2)
193      ap.add(*first1, *first2);
194  }
195
196
197  // Template implementations
198  template <typename T1, typename T2>
199  void AveragerPair::add_values(const T1& x, const T2& y)
200  {
201    for (size_t i=0; i<x.size(); i++) 
202      add(x[i],y[i]);
203  }
204
205
206}}} // of namespace statistics, yat, and theplu
207
208#endif
Note: See TracBrowser for help on using the repository browser.