#include <iterator>
#include <tuple>
-#include <boost/assert.hpp>
-#include <boost/config/header_deprecated.hpp>
+#include <limits>
+#include <boost/math/tools/assert.hpp>
+#include <boost/math/tools/header_deprecated.hpp>
-BOOST_HEADER_DEPRECATED("<boost/math/statistics/bivariate_statistics.hpp>");
+BOOST_MATH_HEADER_DEPRECATED("<boost/math/statistics/bivariate_statistics.hpp>");
namespace boost{ namespace math{ namespace tools {
{
using Real = typename Container::value_type;
using std::size;
- BOOST_ASSERT_MSG(size(u) == size(v), "The size of each vector must be the same to compute covariance.");
- BOOST_ASSERT_MSG(size(u) > 0, "Computing covariance requires at least one sample.");
+ BOOST_MATH_ASSERT_MSG(size(u) == size(v), "The size of each vector must be the same to compute covariance.");
+ BOOST_MATH_ASSERT_MSG(size(u) > 0, "Computing covariance requires at least one sample.");
// See Equation III.9 of "Numerically Stable, Single-Pass, Parallel Statistics Algorithms", Bennet et al.
Real cov = 0;
{
using Real = typename Container::value_type;
using std::size;
- BOOST_ASSERT_MSG(size(u) == size(v), "The size of each vector must be the same to compute covariance.");
- BOOST_ASSERT_MSG(size(u) > 0, "Computing covariance requires at least two samples.");
+ BOOST_MATH_ASSERT_MSG(size(u) == size(v), "The size of each vector must be the same to compute covariance.");
+ BOOST_MATH_ASSERT_MSG(size(u) > 0, "Computing covariance requires at least two samples.");
Real cov = 0;
Real mu_u = u[0];
mu_v = mu_v + v_tmp/(i+1);
}
- // If both datasets are constant, then they are perfectly correlated.
- if (Qu == 0 && Qv == 0)
- {
- return Real(1);
- }
- // If one dataset is constant and the other isn't, then they have no correlation:
+ // If one dataset is constant, then they have no correlation:
+ // See https://stats.stackexchange.com/questions/23676/normalized-correlation-with-a-constant-vector
+ // Thanks to zbjornson for pointing this out.
if (Qu == 0 || Qv == 0)
{
- return Real(0);
+ return std::numeric_limits<Real>::quiet_NaN();
}
// Make sure rho in [-1, 1], even in the presence of numerical noise.