2.2.14 · D2Linear & Logistic Regression

Visual walkthrough — L2 (Ridge) regularization

2,346 words11 min readBack to topic

We assume nothing. If a symbol appears, we first say what it means and show what it looks like.


Step 0 — The picture we are trying to fit

Imagine you have some dots on a graph. Each dot is one example: a horizontal position (the thing you measured) and a vertical position (the thing you want to predict).

Figure — L2 (Ridge) regularization

Look at the figure: the teal dots are data, the orange line is our guess , and each plum stick is one residual. Fitting means: choose so the sticks are collectively as short as possible.


Step 1 — Measuring total wrongness: the sum of squared residuals

WHAT. We turn "the sticks are short" into a single number to minimize.

Reading it term by term:

  • is one plum stick (a residual).
  • We square it so up-gaps and down-gaps both count as positive wrongness (a gap is just as bad as a gap), and so big gaps hurt disproportionately.
  • adds up the squared sticks over all dots.

WHY squares and not absolute value? Squaring makes a smooth parabola in — and parabolas have exactly one bottom you can find with calculus. Absolute value has a sharp corner (that road leads to L1 (Lasso) regularization, a different story).

PICTURE. Plot against the weight . It is a single bowl.

Figure — L2 (Ridge) regularization

The teal bowl dips to its lowest point at the OLS weight — the slope that makes the sticks shortest.


Step 2 — Why the bowl can betray you: chasing noise

WHAT. Watch what happens when the data is a little noisy or the features nearly repeat each other.

WHY this matters. If two features carry almost the same information, the bowl's bottom becomes a long, nearly flat trench instead of a sharp point. A flat trench means: many wildly different weight pairs give almost the same fit. OLS is free to pick a crazy one — like — that happens to sit in the trench.

PICTURE. In 2D (two weights ) the cost is a set of nested contour ovals. Correlated features stretch those ovals into a long valley.

Figure — L2 (Ridge) regularization

The orange ovals are lines of equal cost. Their long axis is the trench. The OLS minimum (orange dot) can drift far from the origin along that trench — huge, cancelling weights. That is the disease Ridge cures.


Step 3 — The fee: adding a penalty on weight size

WHAT. We charge a fee proportional to the squared length of the weight vector and add it to the cost.

Before the formula, two pieces of notation we are about to use:

Term by term:

  • is now a vector of weights (one per feature). is the number of features.
  • is the model's prediction for example : a dot-product that multiplies each feature of that example by its weight and sums the results.
  • is the squared length of the weight vector — how far sits from the origin.
  • is the price per unit of squared length. Small = cheap weights; huge = weights are expensive.

WHY squared length? It is a smooth bowl centred at the origin — a second bowl we add on top of the data bowl. Small weights = near the origin = low fee. It grows fastest for the largest weights, so it punishes exactly the runaway pairs from Step 2.

PICTURE. The fee alone is its own bowl, lowest at .

Figure — L2 (Ridge) regularization

Step 4 — Two bowls add up, and the minimum slides home

WHAT. The Ridge cost is (data bowl) (fee bowl). Adding two upward bowls gives another upward bowl — but its bottom sits between the two.

WHY this is the whole point. The data bowl wants at (possibly far out in the trench). The fee bowl wants at the origin. Their sum compromises: the minimum is pulled from toward . Bigger = steeper fee bowl = more pull. This is shrinkage.

PICTURE. In 1D, watch three bowls stack.

Figure — L2 (Ridge) regularization

The teal data bowl (min at ), the plum fee bowl (min at ), and the orange total bowl whose min sits in between. The orange dot is the Ridge answer.


Step 5 — Do the calculus: find the bottom of the total bowl

WHAT. The bottom of a bowl is where its slope (gradient) is zero. We write the cost in matrix form and set the gradient to .

Matrix pieces (plain words):

  • = the data table, rows (examples) by columns (features).
  • = the column of targets.
  • = all predictions at once.
  • = all residual sticks at once.
  • = the == identity matrix== — a -by- square with s on the diagonal and s elsewhere, the matrix version of the number (it leaves any vector unchanged). It is so it lines up with , which is also ; only then can we add them.

