Skip to content

linear regression

learning

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.

024681002468103
34500.51the line's heightthe line's slope
012301steps takenthe lossthe loss
The four points and the flat line they start from, at height 5, with each miss drawn. Before any step the loss is 3.5. Beside it, the loss's bowl, seen from above. Each dot on it is one whole line: across, its height; up, its slope. The circled dot is the flat line, and the dot in the middle is the best line. The loop joins the lines that have the same loss as the flat line. Last, the loss chart, circled at its first point, with the point the first step will reach beside it.
024681002468101.58
34500.51the line's heightthe line's slope
012301234567steps takenthe lossthe loss

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.

The same four points with the step size and the steps free to change. At 0.1 the first step lands the slope on its best, 0.5. At 0.2 the slope jumps between 1 and 0, and the loss stalls near 2.5. At 0.25 the first step raises the loss from 3.5 to 4.31, and the run stops.
steps takenheightslopeloss
0503.5
14.70.752.05
24.490.381.57
34.340.561.39
44.240.471.31
54.170.521.28
64.120.491.26
74.080.51.26
The run at step size 0.15 as numbers, to two decimal places. The height closes in on 4 from above. The slope jumps past 0.5 and back, closer each time. The loss falls at every step.
Four points, the slope closing in from both sides: 7 moments of its 23 steps, the steps between them left for the watch page
  1. step 1 of 23

    0246810024681001234567

    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.

    watch from here

  2. step 2 of 23

    024681002468103012345673.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.

    what it remembers to know where it is
    step
    1
    height at the cross
    5
    slope
    0
    loss
    3.5

    watch from here

  3. step 3 of 23

    024681002468103012345673.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.

    what it remembers to know where it is
    step
    1
    height at the cross
    5
    slope
    0
    loss
    3.5

    watch from here

  4. step 4 of 23

    02468100246810012345673.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.

    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.

    watch from here

  5. step 5 of 23

    024681002468101.95012345673.52.05

    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

    watch from here

  6. step 7 of 23

    02468100246810012345673.52.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.

    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.

    watch from here

  7. step 23 of 23

    024681002468101.58012345673.52.051.571.391.311.281.261.26

    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

    watch from here

the steps, in plain words
  1. start with a flat linePython lines 10, 11, 12 and 13
  2. repeat for each stepPython line 14
  3. measure every miss, and the lossPython lines 15 and 16
  4. find the loss's slope for the height and for the slopePython lines 20 and 21
  5. take the step size times each awayPython lines 22 and 23
  6. 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, 8n8n against 2n2n.

misses measured, ten times more at each mark up

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

nn, 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
what all 7 steps cost, at a billion misses measured a second
nnmisses measuredtime
1,0008,000about 8 microseconds
100,000800,000about 800 microseconds
10,000,00080,000,000about 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.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
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 stopsworstthe same points with a step size small enough that every step lowers the loss, so all 7 steps are taken
misses measured2n2n8n8n
how the misses measured grow in each case
tight, Θ\ThetaΘ(n)\Theta(n)Θ(n)\Theta(n)
ceiling, OOO(n)O(n)O(n)O(n)
floor, Ω\OmegaΩ(n)\Omega(n)Ω(n)\Omega(n)

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.

pointsmisses, stopped after step 1steps, stopped after step 1misses, all 7 stepssteps, all 7 steps
10201807
10020018007
1,0002,00018,0007
10,00020,000180,0007
The same climbing points fitted twice. With step size 0.3 the run stops after 1 step and 2 measures. With a small step size it takes all 7 steps and 8 measures.

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.

Question 1: the line lies flat at height hh, and one point stands at height 3. Its squared miss is its part of the loss. Call that part's slope, as hh grows, PP. What is PP?
  1. the move: the rise over the distance across

    the line:
    P=(3−(h+1))2−(3−(h−1))22P = \frac{(3 - (h + 1))^2 - (3 - (h - 1))^2}{2}

    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 hh, as the gradient page does. The rise is the square at h+1h + 1 take away the square at h−1h - 1, 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.

  2. the move: take each height from 3

    the line:
    P=(2−h)2−(4−h)22P = \frac{(2 - h)^2 - (4 - h)^2}{2}

    why it is allowed: 3 take away h+1h + 1 is 2−h2 - h. 3 take away h−1h - 1 is 4−h4 - h.

  3. the move: multiply out each square

    the line:
    P=(4−4h+h2)−(16−8h+h2)2P = \frac{(4 - 4h + h^2) - (16 - 8h + h^2)}{2}
    times2minus h
    24minus 2 times h
    minus hminus 2 times hh squared
    2 minus h times itself, box by box. 4, two boxes of minus 2 times h, and h squared.

    why it is allowed: 2−h2 - h times itself is 4, take away four lots of hh, plus h2h^2. 4−h4 - h times itself is 16, take away eight lots of hh, plus h2h^2.

  4. the move: take the second bracket away

    the line:
    P=4h−122P = \frac{4h - 12}{2}

    why it is allowed: The h2h^2 cancel. 4 take away 16 is minus 12. Minus four lots of hh, take away minus eight lots, is four lots.

  5. the move: halve

    the line:
    P=2h−6P = 2h - 6

    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 hh, and minus 2 times it is 2h−62h - 6. With hh 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.

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

