4.5.41 · D3Linear Algebra (Full)

Worked examples — Least squares — normal equations, QR approach

2,736 words12 min readBack to topic

Before we start, one reminder of the vocabulary so no symbol is unearned:

One more symbol we will lean on later:


The scenario matrix

Every least-squares question you meet is one (or a blend) of these cells. The last column names the example that clears it.

# Case class What's special about it Cleared by
C1 Overdetermined, full rank more rows than unknowns, columns independent — the "textbook" case Ex 1
C2 Exactly determined / consistent already sits in , residual Ex 2
C3 QR route on the same data avoid squaring the [[Condition Number condition number]]
C4 Rank-deficient ( singular) dependent columns → infinitely many Ex 4
C5 Weighted / units word problem real-world regression, mixed scales Ex 5
C6 Degenerate limit: two coincident data points what happens as points merge Ex 6
C7 Projection-matrix viewpoint compute , check , split into fit + residual Ex 7
C8 Exam twist: fit through a fixed point constrained model, drop a column Ex 8

We work these in order. Cells C1, C2, C6, C7 get a figure because geometry is doing the talking.


Example 1 — Overdetermined, full rank (cell C1)

The figure below plots the four coral data points and the lavender best-fit line we are about to compute; the mint verticals are the residuals — the up-and-down misses least squares makes as small as possible.

Figure — Least squares — normal equations, QR approach

Step 1 — build and . Why this step? Each row says "predict by ". Column one is all-ones (the intercept ); column two holds the (the slope ).

Step 2 — form and . Why this step? entries are column dot products: , , . dots each column with .

Step 3 — solve the system. Determinant . By Cramer: Why this step? is invertible (full column rank), so is unique.

Best line: . Slope is negative — forecast confirmed. In the figure the lavender line indeed tilts downward.

Verify: predictions at are ; residual . Check orthogonality: and . ✓ (Both are checked in VERIFY.)


Example 2 — Consistent system, zero residual (cell C2)

In the figure the three coral points sit dead-on the lavender line, so there are no mint residual sticks to draw — a picture of a zero residual.

Figure — Least squares — normal equations, QR approach

Step 1 — set up. Why this step? Exactly as before, each row encodes "predict by ": the all-ones first column carries the intercept , the second column carries the inputs for the slope . Only (the measured -values) changes from Ex 1.

Step 2 — normal equations. , . Why this step? We project onto by forcing the residual perpendicular to both columns; the entries are again column dot products (, , ) and dots each column with .

Step 3 — solve. From and : subtract → , then . Why this step? Two linear equations in two unknowns; subtracting the first from the second cancels (both have coefficient ), leaving a single equation in . Back-substitute into the first to recover — the same elimination you'd use on any system.

Line: — the exact line, as forecast.

Verify: predictions , so . When , the projection is and least squares degenerates to an ordinary solve. ✓


Example 3 — Same data via QR (cell C3)

Step 1 — orthonormalise. since . Projection coefficient . Remove it: . Normalise: . Why this step? Orthonormal columns turn projection into simple dot products — no matrix inverse needed.

Step 2 — read off . Why this step? , , .

Step 3 — compute and back-substitute. Solve bottom-up: ; then . Why this step? is upper triangular so the last equation has one unknown — back-substitution is fast and doesn't square the condition number.

Verify: , matching Ex 2 exactly. ✓


Example 4 — Rank-deficient: infinitely many solutions (cell C4)

Step 1 — spot the rank. is a single line spanned by ; rank , not . Why this step? If columns don't span two independent directions, we can't pin down two coordinates.

Step 2 — normal equations. Singular — no inverse. . Why this step? The normal equations are still the right machinery even when columns are dependent — we form them exactly as usual (entries are column dot products, dots each column with ). Computing the determinant tells us in advance that this cannot be inverted, which is the whole point of this case.

Step 3 — describe ALL solutions. The system (the second row is just this) gives . Let be any real number and set ; then , so every with is a valid least-squares solution. Why this step? One equation, two unknowns → the second unknown is free, so a whole line of answers, indexed by the free parameter .

