Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build in function for computing covariance

Is there a way in python to obtain the covariance matrix given the mean and sample data points

Example:

mean = [3 3.6]
data = [[1 2]
        [2 3]
        [3 3]
        [4 5] 
        [5 5]]

I know how to calculate the same by substituting these values in the formula. But is there a build in function in python which does this for me. I know there is one in Matlab, but I am not sure about python.

like image 424
Jannat Arora Avatar asked Mar 29 '12 10:03

Jannat Arora


People also ask

How do you compute covariance?

Covariance is calculated by analyzing at-return surprises (standard deviations from the expected return) or by multiplying the correlation between the two random variables by the standard deviation of each variable.

How do you code covariance in Python?

The covariance may be computed using the Numpy function np. cov() . For example, we have two sets of data x and y , np. cov(x, y) returns a 2D array where entries [0,1] and [1,0] are the covariances.

How do you calculate covariance in C++?

The covariance matrix is given by Σ=(Var(X1)Cov(X1,X2)Cov(X2,X1)Var(X2)). Since Cov(X1,X2)=Cov(X2,X1), only three entries have to be estimated.

How do you calculate covariance in Excel?

We wish to find out covariance in Excel, that is, to determine if there is any relation between the two. The relationship between the values in columns C and D can be calculated using the formula =COVARIANCE. P(C5:C16,D5:D16).


1 Answers

numpy.cov() can be used to compute the covariance matrix:

In [1]: import numpy as np

In [2]: data = np.array([[1,2], [2,3], [3,3], [4,5], [5,5]])

In [3]: np.cov(data.T)
Out[3]: 
array([[ 2.5,  2. ],
       [ 2. ,  1.8]])

By default, np.cov() expects each row to represent a variable, with observations in the columns. I therefore had to transpose your matrix (by using .T).

An alternative way to achieve the same thing is by setting rowvar to False:

In [15]: np.cov(data, rowvar=False)
Out[15]: 
array([[ 2.5,  2. ],
       [ 2. ,  1.8]])
like image 130
NPE Avatar answered Oct 12 '22 13:10

NPE