public static double spearmanRankCorrelationCoefficient ( double [ ] a , double [ ] b ) { check ( a , b ) ; int N = a . length ; int NcubedMinusN = ( N * N * N ) - N ; // Convert a and b into rankings. The last value of this array is the // correction factor for computing the correlation based on the number // of ties. (Eq. 9.6; p. 239). Note that the ranks are not integers // beacused tied values are assigned the average of their ranks (e.g., // a tie at positions 1 and 2 would be be assigned a rank of 1.5). double [ ] rankedA = rank ( a ) ; double [ ] rankedB = rank ( b ) ; double sumDiffs = 0 ; for ( int i = 0 ; i < rankedA . length - 1 ; ++ i ) { double diff = rankedA [ i ] - rankedB [ i ] ; sumDiffs += diff * diff ; } double aCorrectionFactor = rankedA [ rankedA . length - 1 ] ; double bCorrectionFactor = rankedB [ rankedB . length - 1 ] ; double tiesSum = aCorrectionFactor + bCorrectionFactor ; // Compute Spearman's rho using Eq. 9.7 (p. 239) return ( NcubedMinusN - ( 6 * sumDiffs ) - ( ( tiesSum ) / 2d ) ) / Math . sqrt ( ( NcubedMinusN * NcubedMinusN ) - ( tiesSum * NcubedMinusN ) + ( aCorrectionFactor * bCorrectionFactor ) ) ; }
Computes the Spearman rank correlation coefficient for the two arrays .