linear regression
Level 2 of 3
Also called linear regression by gradient descent, least squares line fitting and gradient descent.
how it works
The idea in a few sentences and three pictures, then one fit played step by step.
Linear regression fits a straight line to points, so you can guess a height for a new across. A miss is a point's height minus the line's height there. The loss is the mean of the squared misses, the error and loss page's mean squared error. A smaller loss is a better fit. Gradient descent finds the line by walking downhill on the loss, as the gradient page walks down a curve. The line has two settings: its height at the middle of the points, and its slope. For each setting the loss has a slope of its own: how fast the loss rises as that setting grows. Start flat. Then repeat one step: measure every miss, find the loss's slope for each setting, and take the step size times that slope away from the setting. Take the points 2 3, 4 2, 6 6 and 8 5, each written across then up. Their middle across is 5. The line always starts flat at height 5, halfway up the plane, which runs from 0 to 10. The misses are 3 minus 5, 2 minus 5, 6 minus 5 and 5 minus 5: minus 2, minus 3, 1 and 0. Their squares, 4, 9, 1 and 0, have mean 3.5. That is the loss. The loss's slope for the height is minus twice the mean miss. The misses add to minus 4, so their mean is minus 1 and the slope is 2: raising the line raises the loss. The loss's slope for the line's slope is minus twice the mean of each miss times its distance across from the middle. Those are minus 2 times minus 3, minus 3 times minus 1, 1 times 1 and 0 times 3: 6, 3, 1 and 0, mean 2.5. So that slope is minus 5: turning the line upwards lowers the loss. Questions 1 to 4 below work out both rules. With step size 0.15, the height drops 0.15 times 2, to 4.7. The slope rises 0.15 times 5, to 0.75. Above 2, which is 3 left of the middle, the new line stands at 4.7 take away 3 times 0.75, which is 2.45. The loss falls from 3.5 to 2.05. After 7 steps it is 1.26. The best line stands at height 4 with slope 0.5, and its loss is 1.25, as section 7 draws. Each measure finds every point's miss once, so n points cost n misses a measure.
After 7 steps at step size 0.15, the loss is 1.26. It started at 3.5. The line stands at height 4.08 with slope 0.5.
With scripts on, one slider sets the step size and another the steps. The three drawings and this line change with them.
| steps taken | height | slope | loss |
|---|---|---|---|
| 0 | 5 | 0 | 3.5 |
| 1 | 4.7 | 0.75 | 2.05 |
| 2 | 4.49 | 0.38 | 1.57 |
| 3 | 4.34 | 0.56 | 1.39 |
| 4 | 4.24 | 0.47 | 1.31 |
| 5 | 4.17 | 0.52 | 1.28 |
| 6 | 4.12 | 0.49 | 1.26 |
| 7 | 4.08 | 0.5 | 1.26 |
step 1 of 23
4 points. The line starts flat at height 5, with a cross on it at the middle of the points, 5 across. Your step size is 0.15. On these points, no step size under 0.2 can make the line fit worse.
what it remembers to know where it is
- step
- not yet
- height at the cross
- 5
- slope
- 0
- loss
- not yet
Where it says not yet, nothing is held there at that moment.
step 2 of 23
Step 1. Each band is a miss: a point's height minus the line's height there. The longest is 3. Square every miss and average them. That one number is the loss, 3.5.
what it remembers to know where it is
- step
- 1
- height at the cross
- 5
- slope
- 0
- loss
- 3.5
step 3 of 23
How fast does the loss rise as the line lifts, or as it tilts up? Those are the loss's two slopes: 2 for the line's height and -5 for its slope. A minus number means that way lowers the loss.
what it remembers to know where it is
- step
- 1
- height at the cross
- 5
- slope
- 0
- loss
- 3.5
step 4 of 23
Take 0.15 times each of those away. The line's height changes by -0.3 to 4.7, moving the cross, and its slope changes by 0.75 to 0.75, turning the line about the cross.
what it remembers to know where it is
- step
- 1
- height at the cross
- 4.7
- slope
- 0.75
- loss
- not yet
Where it says not yet, nothing is held there at that moment.
step 5 of 23
Step 2. Measure every miss again: the longest is now 1.95. The loss was 3.5 and is now 2.05.
what it remembers to know where it is
- step
- 2
- height at the cross
- 4.7
- slope
- 0.75
- loss
- 2.05
step 7 of 23
Take 0.15 times each away again. The height changes by -0.21 to 4.49, and the slope by -0.37 to 0.38.
what it remembers to know where it is
- step
- 2
- height at the cross
- 4.49
- slope
- 0.38
- loss
- not yet
Where it says not yet, nothing is held there at that moment.
step 23 of 23
The steps are done: 7 steps. The loss started at 3.5 and is now 1.26.
what it remembers to know where it is
- step
- 7
- height at the cross
- 4.08
- slope
- 0.5
- loss
- 1.26
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- start with a flat linePython lines 10, 11, 12 and 13
- repeat for each stepPython line 14
- measure every miss, and the lossPython lines 15 and 16
- find the loss's slope for the height and for the slopePython lines 20 and 21
- take the step size times each awayPython lines 22 and 23
- until the steps are done, or the loss risesPython lines 17, 18, 19 and 24
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
def linear_regression(points, rate, steps):
"""The line the steps end on: its height at the middle of the points, and its slope.
points are (across, up) tuples. The line starts flat at height 5,
halfway up a plane of 0 to 10. Each step measures every miss, a
point's height less the line's height there, finds the loss's slope
for the height and for the slope, and takes rate times each away.
It stops when the steps are done, or when a step left the loss higher.
"""
n = len(points)
middle = sum(x for x, _ in points) / n
height, slope = 5, 0 # start with a flat line
before = None
for taken in range(steps + 1): # once before any step, and once after each
misses = [y - (height + slope * (x - middle)) for x, y in points] # n misses
loss = sum(m * m for m in misses) / n # the mean of their squares
if taken == steps or (before is not None and loss > before + 1e-9):
break # the steps are done, or the loss rose by more than a rounding slip
before = loss
for_height = -2 * sum(misses) / n # the loss's slope for the height
for_slope = -2 * sum(m * (x - middle) for m, (x, _) in zip(misses, points)) / n # and for the slope
height -= rate * for_height # step against each
slope -= rate * for_slope
return height, slope
how it scales
How the misses grow, from ten points to ten thousand.
Each step across or up is ten times the one before, a log-log plot. Both lines climb one step up for each step across: ten times the points, ten times the misses. The run that takes all 7 steps sits four times higher than the run that stops at once, against .
misses measured, ten times more at each mark up
, how many points there are, ten times more at each mark to the right
- the same points with a step size small enough that every step lowers the loss, so all 7 steps are takensolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
- points on one climbing line with the step size 0.3, too big for them, so the first step raises the loss and the run stopssolid where measured by running it; dashed where its formula carries it on to ten million
| misses measured | time | |
|---|---|---|
| 1,000 | 8,000 | about 8 microseconds |
| 100,000 | 800,000 | about 800 microseconds |
| 10,000,000 | 80,000,000 | about 80 milliseconds |
The misses measured are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count linear regression's misses yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each n it stands n points on one
climbing line and fits them twice, with 7 steps each time: once with the
step size 0.3, too big for them, so the first step raises the loss and the
run stops, and once with a step size small enough that every step lowers
the loss. It prints how many misses each run measures, up to n = 500, in
under a second. The counts are the same on any computer.
"""
def linear_regression(points, rate, steps):
"""The line the steps end on: its height at the middle of the points, and its slope.
points are (across, up) tuples. The line starts flat at height 5,
halfway up a plane of 0 to 10. Each step measures every miss, a
point's height less the line's height there, finds the loss's slope
for the height and for the slope, and takes rate times each away.
It stops when the steps are done, or when a step left the loss higher.
"""
n = len(points)
middle = sum(x for x, _ in points) / n
height, slope = 5, 0 # start with a flat line
before = None
for taken in range(steps + 1): # once before any step, and once after each
misses = [y - (height + slope * (x - middle)) for x, y in points] # n misses
loss = sum(m * m for m in misses) / n # the mean of their squares
if taken == steps or (before is not None and loss > before + 1e-9):
break # the steps are done, or the loss rose by more than a rounding slip
before = loss
for_height = -2 * sum(misses) / n # the loss's slope for the height
for_slope = -2 * sum(m * (x - middle) for m, (x, _) in zip(misses, points)) / n # and for the slope
height -= rate * for_height # step against each
slope -= rate * for_slope
return height, slope
def count(points, rate, steps):
"""The same method, counting each miss it measures."""
n = len(points)
middle = sum(x for x, _ in points) / n
height, slope = 5, 0
before = None
measured = 0
for taken in range(steps + 1):
misses = [y - (height + slope * (x - middle)) for x, y in points]
measured += n # one miss for each point
loss = sum(m * m for m in misses) / n
if taken == steps or (before is not None and loss > before + 1e-9):
break
before = loss
for_height = -2 * sum(misses) / n
for_slope = -2 * sum(m * (x - middle) for m, (x, _) in zip(misses, points)) / n
height -= rate * for_height
slope -= rate * for_slope
return measured
def climbing(n):
"""n points on one climbing line, each as far up as across.
Up to 11 points they run from 0 0 to 10 10 on whole spots; past 11
they stand 1 apart.
"""
if n <= 11:
across = [0 if n == 1 else (10 * i) // (n - 1) for i in range(n)]
else:
across = list(range(n))
return [(x, x) for x in across]
def cheapest(n):
"""The climbing points, the step size 0.3, too big for them, and 7 steps.
One point alone has no spread across, so no step size raises its
loss: its cheapest run asks for the fewest steps, 1.
"""
return climbing(n), 0.3, 1 if n == 1 else 7
def dearest(n):
"""The same points with a step size that lowers the loss at every step."""
points = climbing(n)
middle = sum(x for x, _ in points) / n
spread = sum((x - middle) ** 2 for x, _ in points) / n # how far they spread across
if spread == 0:
rate = 0.3
elif n <= 11:
rate = min(0.3, max(0.01, int(75 / spread) / 100))
else:
rate = 0.75 / spread
return points, rate, 7
if __name__ == "__main__":
print(f"{'n':>6} {'stopped at once':>16} {'all 7 steps':>12}")
for n in (10, 20, 50, 100, 200, 500):
print(f"{n:>6} {count(*cheapest(n)):>16} {count(*dearest(n)):>12}")
best, worst and average
A run costs n misses each time it measures the line. How many times depends on the steps asked for, the step size and where the points stand. Best means cheapest here, not the best fit.
A case is one kind of input, the thing the method is given, such as a set of points with a step size. A bound says how fast that case's cost grows as more points come in.
The finished run's count is in the grid: 8 measures of n misses each, 8n, so all three bounds grow as n.
| bestpoints on one climbing line with the step size 0.3, too big for them, so the first step raises the loss and the run stops | worstthe same points with a step size small enough that every step lowers the loss, so all 7 steps are taken | |
|---|---|---|
| misses measured | ||
| how the misses measured grow in each case | ||
| tight, | ||
| ceiling, | ||
| floor, | ||
extra memory: the line's two settings, the last loss, and the n misses it is measuring now
Extra memory is the room it needs on top of the input itself, while it runs.
| points | misses, stopped after step 1 | steps, stopped after step 1 | misses, all 7 steps | steps, all 7 steps |
|---|---|---|---|---|
| 10 | 20 | 1 | 80 | 7 |
| 100 | 200 | 1 | 800 | 7 |
| 1,000 | 2,000 | 1 | 8,000 | 7 |
| 10,000 | 20,000 | 1 | 80,000 | 7 |
where the formula comes from
The two slopes, one step for each setting, and the counts, worked out in small steps, each naming the line it comes from.
the move: the rise over the distance across
the line:why it is allowed: The miss is 3 minus the line's height, and its square is the point's part. Look 1 either side of , as the gradient page does. The rise is the square at take away the square at , over 2 across. For a square, the slope comes out the same however far either side you look, by the gradient page's question 2.
the move: take each height from 3
the line:why it is allowed: 3 take away is . 3 take away is .
guess it, thenthe move: multiply out each square
the line:times 2 minus h 2 4 minus 2 times h minus h minus 2 times h h squared 2 minus h times itself, box by box. 4, two boxes of minus 2 times h, and h squared. why it is allowed: times itself is 4, take away four lots of , plus . times itself is 16, take away eight lots of , plus .
guess it, thenthe move: take the second bracket away
the line:why it is allowed: The cancel. 4 take away 16 is minus 12. Minus four lots of , take away minus eight lots, is four lots.
guess it, thenthe move: halve
the line:why it is allowed: Half of four lots is two lots, and half of 12 is 6. That is minus twice the miss: the miss is 3 take away , and minus 2 times it is . With at 5 the miss is minus 2, and this point's part has slope 4. The loss's own slope, 2 in section 1, is the mean of the four parts' slopes, as question 3 shows.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: the point's miss
the line:The point 2 3 and the line turned to slope 1 about its cross at 5 5. Above 2 the line stands at 2, so the miss is 1. That is 3 times 1, take away 2. why it is allowed: Above 2 the line stands at 5 plus times minus 3, which is . The miss is the point's height, 3, take away that.
the move: the rise over the distance across
the line:why it is allowed: Look 1 either side of , as in question 1. By line 1, at the miss is , and at it is . The rise is one square take away the other, over 2 across.
guess it, thenthe move: multiply out the first square
the line:why it is allowed: times itself is , two lots of , and 1.
guess it, thenthe move: multiply out the second square
the line:why it is allowed: times itself is , take away ten lots of , plus 25.
guess it, thenthe move: take the second square away
the line:why it is allowed: By lines 3 and 4, the cancel. Six lots of , take away minus thirty lots, is 36 lots. 1 take away 25 is minus 24.
guess it, thenthe move: halve
the line:why it is allowed: That is minus twice the miss times the point's distance across. The miss is and the distance is minus 3. Minus 2 times minus 3 is 6, and 6 lots of is . With at 0 the slope is minus 12.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: every point the same way
the line:point miss slope of its square at h = 5 2 3 3 - h 2h - 6 4 4 2 2 - h 2h - 4 6 6 6 6 - h 2h - 12 -2 8 5 5 - h 2h - 10 0 mean 4 - h 2h - 8 2 Each point's miss under a flat line at height h, and the slope of its square. Their mean is 2h minus 8, minus twice the mean miss. At h = 5 it is 2. why it is allowed: Question 1 worked one point's part, . Each other point is worked the same way, as the table shows. The loss is the mean of the four parts, so its rise over 2 across, as in question 1, is the mean of their four rises. So its slope is the mean of their slopes. A sloping line changes nothing here. Each miss shifts by the line's slope times its point's distance across, and the distances, minus 3, minus 1, 1 and 3, add to 0. So the mean miss, and so this slope, stays the same.
the move: step against the slope
the line:The loss for a flat line at each height from 1 to 7. The marker stands at height 5, where the line starts. From height 4 to height 6 the loss rises from 2.5 to 6.5: 4 up over 2 across, a slope of 2 at height 5. A step goes the other way, down towards the lowest point, at 4. why it is allowed: A step takes the step size times the slope away from the setting. By line 1's table, the loss's slope for the height is .
guess it, thenthe move: multiply the bracket
the line:why it is allowed: 0.15 times is , and 0.15 times 8 is 1.2. Taking away a minus adds.
guess it, thenthe move: gather the heights
the line:why it is allowed: take away leaves . With at 4 the slope is 0, so the step moves nothing: it gives 4, and the best height stays put. From 5 it gives 4.7, then 4.49: each step keeps 0.7 of the gap to 4. Seven 0.7s multiplied together make about 0.08, so after 7 steps the height is 4.08.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: every point the same way
the line:point across from the middle slope of its square at m = 0 2 3 -3 18m - 12 -12 4 2 -1 2m - 6 -6 6 6 1 2m - 2 -2 8 5 3 18m 0 mean 0 10m - 5 -5 Each point's distance across from the middle, and the slope of its squared miss as the line turns. Their mean is 10m minus 5. At m = 0 it is minus 5. why it is allowed: Question 2 worked one point's part, . Each other part is worked the same way, with its own distance across. As in question 3, the loss's slope is the mean of the parts' slopes. The cross's height changes nothing here. Raising it by 1 takes 1 off every miss, so each miss times its distance drops by that distance. The distances add to 0, so the mean of those products, and so this slope, stays the same.
the move: step against the slope
the line:why it is allowed: The same rule as question 3. By line 1's table, the loss's slope for the line's slope is .
guess it, thenthe move: multiply the bracket
the line:step size one step turns m into the gap to 0.5 is multiplied by 0.05 0.5m + 0.25 0.5 0.1 0.5 0 0.15 0.75 - 0.5m -0.5 0.2 1 - m -1 0.25 1.25 - 1.5m -1.5 This question's working at five step sizes, and what one step does to the slope's gap to 0.5. A minus means the slope lands on the other side of 0.5. Under 0.2 the gap shrinks at every step. At 0.2 it only changes sides, for ever. Past 0.2 it grows, so the loss rises in the end: the nearer 0.2, the later. why it is allowed: 0.15 times is , and 0.15 times 5 is 0.75.
guess it, thenthe move: gather the slopes
the line:why it is allowed: take away is minus . With at 0.5 the slope is 0, so it gives 0.5, and the best slope stays put. From 0 it gives 0.75, which is 0.25 past the best, where it began 0.5 short. Each step halves the gap and crosses to the other side: 0.75, 0.38, 0.56 and on. The 0.38 is 0.375, rounded.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: one measure
the line:why it is allowed: To measure the line is to find every point's miss, one for each of the points.
the move: count the measures
the line:measure when misses so far 1 before any step 4 2 after step 1 8 3 after step 2 12 8 after step 7 32 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.
guess it, thenthe move: add them up
the line:why it is allowed: is the misses measured. By lines 1 and 2, for each of the 8 measures.
guess it, thenthe move: eight lots
the line:why it is allowed: Eight lots of . For the four points, 32.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 4, is , so it is at most .
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5, is at most 8 times .
guess it, thenthe move: a floor
the line:why it is allowed: By line 4, is at least too.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, never drops below 8 times .
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 6 and 8 bound by the same growth, .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: the first step overshoots
the line:when slope misses loss before the step 0 -5, 0, 5 16.67 after it 10 45, 0, -45 1,350 The three points 0 0, 5 5 and 10 10 at step size 0.3. One step takes the slope from 0 to 10 and the loss from 16.67 to 1,350. why it is allowed: Take 0 0, 5 5 and 10 10. The best line passes through all three, with slope 1. From a flat line at height 5 the misses are minus 5, 0 and 5, and the loss is 16.67. Each miss here equals its point's distance across, so each product is a square too: 25, 0 and 25, mean 16.67 again. So the loss's slope for the line's slope is minus 33.33. 0.3 times that lifts the slope from 0 to 10, ten times too steep. The loss jumps from 16.67 to 1,350. The plot's runs find the first step raising the loss at every size, up to 10,000 points.
the move: count the measures
the line:why it is allowed: The line is measured before the step and after it. The second measure finds the loss higher, so no more steps are taken.
guess it, thenthe move: add them up
the line:why it is allowed: is the misses measured. By line 2 there are 2 measures, each of misses.
guess it, thenthe move: two lots
the line:why it is allowed: Two lots of . For the three points, 6.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 4, is , so it is at most .
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5, is at most 2 times .
guess it, thenthe move: a floor
the line:why it is allowed: By line 4, is at least too.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, never drops below 2 times .
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 6 and 8 bound by the same growth, .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
count it yourself
Count every step on section 1's four points. After the points come the step size, 0.15, and the number of steps, 7. Then check the misses against the formula.
| step | the row | what happens | misses measured | steps taken |
|---|---|---|---|---|
| 1 | 4 points. The line starts flat at height 5, with a cross on it at the middle of the points, 5 across. Your step size is 0.15. On these points, no step size under 0.2 can make the line fit worse. | 0 | 0 | |
| 2 | 3.5 | Step 1. Each band is a miss: a point's height minus the line's height there. The longest is 3. Square every miss and average them. That one number is the loss, 3.5.3.5 | 4 | 0 |
| 3 | 3.5 | How fast does the loss rise as the line lifts, or as it tilts up? Those are the loss's two slopes: 2 for the line's height and -5 for its slope. A minus number means that way lowers the loss.3.5 | 4 | 0 |
| 4 | 3.5 | Take 0.15 times each of those away. The line's height changes by -0.3 to 4.7, moving the cross, and its slope changes by 0.75 to 0.75, turning the line about the cross.3.5 | 4 | 1 |
| 5 | 3.5, 2.05 | Step 2. Measure every miss again: the longest is now 1.95. The loss was 3.5 and is now 2.05.3.5, 2.05 | 8 | 1 |
| 6 | 3.5, 2.05 | The loss's two slopes now: 1.4 for the line's height and 2.5 for its slope.3.5, 2.05 | 8 | 1 |
| 7 | 3.5, 2.05 | Take 0.15 times each away again. The height changes by -0.21 to 4.49, and the slope by -0.37 to 0.38.3.5, 2.05 | 8 | 2 |
| 8 | 3.5, 2.05, 1.57 | Step 3. Measure every miss again: the longest is now 2.12. The loss was 2.05 and is now 1.57.3.5, 2.05, 1.57 | 12 | 2 |
| 9 | 3.5, 2.05, 1.57 | The loss's two slopes now: 0.98 for the line's height and -1.25 for its slope.3.5, 2.05, 1.57 | 12 | 2 |
| 10 | 3.5, 2.05, 1.57 | Take 0.15 times each away again. The height changes by -0.15 to 4.34, and the slope by 0.18 to 0.56.3.5, 2.05, 1.57 | 12 | 3 |
| 11 | 3.5, 2.05, 1.57, 1.39 | Step 4. Measure every miss again: the longest is now 1.78. The loss was 1.57 and is now 1.39.3.5, 2.05, 1.57, 1.39 | 16 | 3 |
| 12 | 3.5, 2.05, 1.57, 1.39 | The loss's two slopes now: 0.69 for the line's height and 0.63 for its slope.3.5, 2.05, 1.57, 1.39 | 16 | 3 |
| 13 | 3.5, 2.05, 1.57, 1.39 | Take 0.15 times each away again. The height changes by -0.1 to 4.24, and the slope by -0.09 to 0.47.3.5, 2.05, 1.57, 1.39 | 16 | 4 |
| 14 | 3.5, 2.05, 1.57, 1.39, 1.31 | Step 5. Measure every miss again: the longest is now 1.77. The loss was 1.39 and is now 1.31.3.5, 2.05, 1.57, 1.39, 1.31 | 20 | 4 |
| 15 | 3.5, 2.05, 1.57, 1.39, 1.31 | The loss's two slopes now: 0.48 for the line's height and -0.31 for its slope.3.5, 2.05, 1.57, 1.39, 1.31 | 20 | 4 |
| 16 | 3.5, 2.05, 1.57, 1.39, 1.31 | Take 0.15 times each away again. The height changes by -0.07 to 4.17, and the slope by 0.05 to 0.52.3.5, 2.05, 1.57, 1.39, 1.31 | 20 | 5 |
| 17 | 3.5, 2.05, 1.57, 1.39, 1.31, 1.28 | Step 6. Measure every miss again: the longest is now 1.65. The loss was 1.31 and is now 1.28.3.5, 2.05, 1.57, 1.39, 1.31, 1.28 | 24 | 5 |
| 18 | 3.5, 2.05, 1.57, 1.39, 1.31, 1.28 | The loss's two slopes now: 0.34 for the line's height and 0.16 for its slope.3.5, 2.05, 1.57, 1.39, 1.31, 1.28 | 24 | 5 |
| 19 | 3.5, 2.05, 1.57, 1.39, 1.31, 1.28 | Take 0.15 times each away again. The height changes by -0.05 to 4.12, and the slope by -0.03 to 0.49.3.5, 2.05, 1.57, 1.39, 1.31, 1.28 | 24 | 6 |
| 20 | 3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26 | Step 7. Measure every miss again: the longest is now 1.63. The loss was 1.28 and is now 1.26.3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26 | 28 | 6 |
| 21 | 3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26 | The loss's two slopes now: 0.24 for the line's height and -0.08 for its slope.3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26 | 28 | 6 |
| 22 | 3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26 | Take 0.15 times each away again. The height changes by -0.04 to 4.08, and the slope by 0.01 to 0.5.3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26 | 28 | 7 |
| 23 | 3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26, 1.26 | The steps are done: 7 steps. The loss started at 3.5 and is now 1.26.3.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26, 1.26 | 32 | 7 |
In all, 32 misses measured. The formula from section 4, , gives at : the same.
where you meet it
An old pair of ideas, least squares and walking downhill, that today's toolkits still offer.
scikit-learn, a Python library for machine learning
Its SGDRegressor tool fits a line by steps like these. Each step looks at one point, not all of them, and by default the step size shrinks as the run goes on. Its LinearRegression tool takes no steps: it solves for the best line directly.
source: scikit-learn's documentation, the user guide's sections on linear models and on stochastic gradient descent, and the SGDRegressor and LinearRegression references
where it comes from
Adrien-Marie Legendre printed the method of least squares in 1805, in an appendix to a book on comets' orbits, and showed it on measurements of the Earth's shape. Carl Friedrich Gauss printed his own account in 1809, and said he had used it since 1795. Augustin-Louis Cauchy described walking downhill by steps in 1847.
source: Legendre, Nouvelles méthodes pour la détermination des orbites des comètes, 1805. Gauss, Theoria motus corporum coelestium in sectionibus conicis solem ambientium, 1809. Cauchy, Méthode générale pour la résolution des systèmes d'équations simultanées, Comptes rendus de l'Académie des sciences, 1847.
what is used instead
For one line there is a formula: the best height and slope come straight from the points, with no step size to choose. Steps earn their keep when a model has thousands or millions of settings and no formula, as a neural network does. There, each step often measures only a handful of points.
how it compares
Gradient descent beside itself at other step sizes. The same four points, five walks.
| step size | steps taken | height | slope | loss |
|---|---|---|---|---|
| 0.05 | 7 | 4.48 | 0.5 | 1.48 |
| 0.1 | 7 | 4.21 | 0.5 | 1.29 |
| 0.15 | 7 | 4.08 | 0.5 | 1.26 |
| 0.2 | 7 | 4.03 | 1 | 2.5 |
| 0.25 | 1 | 4.5 | 1.25 | 4.31 |