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 .
< ? php function correlationCoefficient ( $ X , $ Y , $ n ) { $ sum_X = 0 ; $ sum_Y = 0 ; $ sum_XY = 0 ; $ squareSum_X = 0 ; $ squareSum_Y = 0 ; for ( $ 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 ] ; } $ corr = ( float ) ( $ n * $ sum_XY - $ sum_X * $ sum_Y ) / sqrt ( ( $ n * $ squareSum_X - $ sum_X * $ sum_X ) * ( $ n * $ squareSum_Y - $ sum_Y * $ sum_Y ) ) ; return $ corr ; } $ X = array ( 15 , 18 , 21 , 24 , 27 ) ; $ Y = array ( 25 , 25 , 27 , 31 , 32 ) ; $ n = sizeof ( $ X ) ; echo correlationCoefficient ( $ X , $ Y , $ n ) ; ? >