Part IV — Supervised Learning: Regression & Linear Classifiers · Chapter 1

Linear regression & ordinary least squares

Hook

Given a scatter of points, what's the single "best" line through them — and what does "best" even mean here?

Intuition

Drag either end of the line. The dashed segments are each point's residual — how far it misses the line, straight up or down. A good fit is one where those misses are all small at once.

Formalize

Squaring each residual before adding them up gives the standard measure of fit — the sum of squared errors (SSE):

SSE=i(yi(wxi+b))2\text{SSE} = \sum_i \big(y_i - (w x_i + b)\big)^2
  • SSE — the sum of squared errors: how far the line's predictions miss the actual data, added up.
  • yiy_i — the actual (observed) value for data point ii.
  • ww — the line's slope.
  • xix_i — the input value for data point ii.
  • bb — the line's intercept.
  1. Every term becomes positive

    Squaring means misses in opposite directions can't cancel out — a point the line overshoots and a point it undershoots both add to the total instead of offsetting each other.

  2. One large miss costs far more than several small ones

    Squaring also makes a big residual disproportionately expensive, so a line has to stay close to every point, not just close on average.

Play
w = 0.00, b = 5.00, SSE = 322.03

Drag toward a better angle and watch ww, bb, and the SSE update together — the line's slope and intercept are just two numbers, and SSE is one number summarizing how well those two numbers fit everything at once.

Worked example

With w=2,b=1w=2, b=1:

  1. Compute the predictions

    y^i=2xi+1\hat y_i = 2x_i+1 at each xx:

    • x=1x=1: 2(1)+1=32(1)+1=3
    • x=3x=3: 2(3)+1=72(3)+1=7
    • x=5x=5: 2(5)+1=112(5)+1=11
    • x=7x=7: 2(7)+1=152(7)+1=15
    • x=9x=9: 2(9)+1=192(9)+1=19

    Against actual values 3.5,6.8,10.5,15.2,18.53.5, 6.8, 10.5, 15.2, 18.5.

  2. Compute the residuals

    yiy^iy_i-\hat y_i at each point:

    • 3.53=0.53.5-3=0.5
    • 6.87=0.26.8-7=-0.2
    • 10.511=0.510.5-11=-0.5
    • 15.215=0.215.2-15=0.2
    • 18.519=0.518.5-19=-0.5
  3. Square and sum them
    SSE=0.25+0.04+0.25+0.04+0.25=0.83\text{SSE} = 0.25+0.04+0.25+0.04+0.25 = 0.83

    Close, clean numbers — and already a good fit, without needing the exact optimum.

Checkpoint

Drag either end of the line until the total squared error, SSE, drops below 2.0.

SSE = 322.03
Drag either end of the line to try it
Summary
SSE=i(yi(wxi+b))2\text{SSE} = \sum_i \big(y_i - (w x_i + b)\big)^2

Fitting a line means choosing ww and bb to make this sum as small as possible. Next: what changes if you measure "small" a different way.