2.2.5 · D3Linear & Logistic Regression

Worked examples — Normal equation closed-form solution

1,610 words7 min readBack to topic

This is the drill-ground for the normal equation. The parent note derived the formula

Here we throw every kind of dataset at it — clean, degenerate, more-equations-than-unknowns, fewer, and a real word problem — so you never meet a case you haven't already seen solved.


The scenario matrix

Every dataset you can hand the normal equation falls into one of these cells. The examples below cover all of them.

Cell Situation What decides it Does the plain formula work?
A Overdetermined, full rank, imperfect fit , columns independent, noisy points ✅ unique least-squares line
B Exact fit possible () points lie exactly on a line ✅ residual is zero
C Zero-slope / degenerate target all equal, or all equal ✅ but geometry is special
D Rank-deficient (redundant features) one feature combo of others singular → use pseudoinverse
E Underdetermined () fewer points than unknowns ❌ infinitely many exact fits → min-norm via (see Underdetermined vs Overdetermined Systems)
F Real-world word problem units, meaning of ✅ interpret the numbers
G Exam twist: scaling / conditioning huge or tiny feature values ✅ but numerically fragile → Feature Scaling

Example 1 — Cell A: overdetermined, noisy, full rank

Forecast: the points roughly climb by per step and start near — guess the answer before reading on.

Figure — Normal equation closed-form solution
  1. Build and .

Why this step? points, feature, so is : first column of 1's (bias), second column the 's.

  1. Form the little square matrix .

Why this step? We can only invert a square matrix. The top-left counts the rows; ; .

  1. Form .

Why this step? This is the right-hand side — it captures how the targets line up with each column.

  1. Determinant then inverse. .

Why this step? confirms full column rank — the plain formula is legal here.

  1. Multiply through.

Result: .

Verify: predictions are ; residuals sum to — exactly what "the residual is orthogonal to the column space" forces. The fit does not pass through every point (Cell A = imperfect), and that is correct.


Example 2 — Cell B: exact fit,

Forecast: these are not perfectly collinear ( is not an arithmetic run) — so predict a near-fit, not an exact one.

  1. From the parent note, , i.e. . Why re-use it? To connect drill and theory — same machinery.

  2. Predict: , , . Why this step? Checking residuals tells us which cell we're really in.

Verify: residuals ; they are non-zero, so this is still Cell A-flavoured. True Cell B needs genuinely collinear points, e.g. exactly, residual . We check that in =VERIFY=.


Example 3 — Cell C: degenerate target (all equal)

Forecast: the truth is obviously , so guess .

Figure — Normal equation closed-form solution
  1. . Why this step? Standard setup; the interesting part is the flat .

  2. , . Why this step? — full rank, formula applies. Varying keeps the columns independent even though is constant.

  3. , then

Result: — a perfectly horizontal line.

Verify: slope is exactly , intercept exactly , residuals all . The machinery gracefully returns a flat line when the data is flat. (Contrast Cell D: it is a degenerate feature, not a degenerate target, that breaks invertibility.)


Example 4 — Cell D: rank-deficient features (the trap)

Forecast: the plain formula will explode — predict "no inverse".

  1. . Notice . Why this step? Spotting a linear relation among columns is the whole point of Cell D.

  2. . Why this step? We compute it to demonstrate its determinant is .

  3. Determinant check: (row/column dependence carries through). Why this step? Zero determinant ⇒ singular does not exist ⇒ plain formula illegal.

  4. Fix: use the pseudoinverse (via SVD/QR), which returns the minimum-norm solution. One exact fit is : . Why this step? Among infinitely many perfect fits, picks the shortest .

Verify: gives ✅ zero error, but the solution is not unique — e.g. -type combos also fit. The determinant equals ; that is the alarm bell.


Example 5 — Cell E: underdetermined ()

Forecast: one equation, two unknowns → infinitely many lines through the point.

  1. (size ), . Why this step? ⇒ underdetermined.

  2. , → singular. Why this step? Underdetermined systems also give a singular .

  3. Min-norm fix: . Here , so

Why this step? When rows are the short dimension, is invertible; this gives the shortest-length solution.

Verify: ✅ passes through the point, and is minimal among all lines through .


Example 6 — Cell F: real-world word problem

Forecast: perfectly linear (?) — guess price \6000x=2.5$.

  1. , . Why this step? Real data → same machinery; units: in $1000s, in $1000s per 100 ft².

  2. , , . Why this step? Full rank ⇒ proceed.

  3. , and

Result: . At : $6000.

Verify: each point fits exactly (), residual ; prediction $6000 matches the forecast, units consistent ($1000s).


Example 7 — Cell G: exam twist (scaling / conditioning)

Forecast: the shape is identical, so should shrink by and stay near .

  1. Fit gives , . Why this step? Slope has units "per "; scaling up by scales slope down by .

  2. But : entries span to . Why this step? Huge range ⇒ ill-conditioned matrix ⇒ inversion loses precision.

Verify: reproduces ✅ etc. The math is right, but the condition number is enormous — this is exactly why we apply Feature Scaling or switch to QR Decomposition / Gradient Descent on real data.

Recall Which cell breaks the plain inverse?

Cells D (redundant features) and E (too few points) ::: both make singular → use the pseudoinverse.

Recall Why does an all-equal

(Cell C) still work? Because the features still vary, so the columns of stay independent ::: only feature/column dependence kills invertibility, not a flat target.


Connections