I have got two matrices say, T1 and T2 each of size mxn. I want to find the correlation coefficient between two matrices
So far I haven't used any built-in library function for it. I am doing the following steps for it:
First I calculate the mean of the two matrices as:
M1 = T1.mean()
M2 = T2.mean()
and then I subtract the mean from the corresponding matrices as:
A = np.subtract(T1, M1)
B = np.subtract(T2, M2)
where np is the numpy library and A and B are the resulting matrices after doing the subtraction.
Now , I calculate the correlation coefficent as:
alpha = np.sum(A*B) / (np.sqrt((np.sum(A))*np.sum(B)))
However, the value i get is far greater than 1 and in not meaningful at all. It should be in between 0 and 1 to get some meaning out of it.
I have also tried to make use absolute values of matrix A and B, but that also did'nt work.
I also tried to use :
np.sum(np.dot(A,B.T)) instead of np.sum(A*B)
in the numerator , but that also didn't work.
Edit1:
This is the formula that I intend to calculate:
In this image, C is one of the matrices and T is another one.
'u' is the mean symbol.
Can somebody tell me where actually i am doing the mistake.
Aand all values inB? If so, just flatten both matrices into vectors and calculate the regular correlation coefficient - Simonnp.sum(A*B)might actually do something else than what is shown in the formula. I guess that the first is calculating the sum of all elements after multiplying the two matrices, whereas the latter is summing up the multiplication of every single pair of values. Maybe you can check that with two very simple 2x2 matrices? - nostradamus