Back to home page

EIC code displayed by LXR

 
 

    


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

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // rolling_variance.hpp
0003 // Copyright (C) 2005 Eric Niebler
0004 // Copyright (C) 2014 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_VARIANCE_HPP_EAN_15_11_2011
0010 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_VARIANCE_HPP_EAN_15_11_2011
0011 
0012 #include <boost/accumulators/accumulators.hpp>
0013 #include <boost/accumulators/statistics/stats.hpp>
0014 
0015 #include <boost/mpl/placeholders.hpp>
0016 #include <boost/accumulators/framework/accumulator_base.hpp>
0017 #include <boost/accumulators/framework/extractor.hpp>
0018 #include <boost/accumulators/numeric/functional.hpp>
0019 #include <boost/accumulators/framework/parameters/sample.hpp>
0020 #include <boost/accumulators/framework/depends_on.hpp>
0021 #include <boost/accumulators/statistics_fwd.hpp>
0022 #include <boost/accumulators/statistics/rolling_mean.hpp>
0023 #include <boost/accumulators/statistics/rolling_moment.hpp>
0024 
0025 #include <boost/type_traits/is_arithmetic.hpp>
0026 #include <boost/utility/enable_if.hpp>
0027 
0028 namespace boost { namespace accumulators
0029 {
0030 namespace impl
0031 {
0032     //! Immediate (lazy) calculation of the rolling variance.
0033     /*!
0034     Calculation of sample variance \f$\sigma_n^2\f$ is done as follows, see also
0035     http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
0036     For a rolling window of size \f$N\f$, when \f$n <= N\f$, the variance is computed according to the formula
0037     \f[
0038     \sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2.
0039     \f]
0040     When \f$n > N\f$, the sample variance over the window becomes:
0041     \f[
0042     \sigma_n^2 = \frac{1}{N-1} \sum_{i = n-N+1}^n (x_i - \mu_n)^2.
0043     \f]
0044     */
0045     ///////////////////////////////////////////////////////////////////////////////
0046     // lazy_rolling_variance_impl
0047     //
0048     template<typename Sample>
0049     struct lazy_rolling_variance_impl
0050         : accumulator_base
0051     {
0052         // for boost::result_of
0053         typedef typename numeric::functional::fdiv<Sample, std::size_t,void,void>::result_type result_type;
0054 
0055         lazy_rolling_variance_impl(dont_care) {}
0056 
0057         template<typename Args>
0058         result_type result(Args const &args) const
0059         {
0060             result_type mean = rolling_mean(args);
0061             size_t nr_samples = rolling_count(args);
0062             if (nr_samples < 2) return result_type();
0063             return nr_samples*(rolling_moment<2>(args) - mean*mean)/(nr_samples-1);
0064         }
0065         
0066         // serialization is done by accumulators it depends on
0067         template<class Archive>
0068         void serialize(Archive & ar, const unsigned int file_version) {}
0069     };
0070 
0071     //! Iterative calculation of the rolling variance.
0072     /*!
0073     Iterative calculation of sample variance \f$\sigma_n^2\f$ is done as follows, see also
0074     http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
0075     For a rolling window of size \f$N\f$, for the first \f$N\f$ samples, the variance is computed according to the formula
0076     \f[
0077     \sigma_n^2 = \frac{1}{n-1} \sum_{i = 1}^n (x_i - \mu_n)^2 = \frac{1}{n-1}M_{2,n},
0078     \f]
0079     where the sum of squares \f$M_{2,n}\f$ can be recursively computed as:
0080     \f[
0081     M_{2,n} = \sum_{i = 1}^n (x_i - \mu_n)^2 = M_{2,n-1} + (x_n - \mu_n)(x_n - \mu_{n-1}),
0082     \f]
0083     and the estimate of the sample mean as:
0084     \f[
0085     \mu_n = \frac{1}{n} \sum_{i = 1}^n x_i = \mu_{n-1} + \frac{1}{n}(x_n - \mu_{n-1}).
0086     \f]
0087     For further samples, when the rolling window is fully filled with data, one has to take into account that the oldest
0088     sample \f$x_{n-N}\f$ is dropped from the window. The sample variance over the window now becomes:
0089     \f[
0090     \sigma_n^2 = \frac{1}{N-1} \sum_{i = n-N+1}^n (x_i - \mu_n)^2 = \frac{1}{n-1}M_{2,n},
0091     \f]
0092     where the sum of squares \f$M_{2,n}\f$ now equals:
0093     \f[
0094     M_{2,n} = \sum_{i = n-N+1}^n (x_i - \mu_n)^2 = M_{2,n-1} + (x_n - \mu_n)(x_n - \mu_{n-1}) - (x_{n-N} - \mu_n)(x_{n-N} - \mu_{n-1}),
0095     \f]
0096     and the estimated mean is:
0097     \f[
0098     \mu_n = \frac{1}{N} \sum_{i = n-N+1}^n x_i = \mu_{n-1} + \frac{1}{n}(x_n - x_{n-N}).
0099     \f]
0100 
0101     Note that the sample variance is not defined for \f$n <= 1\f$.
0102 
0103     */
0104     ///////////////////////////////////////////////////////////////////////////////
0105     // immediate_rolling_variance_impl
0106     //
0107     template<typename Sample>
0108     struct immediate_rolling_variance_impl
0109         : accumulator_base
0110     {
0111         // for boost::result_of
0112         typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
0113 
0114         template<typename Args>
0115         immediate_rolling_variance_impl(Args const &args)
0116             : previous_mean_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
0117             , sum_of_squares_(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
0118         {
0119         }
0120 
0121         template<typename Args>
0122         void operator()(Args const &args)
0123         {
0124             Sample added_sample = args[sample];
0125 
0126             result_type mean = immediate_rolling_mean(args);
0127             sum_of_squares_ += (added_sample-mean)*(added_sample-previous_mean_);
0128 
0129             if(is_rolling_window_plus1_full(args))
0130             {
0131                 Sample removed_sample = rolling_window_plus1(args).front();
0132                 sum_of_squares_ -= (removed_sample-mean)*(removed_sample-previous_mean_);
0133                 prevent_underflow(sum_of_squares_);
0134             }
0135             previous_mean_ = mean;
0136         }
0137 
0138         template<typename Args>
0139         result_type result(Args const &args) const
0140         {
0141             size_t nr_samples = rolling_count(args);
0142             if (nr_samples < 2) return result_type();
0143             return numeric::fdiv(sum_of_squares_,(nr_samples-1));
0144         }
0145         
0146         // make this accumulator serializeable
0147         template<class Archive>
0148         void serialize(Archive & ar, const unsigned int file_version)
0149         { 
0150             ar & previous_mean_;
0151             ar & sum_of_squares_;
0152         }
0153 
0154     private:
0155 
0156         result_type previous_mean_;
0157         result_type sum_of_squares_;
0158 
0159         template<typename T>
0160         void prevent_underflow(T &non_negative_number,typename boost::enable_if<boost::is_arithmetic<T>,T>::type* = 0)
0161         {
0162             if (non_negative_number < T(0)) non_negative_number = T(0);
0163         }
0164         template<typename T>
0165         void prevent_underflow(T &non_arithmetic_quantity,typename boost::disable_if<boost::is_arithmetic<T>,T>::type* = 0)
0166         {
0167         }
0168     };
0169 } // namespace impl
0170 
0171 ///////////////////////////////////////////////////////////////////////////////
0172 // tag:: lazy_rolling_variance
0173 // tag:: immediate_rolling_variance
0174 // tag:: rolling_variance
0175 //
0176 namespace tag
0177 {
0178     struct lazy_rolling_variance
0179         : depends_on< rolling_count, rolling_mean, rolling_moment<2> >
0180     {
0181         /// INTERNAL ONLY
0182         ///
0183         typedef accumulators::impl::lazy_rolling_variance_impl< mpl::_1 > impl;
0184 
0185         #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
0186         /// tag::rolling_window::window_size named parameter
0187         static boost::parameter::keyword<tag::rolling_window_size> const window_size;
0188         #endif
0189     };
0190 
0191     struct immediate_rolling_variance
0192         : depends_on< rolling_window_plus1, rolling_count, immediate_rolling_mean>
0193     {
0194         /// INTERNAL ONLY
0195         ///
0196         typedef accumulators::impl::immediate_rolling_variance_impl< mpl::_1> impl;
0197 
0198         #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
0199         /// tag::rolling_window::window_size named parameter
0200         static boost::parameter::keyword<tag::rolling_window_size> const window_size;
0201         #endif
0202     };
0203 
0204     // make immediate_rolling_variance the default implementation
0205     struct rolling_variance : immediate_rolling_variance {};
0206 } // namespace tag
0207 
0208 ///////////////////////////////////////////////////////////////////////////////
0209 // extract::lazy_rolling_variance
0210 // extract::immediate_rolling_variance
0211 // extract::rolling_variance
0212 //
0213 namespace extract
0214 {
0215     extractor<tag::lazy_rolling_variance> const lazy_rolling_variance = {};
0216     extractor<tag::immediate_rolling_variance> const immediate_rolling_variance = {};
0217     extractor<tag::rolling_variance> const rolling_variance = {};
0218 
0219     BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_variance)
0220     BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_variance)
0221     BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_variance)
0222 }
0223 
0224 using extract::lazy_rolling_variance;
0225 using extract::immediate_rolling_variance;
0226 using extract::rolling_variance;
0227 
0228 // rolling_variance(lazy) -> lazy_rolling_variance
0229 template<>
0230 struct as_feature<tag::rolling_variance(lazy)>
0231 {
0232     typedef tag::lazy_rolling_variance type;
0233 };
0234 
0235 // rolling_variance(immediate) -> immediate_rolling_variance
0236 template<>
0237 struct as_feature<tag::rolling_variance(immediate)>
0238 {
0239     typedef tag::immediate_rolling_variance type;
0240 };
0241 
0242 // for the purposes of feature-based dependency resolution,
0243 // lazy_rolling_variance provides the same feature as rolling_variance
0244 template<>
0245 struct feature_of<tag::lazy_rolling_variance>
0246     : feature_of<tag::rolling_variance>
0247 {
0248 };
0249 
0250 // for the purposes of feature-based dependency resolution,
0251 // immediate_rolling_variance provides the same feature as rolling_variance
0252 template<>
0253 struct feature_of<tag::immediate_rolling_variance>
0254   : feature_of<tag::rolling_variance>
0255 {
0256 };
0257 }} // namespace boost::accumulators
0258 
0259 #endif