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