1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_covariance.hpp
4 // Copyright 2006 Daniel Egloff, Olivier Gygi. 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_COVARIANCE_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
16 #include <boost/mpl/assert.hpp>
17 #include <boost/mpl/bool.hpp>
18 #include <boost/range.hpp>
19 #include <boost/parameter/keyword.hpp>
20 #include <boost/mpl/placeholders.hpp>
21 #include <boost/numeric/ublas/io.hpp>
22 #include <boost/numeric/ublas/matrix.hpp>
23 #include <boost/type_traits/is_scalar.hpp>
24 #include <boost/type_traits/is_same.hpp>
25 #include <boost/accumulators/framework/accumulator_base.hpp>
26 #include <boost/accumulators/framework/extractor.hpp>
27 #include <boost/accumulators/numeric/functional.hpp>
28 #include <boost/accumulators/framework/parameters/sample.hpp>
29 #include <boost/accumulators/statistics_fwd.hpp>
30 #include <boost/accumulators/statistics/count.hpp>
31 #include <boost/accumulators/statistics/covariance.hpp> // for numeric::outer_product() and type traits
32 #include <boost/accumulators/statistics/weighted_mean.hpp>
34 namespace boost
{ namespace accumulators
39 ///////////////////////////////////////////////////////////////////////////////
40 // weighted_covariance_impl
43 @brief Weighted Covariance Estimator
45 An iterative Monte Carlo estimator for the weighted covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
46 and \f$X'\f$ a variate, is given by:
49 \hat{c}_n = \frac{\bar{w}_n-w_n}{\bar{w}_n} \hat{c}_{n-1} + \frac{w_n}{\bar{w}_n-w_n}(X_n - \hat{\mu}_n)(X_n' - \hat{\mu}_n'),
50 \quad n\ge2,\quad\hat{c}_1 = 0,
53 \f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the weighted means of the samples and variates and
54 \f$\bar{w}_n\f$ the sum of the \f$n\f$ first weights \f$w_i\f$.
56 template<typename Sample
, typename Weight
, typename VariateType
, typename VariateTag
>
57 struct weighted_covariance_impl
60 typedef typename
numeric::functional::multiplies
<Weight
, typename
numeric::functional::average
<Sample
, std::size_t>::result_type
>::result_type weighted_sample_type
;
61 typedef typename
numeric::functional::multiplies
<Weight
, typename
numeric::functional::average
<VariateType
, std::size_t>::result_type
>::result_type weighted_variate_type
;
62 // for boost::result_of
63 typedef typename
numeric::functional::outer_product
<weighted_sample_type
, weighted_variate_type
>::result_type result_type
;
65 template<typename Args
>
66 weighted_covariance_impl(Args
const &args
)
68 numeric::outer_product(
69 numeric::average(args
[sample
| Sample()], (std::size_t)1)
70 * numeric::one
<Weight
>::value
71 , numeric::average(args
[parameter::keyword
<VariateTag
>::get() | VariateType()], (std::size_t)1)
72 * numeric::one
<Weight
>::value
78 template<typename Args
>
79 void operator ()(Args
const &args
)
81 std::size_t cnt
= count(args
);
85 extractor
<tag::weighted_mean_of_variates
<VariateType
, VariateTag
> > const some_weighted_mean_of_variates
= {};
87 this->cov_
= this->cov_
* (sum_of_weights(args
) - args
[weight
]) / sum_of_weights(args
)
88 + numeric::outer_product(
89 some_weighted_mean_of_variates(args
) - args
[parameter::keyword
<VariateTag
>::get()]
90 , weighted_mean(args
) - args
[sample
]
91 ) * args
[weight
] / (sum_of_weights(args
) - args
[weight
]);
95 result_type
result(dont_care
) const
106 ///////////////////////////////////////////////////////////////////////////////
107 // tag::weighted_covariance
111 template<typename VariateType
, typename VariateTag
>
112 struct weighted_covariance
113 : depends_on
<count
, sum_of_weights
, weighted_mean
, weighted_mean_of_variates
<VariateType
, VariateTag
> >
115 typedef accumulators::impl::weighted_covariance_impl
<mpl::_1
, mpl::_2
, VariateType
, VariateTag
> impl
;
119 ///////////////////////////////////////////////////////////////////////////////
120 // extract::weighted_covariance
124 extractor
<tag::abstract_covariance
> const weighted_covariance
= {};
127 using extract::weighted_covariance
;
129 }} // namespace boost::accumulators