Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:20

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // rolling_mean.hpp
0003 // Copyright (C) 2008 Eric Niebler.
0004 // Copyright (C) 2012 Pieter Bastiaan Ober (Integricom).
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // (See accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
0010 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
0011 
0012 #include <boost/mpl/placeholders.hpp>
0013 #include <boost/accumulators/framework/accumulator_base.hpp>
0014 #include <boost/accumulators/framework/extractor.hpp>
0015 #include <boost/accumulators/numeric/functional.hpp>
0016 #include <boost/accumulators/framework/parameters/sample.hpp>
0017 #include <boost/accumulators/framework/depends_on.hpp>
0018 #include <boost/accumulators/statistics_fwd.hpp>
0019 #include <boost/accumulators/statistics/rolling_sum.hpp>
0020 #include <boost/accumulators/statistics/rolling_count.hpp>
0021 
0022 namespace boost { namespace accumulators
0023 {
0024    namespace impl
0025    {
0026       ///////////////////////////////////////////////////////////////////////////////
0027       // lazy_rolling_mean_impl
0028       //    returns the mean over the rolling window and is calculated only
0029       //    when the result is requested
0030       template<typename Sample>
0031       struct lazy_rolling_mean_impl
0032          : accumulator_base
0033       {
0034          // for boost::result_of
0035          typedef typename numeric::functional::fdiv<Sample, std::size_t, void, void>::result_type result_type;
0036 
0037          lazy_rolling_mean_impl(dont_care)
0038          {
0039          }
0040 
0041          template<typename Args>
0042          result_type result(Args const &args) const
0043          {
0044             return numeric::fdiv(rolling_sum(args), rolling_count(args));
0045          }
0046         
0047          // serialization is done by accumulators it depends on
0048          template<class Archive>
0049          void serialize(Archive & ar, const unsigned int file_version) {}
0050       };
0051 
0052       ///////////////////////////////////////////////////////////////////////////////
0053       // immediate_rolling_mean_impl
0054       //     The non-lazy version computes the rolling mean recursively when a new
0055       //     sample is added
0056       template<typename Sample>
0057       struct immediate_rolling_mean_impl
0058          : accumulator_base
0059       {
0060          // for boost::result_of
0061          typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
0062 
0063          template<typename Args>
0064          immediate_rolling_mean_impl(Args const &args)
0065             : mean_(numeric::fdiv(args[sample | Sample()],numeric::one<std::size_t>::value))
0066          {
0067          }
0068 
0069          template<typename Args>
0070          void operator()(Args const &args)
0071          {
0072             if(is_rolling_window_plus1_full(args))
0073             {
0074                if (rolling_window_plus1(args).front() > args[sample])
0075                   mean_ -= numeric::fdiv(rolling_window_plus1(args).front()-args[sample],rolling_count(args));
0076                else if (rolling_window_plus1(args).front() < args[sample])
0077                   mean_ += numeric::fdiv(args[sample]-rolling_window_plus1(args).front(),rolling_count(args));
0078             }
0079             else
0080             {
0081                result_type prev_mean = mean_;
0082                if (prev_mean > args[sample])
0083                    mean_ -= numeric::fdiv(prev_mean-args[sample],rolling_count(args));
0084                else if (prev_mean < args[sample])
0085                    mean_ += numeric::fdiv(args[sample]-prev_mean,rolling_count(args));
0086             }
0087          }
0088 
0089          template<typename Args>
0090          result_type result(Args const &) const
0091          {
0092             return mean_;
0093          }
0094         
0095          // make this accumulator serializeable
0096          template<class Archive>
0097          void serialize(Archive & ar, const unsigned int file_version)
0098          { 
0099             ar & mean_;
0100          }
0101 
0102       private:
0103 
0104          result_type mean_;
0105       };
0106    } // namespace impl
0107 
0108    ///////////////////////////////////////////////////////////////////////////////
0109    // tag::lazy_rolling_mean
0110    // tag::immediate_rolling_mean
0111    // tag::rolling_mean
0112    //
0113    namespace tag
0114    {
0115       struct lazy_rolling_mean
0116          : depends_on< rolling_sum, rolling_count >
0117       {
0118          /// INTERNAL ONLY
0119          ///
0120          typedef accumulators::impl::lazy_rolling_mean_impl< mpl::_1 > impl;
0121 
0122 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
0123          /// tag::rolling_window::window_size named parameter
0124          static boost::parameter::keyword<tag::rolling_window_size> const window_size;
0125 #endif
0126       };
0127 
0128       struct immediate_rolling_mean
0129          : depends_on< rolling_window_plus1, rolling_count>
0130       {
0131          /// INTERNAL ONLY
0132          ///
0133          typedef accumulators::impl::immediate_rolling_mean_impl< mpl::_1> impl;
0134 
0135 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
0136          /// tag::rolling_window::window_size named parameter
0137          static boost::parameter::keyword<tag::rolling_window_size> const window_size;
0138 #endif
0139       };
0140 
0141       // make immediate_rolling_mean the default implementation
0142       struct rolling_mean : immediate_rolling_mean {};
0143    } // namespace tag
0144 
0145    ///////////////////////////////////////////////////////////////////////////////
0146    // extract::lazy_rolling_mean
0147    // extract::immediate_rolling_mean
0148    // extract::rolling_mean
0149    //
0150    namespace extract
0151    {
0152       extractor<tag::lazy_rolling_mean> const lazy_rolling_mean = {};
0153       extractor<tag::immediate_rolling_mean> const immediate_rolling_mean = {};
0154       extractor<tag::rolling_mean> const rolling_mean = {};
0155 
0156       BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_mean)
0157          BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_mean)
0158          BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean)
0159    }
0160 
0161    using extract::lazy_rolling_mean;
0162    using extract::immediate_rolling_mean;
0163    using extract::rolling_mean;
0164 
0165    // rolling_mean(lazy) -> lazy_rolling_mean
0166    template<>
0167    struct as_feature<tag::rolling_mean(lazy)>
0168    {
0169       typedef tag::lazy_rolling_mean type;
0170    };
0171 
0172    // rolling_mean(immediate) -> immediate_rolling_mean
0173    template<>
0174    struct as_feature<tag::rolling_mean(immediate)>
0175    {
0176       typedef tag::immediate_rolling_mean type;
0177    };
0178 
0179    // for the purposes of feature-based dependency resolution,
0180    // immediate_rolling_mean provides the same feature as rolling_mean
0181    template<>
0182    struct feature_of<tag::immediate_rolling_mean>
0183       : feature_of<tag::rolling_mean>
0184    {
0185    };
0186 
0187    // for the purposes of feature-based dependency resolution,
0188    // lazy_rolling_mean provides the same feature as rolling_mean
0189    template<>
0190    struct feature_of<tag::lazy_rolling_mean>
0191       : feature_of<tag::rolling_mean>
0192    {
0193    };
0194 }} // namespace boost::accumulators
0195 
0196 #endif