File indexing completed on 2025-01-18 09:28:21
0001
0002
0003
0004
0005
0006
0007
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
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 template<typename Sample>
0049 struct lazy_rolling_variance_impl
0050 : accumulator_base
0051 {
0052
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
0067 template<class Archive>
0068 void serialize(Archive & ar, const unsigned int file_version) {}
0069 };
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 template<typename Sample>
0108 struct immediate_rolling_variance_impl
0109 : accumulator_base
0110 {
0111
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
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 }
0170
0171
0172
0173
0174
0175
0176 namespace tag
0177 {
0178 struct lazy_rolling_variance
0179 : depends_on< rolling_count, rolling_mean, rolling_moment<2> >
0180 {
0181
0182
0183 typedef accumulators::impl::lazy_rolling_variance_impl< mpl::_1 > impl;
0184
0185 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
0186
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
0195
0196 typedef accumulators::impl::immediate_rolling_variance_impl< mpl::_1> impl;
0197
0198 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
0199
0200 static boost::parameter::keyword<tag::rolling_window_size> const window_size;
0201 #endif
0202 };
0203
0204
0205 struct rolling_variance : immediate_rolling_variance {};
0206 }
0207
0208
0209
0210
0211
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
0229 template<>
0230 struct as_feature<tag::rolling_variance(lazy)>
0231 {
0232 typedef tag::lazy_rolling_variance type;
0233 };
0234
0235
0236 template<>
0237 struct as_feature<tag::rolling_variance(immediate)>
0238 {
0239 typedef tag::immediate_rolling_variance type;
0240 };
0241
0242
0243
0244 template<>
0245 struct feature_of<tag::lazy_rolling_variance>
0246 : feature_of<tag::rolling_variance>
0247 {
0248 };
0249
0250
0251
0252 template<>
0253 struct feature_of<tag::immediate_rolling_variance>
0254 : feature_of<tag::rolling_variance>
0255 {
0256 };
0257 }}
0258
0259 #endif