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