libqe 0.2.5
QE C++ library
Loading...
Searching...
No Matches
stability.hpp
Go to the documentation of this file.
1
4#ifndef LIBQE_STABILITY_HPP
5#define LIBQE_STABILITY_HPP
6
7#include <armadillo>
8#include <cmath>
9#include <algorithm>
10
11namespace qe {
12
23inline double dist_dist_correlation(const arma::mat& X, const arma::mat& Y) {
24 size_t n = X.n_rows;
25 if (n != Y.n_rows || n < 3) return 0.0;
26
27 size_t n_pairs = n * (n - 1) / 2;
28 arma::vec dx(n_pairs);
29 arma::vec dy(n_pairs);
30
31 size_t idx = 0;
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));
38 idx++;
39 }
40 }
41
42 double mean_x = arma::mean(dx);
43 double mean_y = arma::mean(dy);
44
45 arma::vec zx = dx - mean_x;
46 arma::vec zy = dy - mean_y;
47
48 double sxx = arma::dot(zx, zx);
49 double syy = arma::dot(zy, zy);
50 double sxy = arma::dot(zx, zy);
51
52 if (sxx <= 1e-12 || syy <= 1e-12) return 0.0;
53 return sxy / std::sqrt(sxx * syy);
54}
55
56} // namespace qe
57
58#endif // LIBQE_STABILITY_HPP
Definition adjacency.hpp:16
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