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

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

changing name of IteratorTraits? to iterator_traits

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpairweighted_
2#define _theplu_yat_statistics_averagerpairweighted_
3
4// $Id: AveragerPairWeighted.h 937 2007-10-05 23:11:18Z 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/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 "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    /// Adding two sequences of data @a x and @a y. The data
78    /// should be paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
79    /// @a x will be treated as having all weights equal to unity
80    ///
81    void add(const classifier::DataLookup1D& x, 
82             const classifier::DataLookupWeighted1D& y);
83
84    ///
85    /// Adding two sequences of data @a x and @a y. The data should be
86    /// paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
87    /// @a y will be treated as having all weights equal to unity
88    ///
89    void add(const classifier::DataLookupWeighted1D& x, 
90             const classifier::DataLookup1D& y);
91
92    ///
93    /// Adding two sequences of weighted data @a x and @a y. The data
94    /// should be paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
95    ///
96    void add(const classifier::DataLookupWeighted1D& x, 
97             const classifier::DataLookupWeighted1D& y);
98
99    ///
100    /// Adding pair of data and corresponding pair of weights in arrays
101    ///
102    ///
103    /// The requirements for the types T1, T2, T3 and T4 of the arrays
104    /// are: operator[] returning an element and function /// size()
105    /// returning the number of elements.
106    ///
107    template <typename T1,typename T2,typename T3,typename T4>
108    void add_values(const T1& x, 
109                    const T2& y, 
110                    const T3& wx, 
111                    const T4& wy);
112
113    ///
114    /// @brief Pearson correlation coefficient.
115    ///
116    /// @return \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sqrt{\sum
117    /// w_xw_y (y-m_y)^2\sum w_xw_y (y-m_y)^2}} \f$ where m is
118    /// calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
119    ///
120    double correlation(void) const;
121 
122    ///
123    /// \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sum w_xw_y} \f$ where m
124    /// is calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
125    ///
126    double covariance(void) const;
127
128    /**
129       @return \f$ \frac{\sum w_xw_y(x-y)^2}{\sum w_xw_y} \f$
130    */
131    double msd(void) const;
132
133    /**
134       @return \f$ \frac{\left(\sum w_x w_y\right)^2}{\sum w_x^2w_y^2} \f$
135    */ 
136    double n(void) const;
137
138    ///
139    /// @brief Reset everything to zero
140    ///
141    void reset(void);
142
143    ///
144    /// @return \f$ \sum w_xw_y \f$
145    ///
146    double sum_w(void) const;
147
148    ///
149    /// @return \f$ \sum w_xw_yxy \f$
150    ///
151    double sum_xy(void) const;
152
153    ///
154    /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as
155    /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
156    ///
157    double sum_xy_centered(void) const;
158
159    ///
160    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
161    ///
162    /// @return AveragerWeighted for x
163    ///
164    const AveragerWeighted& x_averager(void) const;
165
166    ///
167    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
168    ///
169    /// @return AveragerWeighted for y
170    ///
171    const AveragerWeighted& y_averager(void) const;
172
173  private:
174    AveragerWeighted x_; // weighted averager with w = w_x*w_y
175    AveragerWeighted y_; // weighted averager with w = w_x*w_y
176    double wxy_;
177    double w_;
178
179  };
180
181  /**
182     \brief adding a ranges of values to AveragerPairWeighted \a ap
183   */
184  template <class Iter1, class Iter2>
185  void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2)
186  {
187    for ( ; first1 != last1; ++first1, ++first2) {
188      ap.add(utility::iterator_traits_data(first1), 
189             utility::iterator_traits_data(first2),
190             utility::iterator_traits_weight(first1), 
191             utility::iterator_traits_weight(first2));
192    }
193  }
194
195
196  template <typename T1,typename T2,typename T3,typename T4>
197  void  AveragerPairWeighted::add_values(const T1& x, 
198                                         const T2& y, 
199                                         const T3& wx,
200                                         const T4& wy)
201  {
202    for (size_t i=0; i<x.size(); ++i){ 
203      utility::yat_assert<std::runtime_error>(!std::isnan(x[i]));
204      add(x[i],y[i],wx[i],wy[i]);
205    }
206  }
207
208}}} // of namespace statistics, yat, and theplu
209
210#endif
Note: See TracBrowser for help on using the repository browser.