Search code examples
pythonnumpycovariance

numpy covariance matrix


Suppose I have two vectors of length 25, and I want to compute their covariance matrix. I try doing this with numpy.cov, but always end up with a 2x2 matrix.

>>> import numpy as np
>>> x=np.random.normal(size=25)
>>> y=np.random.normal(size=25)
>>> np.cov(x,y)
array([[ 0.77568388,  0.15568432],
       [ 0.15568432,  0.73839014]])

Using the rowvar flag doesn't help either - I get exactly the same result.

>>> np.cov(x,y,rowvar=0)
array([[ 0.77568388,  0.15568432],
       [ 0.15568432,  0.73839014]])

How can I get the 25x25 covariance matrix?


Solution

  • You have two vectors, not 25. The computer I'm on doesn't have python so I can't test this, but try:

    z = zip(x,y)
    np.cov(z)
    

    Of course.... really what you want is probably more like:

    n=100 # number of points in each vector
    num_vects=25
    vals=[]
    for _ in range(num_vects):
        vals.append(np.random.normal(size=n))
    np.cov(vals)
    

    This takes the covariance (I think/hope) of num_vects 1xn vectors