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.
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.
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
| House | Area (m²) | Bedrooms | Price (£k) |
|---|---|---|---|
| 1 | 50 | 1 | 150 |
| 2 | 70 | 2 | 200 |
| 3 | 80 | 2 | 230 |
| 4 | 100 | 3 | 280 |
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
Design matrix shape
2×2 inverse
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.