views:

287

answers:

3

Given two vectors X and Y, I have to find their correlation, i.e. their linear dependence/independence. Both vectors have equal dimension. The result should be a floating point number from [-1.0 .. 1.0].

Example:

X=[-1, 2,    0]
Y=[ 4, 2, -0.3]

Find y = cor(X,Y) such that y belongs to [-1.0 .. 1.0].

It should be a simple construction involving a list-comprehension. No external library is allowed.

UPDATE: ok, if the dot product is enough, then here is my solution:

nX = 1/(sum([x*x for x in X]) ** 0.5)
nY = 1/(sum([y*y for y in Y]) ** 0.5)
cor = sum([(x*nX)*(y*nY)  for x,y in zip(X,Y) ])

right?

+2  A: 

Sounds like a dot product to me.

Solve the equation for the cosine of the angle between the two vectors, which is always in the range [-1, 1], and you'll have what you want.

It's equal to the dot product divided by the magnitudes of two vectors.

duffymo
Doesn't the dot product range rather more widely in its possible values than [-1,1]? :-)
Brandon Craig Rhodes
@Brandon: for normalized vectors it is always on [-1..1]
psihodelia
Not if you normalize it properly. You're really talking about the cosine of the angle between the two, which is always [-1, 1]. Solve the dot product for the cosine and you'll have what you want.
duffymo
+3  A: 

Since range is supposed to be [-1, 1] I think that the Pearson Correlation can be ok for your purposes.

Also dot-product would work but you'll have to normalize vectors before calculating it and you can have a -1,1 range just if you have also negative values.. otherwise you would have 0,1

Jack
+2  A: 

Don't assume because a formula is algebraically correct that its direct implementation in code will work. There can be numerical problems with some definitions of correlation.

See How to calculate correlation accurately

John D. Cook
Thanks for this link
psihodelia