Math for Causal Inference

Day 22. Positive-Definite Matrices & Variance–Covariance Matrices

Random vectors, their covariance matrices, and the rules for the variance of any linear combination.

Video coming soon
Learning outcomes
  • ●Compute means and covariance matrices of random vectors and the variance of any linear combination a′X.
  • ●Prove Var(AX + b) = AΣA′ and that every covariance matrix is positive semi-definite.
  • ●Use the Cholesky factor to simulate correlated variables.
  • ●Compute standard errors of sums and differences of correlated coefficient estimates.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

Random vectors

For a random vector X=(X1,…,Xk)⊤X = (X_1, \dots, X_k)^{\top}, the mean vector is μ=E[X]\mu = E[X] and the variance–covariance matrix is

Σ=Var⁡(X)=E[(X−μ)(X−μ)⊤],Σjj=Var⁡(Xj),Σjℓ=Cov⁡(Xj,Xℓ).\Sigma = \operatorname{Var}(X) = E\big[(X - \mu)(X - \mu)^{\top}\big], \qquad \Sigma_{jj} = \operatorname{Var}(X_j), \quad \Sigma_{j\ell} = \operatorname{Cov}(X_j, X_\ell).

Σ\Sigma is symmetric. Its correlation matrix is D−1/2ΣD−1/2D^{-1/2}\Sigma D^{-1/2} with D=diag⁡(Σ)D = \operatorname{diag}(\Sigma).

The two rules you will use every week

Var⁡(a⊤X)=a⊤Σa,Var⁡(AX+b)=AΣA⊤.\operatorname{Var}(a^{\top}X) = a^{\top}\Sigma a, \qquad \operatorname{Var}(AX + b) = A\Sigma A^{\top}.

They give the variance of a portfolio, a contrast between treatment arms, a cumulative event-study effect, or a predicted value x0⊤β^x_0^{\top}\hat\beta: just plug in Var⁡(β^)\operatorname{Var}(\hat\beta).

Positive (semi-)definiteness

Because a⊤Σaa^{\top}\Sigma a is a variance, it can never be negative: every covariance matrix is PSD. It is PD unless some combination of the variables is constant (e.g. shares that sum to one). A symmetric matrix that is not PSD (e.g. a "correlation matrix" assembled pairwise from different samples) cannot be the covariance matrix of anything.

Cholesky factor and simulation

A PD matrix factors as Σ=LL⊤\Sigma = LL^{\top} with LL lower-triangular (the Cholesky factor). If Z∼N(0,I)Z \sim N(0, I), then

X=μ+LZ∼N(μ,Σ).X = \mu + LZ \sim N(\mu, \Sigma).

This is how simulations generate correlated covariates, errors or potential outcomes.

Sample covariance

With the centred data matrix X~\tilde X (n×kn \times k), S=1n−1X~⊤X~S = \frac{1}{n - 1}\tilde X^{\top}\tilde X, which is PSD by Day 17.

Part 1 · Warm-up

Let Σ=(4229)\Sigma = \begin{pmatrix}4 & 2\\2 & 9\end{pmatrix}.

Exercise 1 · Variance of a sum

Find Var⁡(X1+X2)\operatorname{Var}(X_1 + X_2).

Show solution

a=(1,1)a = (1, 1): a⊤Σa=4+9+2(2)=17a^{\top}\Sigma a = 4 + 9 + 2(2) = 17.

Exercise 2 · Variance of a difference

Find Var⁡(X1−X2)\operatorname{Var}(X_1 - X_2).

Show solution

a=(1,−1)a = (1, -1): 4+9−2(2)=94 + 9 - 2(2) = 9.

Exercise 3 · Correlation

Find the correlation between X1X_1 and X2X_2.

Show solution

2/(49)=2/6≈0.3332/(\sqrt4\sqrt9) = 2/6 \approx 0.333.

Exercise 4 · A valid covariance matrix?

Can (1221)\begin{pmatrix}1 & 2\\2 & 1\end{pmatrix} be a covariance matrix?

