2.2.14 · D3Linear & Logistic Regression

Worked examples — L2 (Ridge) regularization

3,013 words14 min readBack to topic

This page is a drill. The parent note built the theory; here we hit every kind of situation Ridge can throw at you — every sign of , degenerate inputs, limiting values, correlated twins, a word problem, and an exam trap. Guess before you compute.

Before we start, a one-line reminder of the two formulas we lean on the whole way down.


The scenario matrix

Every Ridge problem is really one of these cells. Each worked example below is tagged with the cell(s) it covers.

# Cell (case class) What is special Example
A Ridge = plain OLS Ex 1
B , one feature denominator inflated, weight shrinks Ex 1
C weight (limiting behaviour) Ex 2
D Singular ( or duplicate cols) OLS undefined, Ridge still works Ex 3
E Correlated twins huge cancelling weights → even split Ex 4
F Sign of correlation negative () shrinks a negative weight toward 0 Ex 5
G Two independent features (diagonal ) each weight shrinks by its own factor Ex 6
H SVD view / limiting factors per direction Ex 7
I Real-world word problem choose , standardize Ex 8
J Exam twist (penalized intercept trap) why is excluded Ex 9

Example 1 — Cells A & B: the scalar knob

Step 1. Use the scalar formula . Why this step? One feature means the whole matrix formula collapses to a single fraction — no inverse needed.

Step 2. (this is the OLS answer — Cell A). Why this step? Setting removes the penalty entirely, so we must land back on plain least squares.

Step 3. . The weight halved (Cell B). Why this step? Adding to the denominator is the entire mechanism of shrinkage — bigger denominator, smaller weight.

Step 4. . Why this step? Shows shrinkage is smooth and continuous, not a cliff.

Recall Verify

The scalar Ridge loss is . Differentiate term by term: , and . Adding them and setting to zero: . Plug our numbers (): . ✓ Matches Step 3.


Example 2 — Cell C: the limit

Step 1. . Why this step? Numerator fixed, denominator , so the ratio squeezes to — this is the maximum-bias / underfit extreme.

Step 2. No finite gives exactly : solving needs , impossible. Why this step? Confirms the "shrinks toward but never reaches zero" claim — Ridge asymptotes, it doesn't snap.

Step 3. Solve . Why this step? Concrete demo that reaching near-zero needs a large .

Recall Verify

. ✓ And units: shares units of (a "variance budget"), consistent.


Example 3 — Cell D: singular , OLS dies, Ridge lives

Step 1. Build . With , . Its determinant is singular → OLS's does not exist. Why this step? Shows exactly why plain OLS fails: you cannot invert a zero-determinant matrix.

Step 2. Add : . Determinant invertible. Why this step? This is the "ridge on the diagonal" cure — lifts the diagonal so the matrix becomes positive-definite. See Singular Value Decomposition for the deep reason.

Step 3. Inverse: . And . Why this step? inverse is — plug in and go.

Step 4. . Why this step? Ridge returned a unique, symmetric answer where OLS had none.

Recall Verify

Symmetry: identical columns must get identical weights, so . ✓ Fit: prediction , pulled below because trades a little fit for smaller weights. ✓


Example 4 — Cell E: correlated twins, cancelling weights tamed

Step 1. Both OLS candidates fit training data: and . Identical predictions. Why this step? Establishes that fit alone cannot distinguish them — the penalty must decide.

Step 2. Compare penalties: versus . Why this step? Ridge adds to the cost; the cancelling pair is more expensive, so Ridge will run from it.

Step 3. Now compute the actual Ridge weights. Build . Then and . Why this step? The column dot products fill ; note it's singular (det ), so plain OLS again has no unique answer — Ridge must rescue it.

Step 4. Add : , det , inverse . Why this step? lifts the diagonal from to , making the matrix invertible.

