The full academy launches January 2027. Join the waitlist for the $179 founding price.
Waldbauer Academy of Economics

Courses › Graduate Econometrics

Graduate · Module 3 of 15

Least squares in matrix form

The OLS estimator as a projection, the hat and annihilator matrices, the Frisch–Waugh–Lovell theorem, leverage and influence, and the finite-sample theory behind Gauss–Markov.

By Maximus S. Waldbauer · About 5 to 6 hours, including the problem set and quiz.

Module 1 defined the population best linear predictor β = E[XX′]−1E[XY]. Ordinary least squares replaces population moments with sample moments. Written with matrices, the whole theory of OLS fits on a page, and it reveals a geometry that makes many results obvious: OLS is a projection.

This module covers the algebra and finite-sample properties of OLS: the estimator and its residuals, the projection and annihilator matrices, the Frisch–Waugh–Lovell theorem that explains what “controlling for” means, leverage and influence, and unbiasedness, the variance formula and the Gauss–Markov theorem. Module 4 then asks what survives when we drop the strong finite-sample assumptions and rely on large samples instead.

By the end of this module you will be able to

What this module covers

  1. OLS in matrix form
  2. Projection: the hat and annihilator matrices
  3. The Frisch–Waugh–Lovell theorem
  4. Leverage and influence
  5. Finite-sample properties and the Gauss–Markov theorem
  6. Problem set with step-by-step solutions
  7. Module quiz

Lesson 3.1 Free lesson

OLS in matrix form

Stack n observations. The outcome is the n × 1 vector y; the regressors form the n × k matrix X, whose i-th row is xi′ (usually with a first column of ones for the intercept). The linear model is y = Xβ + e. For a candidate b, the sum of squared residuals is

S(b) = (y − Xb)′(y − Xb) = y′y − 2b′X′y + b′X′Xb

Differentiate with the rules from Module 1: ∂S/∂b = −2X′y + 2X′Xb. Setting this to zero gives the sample normal equations X′Xβ̂ = X′y. If X has full column rank k, then X′X is positive definite and invertible, and

β̂ = (X′X)−1X′y = ( (1/n)Σxixi′ )−1 ( (1/n)Σxiyi )

The second form shows OLS is the sample analogue of the population BLP: replace E[XX′] and E[XY] by averages. The second-order condition holds because the Hessian 2X′X is positive definite, so this is a minimum.

Full column rank means no column of X is an exact linear combination of the others. It fails with perfect collinearity (a dummy for every category plus an intercept) or with more regressors than observations (k > n). When it fails, the normal equations have infinitely many solutions.

Worked example: A four-day example

A café records staff on shift x and hundreds of drinks served y on four days: (1, 3), (2, 5), (3, 4), (4, 8). With an intercept, X′X = [[4, 10], [10, 30]] and X′y = (20, 57)′.

det(X′X) = 120 − 100 = 20, so (X′X)−1 = (1/20)[[30, −10], [−10, 4]].

β̂ = (1/20)(30 × 20 − 10 × 57, −10 × 20 + 4 × 57)′ = (1/20)(30, 28)′ = (1.5, 1.4)′.

Fitted values 2.9, 4.3, 5.7, 7.1; residuals 0.1, 0.7, −1.7, 0.9. They sum to zero, and Σxiêi = 0.1 + 1.4 − 5.1 + 3.6 = 0: the normal equations. The sum of squared residuals is 4.2.

Residuals are orthogonal to the regressors

Rewriting the normal equations as X′(y − Xβ̂) = X′ê = 0 says each regressor is orthogonal to the residual vector. With an intercept, the first of these equations says the residuals sum to zero, so the regression line passes through the point of means. These are the sample versions of E[Xu] = 0, and like that condition they hold by construction.

Key ideas

  • β̂ = (X′X)−1X′y solves the normal equations X′Xβ̂ = X′y and exists when X has full column rank.
  • OLS is the sample analogue of the population best linear predictor.
  • X′ê = 0 by construction: residuals are orthogonal to every regressor and sum to zero with an intercept.

Practice questions: Least squares in matrix form

Try these, then open each answer to check your work.

  1. 1.The OLS normal equations are
    1. X′X β̂ = X′y
    2. XX′ β̂ = Xy
    3. X′y β̂ = X′X
    4. y′X β̂ = y′y
    Show answer

    Answer: A. Setting the derivative of (y − Xb)′(y − Xb) to zero gives −2X′y + 2X′Xb = 0, i.e. X′Xβ̂ = X′y.

  2. 2.Which property does the hat matrix P = X(X′X)−1X′ have?
    1. P is invertible
    2. PX = 0
    3. P is symmetric and PP = P
    4. tr(P) = n − k
    Show answer

    Answer: C. P is symmetric and idempotent, PX = X, and tr(P) = k. It is not invertible unless k = n.

  3. 3.With n = 100 and k = 5, what is tr(M)?
    1. 5
    2. 20
    3. 90
    4. 95
    Show answer

    Answer: D. tr(M) = tr(I) − tr(P) = n − k = 95.

Problems with step-by-step solutions

  1. 4.Three observations: x = (0, 1, 2) and y = (1, 2, 6), with an intercept. Compute X′X, X′y and β̂.
    Show step-by-step solution
    1. X′X = [[3, 3], [3, 5]] and X′y = (9, 14)′ (Σy = 9, Σxy = 0 + 2 + 12 = 14).
    2. det = 15 − 9 = 6; (X′X)−1 = (1/6)[[5, −3], [−3, 3]].
    3. β̂ = (1/6)(45 − 42, −27 + 42)′ = (1/6)(3, 15)′ = (0.5, 2.5)′.
  2. 5.For Problem 1, compute the residuals and verify the normal equations.
    Show step-by-step solution
    1. Fitted: 0.5, 3.0, 5.5. Residuals: 0.5, −1.0, 0.5.
    2. Sum: 0. Σxiêi = 0 − 1 + 1 = 0. Both normal equations hold.

Get the full module

The complete module adds every lesson, interactive graphs, a full problem set with step-by-step solutions, and a quiz. The academy opens in January 2027: join the waitlist for the founding price (lifetime access for $179).

Join the waitlist Try a full module free

More practice: free practice questions for this course.