Math for Causal Inference

Day 27. OLS in Matrix Form

The linear model, the OLS estimator, its mean and variance, s², standard errors and the Gauss–Markov theorem, all in a few lines of matrix algebra.

Video coming soon
Learning outcomes
  • ●Compute β̂ = (X′X)⁻¹X′y, residuals, s² and standard errors by hand for a small data set.
  • ●Prove that OLS is unbiased with Var(β̂ | X) = σ²(X′X)⁻¹ and that E[e′e] = σ²(n − k).
  • ●Sketch the Gauss–Markov theorem and derive the omitted-variable-bias formula in matrix form.
  • ●Interpret dummy, interaction and log coefficients and see how covariates sharpen experiments.
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

The model and the estimator

y=Xβ+ε,β^=(X⊤X)−1X⊤y.y = X\beta + \varepsilon, \qquad \hat\beta = (X^{\top}X)^{-1}X^{\top}y.

Assumptions (the classical set):

  1. Full rank: XX has rank kk (Day 18).
  2. Exogeneity: E[ε∣X]=0E[\varepsilon \mid X] = 0.
  3. Spherical errors: Var⁡(ε∣X)=σ2I\operatorname{Var}(\varepsilon \mid X) = \sigma^2I (homoskedastic, uncorrelated).

Properties

Substituting the model into the estimator gives the key identity

β^=β+(X⊤X)−1X⊤ε.\hat\beta = \beta + (X^{\top}X)^{-1}X^{\top}\varepsilon.
  • Under 1–2, E[β^∣X]=βE[\hat\beta \mid X] = \beta (unbiased).
  • Under 1–3, Var⁡(β^∣X)=σ2(X⊤X)−1\operatorname{Var}(\hat\beta \mid X) = \sigma^2(X^{\top}X)^{-1}.
  • s2=e⊤e/(n−k)s^2 = e^{\top}e/(n - k) is unbiased for σ2\sigma^2; standard errors are the square roots of the diagonal of s2(X⊤X)−1s^2(X^{\top}X)^{-1}, and tj=β^j/SE⁡jt_j = \hat\beta_j/\operatorname{SE}_j.
  • Gauss–Markov: under 1–3, OLS has the smallest variance among all linear unbiased estimators (BLUE).

Assumption 3 is often doubtful; Day 29 replaces it with robust standard errors. Assumption 2 is the causal one: it fails whenever an omitted variable is correlated with XX.

Omitted-variable bias, in matrices

If the truth is y=X1β1+X2β2+εy = X_1\beta_1 + X_2\beta_2 + \varepsilon but you regress on X1X_1 only,

E[β^1∣X]=β1+(X1⊤X1)−1X1⊤X2⏟slopes of X2 on X1β2.E[\hat\beta_1 \mid X] = \beta_1 + \underbrace{(X_1^{\top}X_1)^{-1}X_1^{\top}X_2}_{\text{slopes of } X_2 \text{ on } X_1}\beta_2.

Reading coefficients

  • Dummy: difference in conditional means between the groups.
  • Interaction T×FT \times F: how the effect of TT differs between groups.
  • Log outcome: approximately a proportional effect (Day 23 for the exact conversion).

Part 1 · Warm-up

Data: five workers with x=(0,1,2,3,4)x = (0, 1, 2, 3, 4) years of experience earn y=(1,3,2,5,4)y = (1, 3, 2, 5, 4) (in $10/hour). Regress wages on an intercept and experience.

Exercise 1 · The estimator

Compute X⊤XX^{\top}X, X⊤yX^{\top}y and β^\hat\beta.

Show solution

Step 1. Sums: n=5n = 5, ∑x=10\sum x = 10, ∑x2=30\sum x^2 = 30, ∑y=15\sum y = 15, ∑xy=0+3+4+15+16=38\sum xy = 0 + 3 + 4 + 15 + 16 = 38.

Step 2. Cross-products:

X⊤X=(5101030),X⊤y=(1538).X^{\top}X = \begin{pmatrix}5 & 10\\10 & 30\end{pmatrix}, \qquad X^{\top}y = \begin{pmatrix}15\\38\end{pmatrix}.

