Steply

Least squares line

Linear Algebra ยท 20 problems, easy to harder

โ–ถ Watch the first one solved

How we do it

  1. One equation per point
  2. Write the normal equations
  3. Work out the two sides
  4. Solve for b and m
  5. Check

The steps for Least squares line through (-1, 2), (0, 1), (1, 6). Every solution names its steps, so you always know what comes next.

Worked examples

Least squares line through (-1, 2), (0, 1), (1, 6) Easy

The question

Question: find the least squares line through (โˆ’1, 2), (0, 1) and (1, 6).

(โˆ’1, 2), (0, 1), (1, 6)

We want a line y = b + m x.

These 3 points are not all on one line, so we find the line that fits best.

line: y = b + m x (b: where it starts, m: slope)

1 One equation per point

Each point gives one equation: put its x and y into y = b + m x.

3 points, 3 equations, but only 2 unknowns, b and m.

(โˆ’1, 2): b + (โˆ’1)m = 2
(0, 1): b + 0m = 1

As a matrix: A has a column of 1s for b and a column of the x values for m.

The right side holds the y values.

No line goes through all 3 points exactly.

[1, โˆ’1; 1, 0; 1, 1] ร— [b; m] = [2; 1; 6]

2 Write the normal equations

The best b and m solve A transpose A, times x, equals A transpose y.

These are called the normal equations.

With them, the leftovers are at a right angle to the columns of A: as small as they can be.

ATA x = ATy

A transpose: the rows of A become its columns.

AT = [1, 1, 1; โˆ’1, 0, 1]

Careful!

A transpose goes in front of A, and in front of y.

Both sides get it.

Careful: Aแต€ in front, on both sides

3 Work out the two sides

A transpose times A.

ATA = [1, 1, 1; โˆ’1, 0, 1] ร— [1, โˆ’1; 1, 0; 1, 1]
ATA = [1 ร— 1 + 1 ร— 1 + 1 ร— 1, 1 ร— (โˆ’1) + 1 ร— 0 + 1 ร— 1; โˆ’1 ร— 1 + 0 ร— 1 + 1 ร— 1, โˆ’1 ร— (โˆ’1) + 0 ร— 0 + 1 ร— 1]

The top left counts the points: 3.

ATA = [3, 0; 0, 2]

A transpose times y.

ATy = [1, 1, 1; โˆ’1, 0, 1] ร— [2; 1; 6]
ATy = [1 ร— 2 + 1 ร— 1 + 1 ร— 6; โˆ’1 ร— 2 + 0 ร— 1 + 1 ร— 6]
ATy = [9; 4]

4 Solve for b and m

The two equations as an augmented matrix: the unknowns are b and m.

= [3, 0, 9; 0, 2, 4]

We call the rows R 1 and R 2.

โ†’ [1, 0, 3; 0, 2, 4]
โ†’ [1, 0, 3; 0, 1, 2]

Row 1 says b = 3.

b = 3

Row 2 says m = 2.

m = 2

So the least squares line is y = 2x + 3.

y = 2x + 3

5 Check

Let's check.

The leftover of a point is its y minus the line's y.

For the best line, the leftovers add to 0, and x times leftover adds to 0.

Each y minus the line's y at that x: 1, โˆ’2 and 1.

leftovers: 2 โˆ’ 1 = 1, 1 โˆ’ 3 = โˆ’2, 6 โˆ’ 5 = 1

Added: 0.

added: 1 + (โˆ’2) + 1 = 0

x times leftover, added: 0.

Both are 0, so the answer is right!

x ร— leftover: โˆ’1 ร— 1 + 0 ร— (โˆ’2) + 1 ร— 1 = 0

Let's look back.

First: one equation per point.

Then: write the normal equations.

Then: work out the two sides.

Then: solve for b and m.

Last: check.

Answer
y = 2x + 3

โ–ถ Watch it solved

Least squares line through (0, 1), (1, 3), (2, 4) Medium

The question

Question: find the least squares line through (0, 1), (1, 3) and (2, 4).

(0, 1), (1, 3), (2, 4)

We want a line y = b + m x.

These 3 points are not all on one line, so we find the line that fits best.

line: y = b + m x (b: where it starts, m: slope)

1 One equation per point

Each point gives one equation: put its x and y into y = b + m x.

3 points, 3 equations, but only 2 unknowns, b and m.

(0, 1): b + 0m = 1
(1, 3): b + 1m = 3

As a matrix: A has a column of 1s for b and a column of the x values for m.

The right side holds the y values.

No line goes through all 3 points exactly.

[1, 0; 1, 1; 1, 2] ร— [b; m] = [1; 3; 4]

2 Write the normal equations

The best b and m solve A transpose A, times x, equals A transpose y.

These are called the normal equations.

