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

Last change on this file since 1043 was 1043, checked in by Peter, 13 years ago

fixing #128 - iterators in Averger classes

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