Speed

Ridge vs Lasso

Compare proportional L2 damping against additive L1 soft-thresholding across strong, medium, and weak coordinates.

The problem

Put the two canonical 1D regularized objectives side by side on the same orthonormal signal vector zz:

  • Ridge minimizes 12(w−z)2+12λw2\frac{1}{2}(w - z)^2 + \frac{1}{2}\lambda w^2, yielding wridge=z1+λw_{\text{ridge}} = \frac{z}{1 + \lambda}.
  • Lasso minimizes 12(w−z)2+λ∣w∣\frac{1}{2}(w - z)^2 + \lambda |w|, yielding wlasso=S(z,λ)=sign⁡(z)max⁡(∣z∣−λ,0)w_{\text{lasso}} = S(z, \lambda) = \operatorname{sign}(z)\max(|z| - \lambda, 0).

Their algebraic difference governs every modeling trade-off between them. Ridge divides every coordinate by 1+λ1 + \lambda, shrinking large signals heavily while leaving tiny noise coordinates nonzero. Lasso subtracts a fixed λ\lambda from surviving magnitudes and zeros out anything inside [−λ,λ][-\lambda, \lambda], sparing large signals from proportional bias while pruning weak ones completely.

Work through the numbers

Compare both rules at lam = 1.0 on four coordinates z = [4.0, -2.0, 0.8, -0.4].

Under Ridge, divide every entry by 1+1.0=2.01 + 1.0 = 2.0 to get w_ridge = [2.0, -1.0, 0.4, -0.2]. Every feature loses 50%50\% of its magnitude: strong z1=4.0z_1 = 4.0 forfeits 2.0 units, while weak z3=0.8z_3 = 0.8 and z4=−0.4z_4 = -0.4 linger at 0.4 and -0.2.

Under Lasso, subtract 1.0 from each magnitude and clamp at zero to get w_lasso = [3.0, -1.0, 0.0, 0.0], stacking into [[2.0, -1.0, 0.4, -0.2], [3.0, -1.0, 0.0, 0.0]]. Compare the three regimes across that vector:

  • Strong signal (∣z1∣=4.0>1+λ|z_1| = 4.0 > 1 + \lambda): Lasso keeps 3.0 versus Ridge's 2.0, introducing half as much shrinkage bias.
  • Crossover (∣z2∣=2.0=1+λ|z_2| = 2.0 = 1 + \lambda): 2.0/2.0=1.02.0 / 2.0 = 1.0 and 2.0−1.0=1.02.0 - 1.0 = 1.0 tie (w2=−1.0w_2 = -1.0 in both).
  • Weak tail (∣z3∣,∣z4∣≤λ|z_3|, |z_4| \le \lambda): Lasso prunes both to 0.0 while Ridge retains [0.4, -0.2].

The live model

Each coordinate displays a trio of bars: unpenalized zjz_j (gray), Ridge wridgew_{\text{ridge}} (orange), and Lasso wlassow_{\text{lasso}} (blue bar when active, green dot on the axis when zeroed). Slide lam from 0.0 to 3.5 to watch orange bars compress proportionally while blue bars step down by constant λ\lambda and snap onto green axis dots.

Grouped bar comparison on [4.0, -2.0, 0.8, -0.4] at lam = 1.0 showing Ridge [2.0, -1.0, 0.4, -0.2] beside Lasso [3.0, -1.0, 0.0, 0.0].
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let d = input.size(0);
let w_ridge = input.div(1.0 + lam);
let w_lasso = input.sign() * (input.abs() - lam).relu();
let rows = torch.list().add(w_ridge.reshape(1, d)).add(w_lasso.reshape(1, d));
let answer = torch.cat(rows, 0);
out.set("ridge", w_ridge).set("lasso", w_lasso).set("comparison", answer);
{
  "ridge": [
    2,
    -1,
    0.4,
    -0.2
  ],
  "lasso": [
    3,
    -1,
    0,
    0
  ],
  "comparison": [
    [
      2,
      -1,
      0.4,
      -0.2
    ],
    [
      3,
      -1,
      0,
      0
    ]
  ]
}

Your turn

Return the 2×d2 \times d comparison matrix stacking wridgew_{\text{ridge}} in row 0 and wlassow_{\text{lasso}} in row 1 at penalty lam. On the default input [4.0, -2.0, 0.8, -0.4] the starter returns [2.0, -1.0, 0.4, -0.2] in both rows instead of [3.0, -1.0, 0.0, 0.0] in row 1.

Starter duplicates Ridge [2.0, -1.0, 0.4, -0.2] in both rows instead of computing Lasso [3.0, -1.0, 0.0, 0.0] in the second row.
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let d = input.size(0);
let w_ridge = input.div(1.0 + lam);
let w_lasso = input.div(1.0 + lam);
let rows = torch.list().add(w_ridge.reshape(1, d)).add(w_lasso.reshape(1, d));
let answer = torch.cat(rows, 0);
out.set("ridge", w_ridge).set("lasso", w_lasso).set("comparison", answer);
{
  "ridge": [
    2,
    -1,
    0.4,
    -0.2
  ],
  "lasso": [
    2,
    -1,
    0.4,
    -0.2
  ],
  "comparison": [
    [
      2,
      -1,
      0.4,
      -0.2
    ],
    [
      2,
      -1,
      0.4,
      -0.2
    ]
  ]
}

Return the 2 x d comparison matrix stacking Ridge weights in row 0 and Lasso weights in row 1 at penalty lam. On the default input the starter returns [2.0, -1.0, 0.4, -0.2] in both rows instead of [3.0, -1.0, 0.0, 0.0] in row 1.

Read the result carefully

Neither regularizer dominates across every dataset—which one wins depends on the true data-generating structure:

  • Few large true effects among many irrelevant columns (sparse regime): Lasso wins by zeroing out noise columns and shrinking the few strong predictors by λ\lambda instead of dividing them down.
  • Many small nonzero effects spread across all columns (dense regime): Ridge wins because zeroing half of those small useful contributors throws away real signal, whereas uniform proportional shrinkage reduces variance smoothly across all of them.

Recap

  • Ridge applies multiplicative shrinkage z/(1+λ)z / (1 + \lambda): zero sparsity, heavy damping on the largest weights.
  • Lasso applies additive shrinkage S(z,λ)S(z, \lambda): exact zeros for ∣z∣≤λ|z| \le \lambda, constant λ\lambda shift on large weights.
  • Ridge and Lasso tie in magnitude right at ∣z∣=1+λ|z| = 1 + \lambda (where both shrink to magnitude 1.0); Lasso stays larger above 1+λ1 + \lambda and smaller below it.