bivariate_statistics.hpp 2.7 KB

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