rolling_moment.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // make this accumulator serializeable
  55. template<class Archive>
  56. void serialize(Archive & ar, const unsigned int file_version)
  57. {
  58. ar & sum_;
  59. }
  60. private:
  61. result_type sum_;
  62. };
  63. } // namespace impl
  64. ///////////////////////////////////////////////////////////////////////////////
  65. // tag::rolling_moment
  66. //
  67. namespace tag
  68. {
  69. template<int N>
  70. struct rolling_moment
  71. : depends_on< rolling_window_plus1, rolling_count>
  72. {
  73. /// INTERNAL ONLY
  74. ///
  75. typedef accumulators::impl::rolling_moment_impl<mpl::int_<N>, mpl::_1> impl;
  76. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  77. /// tag::rolling_window::window_size named parameter
  78. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  79. #endif
  80. };
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////
  83. // extract::rolling_moment
  84. //
  85. namespace extract
  86. {
  87. BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, rolling_moment, (int))
  88. }
  89. using extract::rolling_moment;
  90. // There is no weighted_rolling_moment (yet)...
  91. //
  92. //// So that rolling_moment<N> can be automatically substituted with
  93. //// weighted_rolling_moment<N> when the weight parameter is non-void
  94. //template<int N>
  95. //struct as_weighted_feature<tag::rolling_moment<N> >
  96. //{
  97. // typedef tag::weighted_rolling_moment<N> type;
  98. //};
  99. //
  100. //template<int N>
  101. //struct feature_of<tag::weighted_rolling_moment<N> >
  102. // : feature_of<tag::rolling_moment<N> >
  103. //{
  104. //};
  105. }} // namespace boost::accumulators
  106. #endif