123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
- #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
- #include <boost/accumulators/accumulators.hpp>
- #include <boost/accumulators/statistics/stats.hpp>
- #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_mean.hpp>
- #include <boost/accumulators/statistics/rolling_moment.hpp>
- #include <boost/type_traits/is_arithmetic.hpp>
- #include <boost/utility/enable_if.hpp>
- namespace boost { namespace accumulators
- {
- namespace impl
- {
-
-
-
-
-
- template<typename Sample>
- struct lazy_rolling_variance_impl
- : accumulator_base
- {
-
- typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
- lazy_rolling_variance_impl(dont_care) {}
- template<typename Args>
- result_type result(Args const &args) const
- {
- result_type mean = rolling_mean(args);
- size_t nr_samples = rolling_count(args);
- if (nr_samples < 2) return result_type();
- return nr_samples*(rolling_moment<2>(args) - mean*mean)/(nr_samples-1);
- }
-
-
- template<class Archive>
- void serialize(Archive & ar, const unsigned int file_version) {}
- };
-
-
-
-
-
- template<typename Sample>
- struct immediate_rolling_variance_impl
- : accumulator_base
- {
-
- typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
- template<typename Args>
- immediate_rolling_variance_impl(Args const &args)
- : previous_mean_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
- , sum_of_squares_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
- {
- }
- template<typename Args>
- void operator()(Args const &args)
- {
- Sample added_sample = args[sample];
- result_type mean = immediate_rolling_mean(args);
- sum_of_squares_ += (added_sample-mean)*(added_sample-previous_mean_);
- if(is_rolling_window_plus1_full(args))
- {
- Sample removed_sample = rolling_window_plus1(args).front();
- sum_of_squares_ -= (removed_sample-mean)*(removed_sample-previous_mean_);
- prevent_underflow(sum_of_squares_);
- }
- previous_mean_ = mean;
- }
- template<typename Args>
- result_type result(Args const &args) const
- {
- size_t nr_samples = rolling_count(args);
- if (nr_samples < 2) return result_type();
- return numeric::fdiv(sum_of_squares_,(nr_samples-1));
- }
-
- // make this accumulator serializeable
- template<class Archive>
- void serialize(Archive & ar, const unsigned int file_version)
- {
- ar & previous_mean_;
- ar & sum_of_squares_;
- }
- private:
- result_type previous_mean_;
- result_type sum_of_squares_;
- template<typename T>
- void prevent_underflow(T &non_negative_number,typename boost::enable_if<boost::is_arithmetic<T>,T>::type* = 0)
- {
- if (non_negative_number < T(0)) non_negative_number = T(0);
- }
- template<typename T>
- void prevent_underflow(T &non_arithmetic_quantity,typename boost::disable_if<boost::is_arithmetic<T>,T>::type* = 0)
- {
- }
- };
- }
- namespace tag
- {
- struct lazy_rolling_variance
- : depends_on< rolling_count, rolling_mean, rolling_moment<2> >
- {
-
-
- typedef accumulators::impl::lazy_rolling_variance_impl< mpl::_1 > impl;
- #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
-
- static boost::parameter::keyword<tag::rolling_window_size> const window_size;
- #endif
- };
- struct immediate_rolling_variance
- : depends_on< rolling_window_plus1, rolling_count, immediate_rolling_mean>
- {
-
-
- typedef accumulators::impl::immediate_rolling_variance_impl< mpl::_1> impl;
- #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
-
- static boost::parameter::keyword<tag::rolling_window_size> const window_size;
- #endif
- };
-
- struct rolling_variance : immediate_rolling_variance {};
- }
- namespace extract
- {
- extractor<tag::lazy_rolling_variance> const lazy_rolling_variance = {};
- extractor<tag::immediate_rolling_variance> const immediate_rolling_variance = {};
- extractor<tag::rolling_variance> const rolling_variance = {};
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_variance)
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_variance)
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_variance)
- }
- using extract::lazy_rolling_variance;
- using extract::immediate_rolling_variance;
- using extract::rolling_variance;
- template<>
- struct as_feature<tag::rolling_variance(lazy)>
- {
- typedef tag::lazy_rolling_variance type;
- };
- template<>
- struct as_feature<tag::rolling_variance(immediate)>
- {
- typedef tag::immediate_rolling_variance type;
- };
- template<>
- struct feature_of<tag::lazy_rolling_variance>
- : feature_of<tag::rolling_variance>
- {
- };
- template<>
- struct feature_of<tag::immediate_rolling_variance>
- : feature_of<tag::rolling_variance>
- {
- };
- }}
- #endif
|