Step 3. det⁡X⊤X=150−100=50\det X^{\top}X = 150 - 100 = 50, so (X⊤X)−1=150(30−10−105)(X^{\top}X)^{-1} = \frac{1}{50}\begin{pmatrix}30 & -10\\-10 & 5\end{pmatrix}.

Step 4. β^=150(30(15)−10(38)−10(15)+5(38))=150(7040)=(1.40.8)\hat\beta = \frac{1}{50}\begin{pmatrix}30(15) - 10(38)\\-10(15) + 5(38)\end{pmatrix} = \frac{1}{50}\begin{pmatrix}70\\40\end{pmatrix} = \begin{pmatrix}1.4\\0.8\end{pmatrix}.

Answer: intercept 1.4 ($14/hour with no experience) and slope 0.8 ($8/hour per year of experience).

Exercise 2 · Fitted values and residuals

Compute y^\hat y and ee, and check X⊤e=0X^{\top}e = 0.

Show solution

Step 1. Fitted values 1.4+0.8x1.4 + 0.8x: 1.4,2.2,3.0,3.8,4.61.4, 2.2, 3.0, 3.8, 4.6.

Step 2. Residuals y−y^y - \hat y: −0.4,0.8,−1.0,1.2,−0.6-0.4, 0.8, -1.0, 1.2, -0.6.

Step 3. ∑e=0\sum e = 0 and ∑xe=0+0.8−2.0+3.6−2.4=0\sum xe = 0 + 0.8 - 2.0 + 3.6 - 2.4 = 0. ✓

Exercise 3 · The error variance

Compute s2s^2.

Show solution

Step 1. e⊤e=0.16+0.64+1.00+1.44+0.36=3.6e^{\top}e = 0.16 + 0.64 + 1.00 + 1.44 + 0.36 = 3.6.

Step 2. Degrees of freedom n−k=5−2=3n - k = 5 - 2 = 3.

Answer: s2=3.6/3=1.2s^2 = 3.6/3 = 1.2.

Exercise 4 · Standard errors and t

Compute Var⁡^(β^)\widehat{\operatorname{Var}}(\hat\beta), the SE of the slope and its t-statistic.

Show solution

Step 1. Var⁡^(β^)=s2(X⊤X)−1=1.2×150(30−10−105)=(0.72−0.24−0.240.12)\widehat{\operatorname{Var}}(\hat\beta) = s^2(X^{\top}X)^{-1} = 1.2 \times \frac{1}{50}\begin{pmatrix}30 & -10\\-10 & 5\end{pmatrix} = \begin{pmatrix}0.72 & -0.24\\-0.24 & 0.12\end{pmatrix}.

Step 2. SE of the slope =0.12≈0.346= \sqrt{0.12} \approx 0.346.

Step 3. t=0.8/0.346≈2.31t = 0.8/0.346 \approx 2.31; with 3 degrees of freedom, p≈0.10p \approx 0.10.

Answer: SE ≈ 0.35 and t≈2.3t \approx 2.3: suggestive, but five workers are too few for significance at 5%.

Exercise 5 · A dummy regressor

Show that in y=β0+β1T+εy = \beta_0 + \beta_1T + \varepsilon, β^1=yˉ1−yˉ0\hat\beta_1 = \bar y_1 - \bar y_0.

Show solution

Fitted values are group means (Day 20, Exercise 14): β^0=yˉ0\hat\beta_0 = \bar y_0 and β^0+β^1=yˉ1\hat\beta_0 + \hat\beta_1 = \bar y_1. So β^1=yˉ1−yˉ0\hat\beta_1 = \bar y_1 - \bar y_0.

Part 2 · Proofs

Exercise 6 · Unbiasedness

Prove E[β^∣X]=βE[\hat\beta \mid X] = \beta under assumptions 1–2.

Show solution

Step 1. β^=(X⊤X)−1X⊤(Xβ+ε)=β+(X⊤X)−1X⊤ε\hat\beta = (X^{\top}X)^{-1}X^{\top}(X\beta + \varepsilon) = \beta + (X^{\top}X)^{-1}X^{\top}\varepsilon.

