rolling_variance.hpp 9.3 KB

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