123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
- #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
- #include <boost/mpl/placeholders.hpp>
- #include <boost/accumulators/framework/accumulator_base.hpp>
- #include <boost/accumulators/framework/extractor.hpp>
- #include <boost/accumulators/numeric/functional.hpp>
- #include <boost/accumulators/framework/parameters/sample.hpp>
- #include <boost/accumulators/framework/depends_on.hpp>
- #include <boost/accumulators/statistics_fwd.hpp>
- #include <boost/accumulators/statistics/rolling_sum.hpp>
- #include <boost/accumulators/statistics/rolling_count.hpp>
- namespace boost { namespace accumulators
- {
- namespace impl
- {
-
-
-
-
- template<typename Sample>
- struct lazy_rolling_mean_impl
- : accumulator_base
- {
-
- typedef typename numeric::functional::fdiv<Sample, std::size_t, void, void>::result_type result_type;
- lazy_rolling_mean_impl(dont_care)
- {
- }
- template<typename Args>
- result_type result(Args const &args) const
- {
- return numeric::fdiv(rolling_sum(args), rolling_count(args));
- }
-
-
- template<class Archive>
- void serialize(Archive & ar, const unsigned int file_version) {}
- };
-
-
-
-
- template<typename Sample>
- struct immediate_rolling_mean_impl
- : accumulator_base
- {
-
- typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
- template<typename Args>
- immediate_rolling_mean_impl(Args const &args)
- : mean_(numeric::fdiv(args[sample | Sample()],numeric::one<std::size_t>::value))
- {
- }
- template<typename Args>
- void operator()(Args const &args)
- {
- if(is_rolling_window_plus1_full(args))
- {
- if (rolling_window_plus1(args).front() > args[sample])
- mean_ -= numeric::fdiv(rolling_window_plus1(args).front()-args[sample],rolling_count(args));
- else if (rolling_window_plus1(args).front() < args[sample])
- mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args));
- }
- else
- {
- result_type prev_mean = mean_;
- if (prev_mean > args[sample])
- mean_ -= numeric::fdiv(prev_mean-args[sample],rolling_count(args));
- else if (prev_mean < args[sample])
- mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args));
- }
- }
- template<typename Args>
- result_type result(Args const &) const
- {
- return mean_;
- }
-
-
- template<class Archive>
- void serialize(Archive & ar, const unsigned int file_version)
- {
- ar & mean_;
- }
- private:
- result_type mean_;
- };
- }
-
-
-
-
-
- namespace tag
- {
- struct lazy_rolling_mean
- : depends_on< rolling_sum, rolling_count >
- {
-
-
- typedef accumulators::impl::lazy_rolling_mean_impl< mpl::_1 > impl;
- #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
-
- static boost::parameter::keyword<tag::rolling_window_size> const window_size;
- #endif
- };
- struct immediate_rolling_mean
- : depends_on< rolling_window_plus1, rolling_count>
- {
-
-
- typedef accumulators::impl::immediate_rolling_mean_impl< mpl::_1> impl;
- #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
-
- static boost::parameter::keyword<tag::rolling_window_size> const window_size;
- #endif
- };
-
- struct rolling_mean : immediate_rolling_mean {};
- }
-
-
-
-
-
- namespace extract
- {
- extractor<tag::lazy_rolling_mean> const lazy_rolling_mean = {};
- extractor<tag::immediate_rolling_mean> const immediate_rolling_mean = {};
- extractor<tag::rolling_mean> const rolling_mean = {};
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_mean)
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_mean)
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean)
- }
- using extract::lazy_rolling_mean;
- using extract::immediate_rolling_mean;
- using extract::rolling_mean;
-
- template<>
- struct as_feature<tag::rolling_mean(lazy)>
- {
- typedef tag::lazy_rolling_mean type;
- };
-
- template<>
- struct as_feature<tag::rolling_mean(immediate)>
- {
- typedef tag::immediate_rolling_mean type;
- };
-
-
- template<>
- struct feature_of<tag::immediate_rolling_mean>
- : feature_of<tag::rolling_mean>
- {
- };
-
-
- template<>
- struct feature_of<tag::lazy_rolling_mean>
- : feature_of<tag::rolling_mean>
- {
- };
- }}
- #endif
|