Java Distance Calculate distanceCorrelation(final double[][] x, final double[][] y)

Here you can find the source of distanceCorrelation(final double[][] x, final double[][] y)

Description

distance Correlation

License

Open Source License

Declaration

public static double distanceCorrelation(final double[][] x, final double[][] y) 

Method Source Code

//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);
    }
}

Related

  1. DistanceBetweenPoints(double x1, double y1, double x2, double y2)
  2. distanceBetweenPoints(float vx, float vy, float wx, float wy)
  3. distanceBetweenPoints(float[] xyz1, float[] xyz2)
  4. distanceByLBS(double lo1, double la1, double lo2, double la2)
  5. distanceCircle(int i0, int i1, int dir, int size)
  6. distanceDamerauLevenshtein(CharSequence source, CharSequence target)
  7. distanceFast(double lat1, double lon1, double lat2, double lon2)
  8. distanceFrom(double lat1, double long1, double lat2, double long2)
  9. DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)