Step 2. Conditional on XX, the matrix (X⊤X)−1X⊤(X^{\top}X)^{-1}X^{\top} is fixed, so E[β^∣X]=β+(X⊤X)−1X⊤E[ε∣X]=βE[\hat\beta \mid X] = \beta + (X^{\top}X)^{-1}X^{\top}E[\varepsilon \mid X] = \beta.

Exercise 7 · The variance

Prove Var⁡(β^∣X)=σ2(X⊤X)−1\operatorname{Var}(\hat\beta \mid X) = \sigma^2(X^{\top}X)^{-1} under assumptions 1–3.

Show solution

With A=(X⊤X)−1X⊤A = (X^{\top}X)^{-1}X^{\top}, Day 22 gives Var⁡(Aε∣X)=A(σ2I)A⊤=σ2(X⊤X)−1X⊤X(X⊤X)−1=σ2(X⊤X)−1\operatorname{Var}(A\varepsilon \mid X) = A(\sigma^2I)A^{\top} = \sigma^2(X^{\top}X)^{-1}X^{\top}X(X^{\top}X)^{-1} = \sigma^2(X^{\top}X)^{-1}.

Exercise 8 · Why divide by n − k

Prove E[e⊤e∣X]=σ2(n−k)E[e^{\top}e \mid X] = \sigma^2(n - k).

Show solution

Step 1. e=My=Mεe = My = M\varepsilon (since MX=0MX = 0), so e⊤e=ε⊤Mεe^{\top}e = \varepsilon^{\top}M\varepsilon (MM is symmetric and idempotent).

Step 2. A scalar equals its trace: E[ε⊤Mε]=E[tr⁡(Mεε⊤)]=tr⁡(Mσ2I)=σ2(n−k)E[\varepsilon^{\top}M\varepsilon] = E[\operatorname{tr}(M\varepsilon\varepsilon^{\top})] = \operatorname{tr}(M\sigma^2I) = \sigma^2(n - k) (Day 20).

Exercise 9 · Gauss–Markov

Let β~=Cy\tilde\beta = Cy be any linear unbiased estimator and write C=(X⊤X)−1X⊤+DC = (X^{\top}X)^{-1}X^{\top} + D. Show DX=0DX = 0 and Var⁡(β~)−Var⁡(β^)=σ2DD⊤\operatorname{Var}(\tilde\beta) - \operatorname{Var}(\hat\beta) = \sigma^2DD^{\top} (PSD).

Show solution

Step 1. E[β~∣X]=CXβ=β+DXβE[\tilde\beta \mid X] = CX\beta = \beta + DX\beta for all β\beta requires DX=0DX = 0.

Step 2. Then Var⁡(β~)=σ2CC⊤=σ2[(X⊤X)−1+DX(X⊤X)−1+(X⊤X)−1X⊤D⊤+DD⊤]=σ2(X⊤X)−1+σ2DD⊤\operatorname{Var}(\tilde\beta) = \sigma^2CC^{\top} = \sigma^2[(X^{\top}X)^{-1} + DX(X^{\top}X)^{-1} + (X^{\top}X)^{-1}X^{\top}D^{\top} + DD^{\top}] = \sigma^2(X^{\top}X)^{-1} + \sigma^2DD^{\top}, and DD⊤DD^{\top} is PSD.

Exercise 10 · Back to Cov/Var

Show that the slope from the matrix formula, in a model with an intercept and one regressor, is ∑(xi−xˉ)(yi−yˉ)/∑(xi−xˉ)2\sum(x_i - \bar x)(y_i - \bar y)/\sum(x_i - \bar x)^2.

Show solution

Step 1. For X=(1,x)X = (\mathbf 1, x): (X⊤X)−1=1n∑x2−(∑x)2(∑x2−∑x−∑xn)(X^{\top}X)^{-1} = \dfrac{1}{n\sum x^2 - (\sum x)^2}\begin{pmatrix}\sum x^2 & -\sum x\\-\sum x & n\end{pmatrix}.

Step 2. The slope is the second row times X⊤y=(∑y,∑xy)⊤X^{\top}y = (\sum y, \sum xy)^{\top}:

β^1=n∑xy−∑x∑yn∑x2−(∑x)2.\hat\beta_1 = \frac{n\sum xy - \sum x\sum y}{n\sum x^2 - (\sum x)^2}.

