The program is self-explanatory and comments will be helpful

The formula used is :

#This is a program to find correlation between 2 sequences x and y

#In this program , x and y will be arrays passed as arguments to the function

class Correlation

def corr_calculate(x,y)

#first lets fnd mean of x and y

x_mean=0

y_mean=0

for i in 0.. x.length-1

x_mean=x_mean+x[i]

y_mean=y_mean+y[i]

end

x_mean=x_mean/x.length.to_f

y_mean=y_mean/y.length.to_f

#end of i for loop

#This is the code for calculating the standard deviation of x and y

y_sd=0

x_sd=0

for k in 0..x.length-1

x_sd=x_sd+(x[k]-x_mean)**2

y_sd=y_sd+(y[k]-y_mean)**2

end

x_sd=x_sd/x.length

y_sd=y_sd/y.length

x_sd=Math.sqrt(x_sd)

y_sd=Math.sqrt(y_sd)

#Thus we calculated the standard deviation of x and y

#now we calculate numerator for corrleation

num=0

for j in 0..x.length-1

num=num+x[j]*y[j]

end

num=num-(x.length*x_mean*y_mean)

#thus we calculate numerator

#now we calculate denominator for corrleation

den=(x.length)*x_sd*y_sd

#thus we calculate denominator

#finally we calculate correlation

corr=num/den

puts “The co-relation is : “+corr.to_s

end

#end of corr_calculate method

end

#End of correlation class

x=Array.[](68,71,62,75,58,60,67,69,71,69,68,67,63,62,60,63,65,67,63,61)

y=Array.[](4.1,4.6,3.8,4.4,3.2,3.1,3.8,4.1,4.3,3.7,3.5,3.2,3.7,3.3,3.4,4.0,4.1,3.8,3.4,3.6)

Correlation.new.corr_calculate(x,y)

x=Array.[](1,2,3,4,5)

y=Array.[](1,2,3,4,5)

Correlation.new.corr_calculate(x,y)

*Ruby*

Posted on June 25, 20110