weighted_covariance.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_COVARIANCE_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <limits>
  11. #include <numeric>
  12. #include <functional>
  13. #include <complex>
  14. #include <boost/mpl/assert.hpp>
  15. #include <boost/mpl/bool.hpp>
  16. #include <boost/range.hpp>
  17. #include <boost/parameter/keyword.hpp>
  18. #include <boost/mpl/placeholders.hpp>
  19. #include <boost/numeric/ublas/io.hpp>
  20. #include <boost/numeric/ublas/matrix.hpp>
  21. #include <boost/type_traits/is_scalar.hpp>
  22. #include <boost/type_traits/is_same.hpp>
  23. #include <boost/accumulators/framework/accumulator_base.hpp>
  24. #include <boost/accumulators/framework/extractor.hpp>
  25. #include <boost/accumulators/numeric/functional.hpp>
  26. #include <boost/accumulators/framework/parameters/sample.hpp>
  27. #include <boost/accumulators/statistics_fwd.hpp>
  28. #include <boost/accumulators/statistics/count.hpp>
  29. #include <boost/accumulators/statistics/covariance.hpp> // for numeric::outer_product() and type traits
  30. #include <boost/accumulators/statistics/weighted_mean.hpp>
  31. namespace boost { namespace accumulators
  32. {
  33. namespace impl
  34. {
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // weighted_covariance_impl
  37. //
  38. /**
  39. @brief Weighted Covariance Estimator
  40. An iterative Monte Carlo estimator for the weighted covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
  41. and \f$X'\f$ a variate, is given by:
  42. \f[
  43. \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'),
  44. \quad n\ge2,\quad\hat{c}_1 = 0,
  45. \f]
  46. \f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the weighted means of the samples and variates and
  47. \f$\bar{w}_n\f$ the sum of the \f$n\f$ first weights \f$w_i\f$.
  48. */
  49. template<typename Sample, typename Weight, typename VariateType, typename VariateTag>
  50. struct weighted_covariance_impl
  51. : accumulator_base
  52. {
  53. typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<Sample, std::size_t>::result_type>::result_type weighted_sample_type;
  54. typedef typename numeric::functional::multiplies<Weight, typename numeric::functional::fdiv<VariateType, std::size_t>::result_type>::result_type weighted_variate_type;
  55. // for boost::result_of
  56. typedef typename numeric::functional::outer_product<weighted_sample_type, weighted_variate_type>::result_type result_type;
  57. template<typename Args>
  58. weighted_covariance_impl(Args const &args)
  59. : cov_(
  60. numeric::outer_product(
  61. numeric::fdiv(args[sample | Sample()], (std::size_t)1)
  62. * numeric::one<Weight>::value
  63. , numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
  64. * numeric::one<Weight>::value
  65. )
  66. )
  67. {
  68. }
  69. template<typename Args>
  70. void operator ()(Args const &args)
  71. {
  72. std::size_t cnt = count(args);
  73. if (cnt > 1)
  74. {
  75. extractor<tag::weighted_mean_of_variates<VariateType, VariateTag> > const some_weighted_mean_of_variates = {};
  76. this->cov_ = this->cov_ * (sum_of_weights(args) - args[weight]) / sum_of_weights(args)
  77. + numeric::outer_product(
  78. some_weighted_mean_of_variates(args) - args[parameter::keyword<VariateTag>::get()]
  79. , weighted_mean(args) - args[sample]
  80. ) * args[weight] / (sum_of_weights(args) - args[weight]);
  81. }
  82. }
  83. result_type result(dont_care) const
  84. {
  85. return this->cov_;
  86. }
  87. // make this accumulator serializeable
  88. template<class Archive>
  89. void serialize(Archive & ar, const unsigned int file_version)
  90. {
  91. ar & cov_;
  92. }
  93. private:
  94. result_type cov_;
  95. };
  96. } // namespace impl
  97. ///////////////////////////////////////////////////////////////////////////////
  98. // tag::weighted_covariance
  99. //
  100. namespace tag
  101. {
  102. template<typename VariateType, typename VariateTag>
  103. struct weighted_covariance
  104. : depends_on<count, sum_of_weights, weighted_mean, weighted_mean_of_variates<VariateType, VariateTag> >
  105. {
  106. typedef accumulators::impl::weighted_covariance_impl<mpl::_1, mpl::_2, VariateType, VariateTag> impl;
  107. };
  108. }
  109. ///////////////////////////////////////////////////////////////////////////////
  110. // extract::weighted_covariance
  111. //
  112. namespace extract
  113. {
  114. extractor<tag::abstract_covariance> const weighted_covariance = {};
  115. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_covariance)
  116. }
  117. using extract::weighted_covariance;
  118. }} // namespace boost::accumulators
  119. #endif