2.2.3 · D4Linear & Logistic Regression

Exercises — Ordinary least squares derivation

2,218 words10 min readBack to topic

This is a self-testing companion to the OLS derivation. Work each problem BEFORE opening its solution. Problems climb from L1 (Recognition) — "do you know what the symbols mean?" — up to L5 (Mastery) — "can you extend the method to new territory?".

Everything you need was built in the parent note. Two quick reminders in plain words:

Here = number of data points, = number of real features (so columns after we glue on the bias). New notation as it appears: = the matrix that undoes (i.e. , the identity, the "do nothing" matrix); = the determinant, a single number that is exactly when has no inverse.

Prerequisites if you get stuck: Linear Regression Fundamentals, Gradient Descent, Ridge Regression, Pseudoinverse.


Level 1 — Recognition

Recall Solution 1.1

WHAT we do: just count rows and columns.

  • Feature block (no bias): — four points, two features.
  • (bias glued on): — the extra column of s is the third column.
  • : — one number per column of ( for the bias, then ).
  • : — one target per data point.
  • : is , times which is , gives .

WHY it's square: is always . That is why we can ask whether it is invertible at all — only square matrices have inverses.

Recall Solution 1.2
  • holds the given data (fixed once your dataset is chosen).
  • holds the unknowns you solve for.
  • Number of unknowns : one weight per feature () plus the intercept .

Level 2 — Application

Recall Solution 2.1

Step 1 — build and (first column is the bias s): Step 2 — (WHY: the normal equation needs it): (Top-left ; the s are ; bottom-right . For : and .) Step 3 — invert. . Step 4 — solve : Answer: , , so . At : .

Look at the fitted line in the figure below — it splits the gap between the point at (above) and the trend, exactly the balancing act least squares performs.

Figure — Ordinary least squares derivation
Recall Solution 2.2

Predictions: . Residuals . Check :

  • Row of s: . ✓
  • Row of 's : . ✓

Geometric meaning: the residual vector is perpendicular to every column of — the leftover error points straight out of the space of fittable lines, so no adjustment of can shrink it. That perpendicularity is literally why it's called the "normal" equation.


Level 3 — Analysis

Recall Solution 3.1

Look for a linear dependence among columns. Call them , , . Test : solving , gives ; check the third row . ✓ So exactly — the columns are linearly dependent, meaning does not have full column rank. Therefore and the inverse does not exist. The cost has infinitely many minimisers (any shift of along the dependence direction leaves unchanged). Cure: drop the redundant feature, or add regularisation (Ridge Regression).

Recall Solution 3.2

The metres column and the feet column are scalar multiples of each other, so they count as one independent direction. With the bias that gives , but has columns — so it is rank-deficient (). This is the "perfectly correlated features" failure (°C vs °F is the same story). is singular; use Feature Scaling won't save you — you must remove one of the duplicated features or regularise.


Level 4 — Synthesis

Recall Solution 4.1

New cost: . Gradient (using the same rules from the parent: , ): Set to zero: . Why invertible: is always positive semi-definite (eigenvalues ). Adding bumps every eigenvalue up by , so none is zero — the matrix becomes positive definite and always invertible. See Ridge Regression.

Recall Solution 4.2

WHAT: the probability of one point is the Gaussian density WHY log: the full likelihood is the product over all points; taking turns the product into a sum we can differentiate, and is increasing so the maximiser is unchanged. Only the last term depends on , and it carries a minus sign. So maximising = minimising — the least-squares sum. Hence OLS is maximum likelihood under Gaussian noise. See Maximum Likelihood Estimation.


Level 5 — Mastery

Recall Solution 5.1

WHY QR: because , the messy collapses: Plug into : . Cancel the invertible from the left: Because is triangular you solve by back-substitution — no inverse, and it is numerically far kinder (no squaring of the condition number). See QR Decomposition and Pseudoinverse. Numeric check: for the 2.1 data a QR factorisation gives and an upper-triangular whose back-substitution yields — identical to the direct-inverse answer, as it must (both solve the same linear system). ✓

Recall Solution 5.2

Here is : fewer rows than columns (). Now is but singular; instead (a number) is invertible. The correct Moore–Penrose pseudoinverse is the right form: Compute , so , and : Check: ✓, and among the infinitely many that hit the point, this one has the smallest — the minimum-norm solution the pseudoinverse always picks.


Recall Self-test: state each answer from memory

Exercise 2.1 fitted line ::: Why is always square ::: it is , so we can ask if it inverts Condition for to exist ::: has full column rank (independent columns) Ridge normal equation ::: QR-reduced system ::: , solved by back-substitution Wide-matrix () solution :::