1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_variance.hpp
4 // Copyright 2005 Daniel Egloff, Eric Niebler. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_VARIANCE_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_VARIANCE_HPP_EAN_28_10_2005
11 #include <boost/mpl/placeholders.hpp>
12 #include <boost/accumulators/framework/accumulator_base.hpp>
13 #include <boost/accumulators/framework/extractor.hpp>
14 #include <boost/accumulators/numeric/functional.hpp>
15 #include <boost/accumulators/framework/parameters/sample.hpp>
16 #include <boost/accumulators/framework/depends_on.hpp>
17 #include <boost/accumulators/statistics_fwd.hpp>
18 #include <boost/accumulators/statistics/count.hpp>
19 #include <boost/accumulators/statistics/variance.hpp>
20 #include <boost/accumulators/statistics/weighted_sum.hpp>
21 #include <boost/accumulators/statistics/weighted_mean.hpp>
22 #include <boost/accumulators/statistics/weighted_moment.hpp>
24 namespace boost
{ namespace accumulators
29 //! Lazy calculation of variance of weighted samples.
31 The default implementation of the variance of weighted samples is based on the second moment
32 \f$\widehat{m}_n^{(2)}\f$ (weighted_moment<2>) and the mean\f$ \hat{\mu}_n\f$ (weighted_mean):
34 \hat{\sigma}_n^2 = \widehat{m}_n^{(2)}-\hat{\mu}_n^2,
36 where \f$n\f$ is the number of samples.
38 template<typename Sample
, typename Weight
, typename MeanFeature
>
39 struct lazy_weighted_variance_impl
42 typedef typename
numeric::functional::multiplies
<Sample
, Weight
>::result_type weighted_sample
;
43 // for boost::result_of
44 typedef typename
numeric::functional::average
<weighted_sample
, Weight
>::result_type result_type
;
46 lazy_weighted_variance_impl(dont_care
) {}
48 template<typename Args
>
49 result_type
result(Args
const &args
) const
51 extractor
<MeanFeature
> const some_mean
= {};
52 result_type tmp
= some_mean(args
);
53 return weighted_moment
<2>(args
) - tmp
* tmp
;
57 //! Iterative calculation of variance of weighted samples.
59 Iterative calculation of variance of weighted samples:
62 \frac{\bar{w}_n - w_n}{\bar{w}_n}\hat{\sigma}_{n - 1}^2
63 + \frac{w_n}{\bar{w}_n - w_n}\left(X_n - \hat{\mu}_n\right)^2
64 ,\quad n\ge2,\quad\hat{\sigma}_0^2 = 0.
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$
67 the estimate of the mean of the weighted smaples. Note that the sample variance is not defined for
70 template<typename Sample
, typename Weight
, typename MeanFeature
, typename Tag
>
71 struct weighted_variance_impl
74 typedef typename
numeric::functional::multiplies
<Sample
, Weight
>::result_type weighted_sample
;
75 // for boost::result_of
76 typedef typename
numeric::functional::average
<weighted_sample
, Weight
>::result_type result_type
;
78 template<typename Args
>
79 weighted_variance_impl(Args
const &args
)
80 : weighted_variance(numeric::average(args
[sample
| Sample()], numeric::one
<Weight
>::value
))
84 template<typename Args
>
85 void operator ()(Args
const &args
)
87 std::size_t cnt
= count(args
);
91 extractor
<MeanFeature
> const some_mean
= {};
93 result_type tmp
= args
[parameter::keyword
<Tag
>::get()] - some_mean(args
);
95 this->weighted_variance
=
96 numeric::average(this->weighted_variance
* (sum_of_weights(args
) - args
[weight
]), sum_of_weights(args
))
97 + numeric::average(tmp
* tmp
* args
[weight
], sum_of_weights(args
) - args
[weight
] );
101 result_type
result(dont_care
) const
103 return this->weighted_variance
;
107 result_type weighted_variance
;
112 ///////////////////////////////////////////////////////////////////////////////
113 // tag::weighted_variance
114 // tag::immediate_weighted_variance
118 struct lazy_weighted_variance
119 : depends_on
<weighted_moment
<2>, weighted_mean
>
123 typedef accumulators::impl::lazy_weighted_variance_impl
<mpl::_1
, mpl::_2
, weighted_mean
> impl
;
126 struct weighted_variance
127 : depends_on
<count
, immediate_weighted_mean
>
131 typedef accumulators::impl::weighted_variance_impl
<mpl::_1
, mpl::_2
, immediate_weighted_mean
, sample
> impl
;
135 ///////////////////////////////////////////////////////////////////////////////
136 // extract::weighted_variance
137 // extract::immediate_weighted_variance
141 extractor
<tag::lazy_weighted_variance
> const lazy_weighted_variance
= {};
142 extractor
<tag::weighted_variance
> const weighted_variance
= {};
145 using extract::lazy_weighted_variance
;
146 using extract::weighted_variance
;
148 // weighted_variance(lazy) -> lazy_weighted_variance
150 struct as_feature
<tag::weighted_variance(lazy
)>
152 typedef tag::lazy_weighted_variance type
;
155 // weighted_variance(immediate) -> weighted_variance
157 struct as_feature
<tag::weighted_variance(immediate
)>
159 typedef tag::weighted_variance type
;
162 ////////////////////////////////////////////////////////////////////////////
163 //// droppable_accumulator<weighted_variance_impl>
164 //// need to specialize droppable lazy weighted_variance to cache the result at the
165 //// point the accumulator is dropped.
168 //template<typename Sample, typename Weight, typename MeanFeature>
169 //struct droppable_accumulator<impl::weighted_variance_impl<Sample, Weight, MeanFeature> >
170 // : droppable_accumulator_base<
171 // with_cached_result<impl::weighted_variance_impl<Sample, Weight, MeanFeature> >
174 // template<typename Args>
175 // droppable_accumulator(Args const &args)
176 // : droppable_accumulator::base(args)
181 }} // namespace boost::accumulators