Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Learning from Data

Module 3 · Lesson 2 · Core

Least squares: how the line is chosen

Fit a line to a small table by hand by minimising the total squared error.

  • 50 minutes
  • Serves O3
  • Assumes M3.L1

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.

Guessa lineErrorsactual − predictedSquare and addtotalKeepthe smallest total
Least squares: try a line, measure the squared errors, and keep the line that makes their total smallest.

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 xMark yx − 3y − 4product(x − 3)²
12−2−244
24−1001
350100
441001
552124
total610

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.

  1. Means: x̄ = 2.5 and ȳ = 6. (1 mark)
  2. 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)
  3. Slope = 11 ÷ 5 = 2.2; intercept = 6 − 2.2 × 2.5 = 0.5. (1 mark)
  4. Answer: y = 0.5 + 2.2x, which gives 6 at x = 2.5 as it should. (1 mark)

Go deeper: the professor's notes

Least squares picks the line with the smallest total squared error, and it passes through the means.
Deviation

From the mean.

Slope

Ratio of sums.

Check

Mean point.

Meansx and yDeviationsfrom meansSumsproducts and squaresSlopethen intercept
Least squares

Revision

HoursMark
12
35
55

At work: Revision and marks

Five students, slope 0.6.

  1. Find the means.
  2. 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:

2. The intercept is found from:

3. A least-squares line always passes through:

4. If Σ(x − x̄)(y − ȳ) = 8 and Σ(x − x̄)² = 4 the slope is:

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

MistakeWhy it fails
Forgetting to subtract the meansWork with deviations x − x̄ and y − ȳ.
Computing the intercept before the slopeThe intercept needs the slope.
Skipping the check at the meansIt catches most arithmetic slips.

Limits. This formula is for one feature; with several features software does the same job.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What are the parameters of a line model?
achosenbchosenDatadecide
Parameters

The two numbers a and b. Fitting the model means choosing them from data, for example by least squares.

Card 2 of 3 · What does least squares do?
Errorsactual − lineSquareand addSmallestwins
Least squares

It chooses the line with the smallest sum of squared errors, so there is one agreed best line instead of many drawn by eye.

Card 3 of 3 · How do you compute the slope by hand?
Top6Bottom10Slope0.6
Slope formula

Sum of (x − mean x)(y − mean y) divided by the sum of (x − mean x) squared. With sums 6 and 10 the slope is 0.6.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards