Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does corrcoef return a matrix?

Tags:

python

math

numpy

It seems strange to me that np.corrcoef returns a matrix.

 correlation1 = corrcoef(Strategy1Returns,Strategy2Returns)  [[ 1.         -0.99598935]  [-0.99598935  1.        ]] 

Does anyone know why this is the case and whether it is possible to return just one value in the classical sense?

like image 241
Dan Avatar asked Aug 06 '10 15:08

Dan


People also ask

What does NP Corrcoef return?

corrcoef returns the normalised covariance matrix.

How does NP Corrcoef work?

In NumPy, We can compute pearson product-moment correlation coefficients of two given arrays with the help of numpy. corrcoef() function. In this function, we will pass arrays as a parameter and it will return the pearson product-moment correlation coefficients of two given arrays.

How does Corrcoef work in Matlab?

R = corrcoef( A ) returns the matrix of correlation coefficients for A , where the columns of A represent random variables and the rows represent observations. R = corrcoef( A , B ) returns coefficients between two random variables A and B .

What is the purpose of a correlation matrix?

A correlation matrix is simply a table which displays the correlation coefficients for different variables. The matrix depicts the correlation between all the possible pairs of values in a table. It is a powerful tool to summarize a large dataset and to identify and visualize patterns in the given data.


2 Answers

It allows you to compute correlation coefficients of >2 data sets, e.g.

>>> from numpy import * >>> a = array([1,2,3,4,6,7,8,9]) >>> b = array([2,4,6,8,10,12,13,15]) >>> c = array([-1,-2,-2,-3,-4,-6,-7,-8]) >>> corrcoef([a,b,c]) array([[ 1.        ,  0.99535001, -0.9805214 ],        [ 0.99535001,  1.        , -0.97172394],        [-0.9805214 , -0.97172394,  1.        ]]) 

Here we can get the correlation coefficient of a,b (0.995), a,c (-0.981) and b,c (-0.972) at once. The two-data-set case is just a special case of N-data-set class. And probably it's better to keep the same return type. Since the "one value" can be obtained simply with

>>> corrcoef(a,b)[1,0] 0.99535001355530017 

there's no big reason to create the special case.

like image 150
kennytm Avatar answered Oct 02 '22 19:10

kennytm


corrcoef returns the normalised covariance matrix.

The covariance matrix is the matrix

Cov( X, X )    Cov( X, Y )  Cov( Y, X )    Cov( Y, Y ) 

Normalised, this will yield the matrix:

Corr( X, X )    Corr( X, Y )  Corr( Y, X )    Corr( Y, Y ) 

correlation1[0, 0 ] is the correlation between Strategy1Returns and itself, which must be 1. You just want correlation1[ 0, 1 ].

like image 27
Katriel Avatar answered Oct 02 '22 18:10

Katriel