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.
- ●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.
Theory
The model and the estimator
Assumptions (the classical set):
- Full rank: has rank (Day 18).
- Exogeneity: .
- Spherical errors: (homoskedastic, uncorrelated).
Properties
Substituting the model into the estimator gives the key identity
- Under 1–2, (unbiased).
- Under 1–3, .
- is unbiased for ; standard errors are the square roots of the diagonal of , and .
- 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 .
Omitted-variable bias, in matrices
If the truth is but you regress on only,
Reading coefficients
- Dummy: difference in conditional means between the groups.
- Interaction : how the effect of differs between groups.
- Log outcome: approximately a proportional effect (Day 23 for the exact conversion).
Part 1 · Warm-up
Data: five workers with years of experience earn (in $10/hour). Regress wages on an intercept and experience.
Exercise 1 · The estimator
Compute , and .
Show solution
Step 1. Sums: , , , , .
Step 2. Cross-products:
Step 3. , so .
Step 4. .
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 and , and check .
Show solution
Step 1. Fitted values : .
Step 2. Residuals : .
Step 3. and . ✓
Exercise 3 · The error variance
Compute .
Show solution
Step 1. .
Step 2. Degrees of freedom .
Answer: .
Exercise 4 · Standard errors and t
Compute , the SE of the slope and its t-statistic.
Show solution
Step 1. .
Step 2. SE of the slope .
Step 3. ; with 3 degrees of freedom, .
Answer: SE ≈ 0.35 and : suggestive, but five workers are too few for significance at 5%.
Exercise 5 · A dummy regressor
Show that in , .
Show solution
Fitted values are group means (Day 20, Exercise 14): and . So .
Part 2 · Proofs
Exercise 6 · Unbiasedness
Prove under assumptions 1–2.
Show solution
Step 1. .
Step 2. Conditional on , the matrix is fixed, so .
Exercise 7 · The variance
Prove under assumptions 1–3.
Show solution
With , Day 22 gives .
Exercise 8 · Why divide by n − k
Prove .
Show solution
Step 1. (since ), so ( is symmetric and idempotent).
Step 2. A scalar equals its trace: (Day 20).
Exercise 9 · Gauss–Markov
Let be any linear unbiased estimator and write . Show and (PSD).
Show solution
Step 1. for all requires .
Step 2. Then , and 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 .
Show solution
Step 1. For : .
Step 2. The slope is the second row times :
Step 3. Divide top and bottom by and use , : .
Step 4. Check with Exercise 1: . ✓
Part 3 · Applied
Exercise 11 · Interactions
A wage regression gives ( = trained, = female). What is the estimated training effect for men and for women?
Show solution
Step 1. Effect of training: .
Step 2. Men (): 3. Women (): .
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 , where holds schooling and experience and holds unmeasured ability, with . You regress on only. Derive .
Show solution
Step 1. Short-regression estimator: .
Step 2. Substitute the true model:
Step 3. Take expectations given ; the last term has mean zero.
Answer: : (slopes of ability on schooling and experience) × (effect of ability). The bias vanishes only if or .
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 . 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: and every plot is 5 kg from the mean, so .
Step 2. Design B: ; for 0–9 once, , so twice gives 165.
Step 3. Variance ratio: .
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 to .
Step 2. Randomisation makes nearly uncorrelated with the covariate, so the treatment column's partialled-out variance (Day 28) is essentially unchanged.
Step 3. So halves and the SE falls by .
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 still unbiased? Is still right?
Show solution
Step 1. Unbiasedness uses only full rank and (Exercise 6), so is still unbiased.
Step 2. With , Day 22's rule gives .
Step 3. This differs from ; 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
- is the identity behind every OLS property.
- Exogeneity gives unbiasedness; spherical errors give the textbook variance and Gauss–Markov.
- Spreading 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.
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.