1 ///////////////////////////////////////////////////////////////////////////////
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_COVARIANCE_HPP_DE_01_01_2006
9 #define BOOST_ACCUMULATORS_STATISTICS_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/mean.hpp>
33 namespace boost { namespace numeric
37 struct std_vector_tag;
39 ///////////////////////////////////////////////////////////////////////////////
40 // functional::outer_product
41 template<typename Left, typename Right, typename EnableIf = void>
42 struct outer_product_base
43 : functional::multiplies<Left, Right>
46 template<typename Left, typename Right, typename LeftTag = typename tag<Left>::type, typename RightTag = typename tag<Right>::type>
48 : outer_product_base<Left, Right, void>
51 template<typename Left, typename Right>
52 struct outer_product<Left, Right, std_vector_tag, std_vector_tag>
53 : std::binary_function<
57 typename functional::multiplies<
58 typename Left::value_type
59 , typename Right::value_type
66 typename functional::multiplies<
67 typename Left::value_type
68 , typename Right::value_type
74 operator ()(Left & left, Right & right) const
76 std::size_t left_size = left.size();
77 std::size_t right_size = right.size();
78 result_type result(left_size, right_size);
79 for (std::size_t i = 0; i < left_size; ++i)
80 for (std::size_t j = 0; j < right_size; ++j)
81 result(i,j) = numeric::multiplies(left[i], right[j]);
90 : boost::detail::function2<functional::outer_product<_1, _2, functional::tag<_1>, functional::tag<_2> > >
96 op::outer_product const &outer_product = boost::detail::pod_singleton<op::outer_product>::instance;
101 namespace boost { namespace accumulators
106 ///////////////////////////////////////////////////////////////////////////////
110 @brief Covariance Estimator
112 An iterative Monte Carlo estimator for the covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
113 and \f$X'\f$ is a variate, is given by:
116 \hat{c}_n = \frac{n-1}{n} \hat{c}_{n-1} + \frac{1}{n-1}(X_n - \hat{\mu}_n)(X_n' - \hat{\mu}_n'),\quad n\ge2,\quad\hat{c}_1 = 0,
119 \f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the means of the samples and variates.
121 template<typename Sample, typename VariateType, typename VariateTag>
122 struct covariance_impl
125 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type sample_type;
126 typedef typename numeric::functional::fdiv<VariateType, std::size_t>::result_type variate_type;
127 // for boost::result_of
128 typedef typename numeric::functional::outer_product<sample_type, variate_type>::result_type result_type;
130 template<typename Args>
131 covariance_impl(Args const &args)
133 numeric::outer_product(
134 numeric::fdiv(args[sample | Sample()], (std::size_t)1)
135 , numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
141 template<typename Args>
142 void operator ()(Args const &args)
144 std::size_t cnt = count(args);
148 extractor<tag::mean_of_variates<VariateType, VariateTag> > const some_mean_of_variates = {};
150 this->cov_ = this->cov_*(cnt-1.)/cnt
151 + numeric::outer_product(
152 some_mean_of_variates(args) - args[parameter::keyword<VariateTag>::get()]
153 , mean(args) - args[sample]
158 result_type result(dont_care) const
169 ///////////////////////////////////////////////////////////////////////////////
174 template<typename VariateType, typename VariateTag>
176 : depends_on<count, mean, mean_of_variates<VariateType, VariateTag> >
178 typedef accumulators::impl::covariance_impl<mpl::_1, VariateType, VariateTag> impl;
181 struct abstract_covariance
187 ///////////////////////////////////////////////////////////////////////////////
188 // extract::covariance
192 extractor<tag::abstract_covariance> const covariance = {};
194 BOOST_ACCUMULATORS_IGNORE_GLOBAL(covariance)
197 using extract::covariance;
199 template<typename VariateType, typename VariateTag>
200 struct feature_of<tag::covariance<VariateType, VariateTag> >
201 : feature_of<tag::abstract_covariance>
205 // So that covariance can be automatically substituted with
206 // weighted_covariance when the weight parameter is non-void.
207 template<typename VariateType, typename VariateTag>
208 struct as_weighted_feature<tag::covariance<VariateType, VariateTag> >
210 typedef tag::weighted_covariance<VariateType, VariateTag> type;
213 template<typename VariateType, typename VariateTag>
214 struct feature_of<tag::weighted_covariance<VariateType, VariateTag> >
215 : feature_of<tag::covariance<VariateType, VariateTag> >
218 }} // namespace boost::accumulators