Skip to content

linear regression

learning

Level 2 of 3

Also called linear regression by gradient descent, least squares line fitting and gradient descent.

test yourself

Six questions, four to pick from and two for paper. A point such as 7 6 stands 7 across and 6 up. After the points come the step size and the number of steps. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Section 1's four points, 2 3, 4 2, 6 6 and 8 5. The line starts flat at height 5 with slope 0. After one step its slope is 1. What was the step size?

the answer, and why

the answer is A

At the start the loss's slope for the line's slope is minus 5, as section 1 works out. A step takes the step size times that away, so the slope rises by 5 times the step size. 5 times 0.2 is 1.

this tests how it works, on the learn page

if you chose another

  • At 0.1 the slope rises by 5 times 0.1, to 0.5, the best slope, not to 1.
  • That is the best slope, not a step size. 5 times 0.5 would lift the slope to 2.5.
  • That is the loss's slope for the line's slope at the start, without its minus. The step size multiplies it: 5 times 5 would lift the slope to 25.

question 2

Five points, 1 2, 3 3, 5 4, 7 5 and 9 6, step size 0.06 and 7 steps. No step raises the loss. How many misses are measured in all?

0246810024681001234567
the answer, and why

the answer is B

The line is measured 8 times, once before any step and once after each of the 7. Each measure finds 5 misses.

if you chose another

  • That counts 7 measures, leaving out the one before any step.
  • That is how many times the line is measured, not how many misses.
  • That is one measure only.

question 3

Four points, 1 4, 4 4, 6 4 and 9 4. The line starts flat at height 5. What is the loss before any step?

02468100246810012
the answer, and why

the answer is A

Every miss is 4 minus 5, which is minus 1. Each square is 1, so their mean is 1.

if you chose another

  • That adds the four squares without sharing them among the points.
  • The points lie on one line, but not on this one yet. It starts at height 5.
  • That is one miss. A loss squares it, so it is never below 0.

question 4

Which is true of gradient descent on a line?

the answer, and why

the answer is A

On section 1's four points, step size 0.25 raises the loss from 3.5 to 4.31 at its first step.

this tests best, worst and average, on the learn page

if you chose another

  • On those points 0.2 never settles and 0.25 climbs, while 0.1 lands the slope on its best at once.
  • Each step measures every point's miss.
  • No straight line passes through all four of those points, so even the best line has loss 1.25.

pen and paper

question 5about 10 minutes

Section 1's four points, with the step size 0.2 in place of 0.15.

  1. Work the learn page's question 3 again at step size 0.2: where does one step take the height from hh?
  2. From height 5, write the height after 1, 2 and 3 steps.
  3. What share of its gap to the best height, 4, does each step keep? What share did step size 0.15 keep?
a hint

Take the learn page's question 3, line 2, with 0.2 where it has 0.15.

a worked answer

One step takes hh to N=0.6h+1.6N = 0.6h + 1.6. From 5 that is 4.6, then 4.36, then 4.22, which is 4.216 rounded. The gaps to 4 are 1, 0.6, 0.36 and 0.216: each step keeps 0.6 of the gap. Step size 0.15 kept 0.7, so 0.2 closes on the best height faster. Its trouble is the slope, which section 7 shows.

At step size 0.2, one step takes the height from hh to NN. Where is NN?
  1. the move: step against the slope

    the line:
    N=h−0.2×(2h−8)N = h - 0.2 \times (2h - 8)

    why it is allowed: The learn page's question 3, with 0.2 in place of 0.15. The loss's slope for the height is still 2h−82h - 8.

  2. the move: multiply the bracket

    the line:
    N=h−0.4h+1.6N = h - 0.4h + 1.6

    why it is allowed: 0.2 times 2h2h is 0.4h0.4h, and 0.2 times 8 is 1.6. Taking away a minus adds.

  3. the move: gather the heights

    the line:
    N=0.6h+1.6N = 0.6h + 1.6

    why it is allowed: hh take away 0.4h0.4h leaves 0.6h0.6h.

try it with a small hh: at h=5h = 5, line 1 gives N=4.6N = 4.6, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • N=0.6h+1.6N = 0.6h + 1.6.
  • 4.6, 4.36 and 4.22.
  • 0.6 of the gap, against 0.7 at step size 0.15.

question 6about 8 minutes

Nine points, 7 steps, and a step size under which no step raises the loss.

  1. How many times is the line measured?
  2. Count the misses, and check them against 8n8n.
  3. How many would a run stopped by its first step measure?
a hint

The line is measured before any step, too.

a worked answer

The line is measured once before any step and once after each of the 7, so 8 times. Each measure finds 9 misses, so 72 in all. 8n8n for 9 points is 72. A run whose first step raises the loss is measured twice, before the step and after it: 18 misses, 2n2n.

Question 5: on nn points, a run of 7 steps in which no step raises the loss. How many misses, MM, does it measure?
  1. the move: one measure

    the line:
    each measure of the linefinds n misses\begin{gathered} \text{each measure of the line} \\ \text{finds } n \text{ misses} \end{gathered}

    why it is allowed: To measure the line is to find every point's miss, one for each of the nn points.

  2. the move: count the measures

    the line:
    7 steps make8 measures\begin{gathered} \text{7 steps make} \\ \text{8 measures} \end{gathered}
    measurewhenmisses so far
    1before any step4
    2after step 18
    3after step 212
    8after step 732
    Section 1's four points. Each measure adds 4 misses. The eighth, after step 7, brings them to 32.

    why it is allowed: The line is measured once before any step, to find which way to step, and once after each of the 7 steps.

  3. the move: add them up

    the line:
    M=n+n+⋯+nM = n + n + \dots + n

    why it is allowed: MM is the misses measured. By lines 1 and 2, nn for each of the 8 measures.

  4. the move: eight lots

    the line:
    M=8nM = 8n

    why it is allowed: Eight lots of nn. For the four points, 32.

  5. the move: a ceiling

    the line:
    M≤8nM \le 8n

    why it is allowed: By line 4, MM is 8n8n, so it is at most 8n8n.

  6. the move: read it as a bound

    the line:
    M∈O(n)M \in O(n)

    why it is allowed: By line 5, MM is at most 8 times nn.

  7. the move: a floor

    the line:
    M≥8nM \ge 8n

    why it is allowed: By line 4, MM is at least 8n8n too.

  8. the move: read it as a bound

    the line:
    M∈Ω(n)M \in \Omega(n)

    why it is allowed: By line 7, MM never drops below 8 times nn.

  9. the move: ceiling and floor together

    the line:
    M∈Θ(n)M \in \Theta(n)

    why it is allowed: Lines 6 and 8 bound MM by the same growth, nn.

try it with a small nn: at n=4n = 4, line 3 gives M=32M = 32, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • Eight measures.
  • Seventy-two misses, matching 8n8n.
  • Eighteen misses.