Created
August 14, 2014 09:38
-
-
Save croath/135d1ab36963e9548768 to your computer and use it in GitHub Desktop.
pearson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ruby_pearson(x,y) | |
n=x.length | |
sumx=x.inject(0) {|r,i| r + i} | |
sumy=y.inject(0) {|r,i| r + i} | |
sumxSq=x.inject(0) {|r,i| r + i**2} | |
sumySq=y.inject(0) {|r,i| r + i**2} | |
prods=[]; x.each_with_index{|this_x,i| prods << this_x*y[i]} | |
pSum=prods.inject(0){|r,i| r + i} | |
# Calculate Pearson score | |
num=pSum-(sumx*sumy/n) | |
den=((sumxSq-(sumx**2)/n)*(sumySq-(sumy**2)/n))**0.5 | |
if den==0 | |
return 0 | |
end | |
r=num/den | |
return r | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment