source: trunk/yat/statistics/AveragerPairWeighted.h

Last change on this file was 3550, checked in by Peter, 5 years ago

Update copyright years. Happy New Year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpairweighted_
2#define _theplu_yat_statistics_averagerpairweighted_
3
4// $Id: AveragerPairWeighted.h 3550 2017-01-03 05:41:02Z peter $
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 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2009, 2010, 2011, 2016 Peter Johansson
11
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include "AveragerWeighted.h"
29
30#include "yat/utility/iterator_traits.h"
31#include "yat/utility/yat_assert.h"
32
33#include <boost/concept_check.hpp>
34
35#include <cmath>
36#include <stdexcept>
37
38namespace theplu{
39namespace yat{
40namespace statistics{
41  ///
42  /// @brief Class for taking care of mean and covariance of two variables in
43  /// a weighted manner.
44  ///
45  /// \see \ref weighted_statistics
46  ///
47  /// If nothing else stated, each function fulfills the
48  /// following:<br> <ul><li>Setting a weight to zero corresponds to
49  /// removing the data point from the dataset.</li><li> Setting all
50  /// weights to unity, the yields the same result as from
51  /// corresponding function in AveragerPair.</li><li> Rescaling weights
52  /// does not change the performance of the object.</li></ul>
53  ///
54  /// @see Averager AveragerWeighted AveragerPair
55  ///
56  class AveragerPairWeighted
57  {
58  public:
59
60    ///
61    /// @brief The default constructor
62    ///
63    AveragerPairWeighted(void);
64
65    ///
66    /// Adding a pair of data points with value \a x and \a y, and
67    /// their weights. If either of the weights are zero the addition
68    /// is ignored
69    ///
70    void  add(const double x, const double y, 
71              const double wx, const double wy);
72
73    ///
74    /// @brief %Pearson correlation coefficient.
75    ///
76    /// @return \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sqrt{\sum
77    /// w_xw_y (y-m_y)^2\sum w_xw_y (y-m_y)^2}} \f$ where m is
78    /// calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
79    ///
80    double correlation(void) const;
81 
82    ///
83    /// \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sum w_xw_y} \f$ where m
84    /// is calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
85    ///
86    double covariance(void) const;
87
88    /**
89       @return \f$ \frac{\sum w_xw_y(x-y)^2}{\sum w_xw_y} \f$
90    */
91    double msd(void) const;
92
93    /**
94       @return \f$ \frac{\left(\sum w_x w_y\right)^2}{\sum w_x^2w_y^2} \f$
95    */ 
96    double n(void) const;
97
98    ///
99    /// @brief Reset everything to zero
100    ///
101    void reset(void);
102
103    ///
104    /// @return \f$ \sum w_xw_y \f$
105    ///
106    double sum_w(void) const;
107
108    ///
109    /// @return \f$ \sum w_xw_yxy \f$
110    ///
111    double sum_xy(void) const;
112
113    ///
114    /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as
115    /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
116    ///
117    double sum_xy_centered(void) const;
118
119    ///
120    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
121    ///
122    /// @return AveragerWeighted for x
123    ///
124    const AveragerWeighted& x_averager(void) const;
125
126    ///
127    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
128    ///
129    /// @return AveragerWeighted for y
130    ///
131    const AveragerWeighted& y_averager(void) const;
132
133    /**
134       \brief Addition assignment operator.
135
136       \return resulting AveragerPairWeighted
137
138       \since New in yat 0.8
139     */
140    const AveragerPairWeighted& operator+=(const AveragerPairWeighted&);
141
142  private:
143    AveragerWeighted x_; // weighted averager with w = w_x*w_y
144    AveragerWeighted y_; // weighted averager with w = w_x*w_y
145    double wxy_centered_;
146
147    void xy_add(double mx, double my, double xy_centered, double w);
148  };
149
150  /**
151     \brief adding a ranges of values to AveragerPairWeighted \a ap
152
153     Type Requirement:
154     - \c InputIterator1 models \ref concept_data_iterator
155     - \c InputIterator1 is \single_pass_iterator
156     - \c InputIterator2 models \ref concept_data_iterator
157     - \c InputIterator2 is \single_pass_iterator
158
159     \relates AveragerPairWeighted
160   */
161  template <class InputIterator1, class InputIterator2>
162  void add(AveragerPairWeighted& ap, InputIterator1 first1, 
163           InputIterator1 last1, InputIterator2 first2)
164  {
165    BOOST_CONCEPT_ASSERT((utility::DataIteratorConcept<InputIterator1>));
166    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator1>));
167    BOOST_CONCEPT_ASSERT((utility::DataIteratorConcept<InputIterator2>));
168    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator2>));
169
170    utility::iterator_traits<InputIterator1> traits1;
171    utility::iterator_traits<InputIterator2> traits2;
172    for ( ; first1 != last1; ++first1, ++first2) {
173      ap.add(traits1.data(first1), traits2.data(first2), 
174             traits1.weight(first1), traits2.weight(first2));
175    }
176  }
177
178
179  /**
180     \brief adding four ranges of values to AveragerPairWeighted \a ap
181
182     Iterators must be unweighted.
183
184     Type Requirement:
185     - \c InputIterator1 models \single_pass_iterator
186     - \c InputIterator1 models \readable_iterator
187     - \c InputIterator1 is weighted
188     - \c InputIterator2 models \single_pass_iterator
189     - \c InputIterator2 models \readable_iterator
190     - \c InputIterator2 is weighted
191     - \c InputIterator3 models \single_pass_iterator
192     - \c InputIterator3 models \readable_iterator
193     - \c InputIterator3 is weighted
194     - \c InputIterator4 models \single_pass_iterator
195     - \c InputIterator4 models \readable_iterator
196     - \c InputIterator4 is weighted
197
198     \relates AveragerPairWeighted
199   */
200  template <typename InputIterator1, typename InputIterator2
201            , typename InputIterator3, typename InputIterator4>
202  void add(AveragerPairWeighted& ap, InputIterator1 x, InputIterator1 xlast,
203           InputIterator2 y, InputIterator3 wx, InputIterator4 wy)
204  {
205    BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator1>));
206    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator1>));
207    BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator2>));
208    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator2>));
209    BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator3>));
210    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator3>));
211    BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator4>));
212    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator4>));
213
214    utility::check_iterator_is_unweighted(x);
215    utility::check_iterator_is_unweighted(y);
216    utility::check_iterator_is_unweighted(wx);
217    utility::check_iterator_is_unweighted(wy);
218    while (x!=xlast) {
219      ap.add(*x, *y, *wx, *wy);
220      ++x;
221      ++y;
222      ++wx;
223      ++wy;
224    }
225  }
226
227
228}}} // of namespace statistics, yat, and theplu
229
230#endif
Note: See TracBrowser for help on using the repository browser.