25 if (n != Y.n_rows || n < 3)
return 0.0;
27 size_t n_pairs = n * (n - 1) / 2;
28 arma::vec dx(n_pairs);
29 arma::vec dy(n_pairs);
32 for (
size_t i = 0; i < n; ++i) {
33 for (
size_t j = i + 1; j < n; ++j) {
34 arma::rowvec diff_x = X.row(i) - X.row(j);
35 arma::rowvec diff_y = Y.row(i) - Y.row(j);
36 dx[idx] = std::sqrt(arma::dot(diff_x, diff_x));
37 dy[idx] = std::sqrt(arma::dot(diff_y, diff_y));
42 double mean_x = arma::mean(dx);
43 double mean_y = arma::mean(dy);
45 arma::vec zx = dx - mean_x;
46 arma::vec zy = dy - mean_y;
48 double sxx = arma::dot(zx, zx);
49 double syy = arma::dot(zy, zy);
50 double sxy = arma::dot(zx, zy);
52 if (sxx <= 1e-12 || syy <= 1e-12)
return 0.0;
53 return sxy / std::sqrt(sxx * syy);
double dist_dist_correlation(const arma::mat &X, const arma::mat &Y)
Compute Pearson correlation between pairwise Euclidean distance structures of two matrices.
Definition stability.hpp:23