Phase 1: Prepare
Step 1 Warm-up recall
From M3.L1: what does the slope of a line tell you?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- state what least squares minimises (Foundation);
- compute the slope and intercept by hand (Core);
- check that the line passes through the means (Core).
Phase 2: Teach
Step 3 A familiar example
A teacher plots hours of revision against test marks for five students and draws a line through them by eye. Two teachers would draw two lines. Least squares gives one agreed line.
Step 4 Clear explanation
Many lines could be drawn through a cloud of points. Least squares chooses the one with the smallest sum of squared errors, where each error is the gap between an actual y and the line's prediction.
For one feature the best line has a closed formula. Write x̄ and ȳ for the means. Then slope b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² and intercept a = ȳ − b x̄. In the invented table below, five students revised for 1 to 5 hours a week and scored 2, 4, 5, 4 and 5 out of 10. The means are x̄ = 3 and ȳ = 4.
| Hours x | Mark y | x − 3 | y − 4 | product | (x − 3)² |
|---|---|---|---|---|---|
| 1 | 2 | −2 | −2 | 4 | 4 |
| 2 | 4 | −1 | 0 | 0 | 1 |
| 3 | 5 | 0 | 1 | 0 | 0 |
| 4 | 4 | 1 | 0 | 0 | 1 |
| 5 | 5 | 2 | 1 | 2 | 4 |
| total | 6 | 10 |
The slope is 6 ÷ 10 = 0.6. The intercept is 4 − 0.6 × 3 = 2.2. The fitted line is y = 2.2 + 0.6x: each extra hour of weekly revision goes with about 0.6 more marks.
Check: at x = 3 the line gives 2.2 + 1.8 = 4.0, which is the mean of y. A least-squares line always passes through the point (x̄, ȳ), and that is a useful test of your arithmetic.
Step 5 Worked example (4 marks)
Four points have x = 1, 2, 3, 4 and y = 3, 5, 6, 10. Find the least-squares line.
- Means: x̄ = 2.5 and ȳ = 6. (1 mark)
- Products of deviations: (−1.5)(−3) = 4.5, (−0.5)(−1) = 0.5, (0.5)(0) = 0, (1.5)(4) = 6; total 11. Squares of x deviations: 2.25, 0.25, 0.25, 2.25; total 5. (1 mark)
- Slope = 11 ÷ 5 = 2.2; intercept = 6 − 2.2 × 2.5 = 0.5. (1 mark)
- Answer: y = 0.5 + 2.2x, which gives 6 at x = 2.5 as it should. (1 mark)
Go deeper: the professor's notes
From the mean.
Ratio of sums.
Mean point.
Revision
| Hours | Mark |
|---|---|
| 1 | 2 |
| 3 | 5 |
| 5 | 5 |
At work: Revision and marks
Five students, slope 0.6.
- Find the means.
- Compute the slope then the intercept.
What a careful practitioner decides. Check that the line gives the mean of y at the mean of x.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. Least squares chooses the line that minimises:
Total squared error.
2. The intercept is found from:
a = ȳ − b x̄.
3. A least-squares line always passes through:
(x̄, ȳ).
4. If Σ(x − x̄)(y − ȳ) = 8 and Σ(x − x̄)² = 4 the slope is:
8 ÷ 4.
Phase 4: Close
Step 7 Recap
- Least squares minimises the total squared error.
- Slope = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²; intercept = ȳ − b x̄.
- The line passes through the means, which checks the arithmetic.
Step 8 Exit ticket (ungraded)
x = 1, 2, 3 and y = 2, 4, 6. Find the slope and intercept.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Try a different slope by hand on the table above and show that the total squared error is larger than 2.4.
In industry: Anscombe: one point can decide the line
In the fourth of Anscombe data sets, one high-leverage point is enough to produce a high correlation although the other points show no relationship between the variables. In another set, a single outlier offsets the line and lowers the correlation from 1 to 0.816.
Link to this lesson. Squared-error fitting gives a single far-out point great influence, so check outliers.
Source: Anscombe quartet, Wikipedia, citing Anscombe (1973)
Common mistakes
| Mistake | Why it fails |
|---|---|
| Forgetting to subtract the means | Work with deviations x − x̄ and y − ȳ. |
| Computing the intercept before the slope | The intercept needs the slope. |
| Skipping the check at the means | It catches most arithmetic slips. |
Limits. This formula is for one feature; with several features software does the same job.