4.5.41 · D4Linear Algebra (Full)

Exercises — Least squares — normal equations, QR approach

1,871 words9 min readBack to topic

Quick reminder of the two master equations you will use constantly:


Level 1 — Recognition

Problem 1.1

Which of these systems needs least squares (no exact solution guaranteed), and which is an ordinary square solve? State why.

(a) is . (b) is invertible. (c) is but rank .

Recall Solution

What decides it: least squares is for tall systems — more equations than unknowns — where may not lie in the column space.

  • (a) : tall (). Overdetermined → use least squares.
  • (b) invertible: square with an inverse → ordinary solve , exact.
  • (c) rank : square but rank-deficient (columns dependent). No unique inverse; least squares still applies but is not unique — you'd reach for the Pseudoinverse (Moore-Penrose). So: least squares in the generalised sense, minimum-norm solution.

Problem 1.2

Write down, without deriving, the normal equations and the projection matrix onto for a full-column-rank .

Recall Solution

Normal equations: . Explicit solution: . Projection matrix: , satisfying (projecting twice = projecting once) and (symmetric).


Level 2 — Application

Problem 2.1

Fit the best horizontal line (a constant, no slope) to the data -values . Find using the normal equations.

Recall Solution

Set up. Predicting each point by the single constant means with a single column of ones: Compute. , and . Solve. . Meaning: the best constant fit is just the average of the data — least squares with a column of ones is the mean. That's the simplest Linear Regression there is.

Problem 2.2

Fit to points . Find .

Recall Solution

Set up. Row says : Compute (entries are dot products of columns and ): Solve . Subtract (row 1) from row 2: . Then . Best line: . See the figure — the miss-arrows point straight up/down from the line.

Figure — Least squares — normal equations, QR approach

Level 3 — Analysis

Problem 3.1

For Problem 2.2, compute the residual and verify . Explain geometrically what says.

Recall Solution

Predictions with : Residual : Check orthogonality: Both zero, so . ✓ Geometry: the residual arrow is perpendicular to every column of , hence perpendicular to the whole plane . That's the Orthogonal Projection condition — is the shadow of on that plane, and is the straight-out gap.

Problem 3.2

The condition number measures how much a matrix amplifies input errors. Given a matrix with , compare the error-amplification of solving via normal equations vs QR. Which do you trust and why?

Recall Solution

Key fact: forming squares the condition number: .

  • Normal equations solve a system governed by .
  • QR works directly with , whose conditioning matches .

In digits: double precision carries about significant digits. Losing digits (normal equations) leaves good digits; losing digits (QR) leaves . Trust QR — it never forms , so it avoids the squaring. See Condition Number and QR Factorization.


Level 4 — Synthesis

Problem 4.1

Solve Problem 2.2 again, but this time by QR. Build by Gram-Schmidt Process on the columns , then solve . Confirm you get .

Recall Solution

Step 1 — normalise the first column. , so Step 2 — remove 's shadow along . The overlap is . Subtract: Its length is , so . Step 3 — read off (upper triangular by construction): Step 4 — compute with : Step 5 — back-substitute . Bottom row first: Top row: , i.e. , so . Same answer — no was ever formed. ✓

Problem 4.2

Show algebraically that is the same equation as the normal equations when and is invertible.

Recall Solution

Start from the normal equations and substitute : Transpose of a product reverses order: . So the left side is . Because has orthonormal columns, (the identity), which drops out: is invertible (full rank), so is invertible; multiply both sides on the left by : They are algebraically identical — QR is just a numerically safer road to the same .


Level 5 — Mastery

Problem 5.1

A rank-deficient case. Let (a) Show is not invertible. (b) Explain what goes wrong with . (c) Find the projection anyway — it is still unique — and give the minimum-norm .

Recall Solution

(a) Column 2 is exactly column 1, so rank (columns dependent). Compute: Zero determinant ⇒ not invertible. (b) does not exist, so the formula is meaningless; the normal equations have infinitely many solutions (a whole line of them), because any shift along the dependency direction leaves unchanged. (c) The column space is the single direction (since both columns are multiples of it). Project onto : This projection is unique even though is not. To pick one : any with works. The minimum-norm choice (via Pseudoinverse (Moore-Penrose)) minimises subject to ; using gives , and indeed . ✓

Problem 5.2

Weighted least squares. Suppose measurement 2 is twice as reliable as measurements 1 and 3, so you weight the residuals by and minimise . Show this is ordinary least squares on and where , and write the modified normal equations.

Recall Solution

Rewrite the objective. With , because scales each component and sums the squares. This is exactly ordinary least squares with data matrix and target . Apply the normal equations to : Since is diagonal (symmetric), , so with : Meaning: trustworthy points (, so ) pull the fit toward themselves four times harder. This is the bridge to weighted Linear Regression.


Recall One-line self-check for the whole page

Can you, from memory, (1) set up for a line fit, (2) solve via normal equations, (3) solve the same problem via QR, (4) verify , and (5) explain why QR beats normal equations on conditioning? If yes for all five, you own this topic.