Program to find correlation coefficient | Function that returns correlation coefficient . ; Sum of elements of array X . ; Sum of elements of array Y . ; Sum of X [ i ] * Y [ i ] . ; Sum of square of array elements . ; Use formula for calculating correlation coefficient . ; Driver code ; Find the size of array . ; Function call to correlationCoefficient .
function correlationCoefficient ( X , Y , n ) { let sum_X = 0 , sum_Y = 0 , sum_XY = 0 ; let squareSum_X = 0 , squareSum_Y = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum_X = sum_X + X [ i ] ; sum_Y = sum_Y + Y [ i ] ; sum_XY = sum_XY + X [ i ] * Y [ i ] ; squareSum_X = squareSum_X + X [ i ] * X [ i ] ; squareSum_Y = squareSum_Y + Y [ i ] * Y [ i ] ; } let corr = ( n * sum_XY - sum_X * sum_Y ) / ( Math . sqrt ( ( n * squareSum_X - sum_X * sum_X ) * ( n * squareSum_Y - sum_Y * sum_Y ) ) ) ; return corr ; } let X = [ 15 , 18 , 21 , 24 , 27 ] ; let Y = [ 25 , 25 , 27 , 31 , 32 ] ; let n = X . length ; document . write ( correlationCoefficient ( X , Y , n ) ) ;