The two derivative rules we need, in plain words. The gradient just collects the ordinary derivative with respect to each weight into a vector. Two little facts do all the work:

  • Rule A: . Reason: , and the derivative of with respect to is — exactly like in one variable. Stack those up and you get .
  • Rule B: when is symmetric. This is the vector version of : the quadratic form is the multi-dimensional "," and its slope grows linearly as .

Now expand and differentiate term by term. First multiply out the data-fit part:

Differentiate each:

  • has no in it → derivative .
  • is linear; the derivative of is , so this gives .
  • is a quadratic form with the symmetric matrix ; by Rule B its derivative is .
  • The fee by Rule A gives .

Add them:

WHY the gradient? For a bowl, the single point where every direction is flat is the bottom — the global minimum. Because both bowls are convex, their sum has exactly one bottom, so this equation has one answer.

Cancel the 's and gather :

PICTURE. The gradient is an arrow that points uphill; at the minimum the data's uphill push and the fee's inward push cancel.

Figure — L2 (Ridge) regularization

At the orange dot the teal arrow (data wanting to move outward) and the plum arrow (fee wanting to move to origin) are equal and opposite — total gradient zero.


Step 6 — The ridge on the diagonal: why saves a broken problem

WHAT. Look at . Adding down the diagonal is where the name "ridge" comes from.

WHY it matters (degenerate case). If features are perfectly correlated, or if you have more features than examples (), then is singular — you cannot invert it, OLS has no unique answer (the trench of Step 2 is perfectly flat, infinitely long). Adding with lifts the flat trench into a genuine bowl: becomes positive definite → always invertible. This connects to Singular Value Decomposition: every singular value becomes .

PICTURE. A cross-section: the flat OLS trench versus the lifted Ridge bowl.

Figure — L2 (Ridge) regularization

The grey flat line is the singular OLS trench (no unique bottom). Adding the plum fee tilts it into the orange bowl with one clear minimum.


Step 7 — Sweep : all the edge cases at once

WHAT. Trace as goes from to .

  • : fee bowl vanishes → we recover Ordinary Least Squares. Full fit, maybe wild weights.
  • : weights shrink smoothly toward , never reaching exactly (that's L1 (Lasso) regularization, whose corners hit the axes).
  • : fee dominates, every weight → flat, underfit model.

The sweet spot in the middle trades a little bias for a big drop in variance, and you find it with Cross-Validation — never with training error.

PICTURE. The shrinkage path: each weight's value drawn against .

Figure — L2 (Ridge) regularization

Every coloured curve is one weight. At the left () they start at their OLS values (possibly large and opposite); as grows they all slide continuously toward the zero line but never touch it.


The one-picture summary

Figure — L2 (Ridge) regularization

One frame holds the whole derivation: the teal data contours pulling outward, the plum fee circle pulling inward, and the orange Ridge solution sitting at the tangency where the two forces balance — always closer to the origin than the OLS point, always unique.

Recall Feynman retelling — say it to a 12-year-old

You draw a line through some dots and measure how wrong you are by adding up the squared gaps — that's one bowl-shaped scorecard, lowest where the line fits best. Trouble: if two of your inputs are near-twins, that bowl flattens into a long ditch and the computer picks a crazy line, giant slopes that secretly cancel. So you add a second bowl that charges a fee for big slopes, lowest when the slopes are zero. Add the two bowls: the new lowest point is squeezed between "fit the data" and "stay small," so your slopes shrink to something calmer. To find that point you look for where the ground is flat in every direction — set the slope of the total bowl to zero — and out pops . The little added down the diagonal is a magic lift: even when the plain problem has no unique answer, adding the ridge turns the flat ditch into a real bowl with one bottom. Crank up and every slope glides toward zero but never quite lands there — smooth shrinking, not switching off.

Recall Quick self-check

What does do to a singular ? ::: Lifts every eigenvalue by , making the matrix positive-definite and invertible → a unique solution Where does the Ridge minimum sit relative to OLS? ::: Between the OLS point and the origin — pulled toward , more so for larger Why squared residuals and squared weights? ::: Both give smooth convex bowls, so their sum has exactly one global minimum found by setting the gradient to zero