Compute the covariance matrix by hand with Python / Numpy

covariancelinear algebramatricesprobability

I'm trying to compute the covariance matrix (in python 3 and numpy using the formula wikipedia

$$
\Sigma_{X_iX_j} = \text{cov}[X_i, X_j] = E[(X_i – E[X_i])(X_j – E[X_j])]
$$

and the numpy documentation

import numpy as np
x = np.array([[0, 2, 7], [1, 1, 9], [2, 0, 13]]).T
print("matrix:")
print(x)
print("covariance matrix: ")
print(np.cov(x))
print("Sigma_1.1:")
print((x[0,0] - np.mean(x[0]))*(x[0,0] - np.mean(x[0])))
print("Sigma_3.3:")
print((x[2,2] - np.mean(x[2]))*(x[2,2] - np.mean(x[2])))
print("Sigma_3.3 computed 'by hand':")
print((13 - (7+9+13)/3)**2)

matrix:
[[ 0  1  2]
 [ 2  1  0]
 [ 7  9 13]]
covariance matrix:
[[ 1.         -1.          3.        ]
 [-1.          1.         -3.        ]
 [ 3.         -3.          9.33333333]]
Sigma_1.1:
1.0
Sigma_3.3:
11.111111111111114
Sigma_3.3 computed 'by hand':
11.111111111111114

why the entry $\Sigma_{3.3}$ computed by numpy (output: 9.33333333) and mine (output: 11.111111111111114) don't match? what am I doing wrong?

Best Answer

The third variable is not just $13$.

The reading of the third variables are $[7,9,13]$.

$$\frac{7+9+13}{3}=\frac{29}{3}$$

The unbiased estimator is

$$\frac12\left[ \left(7-\frac{29}{3}\right)^2 + (9 - \frac{29}{3})^2 + (13 - \frac{29}{3})^2\right]\approx 9.3333.$$

Note that for the first variables, the readings are $[0,1,2]$ with mean being $1$.

The unbiased estimator is

$$\frac12\left[ \left(0-1\right)^2 + (1-1)^2 + (2-1)^2\right]=1$$

Related Question