Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Var(x) and cov(x, x) don't give the same result in numpy

Tags:

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?

like image 625
Randomtheories Avatar asked Dec 14 '11 14:12

Randomtheories


People also ask

How does Numpy calculate covariance?

cov() function. Covariance provides the a measure of strength of correlation between two variable or more set of variables. The covariance matrix element Cij is the covariance of xi and xj. The element Cii is the variance of xi.

How does Numpy calculate variance in Python?

The variance is the average of the squared deviations from the mean, i.e., var = mean(x) , where x = abs(a - a. mean())**2 . The mean is typically calculated as x. sum() / N , where N = len(x) .

What does NP COV return?

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. Entry [0,0] is the variance of the data in x , and entry [1,1] is the variance of the data in y .


1 Answers

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

like image 150
George Avatar answered Sep 21 '22 14:09

George