annotate DEPENDENCIES/generic/include/boost/accumulators/statistics/variance.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2 // variance.hpp
Chris@16 3 //
Chris@16 4 // Copyright 2005 Daniel Egloff, Eric Niebler. Distributed under the Boost
Chris@16 5 // Software License, Version 1.0. (See accompanying file
Chris@16 6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7
Chris@16 8 #ifndef BOOST_ACCUMULATORS_STATISTICS_VARIANCE_HPP_EAN_28_10_2005
Chris@16 9 #define BOOST_ACCUMULATORS_STATISTICS_VARIANCE_HPP_EAN_28_10_2005
Chris@16 10
Chris@16 11 #include <boost/mpl/placeholders.hpp>
Chris@16 12 #include <boost/accumulators/framework/accumulator_base.hpp>
Chris@16 13 #include <boost/accumulators/framework/extractor.hpp>
Chris@16 14 #include <boost/accumulators/numeric/functional.hpp>
Chris@16 15 #include <boost/accumulators/framework/parameters/sample.hpp>
Chris@16 16 #include <boost/accumulators/framework/depends_on.hpp>
Chris@16 17 #include <boost/accumulators/statistics_fwd.hpp>
Chris@16 18 #include <boost/accumulators/statistics/count.hpp>
Chris@16 19 #include <boost/accumulators/statistics/sum.hpp>
Chris@16 20 #include <boost/accumulators/statistics/mean.hpp>
Chris@16 21 #include <boost/accumulators/statistics/moment.hpp>
Chris@16 22
Chris@16 23 namespace boost { namespace accumulators
Chris@16 24 {
Chris@16 25
Chris@16 26 namespace impl
Chris@16 27 {
Chris@16 28 //! Lazy calculation of variance.
Chris@16 29 /*!
Chris@16 30 Default sample variance implementation based on the second moment \f$ M_n^{(2)} \f$ moment<2>, mean and count.
Chris@16 31 \f[
Chris@16 32 \sigma_n^2 = M_n^{(2)} - \mu_n^2.
Chris@16 33 \f]
Chris@16 34 where
Chris@16 35 \f[
Chris@16 36 \mu_n = \frac{1}{n} \sum_{i = 1}^n x_i.
Chris@16 37 \f]
Chris@16 38 is the estimate of the sample mean and \f$n\f$ is the number of samples.
Chris@16 39 */
Chris@16 40 template<typename Sample, typename MeanFeature>
Chris@16 41 struct lazy_variance_impl
Chris@16 42 : accumulator_base
Chris@16 43 {
Chris@16 44 // for boost::result_of
Chris@16 45 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
Chris@16 46
Chris@16 47 lazy_variance_impl(dont_care) {}
Chris@16 48
Chris@16 49 template<typename Args>
Chris@16 50 result_type result(Args const &args) const
Chris@16 51 {
Chris@16 52 extractor<MeanFeature> mean;
Chris@16 53 result_type tmp = mean(args);
Chris@16 54 return accumulators::moment<2>(args) - tmp * tmp;
Chris@16 55 }
Chris@16 56 };
Chris@16 57
Chris@16 58 //! Iterative calculation of variance.
Chris@16 59 /*!
Chris@16 60 Iterative calculation of sample variance \f$\sigma_n^2\f$ according to the formula
Chris@16 61 \f[
Chris@16 62 \sigma_n^2 = \frac{1}{n} \sum_{i = 1}^n (x_i - \mu_n)^2 = \frac{n-1}{n} \sigma_{n-1}^2 + \frac{1}{n-1}(x_n - \mu_n)^2.
Chris@16 63 \f]
Chris@16 64 where
Chris@16 65 \f[
Chris@16 66 \mu_n = \frac{1}{n} \sum_{i = 1}^n x_i.
Chris@16 67 \f]
Chris@16 68 is the estimate of the sample mean and \f$n\f$ is the number of samples.
Chris@16 69
Chris@16 70 Note that the sample variance is not defined for \f$n <= 1\f$.
Chris@16 71
Chris@16 72 A simplification can be obtained by the approximate recursion
Chris@16 73 \f[
Chris@16 74 \sigma_n^2 \approx \frac{n-1}{n} \sigma_{n-1}^2 + \frac{1}{n}(x_n - \mu_n)^2.
Chris@16 75 \f]
Chris@16 76 because the difference
Chris@16 77 \f[
Chris@16 78 \left(\frac{1}{n-1} - \frac{1}{n}\right)(x_n - \mu_n)^2 = \frac{1}{n(n-1)}(x_n - \mu_n)^2.
Chris@16 79 \f]
Chris@16 80 converges to zero as \f$n \rightarrow \infty\f$. However, for small \f$ n \f$ the difference
Chris@16 81 can be non-negligible.
Chris@16 82 */
Chris@16 83 template<typename Sample, typename MeanFeature, typename Tag>
Chris@16 84 struct variance_impl
Chris@16 85 : accumulator_base
Chris@16 86 {
Chris@16 87 // for boost::result_of
Chris@16 88 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
Chris@16 89
Chris@16 90 template<typename Args>
Chris@16 91 variance_impl(Args const &args)
Chris@16 92 : variance(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
Chris@16 93 {
Chris@16 94 }
Chris@16 95
Chris@16 96 template<typename Args>
Chris@16 97 void operator ()(Args const &args)
Chris@16 98 {
Chris@16 99 std::size_t cnt = count(args);
Chris@16 100
Chris@16 101 if(cnt > 1)
Chris@16 102 {
Chris@16 103 extractor<MeanFeature> mean;
Chris@16 104 result_type tmp = args[parameter::keyword<Tag>::get()] - mean(args);
Chris@16 105 this->variance =
Chris@16 106 numeric::fdiv(this->variance * (cnt - 1), cnt)
Chris@16 107 + numeric::fdiv(tmp * tmp, cnt - 1);
Chris@16 108 }
Chris@16 109 }
Chris@16 110
Chris@16 111 result_type result(dont_care) const
Chris@16 112 {
Chris@16 113 return this->variance;
Chris@16 114 }
Chris@16 115
Chris@16 116 private:
Chris@16 117 result_type variance;
Chris@16 118 };
Chris@16 119
Chris@16 120 } // namespace impl
Chris@16 121
Chris@16 122 ///////////////////////////////////////////////////////////////////////////////
Chris@16 123 // tag::variance
Chris@16 124 // tag::immediate_variance
Chris@16 125 //
Chris@16 126 namespace tag
Chris@16 127 {
Chris@16 128 struct lazy_variance
Chris@16 129 : depends_on<moment<2>, mean>
Chris@16 130 {
Chris@16 131 /// INTERNAL ONLY
Chris@16 132 ///
Chris@16 133 typedef accumulators::impl::lazy_variance_impl<mpl::_1, mean> impl;
Chris@16 134 };
Chris@16 135
Chris@16 136 struct variance
Chris@16 137 : depends_on<count, immediate_mean>
Chris@16 138 {
Chris@16 139 /// INTERNAL ONLY
Chris@16 140 ///
Chris@16 141 typedef accumulators::impl::variance_impl<mpl::_1, mean, sample> impl;
Chris@16 142 };
Chris@16 143 }
Chris@16 144
Chris@16 145 ///////////////////////////////////////////////////////////////////////////////
Chris@16 146 // extract::lazy_variance
Chris@16 147 // extract::variance
Chris@16 148 //
Chris@16 149 namespace extract
Chris@16 150 {
Chris@16 151 extractor<tag::lazy_variance> const lazy_variance = {};
Chris@16 152 extractor<tag::variance> const variance = {};
Chris@16 153
Chris@16 154 BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_variance)
Chris@16 155 BOOST_ACCUMULATORS_IGNORE_GLOBAL(variance)
Chris@16 156 }
Chris@16 157
Chris@16 158 using extract::lazy_variance;
Chris@16 159 using extract::variance;
Chris@16 160
Chris@16 161 // variance(lazy) -> lazy_variance
Chris@16 162 template<>
Chris@16 163 struct as_feature<tag::variance(lazy)>
Chris@16 164 {
Chris@16 165 typedef tag::lazy_variance type;
Chris@16 166 };
Chris@16 167
Chris@16 168 // variance(immediate) -> variance
Chris@16 169 template<>
Chris@16 170 struct as_feature<tag::variance(immediate)>
Chris@16 171 {
Chris@16 172 typedef tag::variance type;
Chris@16 173 };
Chris@16 174
Chris@16 175 // for the purposes of feature-based dependency resolution,
Chris@16 176 // immediate_variance provides the same feature as variance
Chris@16 177 template<>
Chris@16 178 struct feature_of<tag::lazy_variance>
Chris@16 179 : feature_of<tag::variance>
Chris@16 180 {
Chris@16 181 };
Chris@16 182
Chris@16 183 // So that variance can be automatically substituted with
Chris@16 184 // weighted_variance when the weight parameter is non-void.
Chris@16 185 template<>
Chris@16 186 struct as_weighted_feature<tag::variance>
Chris@16 187 {
Chris@16 188 typedef tag::weighted_variance type;
Chris@16 189 };
Chris@16 190
Chris@16 191 // for the purposes of feature-based dependency resolution,
Chris@16 192 // weighted_variance provides the same feature as variance
Chris@16 193 template<>
Chris@16 194 struct feature_of<tag::weighted_variance>
Chris@16 195 : feature_of<tag::variance>
Chris@16 196 {
Chris@16 197 };
Chris@16 198
Chris@16 199 // So that immediate_variance can be automatically substituted with
Chris@16 200 // immediate_weighted_variance when the weight parameter is non-void.
Chris@16 201 template<>
Chris@16 202 struct as_weighted_feature<tag::lazy_variance>
Chris@16 203 {
Chris@16 204 typedef tag::lazy_weighted_variance type;
Chris@16 205 };
Chris@16 206
Chris@16 207 // for the purposes of feature-based dependency resolution,
Chris@16 208 // immediate_weighted_variance provides the same feature as immediate_variance
Chris@16 209 template<>
Chris@16 210 struct feature_of<tag::lazy_weighted_variance>
Chris@16 211 : feature_of<tag::lazy_variance>
Chris@16 212 {
Chris@16 213 };
Chris@16 214
Chris@16 215 ////////////////////////////////////////////////////////////////////////////
Chris@16 216 //// droppable_accumulator<variance_impl>
Chris@16 217 //// need to specialize droppable lazy variance to cache the result at the
Chris@16 218 //// point the accumulator is dropped.
Chris@16 219 ///// INTERNAL ONLY
Chris@16 220 /////
Chris@16 221 //template<typename Sample, typename MeanFeature>
Chris@16 222 //struct droppable_accumulator<impl::variance_impl<Sample, MeanFeature> >
Chris@16 223 // : droppable_accumulator_base<
Chris@16 224 // with_cached_result<impl::variance_impl<Sample, MeanFeature> >
Chris@16 225 // >
Chris@16 226 //{
Chris@16 227 // template<typename Args>
Chris@16 228 // droppable_accumulator(Args const &args)
Chris@16 229 // : droppable_accumulator::base(args)
Chris@16 230 // {
Chris@16 231 // }
Chris@16 232 //};
Chris@16 233
Chris@16 234 }} // namespace boost::accumulators
Chris@16 235
Chris@16 236 #endif