Lesson 3.3 · 11 min read · about 1 h to master at a very slow pace

Ordinary Least Squares (OLS)

OLS picks the line that minimises the residual sum of squares. Residuals and RSS, the design matrix and its dimensions, the normal equations, the closed-form solution, and when it breaks.

Not startedJump to quiz

The problem we're solving

Many lines could pass through a cloud of points. Which is "best"? OLS's answer: the one with the smallest total squared vertical distance to the points.

024612345xy
OLS line ŷ = 2.2 + 0.6xResiduals (vertical gaps)
Dataset B: five points. The dashed lines are residuals. OLS makes the sum of their squares as small as possible.

Residuals and RSS

For observation , the residual is observed minus fitted: . OLS chooses coefficients to minimise the residual sum of squares:

This is exactly ERM with squared loss (lesson 2.2).

Why squares?

Raw residuals would cancel out (positives and negatives). Absolute values are awkward to minimise. Squares are smooth, so calculus gives a closed-form answer, and they punish big misses more.

The design matrix

Stack the data into matrices. With observations and predictors, the design matrix has rows and columns: a first column of ones (for the intercept), then one column per predictor.

Then all the fitted values at once are , and .

Example: area, bedrooms, price

HouseArea (m²)BedroomsPrice (£k)
1501150
2702200
3802230
41003280

Price is the response, so it goes in , not . Two predictors means :

Three coefficients: intercept, area, bedrooms. One coefficient per column of .

The normal equations

Setting the derivative of RSS to zero gives the normal equations, and if is invertible, the closed-form solution:

OLS solution

Shapes: is ; is ; so is .

Show the derivation (optional)

Expand . The gradient with respect to is . Setting it to zero gives . Because RSS is a convex bowl in , this stationary point is the minimum.

Worked example via matrices

Data . Design matrix rows .

For a matrix the inverse is . Here :

So , and every residual is zero (the points lie exactly on the line).

When it breaks

doesn't exist when is singular:

  • Perfect multicollinearity: one predictor is an exact linear combination of others (e.g. height in cm and height in inches).
  • More coefficients than observations: .

If is nearly singular, the solution exists but is unstable (lesson 3.9). Ridge regression (5.4) fixes both cases.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passThe data have Area, Bedrooms and Price. Why does the coefficient vector have three entries, not four?

Count columns of the design matrix, not columns of the spreadsheet. Price is the response, so it goes into , not into . The predictors are Area and Bedrooms (), plus the column of ones for the intercept: columns, so . There's one coefficient per column of , and no .

First passI find the OLS matrix derivation slow. What do I actually need to understand?

Keep these, which carry most of the meaning:

  • and what each piece is (with its dimensions).
  • Residual vector , and RSS = sum of squared residuals.
  • OLS chooses to minimise RSS.
  • The result: normal equations , solution , and that it fails when is singular.

The step-by-step calculus (expanding the quadratic and differentiating) can be deferred until you need it. It is collapsed above under "Show the derivation".

Cheatsheet

RSS

.

OLS normal equations

(if invertible).

Design matrix shape

: , first column ones. : . : .

2×2 inverse

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateA regression has observations and predictors, plus an intercept. What are the dimensions of the design matrix ?
Q2conceptWhen does fail to exist?
Q3conceptWhat does ordinary least squares minimise?
Q4calculateFor the data , with an intercept, what is ?
Q5diagnoseA model includes both "height in cm" and "height in inches" as predictors. What happens when you fit it with OLS?