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)
9 #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
10 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
12 #include <boost/accumulators/accumulators.hpp>
13 #include <boost/accumulators/statistics/stats.hpp>
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>
25 #include <boost/type_traits/is_arithmetic.hpp>
26 #include <boost/utility/enable_if.hpp>
28 namespace boost { namespace accumulators
32 //! Immediate (lazy) calculation of the rolling variance.
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
38 \sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2.
40 When \f$n > N\f$, the sample variance over the window becomes:
42 \sigma_n^2 = \frac{1}{N-1} \sum_{i = n-N+1}^n (x_i - \mu_n)^2.
45 ///////////////////////////////////////////////////////////////////////////////
46 // lazy_rolling_variance_impl
48 template<typename Sample>
49 struct lazy_rolling_variance_impl
52 // for boost::result_of
53 typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
55 lazy_rolling_variance_impl(dont_care) {}
57 template<typename Args>
58 result_type result(Args const &args) const
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);
67 //! Iterative calculation of the rolling variance.
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
73 \sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2 = \frac{1}{n-1}M_{2,n},
75 where the sum of squares \f$M_{2,n}\f$ can be recursively computed as:
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}),
79 and the estimate of the sample mean as:
81 \mu_n = \frac{1}{n} \sum_{i = 1}^n x_i = \mu_{n-1} + \frac{1}{n}(x_n - \mu_{n-1}).
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:
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},
88 where the sum of squares \f$M_{2,n}\f$ now equals:
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}),
92 and the estimated mean is:
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}).
97 Note that the sample variance is not defined for \f$n <= 1\f$.
100 ///////////////////////////////////////////////////////////////////////////////
101 // immediate_rolling_variance_impl
103 template<typename Sample>
104 struct immediate_rolling_variance_impl
107 // for boost::result_of
108 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
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))
117 template<typename Args>
118 void operator()(Args const &args)
120 Sample added_sample = args[sample];
122 result_type mean = immediate_rolling_mean(args);
123 sum_of_squares_ += (added_sample-mean)*(added_sample-previous_mean_);
125 if(is_rolling_window_plus1_full(args))
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_);
131 previous_mean_ = mean;
134 template<typename Args>
135 result_type result(Args const &args) const
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));
144 result_type previous_mean_;
145 result_type sum_of_squares_;
148 void prevent_underflow(T &non_negative_number,typename boost::enable_if<boost::is_arithmetic<T>,T>::type* = 0)
150 if (non_negative_number < T(0)) non_negative_number = T(0);
153 void prevent_underflow(T &non_arithmetic_quantity,typename boost::disable_if<boost::is_arithmetic<T>,T>::type* = 0)
159 ///////////////////////////////////////////////////////////////////////////////
160 // tag:: lazy_rolling_variance
161 // tag:: immediate_rolling_variance
162 // tag:: rolling_variance
166 struct lazy_rolling_variance
167 : depends_on< rolling_count, rolling_mean, rolling_moment<2> >
171 typedef accumulators::impl::lazy_rolling_variance_impl< mpl::_1 > impl;
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;
179 struct immediate_rolling_variance
180 : depends_on< rolling_window_plus1, rolling_count, immediate_rolling_mean>
184 typedef accumulators::impl::immediate_rolling_variance_impl< mpl::_1> impl;
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;
192 // make immediate_rolling_variance the default implementation
193 struct rolling_variance : immediate_rolling_variance {};
196 ///////////////////////////////////////////////////////////////////////////////
197 // extract::lazy_rolling_variance
198 // extract::immediate_rolling_variance
199 // extract::rolling_variance
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 = {};
207 BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_variance)
208 BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_variance)
209 BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_variance)
212 using extract::lazy_rolling_variance;
213 using extract::immediate_rolling_variance;
214 using extract::rolling_variance;
216 // rolling_variance(lazy) -> lazy_rolling_variance
218 struct as_feature<tag::rolling_variance(lazy)>
220 typedef tag::lazy_rolling_variance type;
223 // rolling_variance(immediate) -> immediate_rolling_variance
225 struct as_feature<tag::rolling_variance(immediate)>
227 typedef tag::immediate_rolling_variance type;
230 // for the purposes of feature-based dependency resolution,
231 // lazy_rolling_variance provides the same feature as rolling_variance
233 struct feature_of<tag::lazy_rolling_variance>
234 : feature_of<tag::rolling_variance>
238 // for the purposes of feature-based dependency resolution,
239 // immediate_rolling_variance provides the same feature as rolling_variance
241 struct feature_of<tag::immediate_rolling_variance>
242 : feature_of<tag::rolling_variance>
245 }} // namespace boost::accumulators