public static double spearman(float[] x, float[] y) {
        if (x.length != y.length) {
            throw new IllegalArgumentException("Input vector sizes are different.");
        }

        int n = x.length;
        double[] wksp1 = new double[n];
        double[] wksp2 = new double[n];
        for (int j = 0; j < n; j++) {
            wksp1[j] = x[j];
            wksp2[j] = y[j];
        }

        QuickSort.sort(wksp1, wksp2);
        double sf = crank(wksp1);
        QuickSort.sort(wksp2, wksp1);
        double sg = crank(wksp2);

        double d = 0.0;
        for (int j = 0; j < n; j++) {
            d += Math.sqr(wksp1[j] - wksp2[j]);
        }

        int en = n;
        double en3n = en * en * en - en;
        double fac = (1.0 - sf / en3n) * (1.0 - sg / en3n);
        double rs = (1.0 - (6.0 / en3n) * (d + (sf + sg) / 12.0)) / Math.sqrt(fac);
        return rs;
    }
public static double spearman ( float [ ] x , float [ ] y ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( "Input vector sizes are different." ) ; } int n = x . length ; double [ ] wksp1 = new double [ n ] ; double [ ] wksp2 = new double [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { wksp1 [ j ] = x [ j ] ; wksp2 [ j ] = y [ j ] ; } QuickSort . sort ( wksp1 , wksp2 ) ; double sf = crank ( wksp1 ) ; QuickSort . sort ( wksp2 , wksp1 ) ; double sg = crank ( wksp2 ) ; double d = 0.0 ; for ( int j = 0 ; j < n ; j ++ ) { d += Math . sqr ( wksp1 [ j ] - wksp2 [ j ] ) ; } int en = n ; double en3n = en * en * en - en ; double fac = ( 1.0 - sf / en3n ) * ( 1.0 - sg / en3n ) ; double rs = ( 1.0 - ( 6.0 / en3n ) * ( d + ( sf + sg ) / 12.0 ) ) / Math . sqrt ( fac ) ; return rs ; }
The Spearman Rank Correlation Coefficient is a form of the Pearson
coefficient with the data converted to rankings (ie. when variables
are ordinal). It can be used when there is non-parametric data and hence
Pearson cannot be used.