Step 5. . Why this step? This is the concrete answer. It is the even split pulled slightly toward zero — the "tiny shrink" is exactly the factor , i.e. each weight is scaled by in disguise.

Recall Verify

Symmetry forces . Solve the reduced 1-feature Ridge: . ✓ And , so it's the even split shrunk toward zero, exactly as claimed. ✓


Example 5 — Cell F: shrinking a negative weight

Step 1. . Why this step? Negative numerator → negative OLS weight; the feature pushes down.

Step 2. . Why this step? Denominator inflates exactly as before — magnitude halves, sign unchanged.

Step 3. So as grows. Why this step? Shrinkage means "toward zero," which for a negative weight means moving up toward , not toward .

Recall Verify

✓; sign preserved ( and both negative) ✓; so magnitude shrank ✓.


Example 6 — Cell G: two independent features shrink separately

Step 1. — still diagonal, so it decouples. Why this step? A diagonal matrix inverts entrywise; the two features don't interfere.

Step 2. ; . Why this step? Each feature follows its own scalar rule .

Step 3. Compare to OLS: (dropped to 1.6, a cut); (dropped to 1.5, a cut). Why this step? Reveals the key law: features with a small diagonal entry shrink harder. The same is a bigger fraction of a small diagonal entry.

Recall Verify

✓, ✓. Fractional shrink: strong , weak → weak shrinks more (0.5 < 0.8) ✓.


Example 7 — Cell H: SVD shrinkage factors, both limits

First, the one definition this example needs.

Step 1. Strong: . Why this step? Large dwarfs , so the ratio → this direction is kept.

Step 2. Noisy: . Why this step? Small is swamped by , so the ratio → this direction is shrunk away.

Step 3. Limits: as , both factors (recover OLS, Cell A). As , both (Cell C). Why this step? Confirms the factor smoothly interpolates the two extremes we already met.

Recall Verify

✓; ✓; both at and at ✓.


Example 8 — Cell I: real-world word problem

Step 1. Say the true effect of area needs weight so that matters. To move price the same amount, can be smaller than (which multiplies ). Why this step? Weight magnitude is inversely tied to feature scale — big-valued features earn tiny weights.

Step 2. The penalty then barely touches the tiny but hammers . If and , penalty contributions are vs — a imbalance. Why this step? Shows the penalty is scale-unfair: it silently favours large-valued features by leaving their (tiny) weights almost unpenalized.

Step 3. Fix: standardize each feature to zero mean, unit variance. Now both features live on the same scale and penalizes them equally. Choose by Cross-Validation, never by training error. Why this step? Standardization makes compare apples to apples; CV picks the valley of the validation curve.

Recall Verify

Penalty ratio raw ✓ — precisely the square of the scale ratio , confirming the unfairness scales with (unit ratio).


Example 9 — Cell J: exam twist, penalizing the intercept

Step 1. No penalty: minimize . Differentiate: . Correct baseline. Why this step? The unpenalized intercept is just the mean of — exactly what a baseline should be.

Step 2. Penalized: minimize . Since every , this is the scalar formula with "": . Why this step? Penalizing drags the baseline toward — here from down to , a systematic error for no benefit.

Step 3. Conclusion: exclude from the penalty (or center first). The intercept sets the output level, not the model's complexity. Why this step? Complexity lives in the slopes; shrinking the level just biases every prediction toward zero.

Recall Verify

Unpenalized ✓; penalized ✓; so it's biased toward zero ✓.


Recall

Recall Quick self-test

Scalar Ridge weight with ::: Why does OLS fail on one example with two features? ::: is singular (det ), not invertible Ridge answer for ::: Exact Ridge weights for the twin features at ::: Which shrinks more: strong () or weak () feature at ? ::: the weak one (factor vs ) SVD shrinkage factor at ::: Penalized intercept for ::: (biased below the mean — don't do this) Does any finite set a scalar Ridge weight exactly to ? ::: No — it only asymptotes to