covariance.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // 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_COVARIANCE_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_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/mean.hpp>
  30. namespace boost { namespace numeric
  31. {
  32. namespace functional
  33. {
  34. struct std_vector_tag;
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // functional::outer_product
  37. template<typename Left, typename Right, typename EnableIf = void>
  38. struct outer_product_base
  39. : functional::multiplies<Left, Right>
  40. {};
  41. template<typename Left, typename Right, typename LeftTag = typename tag<Left>::type, typename RightTag = typename tag<Right>::type>
  42. struct outer_product
  43. : outer_product_base<Left, Right, void>
  44. {};
  45. template<typename Left, typename Right>
  46. struct outer_product<Left, Right, std_vector_tag, std_vector_tag>
  47. : std::binary_function<
  48. Left
  49. , Right
  50. , ublas::matrix<
  51. typename functional::multiplies<
  52. typename Left::value_type
  53. , typename Right::value_type
  54. >::result_type
  55. >
  56. >
  57. {
  58. typedef
  59. ublas::matrix<
  60. typename functional::multiplies<
  61. typename Left::value_type
  62. , typename Right::value_type
  63. >::result_type
  64. >
  65. result_type;
  66. result_type
  67. operator ()(Left & left, Right & right) const
  68. {
  69. std::size_t left_size = left.size();
  70. std::size_t right_size = right.size();
  71. result_type result(left_size, right_size);
  72. for (std::size_t i = 0; i < left_size; ++i)
  73. for (std::size_t j = 0; j < right_size; ++j)
  74. result(i,j) = numeric::multiplies(left[i], right[j]);
  75. return result;
  76. }
  77. };
  78. }
  79. namespace op
  80. {
  81. struct outer_product
  82. : boost::detail::function2<functional::outer_product<_1, _2, functional::tag<_1>, functional::tag<_2> > >
  83. {};
  84. }
  85. namespace
  86. {
  87. op::outer_product const &outer_product = boost::detail::pod_singleton<op::outer_product>::instance;
  88. }
  89. }}
  90. namespace boost { namespace accumulators
  91. {
  92. namespace impl
  93. {
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // covariance_impl
  96. //
  97. /**
  98. @brief Covariance Estimator
  99. An iterative Monte Carlo estimator for the covariance \f$\mathrm{Cov}(X,X')\f$, where \f$X\f$ is a sample
  100. and \f$X'\f$ is a variate, is given by:
  101. \f[
  102. \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,
  103. \f]
  104. \f$\hat{\mu}_n\f$ and \f$\hat{\mu}_n'\f$ being the means of the samples and variates.
  105. */
  106. template<typename Sample, typename VariateType, typename VariateTag>
  107. struct covariance_impl
  108. : accumulator_base
  109. {
  110. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type sample_type;
  111. typedef typename numeric::functional::fdiv<VariateType, std::size_t>::result_type variate_type;
  112. // for boost::result_of
  113. typedef typename numeric::functional::outer_product<sample_type, variate_type>::result_type result_type;
  114. template<typename Args>
  115. covariance_impl(Args const &args)
  116. : cov_(
  117. numeric::outer_product(
  118. numeric::fdiv(args[sample | Sample()], (std::size_t)1)
  119. , numeric::fdiv(args[parameter::keyword<VariateTag>::get() | VariateType()], (std::size_t)1)
  120. )
  121. )
  122. {
  123. }
  124. template<typename Args>
  125. void operator ()(Args const &args)
  126. {
  127. std::size_t cnt = count(args);
  128. if (cnt > 1)
  129. {
  130. extractor<tag::mean_of_variates<VariateType, VariateTag> > const some_mean_of_variates = {};
  131. this->cov_ = this->cov_*(cnt-1.)/cnt
  132. + numeric::outer_product(
  133. some_mean_of_variates(args) - args[parameter::keyword<VariateTag>::get()]
  134. , mean(args) - args[sample]
  135. ) / (cnt-1.);
  136. }
  137. }
  138. result_type result(dont_care) const
  139. {
  140. return this->cov_;
  141. }
  142. private:
  143. result_type cov_;
  144. };
  145. } // namespace impl
  146. ///////////////////////////////////////////////////////////////////////////////
  147. // tag::covariance
  148. //
  149. namespace tag
  150. {
  151. template<typename VariateType, typename VariateTag>
  152. struct covariance
  153. : depends_on<count, mean, mean_of_variates<VariateType, VariateTag> >
  154. {
  155. typedef accumulators::impl::covariance_impl<mpl::_1, VariateType, VariateTag> impl;
  156. };
  157. struct abstract_covariance
  158. : depends_on<>
  159. {
  160. };
  161. }
  162. ///////////////////////////////////////////////////////////////////////////////
  163. // extract::covariance
  164. //
  165. namespace extract
  166. {
  167. extractor<tag::abstract_covariance> const covariance = {};
  168. BOOST_ACCUMULATORS_IGNORE_GLOBAL(covariance)
  169. }
  170. using extract::covariance;
  171. template<typename VariateType, typename VariateTag>
  172. struct feature_of<tag::covariance<VariateType, VariateTag> >
  173. : feature_of<tag::abstract_covariance>
  174. {
  175. };
  176. // So that covariance can be automatically substituted with
  177. // weighted_covariance when the weight parameter is non-void.
  178. template<typename VariateType, typename VariateTag>
  179. struct as_weighted_feature<tag::covariance<VariateType, VariateTag> >
  180. {
  181. typedef tag::weighted_covariance<VariateType, VariateTag> type;
  182. };
  183. template<typename VariateType, typename VariateTag>
  184. struct feature_of<tag::weighted_covariance<VariateType, VariateTag> >
  185. : feature_of<tag::covariance<VariateType, VariateTag> >
  186. {};
  187. }} // namespace boost::accumulators
  188. #endif