annotate DEPENDENCIES/generic/include/boost/accumulators/statistics/weighted_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 // weighted_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_WEIGHTED_VARIANCE_HPP_EAN_28_10_2005
Chris@16 9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_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/variance.hpp>
Chris@16 20 #include <boost/accumulators/statistics/weighted_sum.hpp>
Chris@16 21 #include <boost/accumulators/statistics/weighted_mean.hpp>
Chris@16 22 #include <boost/accumulators/statistics/weighted_moment.hpp>
Chris@16 23
Chris@16 24 namespace boost { namespace accumulators
Chris@16 25 {
Chris@16 26
Chris@16 27 namespace impl
Chris@16 28 {
Chris@16 29 //! Lazy calculation of variance of weighted samples.
Chris@16 30 /*!
Chris@16 31 The default implementation of the variance of weighted samples is based on the second moment
Chris@16 32 \f$\widehat{m}_n^{(2)}\f$ (weighted_moment<2>) and the mean\f$ \hat{\mu}_n\f$ (weighted_mean):
Chris@16 33 \f[
Chris@16 34 \hat{\sigma}_n^2 = \widehat{m}_n^{(2)}-\hat{\mu}_n^2,
Chris@16 35 \f]
Chris@16 36 where \f$n\f$ is the number of samples.
Chris@16 37 */
Chris@16 38 template<typename Sample, typename Weight, typename MeanFeature>
Chris@16 39 struct lazy_weighted_variance_impl
Chris@16 40 : accumulator_base
Chris@16 41 {
Chris@16 42 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
Chris@16 43 // for boost::result_of
Chris@16 44 typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
Chris@16 45
Chris@16 46 lazy_weighted_variance_impl(dont_care) {}
Chris@16 47
Chris@16 48 template<typename Args>
Chris@16 49 result_type result(Args const &args) const
Chris@16 50 {
Chris@16 51 extractor<MeanFeature> const some_mean = {};
Chris@16 52 result_type tmp = some_mean(args);
Chris@16 53 return accumulators::weighted_moment<2>(args) - tmp * tmp;
Chris@16 54 }
Chris@16 55 };
Chris@16 56
Chris@16 57 //! Iterative calculation of variance of weighted samples.
Chris@16 58 /*!
Chris@16 59 Iterative calculation of variance of weighted samples:
Chris@16 60 \f[
Chris@16 61 \hat{\sigma}_n^2 =
Chris@16 62 \frac{\bar{w}_n - w_n}{\bar{w}_n}\hat{\sigma}_{n - 1}^2
Chris@16 63 + \frac{w_n}{\bar{w}_n - w_n}\left(X_n - \hat{\mu}_n\right)^2
Chris@16 64 ,\quad n\ge2,\quad\hat{\sigma}_0^2 = 0.
Chris@16 65 \f]
Chris@16 66 where \f$\bar{w}_n\f$ is the sum of the \f$n\f$ weights \f$w_i\f$ and \f$\hat{\mu}_n\f$
Chris@16 67 the estimate of the mean of the weighted samples. Note that the sample variance is not defined for
Chris@16 68 \f$n <= 1\f$.
Chris@16 69 */
Chris@16 70 template<typename Sample, typename Weight, typename MeanFeature, typename Tag>
Chris@16 71 struct weighted_variance_impl
Chris@16 72 : accumulator_base
Chris@16 73 {
Chris@16 74 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
Chris@16 75 // for boost::result_of
Chris@16 76 typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
Chris@16 77
Chris@16 78 template<typename Args>
Chris@16 79 weighted_variance_impl(Args const &args)
Chris@16 80 : weighted_variance(numeric::fdiv(args[sample | Sample()], numeric::one<Weight>::value))
Chris@16 81 {
Chris@16 82 }
Chris@16 83
Chris@16 84 template<typename Args>
Chris@16 85 void operator ()(Args const &args)
Chris@16 86 {
Chris@16 87 std::size_t cnt = count(args);
Chris@16 88
Chris@16 89 if(cnt > 1)
Chris@16 90 {
Chris@16 91 extractor<MeanFeature> const some_mean = {};
Chris@16 92
Chris@16 93 result_type tmp = args[parameter::keyword<Tag>::get()] - some_mean(args);
Chris@16 94
Chris@16 95 this->weighted_variance =
Chris@16 96 numeric::fdiv(this->weighted_variance * (sum_of_weights(args) - args[weight]), sum_of_weights(args))
Chris@16 97 + numeric::fdiv(tmp * tmp * args[weight], sum_of_weights(args) - args[weight] );
Chris@16 98 }
Chris@16 99 }
Chris@16 100
Chris@16 101 result_type result(dont_care) const
Chris@16 102 {
Chris@16 103 return this->weighted_variance;
Chris@16 104 }
Chris@16 105
Chris@16 106 private:
Chris@16 107 result_type weighted_variance;
Chris@16 108 };
Chris@16 109
Chris@16 110 } // namespace impl
Chris@16 111
Chris@16 112 ///////////////////////////////////////////////////////////////////////////////
Chris@16 113 // tag::weighted_variance
Chris@16 114 // tag::immediate_weighted_variance
Chris@16 115 //
Chris@16 116 namespace tag
Chris@16 117 {
Chris@16 118 struct lazy_weighted_variance
Chris@16 119 : depends_on<weighted_moment<2>, weighted_mean>
Chris@16 120 {
Chris@16 121 /// INTERNAL ONLY
Chris@16 122 ///
Chris@16 123 typedef accumulators::impl::lazy_weighted_variance_impl<mpl::_1, mpl::_2, weighted_mean> impl;
Chris@16 124 };
Chris@16 125
Chris@16 126 struct weighted_variance
Chris@16 127 : depends_on<count, immediate_weighted_mean>
Chris@16 128 {
Chris@16 129 /// INTERNAL ONLY
Chris@16 130 ///
Chris@16 131 typedef accumulators::impl::weighted_variance_impl<mpl::_1, mpl::_2, immediate_weighted_mean, sample> impl;
Chris@16 132 };
Chris@16 133 }
Chris@16 134
Chris@16 135 ///////////////////////////////////////////////////////////////////////////////
Chris@16 136 // extract::weighted_variance
Chris@16 137 // extract::immediate_weighted_variance
Chris@16 138 //
Chris@16 139 namespace extract
Chris@16 140 {
Chris@16 141 extractor<tag::lazy_weighted_variance> const lazy_weighted_variance = {};
Chris@16 142 extractor<tag::weighted_variance> const weighted_variance = {};
Chris@16 143
Chris@16 144 BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_weighted_variance)
Chris@16 145 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_variance)
Chris@16 146 }
Chris@16 147
Chris@16 148 using extract::lazy_weighted_variance;
Chris@16 149 using extract::weighted_variance;
Chris@16 150
Chris@16 151 // weighted_variance(lazy) -> lazy_weighted_variance
Chris@16 152 template<>
Chris@16 153 struct as_feature<tag::weighted_variance(lazy)>
Chris@16 154 {
Chris@16 155 typedef tag::lazy_weighted_variance type;
Chris@16 156 };
Chris@16 157
Chris@16 158 // weighted_variance(immediate) -> weighted_variance
Chris@16 159 template<>
Chris@16 160 struct as_feature<tag::weighted_variance(immediate)>
Chris@16 161 {
Chris@16 162 typedef tag::weighted_variance type;
Chris@16 163 };
Chris@16 164
Chris@16 165 ////////////////////////////////////////////////////////////////////////////
Chris@16 166 //// droppable_accumulator<weighted_variance_impl>
Chris@16 167 //// need to specialize droppable lazy weighted_variance to cache the result at the
Chris@16 168 //// point the accumulator is dropped.
Chris@16 169 ///// INTERNAL ONLY
Chris@16 170 /////
Chris@16 171 //template<typename Sample, typename Weight, typename MeanFeature>
Chris@16 172 //struct droppable_accumulator<impl::weighted_variance_impl<Sample, Weight, MeanFeature> >
Chris@16 173 // : droppable_accumulator_base<
Chris@16 174 // with_cached_result<impl::weighted_variance_impl<Sample, Weight, MeanFeature> >
Chris@16 175 // >
Chris@16 176 //{
Chris@16 177 // template<typename Args>
Chris@16 178 // droppable_accumulator(Args const &args)
Chris@16 179 // : droppable_accumulator::base(args)
Chris@16 180 // {
Chris@16 181 // }
Chris@16 182 //};
Chris@16 183
Chris@16 184 }} // namespace boost::accumulators
Chris@16 185
Chris@16 186 #endif