Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / accumulators / statistics / rolling_variance.hpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // rolling_variance.hpp
3 // Copyright (C) 2005 Eric Niebler
4 // Copyright (C) 2014 Pieter Bastiaan Ober (Integricom).
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
10 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
11
12 #include <boost/accumulators/accumulators.hpp>
13 #include <boost/accumulators/statistics/stats.hpp>
14
15 #include <boost/mpl/placeholders.hpp>
16 #include <boost/accumulators/framework/accumulator_base.hpp>
17 #include <boost/accumulators/framework/extractor.hpp>
18 #include <boost/accumulators/numeric/functional.hpp>
19 #include <boost/accumulators/framework/parameters/sample.hpp>
20 #include <boost/accumulators/framework/depends_on.hpp>
21 #include <boost/accumulators/statistics_fwd.hpp>
22 #include <boost/accumulators/statistics/rolling_mean.hpp>
23 #include <boost/accumulators/statistics/rolling_moment.hpp>
24
25 #include <boost/type_traits/is_arithmetic.hpp>
26 #include <boost/utility/enable_if.hpp>
27
28 namespace boost { namespace accumulators
29 {
30 namespace impl
31 {
32     //! Immediate (lazy) calculation of the rolling variance.
33     /*!
34     Calculation of sample variance \f$\sigma_n^2\f$ is done as follows, see also
35     http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
36     For a rolling window of size \f$N\f$, when \f$n <= N\f$, the variance is computed according to the formula
37     \f[
38     \sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2.
39     \f]
40     When \f$n > N\f$, the sample variance over the window becomes:
41     \f[
42     \sigma_n^2 = \frac{1}{N-1} \sum_{i = n-N+1}^n (x_i - \mu_n)^2.
43     \f]
44     */
45     ///////////////////////////////////////////////////////////////////////////////
46     // lazy_rolling_variance_impl
47     //
48     template<typename Sample>
49     struct lazy_rolling_variance_impl
50         : accumulator_base
51     {
52         // for boost::result_of
53         typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
54
55         lazy_rolling_variance_impl(dont_care) {}
56
57         template<typename Args>
58         result_type result(Args const &args) const
59         {
60             result_type mean = rolling_mean(args);
61             size_t nr_samples = rolling_count(args);
62             if (nr_samples < 2) return result_type();
63             return nr_samples*(rolling_moment<2>(args) - mean*mean)/(nr_samples-1);
64         }
65     };
66
67     //! Iterative calculation of the rolling variance.
68     /*!
69     Iterative calculation of sample variance \f$\sigma_n^2\f$ is done as follows, see also
70     http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
71     For a rolling window of size \f$N\f$, for the first \f$N\f$ samples, the variance is computed according to the formula
72     \f[
73     \sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2 = \frac{1}{n-1}M_{2,n},
74     \f]
75     where the sum of squares \f$M_{2,n}\f$ can be recursively computed as:
76     \f[
77     M_{2,n} = \sum_{i = 1}^n (x_i - \mu_n)^2 = M_{2,n-1} + (x_n - \mu_n)(x_n - \mu_{n-1}),
78     \f]
79     and the estimate of the sample mean as:
80     \f[
81     \mu_n = \frac{1}{n} \sum_{i = 1}^n x_i = \mu_{n-1} + \frac{1}{n}(x_n - \mu_{n-1}).
82     \f]
83     For further samples, when the rolling window is fully filled with data, one has to take into account that the oldest
84     sample \f$x_{n-N}\f$ is dropped from the window. The sample variance over the window now becomes:
85     \f[
86     \sigma_n^2 = \frac{1}{N-1} \sum_{i = n-N+1}^n (x_i - \mu_n)^2 = \frac{1}{n-1}M_{2,n},
87     \f]
88     where the sum of squares \f$M_{2,n}\f$ now equals:
89     \f[
90     M_{2,n} = \sum_{i = n-N+1}^n (x_i - \mu_n)^2 = M_{2,n-1} + (x_n - \mu_n)(x_n - \mu_{n-1}) - (x_{n-N} - \mu_n)(x_{n-N} - \mu_{n-1}),
91     \f]
92     and the estimated mean is:
93     \f[
94     \mu_n = \frac{1}{N} \sum_{i = n-N+1}^n x_i = \mu_{n-1} + \frac{1}{n}(x_n - x_{n-N}).
95     \f]
96
97     Note that the sample variance is not defined for \f$n <= 1\f$.
98
99     */
100     ///////////////////////////////////////////////////////////////////////////////
101     // immediate_rolling_variance_impl
102     //
103     template<typename Sample>
104     struct immediate_rolling_variance_impl
105         : accumulator_base
106     {
107         // for boost::result_of
108         typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
109
110         template<typename Args>
111         immediate_rolling_variance_impl(Args const &args)
112             : previous_mean_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
113             , sum_of_squares_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
114         {
115         }
116
117         template<typename Args>
118         void operator()(Args const &args)
119         {
120             Sample added_sample = args[sample];
121
122             result_type mean = immediate_rolling_mean(args);
123             sum_of_squares_ += (added_sample-mean)*(added_sample-previous_mean_);
124
125             if(is_rolling_window_plus1_full(args))
126             {
127                 Sample removed_sample = rolling_window_plus1(args).front();
128                 sum_of_squares_ -= (removed_sample-mean)*(removed_sample-previous_mean_);
129                 prevent_underflow(sum_of_squares_);
130             }
131             previous_mean_ = mean;
132         }
133
134         template<typename Args>
135         result_type result(Args const &args) const
136         {
137             size_t nr_samples = rolling_count(args);
138             if (nr_samples < 2) return result_type();
139             return numeric::fdiv(sum_of_squares_,(nr_samples-1));
140         }
141
142     private:
143
144         result_type previous_mean_;
145         result_type sum_of_squares_;
146
147         template<typename T>
148         void prevent_underflow(T &non_negative_number,typename boost::enable_if<boost::is_arithmetic<T>,T>::type* = 0)
149         {
150             if (non_negative_number < T(0)) non_negative_number = T(0);
151         }
152         template<typename T>
153         void prevent_underflow(T &non_arithmetic_quantity,typename boost::disable_if<boost::is_arithmetic<T>,T>::type* = 0)
154         {
155         }
156     };
157 } // namespace impl
158
159 ///////////////////////////////////////////////////////////////////////////////
160 // tag:: lazy_rolling_variance
161 // tag:: immediate_rolling_variance
162 // tag:: rolling_variance
163 //
164 namespace tag
165 {
166     struct lazy_rolling_variance
167         : depends_on< rolling_count, rolling_mean, rolling_moment<2> >
168     {
169         /// INTERNAL ONLY
170         ///
171         typedef accumulators::impl::lazy_rolling_variance_impl< mpl::_1 > impl;
172
173         #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
174         /// tag::rolling_window::window_size named parameter
175         static boost::parameter::keyword<tag::rolling_window_size> const window_size;
176         #endif
177     };
178
179     struct immediate_rolling_variance
180         : depends_on< rolling_window_plus1, rolling_count, immediate_rolling_mean>
181     {
182         /// INTERNAL ONLY
183         ///
184         typedef accumulators::impl::immediate_rolling_variance_impl< mpl::_1> impl;
185
186         #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
187         /// tag::rolling_window::window_size named parameter
188         static boost::parameter::keyword<tag::rolling_window_size> const window_size;
189         #endif
190     };
191
192     // make immediate_rolling_variance the default implementation
193     struct rolling_variance : immediate_rolling_variance {};
194 } // namespace tag
195
196 ///////////////////////////////////////////////////////////////////////////////
197 // extract::lazy_rolling_variance
198 // extract::immediate_rolling_variance
199 // extract::rolling_variance
200 //
201 namespace extract
202 {
203     extractor<tag::lazy_rolling_variance> const lazy_rolling_variance = {};
204     extractor<tag::immediate_rolling_variance> const immediate_rolling_variance = {};
205     extractor<tag::rolling_variance> const rolling_variance = {};
206
207     BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_variance)
208     BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_variance)
209     BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_variance)
210 }
211
212 using extract::lazy_rolling_variance;
213 using extract::immediate_rolling_variance;
214 using extract::rolling_variance;
215
216 // rolling_variance(lazy) -> lazy_rolling_variance
217 template<>
218 struct as_feature<tag::rolling_variance(lazy)>
219 {
220     typedef tag::lazy_rolling_variance type;
221 };
222
223 // rolling_variance(immediate) -> immediate_rolling_variance
224 template<>
225 struct as_feature<tag::rolling_variance(immediate)>
226 {
227     typedef tag::immediate_rolling_variance type;
228 };
229
230 // for the purposes of feature-based dependency resolution,
231 // lazy_rolling_variance provides the same feature as rolling_variance
232 template<>
233 struct feature_of<tag::lazy_rolling_variance>
234     : feature_of<tag::rolling_variance>
235 {
236 };
237
238 // for the purposes of feature-based dependency resolution,
239 // immediate_rolling_variance provides the same feature as rolling_variance
240 template<>
241 struct feature_of<tag::immediate_rolling_variance>
242   : feature_of<tag::rolling_variance>
243 {
244 };
245 }} // namespace boost::accumulators
246
247 #endif