covariance.hpp 7.0 KB

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