2.2.2 · D4Linear & Logistic Regression

Exercises — Multiple linear regression

2,273 words10 min readBack to topic

Before we start, a one-line reminder of the two workhorses. If you want the pictures behind them, revisit the parent note.


Level 1 — Recognition

Can you read the objects and say what each piece is?

Recall Solution 1.1

The "" for the intercept column makes the width of equal to .

  • : — one row per example, one column per feature (plus the 1's).
  • : — one weight per column of .
  • : — one true target per example.
  • : — a square matrix, the thing we invert.
  • : — same shape as , good sanity check.

The trick: in the inner dimensions must match and cancel; the outer two survive. being (not ) is why the Normal Equation is cheap when features are few.

Recall Solution 1.2

The sign of a coefficient is its direction of effect.

  • → raises .
  • → lowers .

Level 2 — Application

Turn the crank on the formulas.

Recall Solution 2.1

Each row of is dotted with . Row = , so the dot product is . Notice: with one real feature this is just Simple Linear Regression written in matrix clothing.

Recall Solution 2.2

. Each entry of the product is a dot product of two columns of .

  • Top-left = (just counts the examples, ).
  • Off-diagonal = (sum of the feature).
  • Bottom-right = . For :
  • Top = (sum of targets).
  • Bottom = .
Recall Solution 2.3

Inverse of a matrix is . Here the determinant is . Multiply by : So , i.e. . (Here , so this is a genuine least-squares fit, not exact.)


Level 3 — Analysis

Diagnose behaviour, don't just compute.

Recall Solution 3.1

Not invertible. is perfect multicollinearity: one column is an exact linear multiple of another. That makes the columns of linearly dependent, so is singular (determinant ) — even though . Having lots of data cannot cure a redundant column. Geometrically: the model can't tell how to split credit between and ; infinitely many pairs give the identical fit. Fixes: (1) drop the redundant column, or (2) add ridge penalty so becomes invertible again. Gradient Descent also converges without ever inverting.

Recall Solution 3.2

Residuals : . Total spread of around its mean : deviations . An of means the model does exactly as well as predicting the mean every time — no explanatory power here. See R-squared and Adjusted R-squared.


Level 4 — Synthesis

Combine ideas; connect to neighbouring topics.

Recall Solution 4.1

Ridge replaces with . Adding to the diagonal: Determinant . Invertible! The diagonal "bump" lifts the flat direction that made the original singular. That is exactly why Ridge Regression stabilises multicollinear problems.

Recall Solution 4.2

The model is nonlinear in but linear in the coefficients — and the Normal Equation only cares about linearity in . So treat and as two separate feature columns: (columns: intercept, , ). Then works unchanged. That is the whole idea behind Polynomial Regression.


Level 5 — Mastery

Full pipeline, reasoning end-to-end.

Figure — Multiple linear regression
Recall Solution 5.1

(a) Build the pieces. (since , ). . Determinant . So (the red line in the figure). (b) Fitted values at : . Residuals : . Notice they sum to — the fitted line balances the errors (a property of the least-squares intercept). (c) .

Recall Solution 5.2

(a) With , is singular, so strictly the inverse doesn't exist — a solver returns a pseudoinverse solution (minimum-norm), which is why you still got numbers. It is one of infinitely many perfect-fit answers. (b) More unknowns than equations means the plane can thread exactly through every training point → zero residual → . That's memorisation, not learning: textbook Overfitting. Perfect training fit with few examples is a red flag. (c) (1) Regularise with Ridge Regression to shrink coefficients and pick a stable solution; (2) reduce features / gather more data so ; (Gradient Descent with early stopping also helps).


Recall Self-test cloze summary

The Normal Equation is ::: is singular when features are perfectly multicollinear or when ::: there are fewer examples than features () The diagonal fix that restores invertibility is called ridge (add ) means the model does no better than ::: predicting the mean of every time