Here you can find the source of distanceCorrelation(final double[][] x, final double[][] y)
public static double distanceCorrelation(final double[][] x, final double[][] y)
//package com.java2s; public class Main { public static double distanceCorrelation(final double[][] x, final double[][] y) { int n = x.length; if (n != y.length) throw new IndexOutOfBoundsException("x and y should be of same length"); double[][] a = new double[n][n], b = new double[n][n]; double[] a1m = new double[n], a2m = new double[n], b1m = new double[n], b2m = new double[n]; double am = 0, bm = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { double aa = euclDist(x[i], x[j]), bb = euclDist(y[i], y[j]); a[i][j] = aa;/*from w ww. ja v a 2s. co m*/ b[i][j] = bb; a1m[i] += aa; a2m[j] += aa; b1m[i] += bb; b2m[j] += bb; am += aa; bm += bb; } } } double[][] A = new double[n][n], B = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i][j] = a[i][j] - (a1m[i] - a2m[j] - am / n) / n; B[i][j] = b[i][j] - (b1m[i] - b2m[j] - bm / n) / n; } } double dCovXY = 0, dVarX = 0, dVarY = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double AA = A[i][j], BB = B[i][j]; dCovXY += AA * BB; dVarX += AA * AA; dVarY += BB * BB; } } return Math.sqrt(dCovXY) / Math.sqrt(Math.sqrt(dVarX * dVarY)); } private static double euclDist(final double[] xx, final double[] yy) { int n = xx.length; if (n != yy.length) throw new IndexOutOfBoundsException("x and y should be of same length"); double dist = 0; for (int i = 0; i < n; i++) { dist += xx[i] * yy[i]; } return Math.sqrt(dist); } }