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