I want to calculate the variance of vector [0, 3, 4] in Python numpy. My code is
test=np.array([0, 3, 4])
print('test var', test.var(axis=0))
The output is
test var 2.8888888888888893.
Why? I thought it should be 4.333333333333334.
The cov function yields the correct result:
print("Covariance matrix of test:\n", np.cov(test))
Output
Covariance matrix of test:
4.333333333333334
On the other hand , if I have a 2-dimensional array like this
k1=0.1
N=100
x1=np.random.rand(N)
nor=np.random.normal(0,0.5,size=N)
x3=k1*nor+(1-k1)*x1
X=np.vstack((x1,x3)).T
print('X var', X.var(axis=0))
C = np.cov(X.T)
c00 = C[0, 0]
c11 = C[1, 1]
print('c00 ', c00)
print('c00 ', c11)
Output
X var [0.0861854 0.06790817]
c00 0.0870559565349231
c00 0.06859411169279941
Here the var gives the same result as is in the diagonal of C. But with the vector it is not the same. What's going on here ?