rolling_mean.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // rolling_mean.hpp
  3. // Copyright (C) 2008 Eric Niebler.
  4. // Copyright (C) 2012 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_MEAN_HPP_EAN_26_12_2008
  9. #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
  10. #include <boost/mpl/placeholders.hpp>
  11. #include <boost/accumulators/framework/accumulator_base.hpp>
  12. #include <boost/accumulators/framework/extractor.hpp>
  13. #include <boost/accumulators/numeric/functional.hpp>
  14. #include <boost/accumulators/framework/parameters/sample.hpp>
  15. #include <boost/accumulators/framework/depends_on.hpp>
  16. #include <boost/accumulators/statistics_fwd.hpp>
  17. #include <boost/accumulators/statistics/rolling_sum.hpp>
  18. #include <boost/accumulators/statistics/rolling_count.hpp>
  19. namespace boost { namespace accumulators
  20. {
  21. namespace impl
  22. {
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // lazy_rolling_mean_impl
  25. // returns the mean over the rolling window and is calculated only
  26. // when the result is requested
  27. template<typename Sample>
  28. struct lazy_rolling_mean_impl
  29. : accumulator_base
  30. {
  31. // for boost::result_of
  32. typedef typename numeric::functional::fdiv<Sample, std::size_t, void, void>::result_type result_type;
  33. lazy_rolling_mean_impl(dont_care)
  34. {
  35. }
  36. template<typename Args>
  37. result_type result(Args const &args) const
  38. {
  39. return numeric::fdiv(rolling_sum(args), rolling_count(args));
  40. }
  41. };
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // immediate_rolling_mean_impl
  44. // The non-lazy version computes the rolling mean recursively when a new
  45. // sample is added
  46. template<typename Sample>
  47. struct immediate_rolling_mean_impl
  48. : accumulator_base
  49. {
  50. // for boost::result_of
  51. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
  52. template<typename Args>
  53. immediate_rolling_mean_impl(Args const &args)
  54. : mean_(numeric::fdiv(args[sample | Sample()],numeric::one<std::size_t>::value))
  55. {
  56. }
  57. template<typename Args>
  58. void operator()(Args const &args)
  59. {
  60. if(is_rolling_window_plus1_full(args))
  61. {
  62. mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args));
  63. }
  64. else
  65. {
  66. result_type prev_mean = mean_;
  67. mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args));
  68. }
  69. }
  70. template<typename Args>
  71. result_type result(Args const &) const
  72. {
  73. return mean_;
  74. }
  75. private:
  76. result_type mean_;
  77. };
  78. } // namespace impl
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // tag::lazy_rolling_mean
  81. // tag::immediate_rolling_mean
  82. // tag::rolling_mean
  83. //
  84. namespace tag
  85. {
  86. struct lazy_rolling_mean
  87. : depends_on< rolling_sum, rolling_count >
  88. {
  89. /// INTERNAL ONLY
  90. ///
  91. typedef accumulators::impl::lazy_rolling_mean_impl< mpl::_1 > impl;
  92. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  93. /// tag::rolling_window::window_size named parameter
  94. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  95. #endif
  96. };
  97. struct immediate_rolling_mean
  98. : depends_on< rolling_window_plus1, rolling_count>
  99. {
  100. /// INTERNAL ONLY
  101. ///
  102. typedef accumulators::impl::immediate_rolling_mean_impl< mpl::_1> impl;
  103. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  104. /// tag::rolling_window::window_size named parameter
  105. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  106. #endif
  107. };
  108. // make immediate_rolling_mean the default implementation
  109. struct rolling_mean : immediate_rolling_mean {};
  110. } // namespace tag
  111. ///////////////////////////////////////////////////////////////////////////////
  112. // extract::lazy_rolling_mean
  113. // extract::immediate_rolling_mean
  114. // extract::rolling_mean
  115. //
  116. namespace extract
  117. {
  118. extractor<tag::lazy_rolling_mean> const lazy_rolling_mean = {};
  119. extractor<tag::immediate_rolling_mean> const immediate_rolling_mean = {};
  120. extractor<tag::rolling_mean> const rolling_mean = {};
  121. BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_mean)
  122. BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_mean)
  123. BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean)
  124. }
  125. using extract::lazy_rolling_mean;
  126. using extract::immediate_rolling_mean;
  127. using extract::rolling_mean;
  128. // rolling_mean(lazy) -> lazy_rolling_mean
  129. template<>
  130. struct as_feature<tag::rolling_mean(lazy)>
  131. {
  132. typedef tag::lazy_rolling_mean type;
  133. };
  134. // rolling_mean(immediate) -> immediate_rolling_mean
  135. template<>
  136. struct as_feature<tag::rolling_mean(immediate)>
  137. {
  138. typedef tag::immediate_rolling_mean type;
  139. };
  140. // for the purposes of feature-based dependency resolution,
  141. // immediate_rolling_mean provides the same feature as rolling_mean
  142. template<>
  143. struct feature_of<tag::immediate_rolling_mean>
  144. : feature_of<tag::rolling_mean>
  145. {
  146. };
  147. // for the purposes of feature-based dependency resolution,
  148. // lazy_rolling_mean provides the same feature as rolling_mean
  149. template<>
  150. struct feature_of<tag::lazy_rolling_mean>
  151. : feature_of<tag::rolling_mean>
  152. {
  153. };
  154. }} // namespace boost::accumulators
  155. #endif