Show solution

Step 1. No.

Step 2. Its determinant is −3<0-3 < 0, so it has a negative eigenvalue; equivalently the implied correlation is 2.

Step 3. For a=(1,−1)a = (1, -1), a⊤Σa=1+1−4=−2a^{\top}\Sigma a = 1 + 1 - 4 = -2: a negative variance.

Exercise 5 · Cholesky by hand

Find the Cholesky factor of Σ\Sigma.

Show solution

Step 1. Write L=(ℓ110ℓ21ℓ22)L = \begin{pmatrix}\ell_{11} & 0\\\ell_{21} & \ell_{22}\end{pmatrix}, so LL⊤=(ℓ112ℓ11ℓ21ℓ11ℓ21ℓ212+ℓ222)LL^{\top} = \begin{pmatrix}\ell_{11}^2 & \ell_{11}\ell_{21}\\\ell_{11}\ell_{21} & \ell_{21}^2 + \ell_{22}^2\end{pmatrix}.

Step 2. Match to Σ\Sigma: ℓ112=4⇒ℓ11=2\ell_{11}^2 = 4 \Rightarrow \ell_{11} = 2.

Step 3. ℓ11ℓ21=2⇒ℓ21=1\ell_{11}\ell_{21} = 2 \Rightarrow \ell_{21} = 1.

Step 4. ℓ212+ℓ222=9⇒ℓ22=8≈2.828\ell_{21}^2 + \ell_{22}^2 = 9 \Rightarrow \ell_{22} = \sqrt8 \approx 2.828.

Answer: L=(2012.828)L = \begin{pmatrix}2 & 0\\1 & 2.828\end{pmatrix}.

Part 2 · Proofs

Exercise 6 · Variance of a linear combination

Prove Var⁡(a⊤X)=a⊤Σa\operatorname{Var}(a^{\top}X) = a^{\top}\Sigma a.

Show solution

a⊤X−E[a⊤X]=a⊤(X−μ)a^{\top}X - E[a^{\top}X] = a^{\top}(X - \mu), a scalar equal to its own transpose. So

Var⁡(a⊤X)=E[a⊤(X−μ)(X−μ)⊤a]=a⊤E[(X−μ)(X−μ)⊤]a=a⊤Σa.\operatorname{Var}(a^{\top}X) = E\big[a^{\top}(X - \mu)(X - \mu)^{\top}a\big] = a^{\top}E\big[(X - \mu)(X - \mu)^{\top}\big]a = a^{\top}\Sigma a.

Exercise 7 · Linear transformations

Prove Var⁡(AX+b)=AΣA⊤\operatorname{Var}(AX + b) = A\Sigma A^{\top}.

Show solution

AX+b−E[AX+b]=A(X−μ)AX + b - E[AX + b] = A(X - \mu), so Var⁡=E[A(X−μ)(X−μ)⊤A⊤]=AΣA⊤\operatorname{Var} = E[A(X - \mu)(X - \mu)^{\top}A^{\top}] = A\Sigma A^{\top}.

Exercise 8 · Covariance matrices are PSD

Prove that every covariance matrix is positive semi-definite.

Show solution

For any aa, a⊤Σa=Var⁡(a⊤X)≥0a^{\top}\Sigma a = \operatorname{Var}(a^{\top}X) \ge 0 (Exercise 6).

Exercise 9 · Simulating with Cholesky

Prove that if Z∼N(0,I)Z \sim N(0, I) then X=μ+LZX = \mu + LZ has mean μ\mu and covariance LL⊤=ΣLL^{\top} = \Sigma.

Show solution

E[X]=μ+LE[Z]=μE[X] = \mu + LE[Z] = \mu, and by Exercise 7, Var⁡(X)=L I L⊤=Σ\operatorname{Var}(X) = L\,I\,L^{\top} = \Sigma. (A linear function of normals is normal, so X∼N(μ,Σ)X \sim N(\mu, \Sigma).)

Exercise 10 · What PD implies about entries

