4.9.22 · D2Probability Theory & Statistics

Visual walkthrough — Linear regression — least squares, inference on coefficients

1,994 words9 min readBack to topic

Step 1 — The dots and the guess

WHAT. We start with a handful of measured pairs: for each observation we have a horizontal number and a vertical number . Plotting them gives a cloud of points. We suspect the cloud secretly follows a straight line, but noise has jostled every dot.

WHY. A straight line is the simplest possible "story": pick a starting height and a steepness, and you've described infinitely many points with just two numbers. Before we can find the best line we must be able to draw any line and score it.

PICTURE. Below, the black dots are the data. The blue line is one candidate — a guess. Its two knobs are:

Figure — Linear regression — least squares, inference on coefficients

Step 2 — Measuring a miss: the residual

WHAT. For each dot, drop a vertical segment from the dot to the line. Its signed length is the residual:

WHY vertical, not perpendicular? We are trying to predict from . Given an , the line hands us a predicted . The honest error is the gap in the thing we predict — the -direction. Perpendicular distance would mix errors in (which we treat as known) into the score.

PICTURE. Orange segments are the misses. Dots above the line have ; dots below have .

Figure — Linear regression — least squares, inference on coefficients

Step 3 — Why we square the misses

WHAT. We score a line by the sum of squared residuals:

WHY squaring, and not absolute value? Three reasons, each visible:

  1. Squaring turns every miss positive, so nothing cancels.
  2. Squaring punishes big misses harder — a miss of costs , a miss of costs . The line is pulled hardest by its worst offences.
  3. Squaring is smooth: the score curve has no sharp corner, so calculus (Step 5) can find the bottom cleanly. Absolute value has a kink and gives no tidy formula.

PICTURE. Each orange segment of length becomes an orange square of area . The total shaded area is the score . Least squares = shrink the total orange area.

Figure — Linear regression — least squares, inference on coefficients
Why squares?
They punish big misses more, never cancel, and stay smooth so calculus works.

Step 4 — The score lives in a bowl

WHAT. depends on the two knobs . Treat those two numbers as a location and plot as a height above them. Because is a sum of squares, that surface is a smooth upward bowl (a paraboloid) with exactly one lowest point.

WHY this matters. A bowl has a single bottom, and at that bottom the ground is flat in every direction. "Flat" is something calculus can pin down exactly. This is what guarantees a unique best line — no second candidate, no ambiguity.

PICTURE. The bowl over the plane. The red dot is the minimum; the red tangent plane there is horizontal.

Figure — Linear regression — least squares, inference on coefficients

Step 5 — Set the two slopes of the bowl to zero

WHAT. We take the derivative of with respect to each knob and set both to zero. The derivative asks "if I nudge the height, does the score change?"; at the bottom the answer must be "no."

