source: trunk/yat/statistics/AveragerPairWeighted.h @ 1703

Last change on this file since 1703 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 Id
File size: 4.7 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpairweighted_
2#define _theplu_yat_statistics_averagerpairweighted_
3
4// $Id: AveragerPairWeighted.h 1487 2008-09-10 08:41:36Z jari $
5
6/*
7  Copyright (C) 2005 Peter Johansson, Markus Ringnér
8  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2008 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 "AveragerWeighted.h"
28
29#include "yat/utility/iterator_traits.h"
30#include "yat/utility/yat_assert.h"
31
32#include <cmath>
33#include <stdexcept>
34
35namespace theplu{
36namespace yat{
37namespace statistics{
38  ///
39  /// @brief Class for taking care of mean and covariance of two variables in
40  /// a weighted manner.
41  ///
42  /// \see \ref weighted_statistics
43  ///
44  /// If nothing else stated, each function fulfills the
45  /// following:<br> <ul><li>Setting a weight to zero corresponds to
46  /// removing the data point from the dataset.</li><li> Setting all
47  /// weights to unity, the yields the same result as from
48  /// corresponding function in AveragerPair.</li><li> Rescaling weights
49  /// does not change the performance of the object.</li></ul>
50  ///
51  /// @see Averager AveragerWeighted AveragerPair
52  ///
53  class AveragerPairWeighted
54  {
55  public:
56
57    ///
58    /// @brief The default constructor
59    ///
60    AveragerPairWeighted(void);
61
62    ///
63    /// Adding a pair of data points with value \a x and \a y, and
64    /// their weights. If either of the weights are zero the addition
65    /// is ignored
66    ///
67    void  add(const double x, const double y, 
68              const double wx, const double wy);
69
70    ///
71    /// @brief %Pearson correlation coefficient.
72    ///
73    /// @return \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sqrt{\sum
74    /// w_xw_y (y-m_y)^2\sum w_xw_y (y-m_y)^2}} \f$ where m is
75    /// calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
76    ///
77    double correlation(void) const;
78 
79    ///
80    /// \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sum w_xw_y} \f$ where m
81    /// is calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
82    ///
83    double covariance(void) const;
84
85    /**
86       @return \f$ \frac{\sum w_xw_y(x-y)^2}{\sum w_xw_y} \f$
87    */
88    double msd(void) const;
89
90    /**
91       @return \f$ \frac{\left(\sum w_x w_y\right)^2}{\sum w_x^2w_y^2} \f$
92    */ 
93    double n(void) const;
94
95    ///
96    /// @brief Reset everything to zero
97    ///
98    void reset(void);
99
100    ///
101    /// @return \f$ \sum w_xw_y \f$
102    ///
103    double sum_w(void) const;
104
105    ///
106    /// @return \f$ \sum w_xw_yxy \f$
107    ///
108    double sum_xy(void) const;
109
110    ///
111    /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as
112    /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
113    ///
114    double sum_xy_centered(void) const;
115
116    ///
117    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
118    ///
119    /// @return AveragerWeighted for x
120    ///
121    const AveragerWeighted& x_averager(void) const;
122
123    ///
124    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
125    ///
126    /// @return AveragerWeighted for y
127    ///
128    const AveragerWeighted& y_averager(void) const;
129
130  private:
131    AveragerWeighted x_; // weighted averager with w = w_x*w_y
132    AveragerWeighted y_; // weighted averager with w = w_x*w_y
133    double wxy_;
134    double w_;
135
136  };
137
138  /**
139     \brief adding a ranges of values to AveragerPairWeighted \a ap
140   */
141  template <class Iter1, class Iter2>
142  void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2)
143  {
144    for ( ; first1 != last1; ++first1, ++first2) {
145      ap.add(utility::iterator_traits<Iter1>().data(first1), 
146             utility::iterator_traits<Iter2>().data(first2),
147             utility::iterator_traits<Iter1>().weight(first1), 
148             utility::iterator_traits<Iter2>().weight(first2));
149    }
150  }
151
152
153  /**
154     \brief adding four ranges of values to AveragerPairWeighted \a ap
155   */
156  template <class Iter1, class Iter2, class Iter3, class Iter4>
157  void add(AveragerPairWeighted& ap, Iter1 x, Iter1 xlast, Iter2 y, Iter3 wx, 
158           Iter4 wy)
159  {
160    utility::check_iterator_is_unweighted(x);
161    utility::check_iterator_is_unweighted(y);
162    utility::check_iterator_is_unweighted(wx);
163    utility::check_iterator_is_unweighted(wy);
164    while (x!=xlast) {
165      ap.add(*x, *y, *wx, *wy);
166      ++x;
167      ++y;
168      ++wx;
169      ++wy;
170    }
171  }
172
173
174}}} // of namespace statistics, yat, and theplu
175
176#endif
Note: See TracBrowser for help on using the repository browser.