Show that a PD matrix has positive diagonal entries and ∣σ12∣<σ11σ22|\sigma_{12}| < \sqrt{\sigma_{11}\sigma_{22}}.

Show solution

Take a=eja = e_j (the jjth unit vector): σjj=ej⊤Σej>0\sigma_{jj} = e_j^{\top}\Sigma e_j > 0. The 2×2 leading block is also PD, so its determinant (product of its eigenvalues) is positive: σ11σ22−σ122>0\sigma_{11}\sigma_{22} - \sigma_{12}^2 > 0.

Part 3 · Applied

Exercise 11 · Portfolio risk

Two assets have return sds 10% and 20% and correlation 0.3. With weights w=(0.6,0.4)w = (0.6, 0.4), find the portfolio sd.

Show solution

Step 1. Covariance: 0.3×10×20=600.3 \times 10 \times 20 = 60, so Σ=(1006060400)\Sigma = \begin{pmatrix}100 & 60\\60 & 400\end{pmatrix}.

Step 2. w⊤Σw=0.62(100)+0.42(400)+2(0.6)(0.4)(60)=36+64+28.8=128.8w^{\top}\Sigma w = 0.6^2(100) + 0.4^2(400) + 2(0.6)(0.4)(60) = 36 + 64 + 28.8 = 128.8.

Step 3. sd =128.8≈11.35= \sqrt{128.8} \approx 11.35.

Answer: portfolio sd ≈ 11.35%.

Exercise 12 · Minimum-variance weights

For the same assets, find the weight w1w_1 that minimises portfolio variance (with w2=1−w1w_2 = 1 - w_1).

Show solution

Step 1. Variance as a function of w1w_1: V(w1)=w12σ12+(1−w1)2σ22+2w1(1−w1)σ12V(w_1) = w_1^2\sigma_1^2 + (1 - w_1)^2\sigma_2^2 + 2w_1(1 - w_1)\sigma_{12}.

Step 2. Set V′(w1)=2w1σ12−2(1−w1)σ22+2(1−2w1)σ12=0V'(w_1) = 2w_1\sigma_1^2 - 2(1 - w_1)\sigma_2^2 + 2(1 - 2w_1)\sigma_{12} = 0 and solve:

w1=σ22−σ12σ12+σ22−2σ12.w_1 = \frac{\sigma_2^2 - \sigma_{12}}{\sigma_1^2 + \sigma_2^2 - 2\sigma_{12}}.

Step 3. Plug in: w1=(400−60)/(100+400−120)=340/380≈0.895w_1 = (400 - 60)/(100 + 400 - 120) = 340/380 \approx 0.895.

Answer: about 89.5% in the less risky asset (Day 26 derives the general version with a Lagrange multiplier).

Exercise 13 · Are two effects different?

A regression gives β^1=0.5\hat\beta_1 = 0.5 and β^2=0.2\hat\beta_2 = 0.2 with Var⁡(β^1)=0.04\operatorname{Var}(\hat\beta_1) = 0.04, Var⁡(β^2)=0.09\operatorname{Var}(\hat\beta_2) = 0.09, Cov⁡=0.02\operatorname{Cov} = 0.02. Test β1=β2\beta_1 = \beta_2.

Show solution

Step 1. Contrast a=(1,−1)a = (1, -1): Var⁡(β^1−β^2)=0.04+0.09−2(0.02)=0.09\operatorname{Var}(\hat\beta_1 - \hat\beta_2) = 0.04 + 0.09 - 2(0.02) = 0.09.

Step 2. SE =0.3= 0.3; estimate =0.5−0.2=0.3= 0.5 - 0.2 = 0.3.

Step 3. z=0.3/0.3=1z = 0.3/0.3 = 1, p≈0.32p \approx 0.32.

Answer: not significantly different. Ignoring the covariance would give SE 0.13≈0.36\sqrt{0.13} \approx 0.36 instead.

Exercise 14 · A cumulative effect

An event study estimates three post-period effects, each with variance 0.01 and pairwise covariance 0.005. Find the SE of their sum.