With them, the leftovers are at a right angle to the columns of A: as small as they can be.

ATA x = ATy

A transpose: the rows of A become its columns.

AT = [1, 1, 1; 0, 1, 2]

Careful!

A transpose goes in front of A, and in front of y.

Both sides get it.

Careful: Aแต€ in front, on both sides

3 Work out the two sides

A transpose times A.

ATA = [1, 1, 1; 0, 1, 2] ร— [1, 0; 1, 1; 1, 2]
ATA = [1 ร— 1 + 1 ร— 1 + 1 ร— 1, 1 ร— 0 + 1 ร— 1 + 1 ร— 2; 0 ร— 1 + 1 ร— 1 + 2 ร— 1, 0 ร— 0 + 1 ร— 1 + 2 ร— 2]

The top left counts the points: 3.

ATA = [3, 3; 3, 5]

A transpose times y.

ATy = [1, 1, 1; 0, 1, 2] ร— [1; 3; 4]
ATy = [1 ร— 1 + 1 ร— 3 + 1 ร— 4; 0 ร— 1 + 1 ร— 3 + 2 ร— 4]
ATy = [8; 11]

4 Solve for b and m

The two equations as an augmented matrix: the unknowns are b and m.

= [3, 3, 8; 3, 5, 11]

We call the rows R 1 and R 2.

โ†’ [1, 1, 8 over 3; 3, 5, 11]
โ†’ [1, 1, 8 over 3; 0, 2, 3]
โ†’ [1, 1, 8 over 3; 0, 1, 3 over 2]
โ†’ [1, 0, 7 over 6; 0, 1, 3 over 2]

Row 1 says b = 7/6.

b = 76

Row 2 says m = 3/2.

m = 32

So the least squares line is y = 3/2 times x + 7/6.

y = 32x + 76

5 Check

Let's check.

The leftover of a point is its y minus the line's y.

For the best line, the leftovers add to 0, and x times leftover adds to 0.

Each y minus the line's y at that x: โˆ’1/6, 1/3 and โˆ’1/6.

leftovers: 1 โˆ’ 76 = โˆ’16, 3 โˆ’ 83 = 13, 4 โˆ’ 256 = โˆ’16

Added: 0.

added: โˆ’16 + 13 + (โˆ’16) = 0

x times leftover, added: 0.

Both are 0, so the answer is right!

x ร— leftover: 0 ร— (โˆ’16) + 1 ร— 13 + 2 ร— (โˆ’16)
= 0

Let's look back.

First: one equation per point.

Then: write the normal equations.

Then: work out the two sides.

Then: solve for b and m.

Last: check.

Answer
y = 32x + 76

โ–ถ Watch it solved

Least squares line through (-2, 1), (-1, 0), (0, 2), (1, 2), (2, 5) Harder

The question

Question: find the least squares line through (โˆ’2, 1), (โˆ’1, 0), (0, 2), (1, 2) and (2, 5).

(โˆ’2, 1), (โˆ’1, 0), (0, 2), (1, 2), (2, 5)

We want a line y = b + m x.

These 5 points are not all on one line, so we find the line that fits best.

line: y = b + m x (b: where it starts, m: slope)

1 One equation per point

Each point gives one equation: put its x and y into y = b + m x.

5 points, 5 equations, but only 2 unknowns, b and m.

(โˆ’2, 1): b + (โˆ’2)m = 1
(โˆ’1, 0): b + (โˆ’1)m = 0

As a matrix: A has a column of 1s for b and a column of the x values for m.

The right side holds the y values.

No line goes through all 5 points exactly.

[1, โˆ’2; 1, โˆ’1; 1, 0; 1, 1; 1, 2] ร— [b; m] = [1; 0; 2; 2; 5]

2 Write the normal equations

The best b and m solve A transpose A, times x, equals A transpose y.

These are called the normal equations.

With them, the leftovers are at a right angle to the columns of A: as small as they can be.

ATA x = ATy

A transpose: the rows of A become its columns.

AT = [1, 1, 1, 1, 1; โˆ’2, โˆ’1, 0, 1, 2]

Careful!

A transpose goes in front of A, and in front of y.

Both sides get it.

Careful: Aแต€ in front, on both sides

3 Work out the two sides

A transpose times A.

ATA = [1, 1, 1, 1, 1; โˆ’2, โˆ’1, 0, 1, 2] ร— [1, โˆ’2; 1, โˆ’1; 1, 0; 1, 1; 1, 2]
ATA = [1 ร— 1 + 1 ร— 1 + 1 ร— 1 + 1 ร— 1 + 1 ร— 1, 1 ร— (โˆ’2) + 1 ร— (โˆ’1) + 1 ร— 0 + 1 ร— 1 + 1 ร— 2; โˆ’2 ร— 1 + (โˆ’1) ร— 1 + 0 ร— 1 + 1 ร— 1 + 2 ร— 1, โˆ’2 ร— (โˆ’2) + (โˆ’1) ร— (โˆ’1) + 0 ร— 0 + 1 ร— 1 + 2 ร— 2]

