123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_VARIANCE_HPP_EAN_28_10_2005
- #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_VARIANCE_HPP_EAN_28_10_2005
- #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/count.hpp>
- #include <boost/accumulators/statistics/variance.hpp>
- #include <boost/accumulators/statistics/weighted_sum.hpp>
- #include <boost/accumulators/statistics/weighted_mean.hpp>
- #include <boost/accumulators/statistics/weighted_moment.hpp>
- namespace boost { namespace accumulators
- {
- namespace impl
- {
-
-
- template<typename Sample, typename Weight, typename MeanFeature>
- struct lazy_weighted_variance_impl
- : accumulator_base
- {
- typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
-
- typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
- lazy_weighted_variance_impl(dont_care) {}
- template<typename Args>
- result_type result(Args const &args) const
- {
- extractor<MeanFeature> const some_mean = {};
- result_type tmp = some_mean(args);
- return accumulators::weighted_moment<2>(args) - tmp * tmp;
- }
- };
-
-
- template<typename Sample, typename Weight, typename MeanFeature, typename Tag>
- struct weighted_variance_impl
- : accumulator_base
- {
- typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
-
- typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
- template<typename Args>
- weighted_variance_impl(Args const &args)
- : weighted_variance(numeric::fdiv(args[sample | Sample()], numeric::one<Weight>::value))
- {
- }
- template<typename Args>
- void operator ()(Args const &args)
- {
- std::size_t cnt = count(args);
- if(cnt > 1)
- {
- extractor<MeanFeature> const some_mean = {};
- result_type tmp = args[parameter::keyword<Tag>::get()] - some_mean(args);
- this->weighted_variance =
- numeric::fdiv(this->weighted_variance * (sum_of_weights(args) - args[weight]), sum_of_weights(args))
- + numeric::fdiv(tmp * tmp * args[weight], sum_of_weights(args) - args[weight] );
- }
- }
- result_type result(dont_care) const
- {
- return this->weighted_variance;
- }
-
- template<class Archive>
- void serialize(Archive & ar, const unsigned int file_version)
- {
- ar & weighted_variance;
- }
- private:
- result_type weighted_variance;
- };
- }
- namespace tag
- {
- struct lazy_weighted_variance
- : depends_on<weighted_moment<2>, weighted_mean>
- {
-
-
- typedef accumulators::impl::lazy_weighted_variance_impl<mpl::_1, mpl::_2, weighted_mean> impl;
- };
- struct weighted_variance
- : depends_on<count, immediate_weighted_mean>
- {
-
-
- typedef accumulators::impl::weighted_variance_impl<mpl::_1, mpl::_2, immediate_weighted_mean, sample> impl;
- };
- }
- namespace extract
- {
- extractor<tag::lazy_weighted_variance> const lazy_weighted_variance = {};
- extractor<tag::weighted_variance> const weighted_variance = {};
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_weighted_variance)
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_variance)
- }
- using extract::lazy_weighted_variance;
- using extract::weighted_variance;
- template<>
- struct as_feature<tag::weighted_variance(lazy)>
- {
- typedef tag::lazy_weighted_variance type;
- };
- template<>
- struct as_feature<tag::weighted_variance(immediate)>
- {
- typedef tag::weighted_variance type;
- };
- }}
- #endif
|