\;\Longrightarrow\; \sum_i (y_i - \hat\beta_0 - \hat\beta_1 x_i)=0$$ The $-1$ is the chain rule: the miss $e_i = y_i-\hat\beta_0-\hat\beta_1 x_i$ changes by $-1$ when $\hat\beta_0$ grows by $1$. Read the result plainly: **the residuals sum to zero.** $$\frac{\partial S}{\partial \hat\beta_1} = \sum_i 2\big(y_i - \hat\beta_0 - \hat\beta_1 x_i\big)\cdot(-x_i) = 0 \;\Longrightarrow\; \sum_i x_i(y_i - \hat\beta_0 - \hat\beta_1 x_i)=0$$ Here the miss changes by $-x_i$, so this result says **the residuals, weighted by $x$, sum to zero.** **WHY these two equations?** They are the two "flatness" conditions of the bowl — one per knob. Together they are the **normal equations** and they *are* the whole minimisation. **PICTURE.** Two cross-sections of the bowl: slice along $\hat\beta_0$ (holding $\hat\beta_1$ fixed) and along $\hat\beta_1$. Each slice is a parabola; the red mark is where its tangent is flat. ![[deepdives/dd-maths-4.9.22-d2-s05.png]] > [!definition] Normal equations > $$\sum e_i = 0 \qquad\text{and}\qquad \sum x_i e_i = 0.$$ > Two constraints the best-fit residuals *must* obey. (These are exactly the two "used-up" degrees > of freedom that force the $n-2$ divisor later.) --- ## Step 6 — The intercept: the line pins the centre of mass **WHAT.** Take the first normal equation, $\sum(y_i - \hat\beta_0 - \hat\beta_1 x_i)=0$, and divide by $n$. Using $\bar x = \frac1n\sum x_i$ and $\bar y = \frac1n\sum y_i$: $$\bar y - \hat\beta_0 - \hat\beta_1\bar x = 0 \;\Longrightarrow\; \boxed{\;\hat\beta_0 = \bar y - \hat\beta_1\bar x\;}$$ Read it: the height knob is fixed once we know the slope, by demanding the line pass through the average point. **WHY.** Rearranged, this says $\bar y = \hat\beta_0 + \hat\beta_1\bar x$ — i.e. **the point $(\bar x,\bar y)$ lies exactly on the fitted line.** The best line always skewers the *centre of mass* of the cloud. So we don't have to search for $\hat\beta_0$ separately; we only need the tilt, and the line pivots through $(\bar x,\bar y)$. **PICTURE.** The green cross is $(\bar x,\bar y)$. Whatever slope we choose, the winning line rotates about that pivot. ![[deepdives/dd-maths-4.9.22-d2-s06.png]] Which point does every least-squares line pass through? ::: The centre of mass $(\bar x,\bar y)$. --- ## Step 7 — The slope: co-movement divided by spread **WHAT.** Now substitute $\hat\beta_0 = \bar y - \hat\beta_1\bar x$ into the second normal equation and clean up. The trick that makes it tidy: measure everything *relative to the means*. Write the centred coordinates $x_i-\bar x$ and $y_i-\bar y$, and define $$S_{xx} = \sum_i (x_i-\bar x)^2 \quad(\text{how spread-out } x \text{ is}), \qquad S_{xy} = \sum_i (x_i-\bar x)(y_i-\bar y) \quad(\text{how } x,y \text{ move together}).$$ The algebra collapses to $$\boxed{\;\hat\beta_1 = \frac{S_{xy}}{S_{xx}} = \frac{\sum_i (x_i-\bar x)(y_i-\bar y)}{\sum_i (x_i-\bar x)^2}\;}$$ **WHY this shape.** $S_{xy}$ is large and positive when dots to the *right* of centre tend to sit *above* centre (both factors positive) and dots to the *left* sit *below* (both negative → product still positive). That's an upward tilt. Dividing by $S_{xx}$ rescales it into a *rise per unit run*. This is exactly [[Covariance]] over the variance of $x$ (times $n$'s that cancel), and it links straight to the [[Correlation coefficient]]. **PICTURE.** Each dot's contribution to $S_{xy}$ is a signed rectangle of width $(x_i-\bar x)$ and height $(y_i-\bar y)$, drawn from the green centre. Blue rectangles vote "tilt up," red vote "tilt down"; the net decides the slope. ![[deepdives/dd-maths-4.9.22-d2-s07.png]] > [!formula] The whole result, assembled > $$\hat\beta_1 = \frac{S_{xy}}{S_{xx}}, \qquad \hat\beta_0 = \bar y - \hat\beta_1\bar x.$$ > Slope = co-movement ÷ spread; intercept = pin the line through the mean point. --- ## Step 8 — Degenerate case: what if $x$ never moves? **WHAT.** Suppose every $x_i$ equals the same value (all dots stacked in one vertical column). Then $x_i-\bar x = 0$ for all $i$, so $S_{xx}=0$, and the slope formula becomes $S_{xy}/0$ — undefined. **WHY it *should* break.** With no horizontal spread, there is no "run" to measure a rise against. You can pass infinitely many lines through a vertical column of dots; every one predicts the mean $\bar y$ equally well. There is genuinely *no unique slope* — the maths correctly refuses to invent one. The bowl of Step 4 degenerates into a flat-bottomed *trough*: infinitely many minima. **PICTURE.** Left: healthy spread → one clear best tilt. Right: zero spread → the score is flat along the slope direction, so no single answer. ![[deepdives/dd-maths-4.9.22-d2-s08.png]] > [!mistake] "More data always fixes an undefined slope." > *Why it feels right:* usually more dots = more certainty. **Fix:** if all your $x$'s share one > value, adding a *thousand* more at that same value keeps $S_{xx}=0$. You need *spread in $x$*, not > just *more $x$*. This is the same lever-arm idea that makes $\text{Var}(\hat\beta_1)=\sigma^2/S_{xx}$ > blow up as $S_{xx}\to 0$. --- ## The one-picture summary Every idea, stacked into a single frame: the dots, the pivot at $(\bar x,\bar y)$, the winning line whose tilt is $S_{xy}/S_{xx}$, and the orange squares whose total area we drove to its minimum. ![[deepdives/dd-maths-4.9.22-d2-s09.png]] > [!recall]- Feynman retelling of the whole walkthrough > Scatter some dots. Draw a straight line with two knobs: a height and a tilt. For each dot, measure > straight *up or down* to the line — that's its miss. Turn every miss into a square (so big misses > scream and nothing cancels) and add all the squares up. That total is a bowl-shaped landscape over > the two knobs, with one lowest point. At the bottom the ground is flat both ways — set both slopes > to zero. The first flatness rule says the misses cancel out, which forces the line through the > average dot $(\bar x,\bar y)$. The second rule, once you measure everything from that average > point, hands you the tilt: add up how much each dot's sideways offset lines up with its up-down > offset (that's $S_{xy}$), and divide by how spread out the dots are sideways (that's $S_{xx}$). > Big shared movement and a wide sideways spread give you a confident tilt. And if the dots never > spread sideways at all, there's no run to measure against — the formula divides by zero and > honestly admits it can't pick a slope. > [!mnemonic] Carry the picture > **"Square the misses, empty the bowl; pivot at the mean, tilt by co-move-over-spread."** --- **See also:** [[Gauss–Markov Theorem]] (why this line is *best*), [[Maximum Likelihood Estimation]] (why squares under Gaussian noise), [[t-distribution]] & [[Hypothesis Testing]] (trusting the tilt), [[Multiple Regression]] (many knobs at once).