bivariate_statistics.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // (C) Copyright Nick Thompson 2018.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_STATISTICS_BIVARIATE_STATISTICS_HPP
  6. #define BOOST_MATH_STATISTICS_BIVARIATE_STATISTICS_HPP
  7. #include <iterator>
  8. #include <tuple>
  9. #include <boost/assert.hpp>
  10. namespace boost{ namespace math{ namespace statistics {
  11. template<class Container>
  12. auto means_and_covariance(Container const & u, Container const & v)
  13. {
  14. using Real = typename Container::value_type;
  15. using std::size;
  16. BOOST_ASSERT_MSG(size(u) == size(v), "The size of each vector must be the same to compute covariance.");
  17. BOOST_ASSERT_MSG(size(u) > 0, "Computing covariance requires at least one sample.");
  18. // See Equation III.9 of "Numerically Stable, Single-Pass, Parallel Statistics Algorithms", Bennet et al.
  19. Real cov = 0;
  20. Real mu_u = u[0];
  21. Real mu_v = v[0];
  22. for(size_t i = 1; i < size(u); ++i)
  23. {
  24. Real u_tmp = (u[i] - mu_u)/(i+1);
  25. Real v_tmp = v[i] - mu_v;
  26. cov += i*u_tmp*v_tmp;
  27. mu_u = mu_u + u_tmp;
  28. mu_v = mu_v + v_tmp/(i+1);
  29. }
  30. return std::make_tuple(mu_u, mu_v, cov/size(u));
  31. }
  32. template<class Container>
  33. auto covariance(Container const & u, Container const & v)
  34. {
  35. auto [mu_u, mu_v, cov] = boost::math::statistics::means_and_covariance(u, v);
  36. return cov;
  37. }
  38. template<class Container>
  39. auto correlation_coefficient(Container const & u, Container const & v)
  40. {
  41. using Real = typename Container::value_type;
  42. using std::size;
  43. BOOST_ASSERT_MSG(size(u) == size(v), "The size of each vector must be the same to compute covariance.");
  44. BOOST_ASSERT_MSG(size(u) > 0, "Computing covariance requires at least two samples.");
  45. Real cov = 0;
  46. Real mu_u = u[0];
  47. Real mu_v = v[0];
  48. Real Qu = 0;
  49. Real Qv = 0;
  50. for(size_t i = 1; i < size(u); ++i)
  51. {
  52. Real u_tmp = u[i] - mu_u;
  53. Real v_tmp = v[i] - mu_v;
  54. Qu = Qu + (i*u_tmp*u_tmp)/(i+1);
  55. Qv = Qv + (i*v_tmp*v_tmp)/(i+1);
  56. cov += i*u_tmp*v_tmp/(i+1);
  57. mu_u = mu_u + u_tmp/(i+1);
  58. mu_v = mu_v + v_tmp/(i+1);
  59. }
  60. // If both datasets are constant, then they are perfectly correlated.
  61. if (Qu == 0 && Qv == 0)
  62. {
  63. return Real(1);
  64. }
  65. // If one dataset is constant and the other isn't, then they have no correlation:
  66. if (Qu == 0 || Qv == 0)
  67. {
  68. return Real(0);
  69. }
  70. // Make sure rho in [-1, 1], even in the presence of numerical noise.
  71. Real rho = cov/sqrt(Qu*Qv);
  72. if (rho > 1) {
  73. rho = 1;
  74. }
  75. if (rho < -1) {
  76. rho = -1;
  77. }
  78. return rho;
  79. }
  80. }}}
  81. #endif