Interactive tool

Covariance explorer

Change the spread and correlation of two variables and watch the covariance matrix, its ellipses and the principal axes follow.

Any two variables with finite variances have a 2×22 \times 2 covariance matrix, and that matrix has a shape you can see. Set the standard deviations σ1\sigma_1 and σ2\sigma_2 and the correlation ρ\rho, and the cloud of points, the ellipses and the principal axes move with them.

−3−3−2−2−1−1112233x1x2
  • Sample points
  • Mahalanobis distance 1
  • Mahalanobis distance 2
  • First principal axis
  • Second principal axis
1.50
1.00
0.60

Covariance matrix

Covariance matrix with first row 2.25 and 0.90, and second row 0.90 and 1.00

Variances on the diagonal, covariance off it

Largest eigenvalue λ1
2.72
Smallest eigenvalue λ2
0.53
First component share λ1 / tr Σ
83.7%
Correlation ρ
0.60
Total variance tr Σ
3.25
Generalized variance det Σ
1.44

The points are one fixed random sample. Every setting reuses the same draws, so you can follow a single point as the cloud stretches and turns.

Things to try

  • Set σ1=σ2=1\sigma_1 = \sigma_2 = 1 and ρ=0\rho = 0, or press Circle. Both ellipses become circles, λ1=λ2=1\lambda_1 = \lambda_2 = 1, and every direction through the center is a principal axis.
  • Keep ρ=0\rho = 0, set σ1\sigma_1 below σ2\sigma_2 and slide it up past σ2\sigma_2. The first principal axis switches from vertical to horizontal. With no correlation the principal axes are the coordinate axes, and the first one follows the larger variance.
  • Push ρ\rho toward 0.950.95. The total variance σ12+σ22\sigma_1^2 + \sigma_2^2 does not move, but det⁡(Σ)=σ12σ22(1−ρ2)\det(\boldsymbol\Sigma) = \sigma_1^2 \sigma_2^2 (1 - \rho^2) shrinks toward 00 and the cloud flattens toward a line.
  • Make σ1\sigma_1 and σ2\sigma_2 equal and try any nonzero ρ\rho. The first axis sits at exactly 45∘45^\circ when ρ\rho is positive and −45∘-45^\circ when it is negative, and the eigenvalues are σ12(1+∣ρ∣)\sigma_1^2 (1 + \lvert \rho \rvert) and σ12(1−∣ρ∣)\sigma_1^2 (1 - \lvert \rho \rvert).
  • Flip the sign of ρ\rho. The ellipses and the principal axes reflect across the horizontal axis, while the eigenvalues, the trace and the determinant stay exactly the same.
  • Pick a point just inside the inner ellipse and move any slider. It never crosses the ellipse, because each point keeps the same Mahalanobis distance however Σ\boldsymbol\Sigma changes. The next section shows why.

What you are seeing

With these parameters the covariance matrix is

Σ=[σ12ρ σ1σ2ρ σ1σ2σ22],σ1,σ2>0,−1<ρ<1.\boldsymbol\Sigma = \begin{bmatrix} \sigma_1^2 & \rho \, \sigma_1 \sigma_2 \\ \rho \, \sigma_1 \sigma_2 & \sigma_2^2 \end{bmatrix}, \qquad \sigma_1, \sigma_2 > 0, \quad -1 < \rho < 1.

It is positive definite because ∣ρ∣<1\lvert \rho \rvert < 1. Its eigenvalues λ1≥λ2>0\lambda_1 \ge \lambda_2 > 0 come with orthonormal eigenvectors e1\mathbf e_1 and e2\mathbf e_2, which give the spectral decomposition

Σ=λ1e1e1′+λ2e2e2′.\boldsymbol\Sigma = \lambda_1 \mathbf e_1 \mathbf e_1' + \lambda_2 \mathbf e_2 \mathbf e_2'.

For a 2×22 \times 2 matrix the eigenvalues have a closed form.

λ1,2=σ12+σ222±(σ12−σ222)2+ρ2σ12σ22\lambda_{1,2} = \frac{\sigma_1^2 + \sigma_2^2}{2} \pm \sqrt{\left( \frac{\sigma_1^2 - \sigma_2^2}{2} \right)^2 + \rho^2 \sigma_1^2 \sigma_2^2}

Their sum is the total variance and their product is the generalized variance.

λ1+λ2=tr⁡(Σ)=σ12+σ22,λ1λ2=det⁡(Σ)=σ12σ22(1−ρ2)\lambda_1 + \lambda_2 = \operatorname{tr}(\boldsymbol\Sigma) = \sigma_1^2 + \sigma_2^2, \qquad \lambda_1 \lambda_2 = \det(\boldsymbol\Sigma) = \sigma_1^2 \sigma_2^2 (1 - \rho^2)

The share λ1/(λ1+λ2)\lambda_1 / (\lambda_1 + \lambda_2) is the proportion of the total variance along the first principal component.

The ellipses are the points at Mahalanobis distance cc from the mean 0\mathbf 0, with c=1c = 1 for the inner one and c=2c = 2 for the outer one.

x′Σ−1x=c2\mathbf x' \boldsymbol\Sigma^{-1} \mathbf x = c^2

Each ellipse has its axes along e1\mathbf e_1 and e2\mathbf e_2, with half lengths cλ1c \sqrt{\lambda_1} and cλ2c \sqrt{\lambda_2}. The two colored segments are these axes for c=2c = 2.

Each sample point starts as a pair z\mathbf z of independent standard normal values, drawn once. The plot shows x=Lz\mathbf x = \mathbf L \mathbf z, where L\mathbf L is the Cholesky factor of Σ\boldsymbol\Sigma.

L=[σ10ρ σ2σ21−ρ2],LL′=Σ\mathbf L = \begin{bmatrix} \sigma_1 & 0 \\ \rho \, \sigma_2 & \sigma_2 \sqrt{1 - \rho^2} \end{bmatrix}, \qquad \mathbf L \mathbf L' = \boldsymbol\Sigma

That makes x\mathbf x a draw from N2(0,Σ)N_2(\mathbf 0, \boldsymbol\Sigma), and for every setting its squared distance is

x′Σ−1x=z′L′(LL′)−1Lz=z′z.\mathbf x' \boldsymbol\Sigma^{-1} \mathbf x = \mathbf z' \mathbf L' (\mathbf L \mathbf L')^{-1} \mathbf L \mathbf z = \mathbf z' \mathbf z.

Under this model x′Σ−1x\mathbf x' \boldsymbol\Sigma^{-1} \mathbf x has a χ22\chi^2_2 distribution, so

P(x′Σ−1x≤c2)=1−e−c2/2.P\left( \mathbf x' \boldsymbol\Sigma^{-1} \mathbf x \le c^2 \right) = 1 - e^{-c^2 / 2}.

That is about 0.390.39 inside the inner ellipse and 0.860.86 inside the outer one. The 240 points here are one random draw, so their shares come out near those values but not exactly on them.

The multivariate statistics reference has the notation and the main results in one place, and the linear algebra refresher goes over eigenvalues and quadratic forms. The videos are on the Multivariate Statistical Analysis course page.