Exercises — Normal equation closed-form solution
This is the practice ladder for the parent note. Every problem below is solved in full inside a collapsible box, so you can hide the answer, try it yourself, then reveal. The single tool we use throughout is the normal equation:
How the ladder works. L1 = recognise the pieces. L2 = apply the formula on numbers. L3 = analyse when it breaks. L4 = combine it with other ideas (Ridge Regression, Feature Scaling, Gradient Descent). L5 = master the edge behaviour that trips up experts.
Prerequisite refreshers if any step feels shaky: Linear Regression Fundamentals, Underdetermined vs Overdetermined Systems, Moore-Penrose Pseudoinverse.
Level 1 — Recognition
These check that you can read the objects before computing anything.
Recall Solution 1.1
- is . The is the leading column of 's that pairs with the intercept .
- is — one entry for the intercept and one per feature.
- is .
- : is , times (), giving .
Picture it: is always a small square of side no matter how many rows of data you have — that is why the normal equation is cheap when features are few even if data is huge.
Recall Solution 1.2
The first column is all 's so that row dotted with gives . Forget that column and you force the line through the origin.
Level 2 — Application
Now we turn the formula's crank on real numbers.
Recall Solution 2.1
Build. .
Gram matrix. (top-left count ; the 's are ; bottom-right ).
Right side. .
Invert. , so .
Solve. .
Answer: . Line: . See the fit and its residuals below.

Recall Solution 2.2
, .
Solving the system (columns are independent, so it is invertible) gives .
Check: row 1 ✓, row 2 ✓, row 3 ✓. A perfect fit, because 3 points with 3 free parameters can be matched exactly.
Level 3 — Analysis
Here we probe when the formula is legal and what "least squares" geometrically means.
Recall Solution 3.1
Column 3 column 2, so the columns are linearly dependent. Thus , and is singular — its determinant is , so no inverse exists.
Consequence: infinitely many give the same predictions (you can trade weight between the two proportional columns). The plain normal equation cannot be used. The correct tool is the Moore-Penrose Pseudoinverse (computed via SVD), which returns the minimum-norm least-squares solution. Alternatively, drop the redundant feature, or add a Ridge Regression penalty to restore invertibility (see L4).
Recall Solution 3.2
At the optimum the gradient is zero: . Factor: Geometry: every column of is perpendicular ("normal") to the residual. So is the orthogonal projection of onto the column space of — the closest reachable point, which is exactly why the error is minimised. This is the origin of the name normal equation. The projection picture is below.

Level 4 — Synthesis
Combine the normal equation with neighbouring machinery.
Recall Solution 4.1
is symmetric positive semidefinite, so its eigenvalues are ; being singular means at least one eigenvalue is exactly . Adding with shifts every eigenvalue up by : Now all eigenvalues are , so the matrix is positive definite and hence invertible. The formula is well-defined and yields a unique — the ridge penalty picks the shrunken solution among the previously infinite ones. This is the invertibility bridge between the normal equation and Ridge Regression.
Recall Solution 4.2
The normal equation inverts , an matrix, costing operations — brutal. With it is also rank-deficient (), so the plain inverse doesn't even exist.
Reveal: this is the underdetermined regime (more unknowns than equations). Prefer Gradient Descent (cost per step ) or a QR Decomposition / pseudoinverse route. Rule of thumb: normal equation shines when is small (roughly ) and ; otherwise iterate.
Recall Solution 4.3
The trick: treat as a new feature. Design matrix uses columns : , .
Solving gives , i.e. . Check: ✓, ✓, ✓. The model is nonlinear in but linear in , which is all the normal equation needs.
Level 5 — Mastery
Edge behaviour and limiting cases the pros still get wrong.
Recall Solution 5.1
Predictions are — only the sum matters. Least squares onto the direction projects : the best is the mean-matched value .
Among all with , the minimum-norm one minimises , which by symmetry is . Interpretation: the pseudoinverse spreads weight equally across redundant features rather than dumping it all on one — the least-committal, most stable choice.
Recall Solution 5.2
(), so , — singular. One equation, two unknowns: underdetermined, infinitely many lines pass through one point.
The pseudoinverse gives the minimum-norm fit. Since prediction is , minimise on that line: geometrically the closest point on the line to the origin. That point is . Check: ✓.
Recall Solution 5.3
The column space of has dimension . When , that column space fills all of , so the projection of is itself — residual , an exact interpolation (this is why Exercise 2.2 and 4.3 fit perfectly). When , the column space is a proper subspace of ; generally sticks out of it, so its projection and a non-zero residual is unavoidable — the overdetermined regime. Least squares finds the closest reachable , nothing more.

Recall Quick self-test (cloze)
The matrix you invert in the normal equation has size ====. A matrix is invertible exactly when has full column rank. At the optimum the residual is orthogonal (normal) to the column space of . Adding shifts every eigenvalue up by ====, restoring invertibility.
When should you NOT use the plain normal equation? ::: When is singular/ill-conditioned, or when is very large (costly inverse) — use pseudoinverse/ridge/gradient descent instead. What does the pseudoinverse return for a rank-deficient system? ::: The minimum-norm least-squares solution.