20

A property of the covariance is, that cov(x, x) = var(x)

However, in numpy I don't get the same result.

from numpy import var, cov

x = range(10)
y = var(x)
z = cov(x, x)[0][1]
print y, z

Am I doing something wrong here? How can I obtain the correct result?

Randomtheories
  • 1,220
  • 18
  • 22

2 Answers2

24

You must use z=cov(x,bias=1) in order to normalize by N ,because var is also norm by N (according to this

George
  • 5,808
  • 15
  • 83
  • 160
12

The default ddof of cov (None) and var (0) are different. Try specifying the ddof (or bias):

>>> cov(x, x, ddof=0)
array([[ 8.25,  8.25],
       [ 8.25,  8.25]])
>>> var(x)
8.25
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005