Return to Snippet

Revision: 41039
at February 11, 2011 04:00 by adkatrit


Initial Code
def pearson(x,y):
	n=len(x)
	vals=range(n)
	
	#regular sums
	sumx=sum([float(x[i]) for i in vals])
	sumy=sum([float(y[i]) for i in vals])
	
	#sum of the squares
	sumxSq=sum([x[i]**2.0 for i in vals])
	sumySq=sum([y[i]**2.0 for i in vals])
	
	#sum of the products
	pSum=sum([x[i]*y[i] for i in vals])
	
	#do pearson score
	num=pSum-(sumx*sumy/n)
	den=((sumxSq-pow(sumx,2)/n)*(sumySq-pow(sumy,2))**.5
	if den=0: return 1
	r=num/den
	return r

Initial URL


Initial Description


Initial Title
Pearson Correlation Coefficient

Initial Tags


Initial Language
Python