1 | #ifndef _theplu_yat_statistics_averagerpair_ |
---|
2 | #define _theplu_yat_statistics_averagerpair_ |
---|
3 | |
---|
4 | // $Id: AveragerPair.h 2506 2011-07-01 18:06:06Z peter $ |
---|
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, 2008 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2009, 2010 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "Averager.h" |
---|
29 | |
---|
30 | #include "yat/utility/iterator_traits.h" |
---|
31 | |
---|
32 | #include <boost/concept_check.hpp> |
---|
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 | /// The copy constructor |
---|
57 | /// |
---|
58 | AveragerPair(const AveragerPair&); |
---|
59 | |
---|
60 | /// |
---|
61 | /// Adding \a n pairs of data points with value \a x and \a y. |
---|
62 | /// |
---|
63 | void add(const double x, const double y, const long n=1); |
---|
64 | |
---|
65 | /** |
---|
66 | \brief Concordence correlation coefficient. |
---|
67 | |
---|
68 | \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sum_i |
---|
69 | (x_i-m_x)^2+\sum_i (y_i-m_y)^2 + n(m_x-m_y)^2} \f$ |
---|
70 | |
---|
71 | */ |
---|
72 | double ccc(void) const; |
---|
73 | |
---|
74 | /** |
---|
75 | \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i |
---|
76 | (x_i-m_x)^2\sum_i (y_i-m_y)^2}} \f$ |
---|
77 | |
---|
78 | \return %Pearson correlation coefficient. |
---|
79 | */ |
---|
80 | double correlation(void) const; |
---|
81 | |
---|
82 | /// |
---|
83 | /// Calculating covariance using |
---|
84 | /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y) \f$, |
---|
85 | /// where \f$ m \f$ is the mean. |
---|
86 | /// |
---|
87 | /// @return The covariance. |
---|
88 | /// |
---|
89 | double covariance(void) const; |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return The mean of xy. |
---|
93 | /// |
---|
94 | double mean_xy(void) const; |
---|
95 | |
---|
96 | /** |
---|
97 | \return Average squared deviation between x and y \f$ |
---|
98 | \frac{1}{N} \sum (x-y)^2 \f$ |
---|
99 | */ |
---|
100 | double msd(void) const; |
---|
101 | |
---|
102 | /// |
---|
103 | /// @return The number of pair of data points. |
---|
104 | /// |
---|
105 | long n(void) const; |
---|
106 | |
---|
107 | /// |
---|
108 | /// @brief Reset everything to zero |
---|
109 | /// |
---|
110 | void reset(void); |
---|
111 | |
---|
112 | /// |
---|
113 | /// @return The sum of xy. |
---|
114 | /// |
---|
115 | double sum_xy(void) const; |
---|
116 | |
---|
117 | /** |
---|
118 | \return Sum of squared deviation between x and y \f$ |
---|
119 | \sum (x-y)^2 \f$ |
---|
120 | */ |
---|
121 | double sum_squared_deviation(void) const; |
---|
122 | |
---|
123 | /// |
---|
124 | /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y) \f$ |
---|
125 | /// |
---|
126 | double sum_xy_centered(void) const; |
---|
127 | |
---|
128 | /// |
---|
129 | /// @return A const refencer to the averager object for x. |
---|
130 | /// |
---|
131 | const Averager& x_averager(void) const; |
---|
132 | |
---|
133 | /// |
---|
134 | /// @return A const reference to the averager object for y |
---|
135 | /// |
---|
136 | const Averager& y_averager(void) const; |
---|
137 | |
---|
138 | /// |
---|
139 | /// @brief The assigment operator |
---|
140 | /// |
---|
141 | const AveragerPair& operator=(const AveragerPair& a); |
---|
142 | |
---|
143 | /// |
---|
144 | /// Operator to add another Averager |
---|
145 | /// |
---|
146 | const AveragerPair& operator+=(const AveragerPair&); |
---|
147 | |
---|
148 | private: |
---|
149 | Averager x_; |
---|
150 | Averager y_; |
---|
151 | double xy_; |
---|
152 | |
---|
153 | }; |
---|
154 | |
---|
155 | /** |
---|
156 | \brief adding data from two ranges to AveragerPair \a ap |
---|
157 | |
---|
158 | \relates AveragerPair |
---|
159 | */ |
---|
160 | template <class InputIterator1, class InputIterator2> |
---|
161 | void add(AveragerPair& ap, InputIterator1 first1, InputIterator1 last1, |
---|
162 | InputIterator2 first2) |
---|
163 | { |
---|
164 | BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator1>)); |
---|
165 | BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator2>)); |
---|
166 | utility::check_iterator_is_unweighted(first1); |
---|
167 | utility::check_iterator_is_unweighted(first2); |
---|
168 | for ( ; first1 != last1; ++first1, ++first2) |
---|
169 | ap.add(*first1, *first2); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | }}} // of namespace statistics, yat, and theplu |
---|
174 | |
---|
175 | #endif |
---|