5.6.1 · D2Machine Learning (Aerospace Applications)

Visual walkthrough — Linear regression — normal equation, gradient descent derivation

2,131 words10 min readBack to topic

This page rebuilds the normal equation one picture at a time, starting from a scatter of dots and ending at the single formula

Every symbol here is earned before it is used. If you have never seen a matrix, a dot product, or a derivative, start at line one — we build all of it.


Step 1 — The dots and the line we are hunting

WHAT. We are given measurements. Each measurement is a pair: an input (say, altitude) and an output (say, fuel-burn rate). The little is just a name-tag: is the first dot, the second, and so on up to (there are dots).

We want a line

  • (read "y-hat") is the line's prediction — the height the line claims at input .
  • is the slope: how steeply the line rises. Change by 1, changes by .
  • is the bias (or intercept): the height of the line where .

WHY a line? Because it is the simplest honest guess: constant slope, no curves. Everything harder is built on top of this.

PICTURE. Dots in blue, one candidate line in yellow. For each dot, the vertical red gap between the dot and the line is the error — how wrong the line is there.

Figure — Linear regression — normal equation, gradient descent derivation

Step 2 — Measuring total wrongness: why we square

WHAT. For dot the error is (true height minus predicted height). We add up all errors — but squared:

Reading the symbols:

  • means "add up the following for every dot from to ".
  • is one red gap, squared — always positive.
  • is a tidy-up factor: dividing by makes it an average, and the will cancel a that appears when we differentiate later. It changes where the minimum's value is, never where the minimum sits.
  • is the total badness score. Smaller = better line.

WHY square, not just add? Two reasons, both shown in the figure. (1) A dot above the line has a positive gap, one below has a negative gap — if we added them raw, they would cancel and a terrible line could score zero. Squaring makes every gap count as positive. (2) The squared curve is smooth (a parabola), so calculus — which needs slopes to exist everywhere — works cleanly.

PICTURE. Left: raw gaps cancelling (bad). Right: each gap turned into a red square whose area is the penalty; big gaps get punished much harder than small ones.

Figure — Linear regression — normal equation, gradient descent derivation

Step 3 — Packing all the dots into a matrix

WHAT. Writing over and over is clumsy. We stack the data into a matrix and a vector .

  • A vector is a column of numbers, e.g. — one target per row.
  • A matrix is a grid of numbers. We make with one row per dot. To also capture the bias , we glue a column of s in front:

Now — matrix times vector — is the whole column of predictions at once. Row of the product is . The column of s is what turns into a real column so it rides along in the multiplication.

WHY. So the entire cost becomes one clean object:

Here (the squared norm) means "add up the squares of the entries of " — exactly our sum of squared gaps, now in one symbol.

PICTURE. The dot-by-dot table on the left collapses into the boxed grid , column of s highlighted, arrow to .

Figure — Linear regression — normal equation, gradient descent derivation

Step 4 — The whole geometry lives in one flat sheet