Question 2: now the line turns about its cross at height 5, with slope mm. The point 2 3 stands 3 left of the middle. Call the slope of its squared miss, as mm grows, TT. What is TT?
  1. the move: the point's miss

    the line:
    3−(5−3m)=3m−23 - (5 - 3m) = 3m - 2
    024681002468101
    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 mm times minus 3, which is 5−3m5 - 3m. The miss is the point's height, 3, take away that.

  2. the move: the rise over the distance across

    the line:
    T=(3m+1)2−(3m−5)22T = \frac{(3m + 1)^2 - (3m - 5)^2}{2}

    why it is allowed: Look 1 either side of mm, as in question 1. By line 1, at m+1m + 1 the miss is 3m+13m + 1, and at m−1m - 1 it is 3m−53m - 5. The rise is one square take away the other, over 2 across.

  3. the move: multiply out the first square

    the line:
    (3m+1)2=9m2+6m+1(3m + 1)^2 = 9m^2 + 6m + 1

    why it is allowed: 3m+13m + 1 times itself is 9m29m^2, two lots of 3m3m, and 1.

  4. the move: multiply out the second square

    the line:
    (3m−5)2=9m2−30m+25(3m - 5)^2 = 9m^2 - 30m + 25

    why it is allowed: 3m−53m - 5 times itself is 9m29m^2, take away ten lots of 3m3m, plus 25.

  5. the move: take the second square away

    the line:
    T=36m−242T = \frac{36m - 24}{2}

    why it is allowed: By lines 3 and 4, the 9m29m^2 cancel. Six lots of mm, take away minus thirty lots, is 36 lots. 1 take away 25 is minus 24.

  6. the move: halve

    the line:
    T=18m−12T = 18m - 12

    why it is allowed: That is minus twice the miss times the point's distance across. The miss is 3m−23m - 2 and the distance is minus 3. Minus 2 times minus 3 is 6, and 6 lots of 3m−23m - 2 is 18m−1218m - 12. With mm at 0 the slope is minus 12.

try it with a small mm: at m=1m = 1, line 2 gives T=6T = 6, which you can work out by hand

