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

Last change on this file since 683 was 683, checked in by Jari Häkkinen, 16 years ago

Addresses #153. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1#ifndef _theplu_yat_statistics_averagerpairweighted_
2#define _theplu_yat_statistics_averagerpairweighted_
3
4// $Id: AveragerPairWeighted.h 683 2006-10-11 22:20:36Z jari $
5
6/*
7  Copyright (C) The authors contributing to this file.
8
9  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "AveragerWeighted.h"
28
29#include <cmath>
30
31namespace theplu{
32namespace yat{
33namespace classifier{
34  class DataLookup1D;
35  class DataLookupWeighted1D;
36}
37namespace statistics{
38  ///
39  /// Class for taking care of mean and covariance of two variables in
40  /// a weighted manner.
41  ///
42  /// <a href="Statistics/index.html">Weighted Statistics document</a>
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    AveragerPairWeighted()
58      : wxy_(0), w_(0)
59    {
60    }
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    /// Adding two sequences of data @a x and @a y. The data
72    /// should be paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
73    /// @a x will be treated as having all weights equal to unity
74    ///
75    void add(const classifier::DataLookup1D& x, 
76             const classifier::DataLookupWeighted1D& y);
77
78    ///
79    /// Adding two sequences of data @a x and @a y. The data should be
80    /// paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
81    /// @a y will be treated as having all weights equal to unity
82    ///
83    inline void add(const classifier::DataLookupWeighted1D& x, 
84                    const classifier::DataLookup1D& y){ add(y,x); }
85
86    ///
87    /// Adding two sequences of weighted data @a x and @a y. The data
88    /// should be paired so \f$ x(i) \f$ is associated to \f$ y(i) \f$
89    ///
90    void add(const classifier::DataLookupWeighted1D& x, 
91             const classifier::DataLookupWeighted1D& y);
92
93    ///
94    /// Adding pair of data and corresponding pair of weights in arrays
95    ///
96    ///
97    /// The requirements for the types T1, T2, T3 and T4 of the arrays
98    /// are: operator[] returning an element and function /// size()
99    /// returning the number of elements.
100    ///
101    template <typename T1,typename T2,typename T3,typename T4>
102    void add_values(const T1& x, 
103                    const T2& y, 
104                    const T3& wx, 
105                    const T4& wy);
106
107    ///
108    /// @brief Pearson correlation coefficient.
109    ///
110    /// @return \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sqrt{\sum
111    /// w_xw_y (y-m_y)^2\sum w_xw_y (y-m_y)^2}} \f$ where m is
112    /// calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
113    ///
114    inline double correlation(void) const 
115    { return covariance() / ( x_.std()*y_.std() ); }
116 
117    ///
118    /// \f$ \frac{\sum w_xw_y (x-m_x)(y-m_y)}{\sum w_xw_y} \f$ where m
119    /// is calculated as \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
120    ///
121    inline double covariance(void) const { return sum_xy_centered()/sum_w(); }
122
123    ///
124    /// reset everything to zero
125    ///
126    inline void reset(void) { x_.reset(); y_.reset(); wxy_=0; w_=0; }
127
128    ///
129    /// @return \f$ \sum w_xw_y \f$
130    ///
131    inline double sum_w(void) const { return w_; }
132
133    ///
134    /// @return \f$ \sum w_xw_yxy \f$
135    ///
136    inline double sum_xy(void) const { return wxy_; }
137
138    ///
139    /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as
140    /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$
141    ///
142    inline double sum_xy_centered(void) const 
143    { return sum_xy() - x_.sum_wx()*y_.mean(); }
144
145    ///
146    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
147    ///
148    /// @return AveragerWeighted for x
149    ///
150    inline const AveragerWeighted& x_averager(void) const { return x_; }
151
152    ///
153    /// @note the weights are calculated as \f$ w = w_x * w_y \f$
154    ///
155    /// @return AveragerWeighted for y
156    ///
157    inline const AveragerWeighted& y_averager(void) const { return y_; }
158
159  private:
160    AveragerWeighted x_; // weighted averager with w = w_x*w_y
161    AveragerWeighted y_; // weighted averager with w = w_x*w_y
162    double wxy_;
163    double w_;
164
165  };
166
167  template <typename T1,typename T2,typename T3,typename T4>
168  void  AveragerPairWeighted::add_values(const T1& x, 
169                                         const T2& y, 
170                                         const T3& wx,
171                                         const T4& wy)
172  {
173    for (size_t i=0; i<x.size(); i++) 
174      add(x[i],y[i],wx[i],wy[i]);
175  }
176
177}}} // of namespace statistics, yat, and theplu
178
179#endif
Note: See TracBrowser for help on using the repository browser.