Question

I have two vectors X0 and X1 (mx0 and mx1 are the means of each vector) and I am trying to find the covariance matrix between them. I have managed to find each element in the matrix through doing:

b1=numpy.zeros(N*1).reshape((N,1))
b2=numpy.zeros(N*1).reshape((N,1))

for i in range(0,N):
    b1[i]=X0[i]-mX0
for j in range(0,N):
    b2[j]=X1[j]-mX1

bii=sum(p*q for p,q in zip(b1,b1))/(N-1)
bij=sum(p*q for p,q in zip(b1,b2))/(N-1)
bji=sum(p*q for p,q in zip(b2,b1))/(N-1)
bjj=sum(p*q for p,q in zip(b2,b2))/(N-1)

but I want a nicer way of doing this through a loop, rather than doing each element separately.

Was it helpful?

Solution

If you want to compute the covariance matrix by hand, study/mimick how numpy.cov does it, or if you just want the result, use np.cov(b1, b2) directly.

import numpy as np
np.random.seed(1)
N = 10
b1 = np.random.rand(N)
b2 = np.random.rand(N)
X = np.column_stack([b1, b2])
X -= X.mean(axis=0) 
fact = N - 1 
by_hand = np.dot(X.T, X.conj()) / fact
print(by_hand)
# [[ 0.04735338  0.01242557]
#  [ 0.01242557  0.07669083]]

using_cov = np.cov(b1, b2)

assert np.allclose(by_hand, using_cov)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top