1 | #ifndef _theplu_yat_statistics_averagerpair_ |
---|
2 | #define _theplu_yat_statistics_averagerpair_ |
---|
3 | |
---|
4 | // $Id: AveragerPair.h 1294 2008-05-12 08:21:19Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2008 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 2 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
27 | 02111-1307, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "Averager.h" |
---|
31 | |
---|
32 | #include "yat/utility/iterator_traits.h" |
---|
33 | |
---|
34 | #include <cmath> |
---|
35 | #include <utility> |
---|
36 | |
---|
37 | namespace theplu{ |
---|
38 | namespace yat{ |
---|
39 | namespace statistics{ |
---|
40 | |
---|
41 | /// |
---|
42 | /// @brief Class for taking care of mean and covariance of two variables. |
---|
43 | /// |
---|
44 | /// @see Averager AveragerWeighted AveragerPairWeighted |
---|
45 | /// |
---|
46 | class AveragerPair |
---|
47 | { |
---|
48 | public: |
---|
49 | |
---|
50 | /// |
---|
51 | /// @brief The default constructor |
---|
52 | /// |
---|
53 | AveragerPair(void); |
---|
54 | |
---|
55 | /// |
---|
56 | /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and |
---|
57 | /// number of pair of values \a n |
---|
58 | /// |
---|
59 | //AveragerPair(const double x, const double xx, const double y, |
---|
60 | // const double yy, const double xy, const unsigned long n) |
---|
61 | // : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy) {} |
---|
62 | |
---|
63 | /// |
---|
64 | /// The copy constructor |
---|
65 | /// |
---|
66 | AveragerPair(const AveragerPair&); |
---|
67 | |
---|
68 | /// |
---|
69 | /// Adding \a n pairs of data points with value \a x and \a y. |
---|
70 | /// |
---|
71 | void add(const double x, const double y, const unsigned long n=1); |
---|
72 | |
---|
73 | /** |
---|
74 | \brief Concordence correlation coefficient. |
---|
75 | |
---|
76 | \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sum_i |
---|
77 | (x_i-m_x)^2+\sum_i (y_i-m_y)^2 + n(m_x-m_y)^2} \f$ |
---|
78 | |
---|
79 | */ |
---|
80 | double ccc(void) const; |
---|
81 | |
---|
82 | /** |
---|
83 | \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i |
---|
84 | (x_i-m_x)^2\sum_i (y_i-m_y)^2}} \f$ |
---|
85 | |
---|
86 | \return %Pearson correlation coefficient. |
---|
87 | */ |
---|
88 | double correlation(void) const; |
---|
89 | |
---|
90 | /// |
---|
91 | /// Calculating covariance using |
---|
92 | /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y) \f$, |
---|
93 | /// where \f$ m \f$ is the mean. |
---|
94 | /// |
---|
95 | /// @return The covariance. |
---|
96 | /// |
---|
97 | double covariance(void) const; |
---|
98 | |
---|
99 | /// |
---|
100 | /// @return The mean of xy. |
---|
101 | /// |
---|
102 | double mean_xy(void) const; |
---|
103 | |
---|
104 | /** |
---|
105 | \return Average squared deviation between x and y \f$ |
---|
106 | \frac{1}{N} \sum (x-y)^2 \f$ |
---|
107 | */ |
---|
108 | double msd(void) const; |
---|
109 | |
---|
110 | /// |
---|
111 | /// @return The number of pair of data points. |
---|
112 | /// |
---|
113 | unsigned long n(void) const; |
---|
114 | |
---|
115 | /// |
---|
116 | /// @brief Reset everything to zero |
---|
117 | /// |
---|
118 | void reset(void); |
---|
119 | |
---|
120 | /// |
---|
121 | /// @return The sum of xy. |
---|
122 | /// |
---|
123 | double sum_xy(void) const; |
---|
124 | |
---|
125 | /** |
---|
126 | \return Sum of squared deviation between x and y \f$ |
---|
127 | \sum (x-y)^2 \f$ |
---|
128 | */ |
---|
129 | double sum_squared_deviation(void) const; |
---|
130 | |
---|
131 | /// |
---|
132 | /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y) \f$ |
---|
133 | /// |
---|
134 | double sum_xy_centered(void) const; |
---|
135 | |
---|
136 | /// |
---|
137 | /// @return A const refencer to the averager object for x. |
---|
138 | /// |
---|
139 | const Averager& x_averager(void) const; |
---|
140 | |
---|
141 | /// |
---|
142 | /// @return A const reference to the averager object for y |
---|
143 | /// |
---|
144 | const Averager& y_averager(void) const; |
---|
145 | |
---|
146 | /// |
---|
147 | /// @brief The assigment operator |
---|
148 | /// |
---|
149 | const AveragerPair& operator=(const AveragerPair& a); |
---|
150 | |
---|
151 | /// |
---|
152 | /// Operator to add another Averager |
---|
153 | /// |
---|
154 | const AveragerPair& operator+=(const AveragerPair&); |
---|
155 | |
---|
156 | private: |
---|
157 | Averager x_; |
---|
158 | Averager y_; |
---|
159 | double xy_; |
---|
160 | |
---|
161 | }; |
---|
162 | |
---|
163 | /** |
---|
164 | \brief adding data from two ranges to AveragerPair \a ap |
---|
165 | */ |
---|
166 | template <class Iter1, class Iter2> |
---|
167 | void add(AveragerPair& ap, Iter1 first1, Iter1 last1, Iter2 first2) |
---|
168 | { |
---|
169 | utility::check_iterator_is_unweighted(first1); |
---|
170 | utility::check_iterator_is_unweighted(first2); |
---|
171 | for ( ; first1 != last1; ++first1, ++first2) |
---|
172 | ap.add(*first1, *first2); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | }}} // of namespace statistics, yat, and theplu |
---|
177 | |
---|
178 | #endif |
---|