rolling_moment.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // rolling_moment.hpp
  3. // Copyright 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_MOMENT_HPP_EAN_27_11_2005
  9. #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MOMENT_HPP_EAN_27_11_2005
  10. #include <boost/config/no_tr1/cmath.hpp>
  11. #include <boost/mpl/int.hpp>
  12. #include <boost/mpl/assert.hpp>
  13. #include <boost/mpl/placeholders.hpp>
  14. #include <boost/accumulators/framework/accumulator_base.hpp>
  15. #include <boost/accumulators/framework/extractor.hpp>
  16. #include <boost/accumulators/numeric/functional.hpp>
  17. #include <boost/accumulators/framework/parameters/sample.hpp>
  18. #include <boost/accumulators/framework/depends_on.hpp>
  19. #include <boost/accumulators/statistics_fwd.hpp>
  20. #include <boost/accumulators/statistics/moment.hpp>
  21. #include <boost/accumulators/statistics/rolling_count.hpp>
  22. namespace boost { namespace accumulators
  23. {
  24. namespace impl
  25. {
  26. ///////////////////////////////////////////////////////////////////////////////
  27. // rolling_moment_impl
  28. template<typename N, typename Sample>
  29. struct rolling_moment_impl
  30. : accumulator_base
  31. {
  32. BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
  33. // for boost::result_of
  34. typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
  35. template<typename Args>
  36. rolling_moment_impl(Args const &args)
  37. : sum_(args[sample | Sample()])
  38. {
  39. }
  40. template<typename Args>
  41. void operator ()(Args const &args)
  42. {
  43. if(is_rolling_window_plus1_full(args))
  44. {
  45. this->sum_ -= numeric::pow(rolling_window_plus1(args).front(), N());
  46. }
  47. this->sum_ += numeric::pow(args[sample], N());
  48. }
  49. template<typename Args>
  50. result_type result(Args const &args) const
  51. {
  52. return numeric::fdiv(this->sum_, rolling_count(args));
  53. }
  54. private:
  55. result_type sum_;
  56. };
  57. } // namespace impl
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // tag::rolling_moment
  60. //
  61. namespace tag
  62. {
  63. template<int N>
  64. struct rolling_moment
  65. : depends_on< rolling_window_plus1, rolling_count>
  66. {
  67. /// INTERNAL ONLY
  68. ///
  69. typedef accumulators::impl::rolling_moment_impl<mpl::int_<N>, mpl::_1> impl;
  70. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  71. /// tag::rolling_window::window_size named parameter
  72. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  73. #endif
  74. };
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // extract::rolling_moment
  78. //
  79. namespace extract
  80. {
  81. BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, rolling_moment, (int))
  82. }
  83. using extract::rolling_moment;
  84. // There is no weighted_rolling_moment (yet)...
  85. //
  86. //// So that rolling_moment<N> can be automatically substituted with
  87. //// weighted_rolling_moment<N> when the weight parameter is non-void
  88. //template<int N>
  89. //struct as_weighted_feature<tag::rolling_moment<N> >
  90. //{
  91. // typedef tag::weighted_rolling_moment<N> type;
  92. //};
  93. //
  94. //template<int N>
  95. //struct feature_of<tag::weighted_rolling_moment<N> >
  96. // : feature_of<tag::rolling_moment<N> >
  97. //{
  98. //};
  99. }} // namespace boost::accumulators
  100. #endif