Step 4 — the projection is still unique. For any such , . Why this step? Different 's along the ambiguous direction all map to the same point of — the projection of . The pseudoinverse picks the minimum-norm representative.

Verify: residual ; check . Minimum-norm choice minimises : derivative , giving . ✓


Example 5 — Word problem with real units (cell C5)

Step 1 — one-column . Why this step? Model has one unknown and passes through the origin, so there's no all-ones column — just the values. This is regression through the origin.

Step 2 — scalar normal equation. With one column, and are scalars: Why this step? The dot product weights larger stretches more — they carry more "leverage".

Step 3 — solve. N/cm. Why this step? With only one unknown the normal equation collapses to a single scalar equation ; dividing both sides by the scalar isolates — the one-dimensional version of "invert ".

Verify: units check — N/cm. ✓ Predicted forces hug the data; residuals small. ✓ ( checked in VERIFY.)


Example 6 — Degenerate limit: coincident data (cell C6)

In the figure the two clashing coral points sit stacked at ( and ); the butter dot marks where the lavender fit-line actually passes — right between them at , the vertical average shown by the mint stick.

Figure — Least squares — normal equations, QR approach

Step 1 — set up. Why this step? Two rows share the same ; the design is still full column rank (columns and are independent), so a unique line exists.

Step 2 — normal equations. , , .

Step 3 — interpret. Line . At it predicts — exactly the average of the two clashing readings and , as forecast. Why this step? When several rows share the same input, least squares can only pick one output there, and the distance-minimising choice is their mean.

Verify: predictions , residual ; , . ✓


Example 7 — Projection matrix, split (cell C7)

The figure is a 2D schematic: the lavender line is , the coral arrow is , the mint arrow is its shadow on the line, and the dashed slate segment is the residual — meeting the line at a right angle (marked), which is exactly the orthogonality least squares enforces.

Figure — Least squares — normal equations, QR approach

Step 1 — build from the definition. Recall . Here is the single column , so (a scalar), its inverse is , and is the all-ones matrix. Substituting, Why this step? With one column, the general formula collapses to — the orthogonal projection onto that line. Each row of is , i.e. "take the average of the three entries" — precisely what projecting onto should do.

Step 2 — idempotence check. because averaging an already-constant vector changes nothing, and since the all-ones matrix is symmetric. Why this step? Projecting twice = projecting once; these two identities (, ) are the defining fingerprints of any projection matrix, confirming we built correctly.

Step 3 — split into fit + residual. Why this step? lies in the line (the fit — it is a multiple of ), is perpendicular to it (the miss); the two pieces add back to , splitting it into "explained" plus "leftover".

Verify: ✓; (Pythagoras). ✓


Example 8 — Exam twist: fit forced through a fixed point (cell C8)

Step 1 — reduce the model. Forcing removes the intercept column, leaving a single column of -values: Why this step? A hard constraint "pass through the origin" isn't an extra equation to bolt on — it deletes the unknown , shrinking from two columns to one. Fewer unknowns, same least-squares machinery.

Step 2 — scalar solve. With one column, and , so Why this step? Same origin-regression scalar formula as Ex 5: one unknown means the normal equation is a single scalar equation , solved by dividing.

Step 3 — interpret. Best origin line: , slope inside the forecast band .

Verify: ; predictions , residual , check . ✓


Recall Which cell am I in? (decision flow)

Is invertible? ::: Yes → unique (C1/C2/C5/C6/C8). No → infinitely many, use pseudoinverse (C4). Is already in Col(A)? ::: Then residual is zero and least squares reduces to an ordinary exact solve (C2). Model must pass through a fixed point? ::: Drop the corresponding column, re-solve (C8). Worried about rounding / big condition number? ::: Use QR: (C3).

Active Recall

Why is the fit unique even when is not (rank-deficient case)?
All solutions map to the same point of Col(A) — the projection of — differing only along the null space of .
For a coincident-input dataset, what does the line predict there?
The average of the clashing -values (distance-minimising single output).
How do you enforce "line through the origin" in least squares?
Drop the intercept (all-ones) column, leaving a one-column origin regression .
When does the residual equal zero?
When (consistent system); the projection returns itself.
Write the projection matrix onto Col(A) and its two defining properties.
, with and .