Step 3. Divide top and bottom by nn and use ∑x=nxˉ\sum x = n\bar x, ∑y=nyˉ\sum y = n\bar y: β^1=∑xy−nxˉyˉ∑x2−nxˉ2=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2\hat\beta_1 = \dfrac{\sum xy - n\bar x\bar y}{\sum x^2 - n\bar x^2} = \dfrac{\sum(x_i - \bar x)(y_i - \bar y)}{\sum(x_i - \bar x)^2}.

Step 4. Check with Exercise 1: (38−5⋅2⋅3)/(30−5⋅4)=8/10=0.8(38 - 5 \cdot 2 \cdot 3)/(30 - 5 \cdot 4) = 8/10 = 0.8. ✓

Part 3 · Applied

Exercise 11 · Interactions

A wage regression gives y^=20+3T+2F−1.5 T ⁣× ⁣F\hat y = 20 + 3T + 2F - 1.5\,T\!\times\!F (TT = trained, FF = female). What is the estimated training effect for men and for women?

Show solution

Step 1. Effect of training: ∂y^/∂T=3−1.5F\partial\hat y/\partial T = 3 - 1.5F.

Step 2. Men (F=0F = 0): 3. Women (F=1F = 1): 3−1.5=1.53 - 1.5 = 1.5.

Answer: $30/hour for men and $15/hour for women (wages in $10). The interaction coefficient is the difference in effects; its SE tests whether the effects differ.

Exercise 12 · Omitted ability in matrix form

The true wage model is y=X1β1+X2β2+εy = X_1\beta_1 + X_2\beta_2 + \varepsilon, where X1X_1 holds schooling and experience and X2X_2 holds unmeasured ability, with E[ε∣X]=0E[\varepsilon \mid X] = 0. You regress yy on X1X_1 only. Derive E[β^1short∣X]E[\hat\beta_1^{\text{short}} \mid X].

Show solution

Step 1. Short-regression estimator: β^1short=(X1⊤X1)−1X1⊤y\hat\beta_1^{\text{short}} = (X_1^{\top}X_1)^{-1}X_1^{\top}y.

Step 2. Substitute the true model:

β^1short=β1+(X1⊤X1)−1X1⊤X2β2+(X1⊤X1)−1X1⊤ε.\hat\beta_1^{\text{short}} = \beta_1 + (X_1^{\top}X_1)^{-1}X_1^{\top}X_2\beta_2 + (X_1^{\top}X_1)^{-1}X_1^{\top}\varepsilon.

Step 3. Take expectations given XX; the last term has mean zero.

Answer: E[β^1short∣X]=β1+(X1⊤X1)−1X1⊤X2β2E[\hat\beta_1^{\text{short}} \mid X] = \beta_1 + (X_1^{\top}X_1)^{-1}X_1^{\top}X_2\beta_2: (slopes of ability on schooling and experience) × (effect of ability). The bias vanishes only if β2=0\beta_2 = 0 or X1⊤X2=0X_1^{\top}X_2 = 0.

Exercise 13 · Designing a fertiliser trial for precision

An agronomist has 20 plots to estimate the yield response to fertiliser, and the slope's variance is σ2/∑(xi−xˉ)2\sigma^2/\sum(x_i - \bar x)^2. Compare two designs: (A) half the plots at 0 kg and half at 10 kg; (B) two plots at each dose 0, 1, …, 9 kg. What is the trade-off?

Show solution

Step 1. Design A: xˉ=5\bar x = 5 and every plot is 5 kg from the mean, so ∑(x−xˉ)2=20×25=500\sum(x - \bar x)^2 = 20 \times 25 = 500.

Step 2. Design B: xˉ=4.5\bar x = 4.5; for 0–9 once, ∑(x−4.5)2=82.5\sum(x - 4.5)^2 = 82.5, so twice gives 165.

Step 3. Variance ratio: 500/165≈3500/165 \approx 3.

Answer: design A estimates the slope with about one-third of the variance. But it assumes the response is linear; design B can reveal curvature (e.g. diminishing returns), which design A cannot.

Exercise 14 · Regression adjustment in an experiment

In an RCT, a baseline covariate explains 50% of the outcome variance. By how much does including it shrink the SE of the treatment effect, and what sample-size increase is that worth?