Show solution

Step 1. The sum is 1⊤β^\mathbf 1^{\top}\hat\beta, so its variance is 1⊤V1\mathbf 1^{\top}V\mathbf 1 = the sum of all entries of VV.

Step 2. Diagonal: 3×0.01=0.033 \times 0.01 = 0.03. Off-diagonal: 6 entries × 0.005 = 0.03.

Step 3. Total 0.06, SE =0.06≈0.245= \sqrt{0.06} \approx 0.245.

Answer: 0.245; assuming independence would give 0.173, far too small.

Exercise 15 · An impossible set of survey correlations

A report on a household survey lists pairwise correlations computed from different sub-samples: income–education 0.9, income–health 0.9, and education–health −0.9. Could these be the correlations of three real variables?

Show solution

Step 1. A valid correlation matrix must be PSD, so its determinant (the product of the eigenvalues) cannot be negative.

Step 2. Cofactor expansion of R=(10.90.90.91−0.90.9−0.91)R = \begin{pmatrix}1 & 0.9 & 0.9\\0.9 & 1 & -0.9\\0.9 & -0.9 & 1\end{pmatrix}:

det⁡R=1(1−0.81)−0.9(0.9+0.81)+0.9(−0.81−0.9)=0.19−1.539−1.539≈−2.89.\det R = 1(1 - 0.81) - 0.9(0.9 + 0.81) + 0.9(-0.81 - 0.9) = 0.19 - 1.539 - 1.539 \approx -2.89.

Step 3. Negative, so RR has a negative eigenvalue (−0.8, from the code).

Answer: impossible. If income is strongly correlated with both education and health, those two cannot be strongly negatively correlated. Pairwise estimates from different samples can produce such incoherent matrices.

Verify in Python

import numpy as np

S = np.array([[4, 2], [2, 9.]])
for a in ([1, 1], [1, -1]):
    a = np.array(a); print("Var(a'X) for a =", a, ":", a @ S @ a)
L = np.linalg.cholesky(S); print("Cholesky L =\n", L.round(3))

rng = np.random.default_rng(22)
Z = rng.normal(size=(2, 200_000)); X = (L @ Z).T
print("simulated covariance:\n", np.cov(X.T).round(2))

w = np.array([0.6, 0.4]); Sig = np.array([[100, 60], [60, 400.]])
print("portfolio sd:", round(np.sqrt(w @ Sig @ w), 2))
V = np.array([[0.01, 0.005, 0.005], [0.005, 0.01, 0.005], [0.005, 0.005, 0.01]])
print("SE of cumulative effect:", round(np.sqrt(np.ones(3) @ V @ np.ones(3)), 3))
R = np.array([[1, 0.9, 0.9], [0.9, 1, -0.9], [0.9, -0.9, 1]])
print("eigenvalues of the 'correlation matrix':", np.linalg.eigvalsh(R).round(3))

Key takeaways

  • Var⁡(a⊤X)=a⊤Σa\operatorname{Var}(a^{\top}X) = a^{\top}\Sigma a and Var⁡(AX+b)=AΣA⊤\operatorname{Var}(AX + b) = A\Sigma A^{\top} give the variance of any linear combination.
  • Covariance matrices are PSD; a matrix that is not PSD cannot describe real data.
  • Cholesky factors turn independent normals into correlated ones, the basis of most simulation studies.

Simulation Lab: Covariance Ellipses & a′Σa

Open full screen →

Correlated draws built with a Cholesky factor, their covariance ellipses, and the variance of any linear combination.

a′Σa (theory)
2.525
var(a′X) sample
2.727
eigenvalues of Σ
2.72, 0.53
max / min of a′Σa
Cholesky L
[1.50 0; 0.60 0.80]

500 draws made from independent normals with the Cholesky factor L. The ellipses are the 1- and 2-sd contours; their axes are the eigenvectors of Σ. The orange direction a gives a combination a′X whose variance is a′Σa.

Day 21Day 23