Search code examples
pythonnumpycovariance-matrix

np.cov() matrix returns unexpected values


I am trying to find the covariance matrix of all possible images(flattened) with each pixel - {0,1}.

I have written following code using numpy:

import numpy as np

a = np.array(np.meshgrid([1,0], [1, 0], [1, 0],[1,0],[0,1])).T.reshape(-1,5)
a = np.transpose(a)

covariance = np.cov(a)
print(covariance)                                                                                                                                                                      

I get output 0.25806452 in the diagonal. But I think the diagonal should be exactly 0.25.

Can anyone explain why it isn't 0.25?


Solution

  • It is being normalised by 1/(N-1), not 1/N. Set the ddof parameter to change this behaviour.