1 | #ifndef _theplu_yat_statistics_averagerpair_ |
---|
2 | #define _theplu_yat_statistics_averagerpair_ |
---|
3 | |
---|
4 | // $Id: AveragerPair.h 1000 2007-12-23 20:09:15Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
9 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://trac.thep.lu.se/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 "Averager.h" |
---|
30 | |
---|
31 | #include "yat/utility/iterator_traits.h" |
---|
32 | |
---|
33 | #include <cmath> |
---|
34 | #include <utility> |
---|
35 | |
---|
36 | namespace theplu{ |
---|
37 | namespace yat{ |
---|
38 | namespace statistics{ |
---|
39 | |
---|
40 | /// |
---|
41 | /// @brief Class for taking care of mean and covariance of two variables. |
---|
42 | /// |
---|
43 | /// @see Averager AveragerWeighted AveragerPairWeighted |
---|
44 | /// |
---|
45 | class AveragerPair |
---|
46 | { |
---|
47 | public: |
---|
48 | |
---|
49 | /// |
---|
50 | /// @brief The default constructor |
---|
51 | /// |
---|
52 | AveragerPair(void); |
---|
53 | |
---|
54 | /// |
---|
55 | /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and |
---|
56 | /// number of pair of values \a n |
---|
57 | /// |
---|
58 | //AveragerPair(const double x, const double xx, const double y, |
---|
59 | // const double yy, const double xy, const unsigned long n) |
---|
60 | // : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy) {} |
---|
61 | |
---|
62 | /// |
---|
63 | /// The copy constructor |
---|
64 | /// |
---|
65 | AveragerPair(const AveragerPair&); |
---|
66 | |
---|
67 | /// |
---|
68 | /// Adding \a n pairs of data points with value \a x and \a y. |
---|
69 | /// |
---|
70 | void add(const double x, const double y, const unsigned long n=1); |
---|
71 | |
---|
72 | /// |
---|
73 | /// Adding pairs of arrays with data points \a x and \a y. |
---|
74 | /// |
---|
75 | /// The requirements for the types T1 and T2 of the arrays \a x |
---|
76 | /// and \a y are: operator[] returning an element and function |
---|
77 | /// size() returning the number of elements. |
---|
78 | /// |
---|
79 | template <typename T1, typename T2> |
---|
80 | void add_values(const T1& x, const T2& y); |
---|
81 | |
---|
82 | /// |
---|
83 | /// \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sum_i |
---|
84 | /// (x_i-m_x)^2+\sum_i (y_i-m_y)^2 + n(m_x-m_y)^2} \f$ |
---|
85 | /// |
---|
86 | /// In case of a zero denominator - zero is returned. |
---|
87 | /// |
---|
88 | /// @return Concordence correlation coefficient. |
---|
89 | /// |
---|
90 | double ccc(void) const; |
---|
91 | |
---|
92 | /// |
---|
93 | /// \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i |
---|
94 | /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}} \f$ |
---|
95 | /// |
---|
96 | /// @return Pearson correlation coefficient. |
---|
97 | /// |
---|
98 | double correlation(void) const; |
---|
99 | |
---|
100 | /// |
---|
101 | /// Calculating covariance using |
---|
102 | /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y) \f$, |
---|
103 | /// where \f$ m \f$ is the mean. |
---|
104 | /// |
---|
105 | /// @return The covariance. |
---|
106 | /// |
---|
107 | double covariance(void) const; |
---|
108 | |
---|
109 | /// |
---|
110 | /// @return The mean of xy. |
---|
111 | /// |
---|
112 | double mean_xy(void) const; |
---|
113 | |
---|
114 | /// |
---|
115 | /// @return Average squared deviation between x and y \f$ |
---|
116 | /// \frac{1}{N} \sum (x-y)^2 \f$ |
---|
117 | /// |
---|
118 | double msd(void) const; |
---|
119 | |
---|
120 | /// |
---|
121 | /// @return The number of pair of data points. |
---|
122 | /// |
---|
123 | unsigned long n(void) const; |
---|
124 | |
---|
125 | /// |
---|
126 | /// @brief Reset everything to zero |
---|
127 | /// |
---|
128 | void reset(void); |
---|
129 | |
---|
130 | /// |
---|
131 | /// @return The sum of xy. |
---|
132 | /// |
---|
133 | double sum_xy(void) const; |
---|
134 | |
---|
135 | /// |
---|
136 | /// @return Sum of squared deviation between x and y \f$ |
---|
137 | /// \sum (x-y)^2 \f$ |
---|
138 | /// |
---|
139 | double sum_squared_deviation(void) const; |
---|
140 | |
---|
141 | /// |
---|
142 | /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y) \f$ |
---|
143 | /// |
---|
144 | double sum_xy_centered(void) const; |
---|
145 | |
---|
146 | /// |
---|
147 | /// @return A const refencer to the averager object for x. |
---|
148 | /// |
---|
149 | const Averager& x_averager(void) const; |
---|
150 | |
---|
151 | /// |
---|
152 | /// @return A const reference to the averager object for y |
---|
153 | /// |
---|
154 | const Averager& y_averager(void) const; |
---|
155 | |
---|
156 | /// |
---|
157 | /// @brief The assigment operator |
---|
158 | /// |
---|
159 | const AveragerPair& operator=(const AveragerPair& a); |
---|
160 | |
---|
161 | /// |
---|
162 | /// Operator to add another Averager |
---|
163 | /// |
---|
164 | const AveragerPair& operator+=(const AveragerPair&); |
---|
165 | |
---|
166 | private: |
---|
167 | Averager x_; |
---|
168 | Averager y_; |
---|
169 | double xy_; |
---|
170 | |
---|
171 | }; |
---|
172 | |
---|
173 | /** |
---|
174 | \brief adding a ranges of values to AveragerPair \a ap |
---|
175 | */ |
---|
176 | template <class Iter1, class Iter2> |
---|
177 | void add(AveragerPair& ap, Iter1 first1, Iter1 last1, Iter2 first2) |
---|
178 | { |
---|
179 | utility::check_iterator_is_unweighted(first1); |
---|
180 | utility::check_iterator_is_unweighted(first2); |
---|
181 | for ( ; first1 != last1; ++first1, ++first2) |
---|
182 | ap.add(*first1, *first2); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | // Template implementations |
---|
187 | template <typename T1, typename T2> |
---|
188 | void AveragerPair::add_values(const T1& x, const T2& y) |
---|
189 | { |
---|
190 | for (size_t i=0; i<x.size(); i++) |
---|
191 | add(x[i],y[i]); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | }}} // of namespace statistics, yat, and theplu |
---|
196 | |
---|
197 | #endif |
---|