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

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

changed euclidean to fulfill design criteria, removes sum_squared_deviation from AveragerPairWeighted?, also changed vector::sort to use stl rather than gsl. refs #248 and fixes #253

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpairweighted_
2#define _theplu_yat_statistics_averagerpairweighted_
3
4// $Id: AveragerPairWeighted.h 899 2007-09-26 21:26:25Z 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 <cmath>
32
33namespace theplu{
34namespace yat{
35namespace classifier{
36  class DataLookup1D;
37  class DataLookupWeighted1D;
38}
39namespace statistics{
40  ///
41  /// @brief Class for taking care of mean and covariance of two variables in
42  /// a weighted manner.
43  ///
44  /// <a href="Statistics/index.html">Weighted Statistics document</a>
45  ///
46  /// If nothing else stated, each function fulfills the
47  /// following:<br> <ul><li>Setting a weight to zero corresponds to
48  /// removing the data point from the dataset.</li><li> Setting all
49  /// weights to unity, the yields the same result as from
50  /// corresponding function in AveragerPair.</li><li> Rescaling weights
51  /// does not change the performance of the object.</li></ul>
52  ///
53  /// @see Averager AveragerWeighted AveragerPair
54  ///
55  class AveragerPairWeighted
56  {
57  public:
58
59    ///
60    /// @brief The default constructor
61    ///
62    AveragerPairWeighted(void);
63
64    ///
65    /// Adding a pair of data points with value \a x and \a y, and
66    /// their weights. If either of the weights are zero the addition
67    /// is ignored
68    ///
69    void  add(const double x, const double y, 
70              const double wx, const double wy);
71
72    ///
73    /// Adding two sequences of data @a x and @a y. The data
74    /// should be paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
75    /// @a x will be treated as having all weights equal to unity
76    ///
77    void add(const classifier::DataLookup1D& x, 
78             const classifier::DataLookupWeighted1D& y);
79
80    ///
81    /// Adding two sequences of data @a x and @a y. The data should be
82    /// paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
83    /// @a y will be treated as having all weights equal to unity
84    ///
85    void add(const classifier::DataLookupWeighted1D& x, 
86             const classifier::DataLookup1D& y);
87
88    ///
89    /// Adding two sequences of weighted data @a x and @a y. The data
90    /// should be paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
91    ///
92    void add(const classifier::DataLookupWeighted1D& x, 
93             const classifier::DataLookupWeighted1D& y);
94
95    ///
96    /// Adding pair of data and corresponding pair of weights in arrays
97    ///
98    ///
99    /// The requirements for the types T1, T2, T3 and T4 of the arrays
100    /// are: operator[] returning an element and function /// size()
101    /// returning the number of elements.
102    ///
103    template <typename T1,typename T2,typename T3,typename T4>
104    void add_values(const T1& x, 
105                    const T2& y, 
106                    const T3& wx, 
107                    const T4& wy);
108
109    ///
110    /// @brief Pearson correlation coefficient.
111    ///
112    /// @return \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sqrt{\sum
113    /// w_xw_y (y-m_y)^2\sum w_xw_y (y-m_y)^2}} \f$ where m is
114    /// calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
115    ///
116    double correlation(void) const;
117 
118    ///
119    /// \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sum w_xw_y} \f$ where m
120    /// is calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
121    ///
122    double covariance(void) const;
123
124    /**
125       @return \f$ \frac{\sum w_xw_y(x-y)^2}{\sum w_xw_y} \f$
126    */
127    double msd(void) const;
128
129    /**
130       @return \f$ \frac{\left(\sum w_x w_y\right)^2}{\sum w_x^2w_y^2} \f$
131    */ 
132    double n(void) const;
133
134    ///
135    /// @brief Reset everything to zero
136    ///
137    void reset(void);
138
139    ///
140    /// @return \f$ \sum w_xw_y \f$
141    ///
142    double sum_w(void) const;
143
144    ///
145    /// @return \f$ \sum w_xw_yxy \f$
146    ///
147    double sum_xy(void) const;
148
149    ///
150    /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as
151    /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
152    ///
153    double sum_xy_centered(void) const;
154
155    ///
156    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
157    ///
158    /// @return AveragerWeighted for x
159    ///
160    const AveragerWeighted& x_averager(void) const;
161
162    ///
163    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
164    ///
165    /// @return AveragerWeighted for y
166    ///
167    const AveragerWeighted& y_averager(void) const;
168
169  private:
170    AveragerWeighted x_; // weighted averager with w = w_x*w_y
171    AveragerWeighted y_; // weighted averager with w = w_x*w_y
172    double wxy_;
173    double w_;
174
175  };
176
177  /**
178     \brief adding a ranges of values to AveragerPairWeighted \a ap
179  */
180  template <class Iter1, class Iter2>
181  void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2)
182  {
183    for ( ; first1 != last1; ++first1, ++first2)
184      ap.add(first1.data(), first2.data(),first1.weight(),first2.weight());
185  }
186
187
188
189
190  template <typename T1,typename T2,typename T3,typename T4>
191  void  AveragerPairWeighted::add_values(const T1& x, 
192                                         const T2& y, 
193                                         const T3& wx,
194                                         const T4& wy)
195  {
196    for (size_t i=0; i<x.size(); i++) 
197      add(x[i],y[i],wx[i],wy[i]);
198  }
199
200}}} // of namespace statistics, yat, and theplu
201
202#endif
Note: See TracBrowser for help on using the repository browser.