This stackoverflow post describes computing a Pearson correlation of [1,2,3] and [1,5,7] in several different ways in Python. The most straightforward implementation from the definition in Wikipedia comes up with
0.973328526785
while Excel, R, NumPy, an online calculator, and a different Python implementation (involving what looks like a more numerically unstable calculation to me) come up with
0.981980506
I am just curious to know what you think.
sum(x) / len(x)you divide ints, not floats. Sosum([1,5,7]) / len([1,5,7]) = 13 / 3 = 4, according to integer division (whereas you want13. / 3. = 4.33...). To fix it rewrite this line asfloat(sum(x)) / float(len(x))(one float suffices, as Python converts it automatically). – Piotr Migdal Oct 30 '11 at 19:47