Show solution

Step 1. Adding the covariate cuts the residual variance from σ2\sigma^2 to σ2(1−R2)=0.5σ2\sigma^2(1 - R^2) = 0.5\sigma^2.

Step 2. Randomisation makes TT nearly uncorrelated with the covariate, so the treatment column's partialled-out variance (Day 28) is essentially unchanged.

Step 3. So Var⁡(τ^)\operatorname{Var}(\hat\tau) halves and the SE falls by 0.5≈0.71\sqrt{0.5} \approx 0.71.

Step 4. Halving the variance by sample size alone would need twice the sample.

Answer: about 29% smaller SEs, worth as much as doubling the sample.

Exercise 15 · Correlated errors break the formula

Errors are correlated within villages. Is β^\hat\beta still unbiased? Is s2(X⊤X)−1s^2(X^{\top}X)^{-1} still right?

Show solution

Step 1. Unbiasedness uses only full rank and E[ε∣X]=0E[\varepsilon \mid X] = 0 (Exercise 6), so β^\hat\beta is still unbiased.

Step 2. With Var⁡(ε∣X)=Ω≠σ2I\operatorname{Var}(\varepsilon \mid X) = \Omega \ne \sigma^2I, Day 22's rule gives Var⁡(β^∣X)=(X⊤X)−1X⊤ΩX(X⊤X)−1\operatorname{Var}(\hat\beta \mid X) = (X^{\top}X)^{-1}X^{\top}\Omega X(X^{\top}X)^{-1}.

Step 3. This differs from σ2(X⊤X)−1\sigma^2(X^{\top}X)^{-1}; with positive within-village correlation the classical SEs are usually too small.

Answer: unbiased, but the usual SEs are wrong. Day 29 fixes this with cluster-robust variances.

Verify in Python

import numpy as np
from scipy import stats

x = np.array([0, 1, 2, 3, 4.]); y = np.array([1, 3, 2, 5, 4.])
X = np.column_stack([np.ones(5), x]); n, k = X.shape
XtX_inv = np.linalg.inv(X.T @ X); b = XtX_inv @ X.T @ y
e = y - X @ b; s2 = e @ e / (n - k); V = s2 * XtX_inv
se = np.sqrt(np.diag(V)); t = b / se
print("beta:", b, " residuals:", e.round(2), " s2:", round(s2, 3))
print("SE:", se.round(4), " t:", t.round(3), " p:", (2 * stats.t.sf(np.abs(t), n - k)).round(3))

# Monte Carlo: unbiasedness and the variance formula
rng = np.random.default_rng(27)
draws = np.array([XtX_inv @ X.T @ (X @ [1.4, 0.8] + rng.normal(0, 1.1, n)) for _ in range(50_000)])
print("mean of beta-hat:", draws.mean(0).round(3), " simulated Var(slope):", draws[:, 1].var().round(4),
      " formula:", round(1.1**2 * XtX_inv[1, 1], 4))

Key takeaways

  • β^=β+(X⊤X)−1X⊤ε\hat\beta = \beta + (X^{\top}X)^{-1}X^{\top}\varepsilon is the identity behind every OLS property.
  • Exogeneity gives unbiasedness; spherical errors give the textbook variance and Gauss–Markov.
  • Spreading xx and adding predictive covariates both buy precision.

Simulation Lab: OLS by Matrices

Open full screen →

Simulated data with X′X, (X′X)⁻¹, β̂, s² and standard errors computed live from the matrix formulas.

02.557.50246810xy
dataOLS fittrue line y = 1 + β₁x
X′X = [20.00096.66696.666546]   X′y = [104555]
(X′X)⁻¹ = [0.346-0.061-0.0610.013]
β̂ = (X′X)⁻¹X′y = [2.1510.636]
s²
2.334
true σ² = 2.25
SE(β̂₁)
0.1720
t = β̂₁/SE
3.70
95% CI for β₁: [0.276, 0.996]

Every number on the right is computed from the matrices: β̂ = (X′X)⁻¹X′y, s² = e′e/(n − k), Var(β̂) = s²(X′X)⁻¹. Spread out x or add data and watch (X′X)⁻¹, and with it the standard errors, shrink.

Day 26Day 28