1 | #ifndef _theplu_yat_statistics_averagerpairweighted_ |
---|
2 | #define _theplu_yat_statistics_averagerpairweighted_ |
---|
3 | |
---|
4 | // $Id: AveragerPairWeighted.h 890 2007-09-25 09:35:25Z markus $ |
---|
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 | |
---|
33 | namespace theplu{ |
---|
34 | namespace yat{ |
---|
35 | namespace classifier{ |
---|
36 | class DataLookup1D; |
---|
37 | class DataLookupWeighted1D; |
---|
38 | } |
---|
39 | namespace 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 | /// @brief Reset everything to zero |
---|
131 | /// |
---|
132 | void reset(void); |
---|
133 | |
---|
134 | /// |
---|
135 | /// @return Sum of weighted squared deviation between x and y \f$ |
---|
136 | /// \sum (w_xx-wyy)^2 \f$ |
---|
137 | /// |
---|
138 | double sum_squared_deviation(void) const; |
---|
139 | |
---|
140 | /// |
---|
141 | /// @return \f$ \sum w_xw_y \f$ |
---|
142 | /// |
---|
143 | double sum_w(void) const; |
---|
144 | |
---|
145 | /// |
---|
146 | /// @return \f$ \sum w_xw_yxy \f$ |
---|
147 | /// |
---|
148 | double sum_xy(void) const; |
---|
149 | |
---|
150 | /// |
---|
151 | /// @return \f$ \sum w_xw_y (x-m_x)(y-m_y) \f$ where m is calculated as |
---|
152 | /// \f$ m_x = \frac {\sum w_xw_yx}{\sum w} \f$ |
---|
153 | /// |
---|
154 | double sum_xy_centered(void) const; |
---|
155 | |
---|
156 | /// |
---|
157 | /// @note the weights are calculated as \f$ w = w_x * w_y \f$ |
---|
158 | /// |
---|
159 | /// @return AveragerWeighted for x |
---|
160 | /// |
---|
161 | const AveragerWeighted& x_averager(void) const; |
---|
162 | |
---|
163 | /// |
---|
164 | /// @note the weights are calculated as \f$ w = w_x * w_y \f$ |
---|
165 | /// |
---|
166 | /// @return AveragerWeighted for y |
---|
167 | /// |
---|
168 | const AveragerWeighted& y_averager(void) const; |
---|
169 | |
---|
170 | private: |
---|
171 | AveragerWeighted x_; // weighted averager with w = w_x*w_y |
---|
172 | AveragerWeighted y_; // weighted averager with w = w_x*w_y |
---|
173 | double wxy_; |
---|
174 | double w_; |
---|
175 | |
---|
176 | }; |
---|
177 | |
---|
178 | /** |
---|
179 | \brief adding a ranges of values to AveragerPairWeighted \a ap |
---|
180 | */ |
---|
181 | template <class Iter1, class Iter2> |
---|
182 | void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2) |
---|
183 | { |
---|
184 | for ( ; first1 != last1; ++first1, ++first2) |
---|
185 | ap.add(first1.data(), first2.data(),first1.weight(),first2.weight()); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | template <typename T1,typename T2,typename T3,typename T4> |
---|
192 | void AveragerPairWeighted::add_values(const T1& x, |
---|
193 | const T2& y, |
---|
194 | const T3& wx, |
---|
195 | const T4& wy) |
---|
196 | { |
---|
197 | for (size_t i=0; i<x.size(); i++) |
---|
198 | add(x[i],y[i],wx[i],wy[i]); |
---|
199 | } |
---|
200 | |
---|
201 | }}} // of namespace statistics, yat, and theplu |
---|
202 | |
---|
203 | #endif |
---|