5.6.1 · D4Machine Learning (Aerospace Applications)

Exercises — Linear regression — normal equation, gradient descent derivation

2,377 words11 min readBack to topic

This page is a self-test ladder. Each problem is graded from L1 (just recognise the pieces) up to L5 (mastery — combine ideas you had to build yourself). Every solution is hidden inside a collapsible callout: try first, then reveal.

Before symbols appear, here is the vocabulary you actually need, in plain words and pictures.

Figure — Linear regression — normal equation, gradient descent derivation

Recall the two tools we keep reusing (built fully in the parent note):

Related ideas you may want to open alongside: Least Squares Estimation, Matrix Pseudoinverse, Convex Optimization, Feature Scaling, Regularization (Ridge, Lasso).


Level 1 — Recognition

Recall Solution L1.1

With the bias column there are columns.

  • is (rows points, columns featuresbias).
  • is , so is .
  • is .
  • solves a system so it is .

Sanity check: the number of unknowns in () always equals the number of columns of .

Recall Solution L1.2

(b). (a) is absolute error — kinks make its derivative undefined at , so calculus is harder. (c) is not squared, so errors above and below the line cancel; you could have huge errors summing to zero. Squaring in (b) makes every error positive and gives a smooth bowl to slide down.


Level 2 — Application

Recall Solution L2.1

Top-left of counts the points: . Off-diagonal is . Bottom-right is . : first entry ; second .

Recall Solution L2.2

Determinant: . So , . Line: .

Check by eye: at it predicts (true ), at predicts (true ) — errors balanced, as least squares demands.

Recall Solution L2.3

Predictions with are all , so error . Gradient :

  • first entry
  • second entry

Level 3 — Analysis

Recall Solution L3.1

The normal equation is . Rearrange: Meaning (see figure): is the shadow (projection) of onto the flat plane spanned by 's columns. The leftover points straight out of that plane — the shortest possible leftover. Any other leaves a longer residual.

Figure — Linear regression — normal equation, gradient descent derivation
Recall Solution L3.2

Column 3 column 2, so the columns are linearly dependent. Then is singular (determinant ) and does not exist — the normal equation has infinitely many solutions. Geometrically the "plane" the columns span collapses to a lower dimension, so 's projection is well-defined but the coordinates that reach it are not unique. This is multicollinearity. Fixes: drop a redundant feature, or add Ridge which replaces with (always invertible for ), or use the Matrix Pseudoinverse which picks the minimum-norm solution.

Recall Solution L3.3

The second derivative (Hessian) of is . For any nonzero direction , , and it is when the columns are independent. A matrix with all positive curvatures is positive definite, so is strictly convex — a single bowl. Every downhill path reaches the same bottom, which is why gradient descent cannot get stuck in a fake local minimum here.


Level 4 — Synthesis

Recall Solution L4.1

, . , det . : ; . So . (ii) Because is strictly convex (L3.3) and is invertible, gradient descent with small enough converges to the same unique minimum . The two methods must agree — they solve the same equation, one in a jump, one by walking.

Recall Solution L4.2

The curvature in each direction is set by that feature's . With ranges vs the bowl is a long thin canyon: steep across the small feature, nearly flat along the big one. A single learning rate must be tiny enough not to overshoot the steep wall — so it barely moves along the flat floor. Result: zig-zagging, glacial convergence. Scaling (e.g. subtract mean, divide by std) makes all comparable, turning the canyon into a round bowl where one works in every direction. The normal equation is immune (no ), which is one reason it's convenient for small .

Recall Solution L4.3

Take the gradient and set to zero. The extra term contributes : Multiply through by and group : Since has eigenvalues , adding () shifts every eigenvalue up by — none can be zero, so the matrix is invertible even under the multicollinearity of L3.2. See Regularization (Ridge, Lasso).


Level 5 — Mastery

Recall Solution L5.1

Normal equation is out: forming and inverting () costs operations plus entries of memory — infeasible, and it can't ingest a live stream. Full batch GD touches all rows per step — expensive per iteration. The right choice is mini-batch / SGD: each step uses a small batch (e.g. 256 rows), cost per step, and it naturally consumes the stream in real time. Trade-off: noisier steps, so use a decaying . This is exactly the setting behind Kalman Filtering and online estimators, and mini-batch GD is the workhorse of Neural Networks for the same scaling reason.

Recall Solution L5.2

, . (det ). : , . (ii) Predictions : at give . Residuals . . Cost . (iii) : first entry ; second . ✓ Residual is orthogonal to both columns, confirming the projection picture.

Recall Solution L5.3

Convergence needs the contraction factor's magnitude : Above this, oscillates without shrinking, and diverges — each step overshoots further than the last. The safe rule: (in general, of the Hessian).

Recall Self-test cloze

The normal equation solution is ::: Gradient descent diverges when exceeds ::: of the Hessian Ridge stays invertible because it adds ::: , lifting every eigenvalue above zero At the optimum the residual is ::: orthogonal to every column of ()