WHAT. Here is the picture that makes the formula inevitable. Think of as a single arrow in space (one axis per dot — axes). Every possible line we could draw gives some , another arrow. As we turn the dials and , the tip of sweeps out a flat sheet through the origin: the column space of (all combinations of 's columns).

WHY this matters. Our target arrow almost never lies on that sheet (real data doesn't fit a line perfectly). So "best line" = "the point on the sheet closest to ". Distance in this space is exactly — our error. Minimizing squared error = finding the closest point on the sheet.

PICTURE. The green sheet is the column space. (blue) floats above it. Candidate predictions live on the sheet.

Figure — Linear regression — normal equation, gradient descent derivation

Step 5 — Closest point = drop a perpendicular

WHAT. The closest point on a flat sheet to an outside point is found by dropping straight down — perpendicular to the sheet. Call that foot of the perpendicular . The leftover arrow,

the residual (red), must be perpendicular to the sheet.

WHY. If the residual leaned even slightly along the sheet, we could slide a bit that way and get closer — so we would not yet be at the minimum. At the true minimum the residual points straight out. This is the geometric twin of "the derivative is zero at the bottom".

Perpendicular to the whole sheet means perpendicular to every column of . A dot product of zero means perpendicular, so:

Every entry of is one column of dotted with ; setting them all to zero says each column.

PICTURE. Red residual meets the green sheet at a right-angle mark; the yellow columns of all lie flat in the sheet, each at to the residual.

Figure — Linear regression — normal equation, gradient descent derivation

Step 6 — Same answer by calculus (the slope-is-zero view)

WHAT. Let us re-derive Step 5 with algebra, so the two pictures agree. Expand the cost using the dot-product facts from Step 3:

WHY. As a function of the dials , this is a bowl (a paraboloid) — the quadratic term guarantees it curves upward. A bowl has exactly one lowest point, and at the bottom its slope is zero in every direction. The slope-in-every-direction is the gradient : a vector whose entries are the partial derivatives (the sensitivity of to nudging each dial). Setting it to :

This normal equation is identical to Step 5 (expand ). Geometry and calculus land on the same line.

PICTURE. The bowl-shaped cost surface over the plane; a ball rolls to the single bottom where the tangent plane is flat.

Figure — Linear regression — normal equation, gradient descent derivation

Step 7 — Solve for the winning dials

WHAT. From , undo the matrix on the left by multiplying by its inverse (the matrix version of dividing):

Worked check (the parent's Example 1). Data :

So , : the fitted line is .

PICTURE. The three dots with the solved yellow line, residuals shown, and the fitted numbers labelled.

Figure — Linear regression — normal equation, gradient descent derivation

Step 8 — The degenerate case: when the inverse dies

WHAT. Everything above needed to exist. It fails when two columns of carry the same information — e.g. altitude in metres and altitude in feet. Then the green sheet is squashed: infinitely many lines tie for best, and is singular (no inverse).

WHY it breaks. Geometrically the columns are parallel, so the "sheet" is really a thinner slab — the foot of the perpendicular is not unique. Algebraically , and you cannot divide.

The fix. Use the Moore–Penrose pseudoinverse , which picks the smallest valid , or add a nudge (see ridge regression): , which is always invertible for . Also, wildly different feature scales make nearly singular numerically — cured by Feature Scaling.

PICTURE. Left: healthy full sheet, unique foot. Right: collapsed slab, a whole line of tied feet — the ambiguity.

Figure — Linear regression — normal equation, gradient descent derivation

The one-picture summary

Dots → squared gaps → column-space sheet → drop the perpendicular → normal equation → solved line. One figure, the whole journey.

Figure — Linear regression — normal equation, gradient descent derivation
Recall Feynman retelling — say it like a story

We scattered some dots and wanted the straightest, fairest line through them. "Fair" meant: add up how far off it is at every dot, but square each miss so ups and downs can't secretly cancel and so the math stays smooth. We packed all the dots into a grid (with a column of s so the line's height comes along for free) and all the answers into a column . Then came the beautiful trick: think of as an arrow, and every possible line as an arrow lying on a flat sheet. The best line is the shadow of on that sheet — you drop straight down, and the leftover error points dead-perpendicular to the sheet. "Perpendicular to every column" written in symbols is , which rearranges into . Calculus agrees: the cost is a bowl, and the bottom of a bowl is where every slope is zero — same equation. The only catch is when two columns say the same thing; then the sheet collapses, the inverse dies, and we lean on the pseudoinverse or a tiny ridge nudge. For our three dots it spat out , no guessing required.

Recall Quick self-test

Why do we square the errors instead of adding them raw? ::: So positive and negative gaps can't cancel, and so the cost is a smooth parabola/bowl with derivatives everywhere. What does mean geometrically? ::: The residual (error arrow) is perpendicular to every column of , i.e. to the whole column-space sheet. When does fail to exist? ::: When columns of are redundant (multicollinearity), making singular. For data , what line does the normal equation give? ::: .