The top left counts the points: 5.

ATA = [5, 0; 0, 10]

A transpose times y.

ATy = [1, 1, 1, 1, 1; โˆ’2, โˆ’1, 0, 1, 2] ร— [1; 0; 2; 2; 5]
ATy = [1 ร— 1 + 1 ร— 0 + 1 ร— 2 + 1 ร— 2 + 1 ร— 5; โˆ’2 ร— 1 + (โˆ’1) ร— 0 + 0 ร— 2 + 1 ร— 2 + 2 ร— 5]
ATy = [10; 10]

4 Solve for b and m

The two equations as an augmented matrix: the unknowns are b and m.

= [5, 0, 10; 0, 10, 10]

We call the rows R 1 and R 2.

โ†’ [1, 0, 2; 0, 10, 10]
โ†’ [1, 0, 2; 0, 1, 1]

Row 1 says b = 2.

b = 2

Row 2 says m = 1.

m = 1

So the least squares line is y = x + 2.

y = x + 2

5 Check

Let's check.

The leftover of a point is its y minus the line's y.

For the best line, the leftovers add to 0, and x times leftover adds to 0.

Each y minus the line's y at that x: 1, โˆ’1, 0, โˆ’1 and 1.

leftovers: 1 โˆ’ 0 = 1, 0 โˆ’ 1 = โˆ’1, 2 โˆ’ 2 = 0, 2 โˆ’ 3 = โˆ’1, 5 โˆ’ 4 = 1

Added: 0.

added: 1 + (โˆ’1) + 0 + (โˆ’1) + 1 = 0

x times leftover, added: 0.

Both are 0, so the answer is right!

x ร— leftover: โˆ’2 ร— 1 + โˆ’1 ร— (โˆ’1) + 0 ร— 0 + 1 ร— (โˆ’1) + 2 ร— 1
= 0

Let's look back.

First: one equation per point.

Then: write the normal equations.

Then: work out the two sides.

Then: solve for b and m.

Last: check.

Answer
y = x + 2

โ–ถ Watch it solved

Practice

Try each one, then open the answer. Stuck? Open the problem to watch every step.

Easy

  1. least squares line through (-1, 2), (0, 1), (1, 6)
    Answer
    y = 2x + 3
  2. least squares line through (-1, 3), (0, 1), (1, 5)
    Answer
    y = x + 3
  3. least squares line through (-1, 1), (0, 1), (1, 4)
    Answer
    y = 32x + 2
  4. least squares line through (-1, 0), (0, 3), (1, 3)
    Answer
    y = 32x + 2
  5. least squares line through (-1, 4), (0, 2), (1, 3)
    Answer
    y = โˆ’12x + 3
  6. least squares line through (-1, 1), (0, 0), (1, 5)
    Answer
    y = 2x + 2

Medium

  1. least squares line through (0, 1), (1, 3), (2, 4)
    Answer
    y = 32x + 76
  2. least squares line through (0, 0), (1, 2), (2, 2)
    Answer
    y = x + 13
  3. least squares line through (0, 1), (1, 1), (2, 3)
    Answer
    y = x + 23
  4. least squares line through (-1, 5), (0, 2), (1, 2)
    Answer
    y = โˆ’32x + 3
  5. least squares line through (1, 2), (2, 2), (3, 5)
    Answer
    y = 32x
  6. least squares line through (0, 3), (1, 1), (2, 2)
    Answer
    y = โˆ’12x + 52
  7. least squares line through (1, 1), (2, 3), (3, 3)
    Answer
    y = x + 13
  8. least squares line through (0, 2), (1, 0), (2, 1)
    Answer
    y = โˆ’12x + 32

Harder

  1. least squares line through (-2, 1), (-1, 0), (0, 2), (1, 2), (2, 5)
    Answer
    y = x + 2
  2. least squares line through (0, 1), (1, 2), (2, 2), (3, 4)
    Answer
    y = 910x + 910
  3. least squares line through (-2, 0), (-1, 1), (0, 1), (1, 3), (2, 5)
    Answer
    y = 65x + 2
  4. least squares line through (-2, 4), (-1, 3), (0, 3), (1, 1), (2, 0)
    Answer
    y = โˆ’x + 115
  5. least squares line through (-1, 0), (0, 1), (1, 1), (2, 4)
    Answer
    y = 65x + 910
  6. least squares line through (-1, 2), (0, 0), (1, 1)
    Answer
    y = โˆ’12x + 1