rolling_mean.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // serialization is done by accumulators it depends on
  42. template<class Archive>
  43. void serialize(Archive & ar, const unsigned int file_version) {}
  44. };
  45. ///////////////////////////////////////////////////////////////////////////////
  46. // immediate_rolling_mean_impl
  47. // The non-lazy version computes the rolling mean recursively when a new
  48. // sample is added
  49. template<typename Sample>
  50. struct immediate_rolling_mean_impl
  51. : accumulator_base
  52. {
  53. // for boost::result_of
  54. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
  55. template<typename Args>
  56. immediate_rolling_mean_impl(Args const &args)
  57. : mean_(numeric::fdiv(args[sample | Sample()],numeric::one<std::size_t>::value))
  58. {
  59. }
  60. template<typename Args>
  61. void operator()(Args const &args)
  62. {
  63. if(is_rolling_window_plus1_full(args))
  64. {
  65. if (rolling_window_plus1(args).front() > args[sample])
  66. mean_ -= numeric::fdiv(rolling_window_plus1(args).front()-args[sample],rolling_count(args));
  67. else if (rolling_window_plus1(args).front() < args[sample])
  68. mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args));
  69. }
  70. else
  71. {
  72. result_type prev_mean = mean_;
  73. if (prev_mean > args[sample])
  74. mean_ -= numeric::fdiv(prev_mean-args[sample],rolling_count(args));
  75. else if (prev_mean < args[sample])
  76. mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args));
  77. }
  78. }
  79. template<typename Args>
  80. result_type result(Args const &) const
  81. {
  82. return mean_;
  83. }
  84. // make this accumulator serializeable
  85. template<class Archive>
  86. void serialize(Archive & ar, const unsigned int file_version)
  87. {
  88. ar & mean_;
  89. }
  90. private:
  91. result_type mean_;
  92. };
  93. } // namespace impl
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // tag::lazy_rolling_mean
  96. // tag::immediate_rolling_mean
  97. // tag::rolling_mean
  98. //
  99. namespace tag
  100. {
  101. struct lazy_rolling_mean
  102. : depends_on< rolling_sum, rolling_count >
  103. {
  104. /// INTERNAL ONLY
  105. ///
  106. typedef accumulators::impl::lazy_rolling_mean_impl< mpl::_1 > impl;
  107. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  108. /// tag::rolling_window::window_size named parameter
  109. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  110. #endif
  111. };
  112. struct immediate_rolling_mean
  113. : depends_on< rolling_window_plus1, rolling_count>
  114. {
  115. /// INTERNAL ONLY
  116. ///
  117. typedef accumulators::impl::immediate_rolling_mean_impl< mpl::_1> impl;
  118. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  119. /// tag::rolling_window::window_size named parameter
  120. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  121. #endif
  122. };
  123. // make immediate_rolling_mean the default implementation
  124. struct rolling_mean : immediate_rolling_mean {};
  125. } // namespace tag
  126. ///////////////////////////////////////////////////////////////////////////////
  127. // extract::lazy_rolling_mean
  128. // extract::immediate_rolling_mean
  129. // extract::rolling_mean
  130. //
  131. namespace extract
  132. {
  133. extractor<tag::lazy_rolling_mean> const lazy_rolling_mean = {};
  134. extractor<tag::immediate_rolling_mean> const immediate_rolling_mean = {};
  135. extractor<tag::rolling_mean> const rolling_mean = {};
  136. BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_mean)
  137. BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_mean)
  138. BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean)
  139. }
  140. using extract::lazy_rolling_mean;
  141. using extract::immediate_rolling_mean;
  142. using extract::rolling_mean;
  143. // rolling_mean(lazy) -> lazy_rolling_mean
  144. template<>
  145. struct as_feature<tag::rolling_mean(lazy)>
  146. {
  147. typedef tag::lazy_rolling_mean type;
  148. };
  149. // rolling_mean(immediate) -> immediate_rolling_mean
  150. template<>
  151. struct as_feature<tag::rolling_mean(immediate)>
  152. {
  153. typedef tag::immediate_rolling_mean type;
  154. };
  155. // for the purposes of feature-based dependency resolution,
  156. // immediate_rolling_mean provides the same feature as rolling_mean
  157. template<>
  158. struct feature_of<tag::immediate_rolling_mean>
  159. : feature_of<tag::rolling_mean>
  160. {
  161. };
  162. // for the purposes of feature-based dependency resolution,
  163. // lazy_rolling_mean provides the same feature as rolling_mean
  164. template<>
  165. struct feature_of<tag::lazy_rolling_mean>
  166. : feature_of<tag::rolling_mean>
  167. {
  168. };
  169. }} // namespace boost::accumulators
  170. #endif