1 ///////////////////////////////////////////////////////////////////////////////
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)
9 #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MOMENT_HPP_EAN_27_11_2005
10 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MOMENT_HPP_EAN_27_11_2005
12 #include <boost/config/no_tr1/cmath.hpp>
13 #include <boost/mpl/int.hpp>
14 #include <boost/mpl/assert.hpp>
15 #include <boost/mpl/placeholders.hpp>
16 #include <boost/accumulators/framework/accumulator_base.hpp>
17 #include <boost/accumulators/framework/extractor.hpp>
18 #include <boost/accumulators/numeric/functional.hpp>
19 #include <boost/accumulators/framework/parameters/sample.hpp>
20 #include <boost/accumulators/framework/depends_on.hpp>
21 #include <boost/accumulators/statistics_fwd.hpp>
22 #include <boost/accumulators/statistics/moment.hpp>
23 #include <boost/accumulators/statistics/rolling_count.hpp>
25 namespace boost { namespace accumulators
29 ///////////////////////////////////////////////////////////////////////////////
30 // rolling_moment_impl
31 template<typename N, typename Sample>
32 struct rolling_moment_impl
35 BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
36 // for boost::result_of
37 typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
39 template<typename Args>
40 rolling_moment_impl(Args const &args)
41 : sum_(args[sample | Sample()])
45 template<typename Args>
46 void operator ()(Args const &args)
48 if(is_rolling_window_plus1_full(args))
50 this->sum_ -= numeric::pow(rolling_window_plus1(args).front(), N());
52 this->sum_ += numeric::pow(args[sample], N());
55 template<typename Args>
56 result_type result(Args const &args) const
58 return numeric::fdiv(this->sum_, rolling_count(args));
66 ///////////////////////////////////////////////////////////////////////////////
67 // tag::rolling_moment
73 : depends_on< rolling_window_plus1, rolling_count>
77 typedef accumulators::impl::rolling_moment_impl<mpl::int_<N>, mpl::_1> impl;
79 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
80 /// tag::rolling_window::window_size named parameter
81 static boost::parameter::keyword<tag::rolling_window_size> const window_size;
86 ///////////////////////////////////////////////////////////////////////////////
87 // extract::rolling_moment
91 BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, rolling_moment, (int))
94 using extract::rolling_moment;
96 // There is no weighted_rolling_moment (yet)...
98 //// So that rolling_moment<N> can be automatically substituted with
99 //// weighted_rolling_moment<N> when the weight parameter is non-void
101 //struct as_weighted_feature<tag::rolling_moment<N> >
103 // typedef tag::weighted_rolling_moment<N> type;
107 //struct feature_of<tag::weighted_rolling_moment<N> >
108 // : feature_of<tag::rolling_moment<N> >
111 }} // namespace boost::accumulators