1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_covariance.hpp
3 //
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)
7 
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
10 
11 #include <vector>
12 #include <limits>
13 #include <numeric>
14 #include <functional>
15 #include <complex>
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>
33 
34 namespace boost { namespace accumulators
35 {
36 
37 namespace impl
38 {
39     ///////////////////////////////////////////////////////////////////////////////
40     // weighted_covariance_impl
41     //
42     /**
43         @brief Weighted Covariance Estimator
44 
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:
47 
48         \f[
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,
51         \f]
52 
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$.
55     */
56     template<typename Sample, typename Weight, typename VariateType, typename VariateTag>
57     struct weighted_covariance_impl
58       : accumulator_base
59     {
60         typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<Sample, std::size_t>::result_type>::result_type weighted_sample_type;
61         typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<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;
64 
65         template<typename Args>
weighted_covariance_implboost::accumulators::impl::weighted_covariance_impl66         weighted_covariance_impl(Args const &args)
67           : cov_(
68                 numeric::outer_product(
69                     numeric::fdiv(args[sample | Sample()], (std::size_t)1)
70                       * numeric::one<Weight>::value
71                   , numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
72                       * numeric::one<Weight>::value
73                 )
74             )
75         {
76         }
77 
78         template<typename Args>
operator ()boost::accumulators::impl::weighted_covariance_impl79         void operator ()(Args const &args)
80         {
81             std::size_t cnt = count(args);
82 
83             if (cnt > 1)
84             {
85                 extractor<tag::weighted_mean_of_variates<VariateType, VariateTag> > const some_weighted_mean_of_variates = {};
86 
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]);
92             }
93         }
94 
resultboost::accumulators::impl::weighted_covariance_impl95         result_type result(dont_care) const
96         {
97             return this->cov_;
98         }
99 
100         // make this accumulator serializeable
101         template<class Archive>
serializeboost::accumulators::impl::weighted_covariance_impl102         void serialize(Archive & ar, const unsigned int file_version)
103         {
104             ar & cov_;
105         }
106 
107     private:
108         result_type cov_;
109     };
110 
111 } // namespace impl
112 
113 ///////////////////////////////////////////////////////////////////////////////
114 // tag::weighted_covariance
115 //
116 namespace tag
117 {
118     template<typename VariateType, typename VariateTag>
119     struct weighted_covariance
120       : depends_on<count, sum_of_weights, weighted_mean, weighted_mean_of_variates<VariateType, VariateTag> >
121     {
122         typedef accumulators::impl::weighted_covariance_impl<mpl::_1, mpl::_2, VariateType, VariateTag> impl;
123     };
124 }
125 
126 ///////////////////////////////////////////////////////////////////////////////
127 // extract::weighted_covariance
128 //
129 namespace extract
130 {
131     extractor<tag::abstract_covariance> const weighted_covariance = {};
132 
133     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_covariance)
134 }
135 
136 using extract::weighted_covariance;
137 
138 }} // namespace boost::accumulators
139 
140 #endif
141