public static double sampleCorCoeff ( Vec xData , Vec yData ) { if ( yData . length ( ) != xData . length ( ) ) throw new ArithmeticException ( "X and Y data sets must have the same length" ) ; double xMean = xData . mean ( ) ; double yMean = yData . mean ( ) ; double topSum = 0 ; for ( int i = 0 ; i < xData . length ( ) ; i ++ ) { topSum += ( xData . get ( i ) - xMean ) * ( yData . get ( i ) - yMean ) ; } return topSum / ( ( xData . length ( ) - 1 ) * xData . standardDeviation ( ) * yData . standardDeviation ( ) ) ; }
Computes the sample correlation coefficient for two data sets X and Y . The lengths of X and Y must be the same and each element in X should correspond to the element in Y .