Question 3: at step size 0.15, one step takes the height from hh to NN. Where is NN?
  1. the move: every point the same way

    the line:
    the loss’s slope for theheight is minus twicethe mean miss\begin{gathered} \text{the loss's slope for the} \\ \text{height is minus twice} \\ \text{the mean miss} \end{gathered}
    pointmissslope of its squareat h = 5
    2 33 - h2h - 64
    4 22 - h2h - 46
    6 66 - h2h - 12-2
    8 55 - h2h - 100
    mean4 - h2h - 82
    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, P=2h−6P = 2h - 6. 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.

  2. the move: step against the slope

    the line:
    N=h−0.15×(2h−8)N = h - 0.15 \times (2h - 8)
    05101234567the line's heightthe lossthe loss
    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 2h−82h - 8.

  3. the move: multiply the bracket

    the line:
    N=h−0.3h+1.2N = h - 0.3h + 1.2

    why it is allowed: 0.15 times 2h2h is 0.3h0.3h, and 0.15 times 8 is 1.2. Taking away a minus adds.

  4. the move: gather the heights

    the line:
    N=0.7h+1.2N = 0.7h + 1.2

    why it is allowed: hh take away 0.3h0.3h leaves 0.7h0.7h. With hh at 4 the slope 2h−82h - 8 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.

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

Question 4: at step size 0.15, one step takes the slope from mm to RR. Where is RR?
  1. the move: every point the same way

    the line:
    the loss’s slope for theline’s slope is minus twicethe mean of each misstimes its distance\begin{gathered} \text{the loss's slope for the} \\ \text{line's slope is minus twice} \\ \text{the mean of each miss} \\ \text{times its distance} \end{gathered}
    pointacross from the middleslope of its squareat m = 0
    2 3-318m - 12-12
    4 2-12m - 6-6
    6 612m - 2-2
    8 5318m0
    mean010m - 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, T=18m−12T = 18m - 12. 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.

  2. the move: step against the slope

    the line:
    R=m−0.15×(10m−5)R = m - 0.15 \times (10m - 5)

    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 10m−510m - 5.

  3. the move: multiply the bracket

    the line:
    R=m−1.5m+0.75R = m - 1.5m + 0.75
    step sizeone step turns m intothe gap to 0.5 is multiplied by
    0.050.5m + 0.250.5
    0.10.50
    0.150.75 - 0.5m-0.5
    0.21 - m-1
    0.251.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 10m10m is 1.5m1.5m, and 0.15 times 5 is 0.75.

  4. the move: gather the slopes

    the line:
    R=0.75−0.5mR = 0.75 - 0.5m

    why it is allowed: mm take away 1.5m1.5m is minus 0.5m0.5m. With mm at 0.5 the slope 10m−510m - 5 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.

try it with a small mm: at m=1m = 1, line 2 gives R=0.25R = 0.25, which you can work out by hand

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

Question 6: nn points spaced evenly along one climbing line, each as far up as across, from 0 0 to 10 10 or wider, and the step size 0.3. The first step raises the loss. How many misses, SS, does the run measure?
  1. the move: the first step overshoots

    the line:
    the first step throwsthe slope past its best,and the loss rises\begin{gathered} \text{the first step throws} \\ \text{the slope past its best,} \\ \text{and the loss rises} \end{gathered}
    whenslopemissesloss
    before the step0-5, 0, 516.67
    after it1045, 0, -451,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.

  2. the move: count the measures

    the line:
    the run stops after2 measures\begin{gathered} \text{the run stops after} \\ \text{2 measures} \end{gathered}

    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.

  3. the move: add them up

    the line:
    S=n+nS = n + n

    why it is allowed: SS is the misses measured. By line 2 there are 2 measures, each of nn misses.

  4. the move: two lots

    the line:
    S=2nS = 2n

    why it is allowed: Two lots of nn. For the three points, 6.

  5. the move: a ceiling

    the line:
    S≤2nS \le 2n

    why it is allowed: By line 4, SS is 2n2n, so it is at most 2n2n.

  6. the move: read it as a bound

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

    why it is allowed: By line 5, SS is at most 2 times nn.

  7. the move: a floor

    the line:
    S≥2nS \ge 2n

    why it is allowed: By line 4, SS is at least 2n2n too.

  8. the move: read it as a bound

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

    why it is allowed: By line 7, SS never drops below 2 times nn.

  9. the move: ceiling and floor together

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

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

try it with a small nn: at n=3n = 3, line 3 gives S=6S = 6, 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.

2 3, 4 2, 6 6, 8 5; 0.15; 7, every step written down
stepthe rowwhat happensmisses measuredsteps taken
14 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.00
23.5Step 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.540
33.5How 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.540
43.5Take 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.541
53.5, 2.05Step 2. Measure every miss again: the longest is now 1.95. The loss was 3.5 and is now 2.05.3.5, 2.0581
63.5, 2.05The loss's two slopes now: 1.4 for the line's height and 2.5 for its slope.3.5, 2.0581
73.5, 2.05Take 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.0582
83.5, 2.05, 1.57Step 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.57122
93.5, 2.05, 1.57The loss's two slopes now: 0.98 for the line's height and -1.25 for its slope.3.5, 2.05, 1.57122
103.5, 2.05, 1.57Take 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.57123
113.5, 2.05, 1.57, 1.39Step 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.39163
123.5, 2.05, 1.57, 1.39The 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.39163
133.5, 2.05, 1.57, 1.39Take 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.39164
143.5, 2.05, 1.57, 1.39, 1.31Step 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.31204
153.5, 2.05, 1.57, 1.39, 1.31The 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.31204
163.5, 2.05, 1.57, 1.39, 1.31Take 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.31205
173.5, 2.05, 1.57, 1.39, 1.31, 1.28Step 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.28245
183.5, 2.05, 1.57, 1.39, 1.31, 1.28The 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.28245
193.5, 2.05, 1.57, 1.39, 1.31, 1.28Take 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.28246
203.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26Step 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.26286
213.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26The 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.26286
223.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26Take 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.26287
233.5, 2.05, 1.57, 1.39, 1.31, 1.28, 1.26, 1.26The 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.26327

In all, 32 misses measured. The formula from section 4, 8n8n, gives M=32M = 32 at n=4n = 4: 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 sizesteps takenheightslopeloss
0.0574.480.51.48
0.174.210.51.29
0.1574.080.51.26
0.274.0312.5
0.2514.51.254.31
Section 1's four points after 7 steps at five step sizes. The loss ends lowest at 0.15. At 0.2 the slope ends on 1, far from 0.5. At 0.25 the run stops after 1 step, worse than it began.
024681002468102.03
34500.51the line's heightthe line's slope
012301234567steps takenthe lossthe loss
Step size 0.2 after 7 steps. The slope jumps between 1 and 0 and never settles, so the loss stalls near 2.5, twice the best line's 1.25.
024681002468100.51.51.50.5
The line every walk is after: height 4 above the middle, slope 0.5. Its misses are 0.5, minus 1.5, 1.5 and minus 0.5, a minus for a point under the line. Their squares, 0.25, 2.25, 2.25 and 0.25, have mean 1.25, the least any line reaches on these points.

race them on one row of your own