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 :
- Ridge minimizes , yielding .
- Lasso minimizes , yielding .
Their algebraic difference governs every modeling trade-off between them. Ridge divides every coordinate by , shrinking large signals heavily while leaving tiny noise coordinates nonzero. Lasso subtracts a fixed from surviving magnitudes and zeros out anything inside , 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 to get w_ridge = [2.0, -1.0, 0.4, -0.2]. Every feature loses of its magnitude: strong forfeits 2.0 units, while weak and 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 (): Lasso keeps
3.0versus Ridge's2.0, introducing half as much shrinkage bias. - Crossover (): and tie ( in both).
- Weak tail (): Lasso prunes both to
0.0while Ridge retains[0.4, -0.2].
The live model
Each coordinate displays a trio of bars: unpenalized (gray), Ridge (orange), and 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 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].](../assets/ml/ridge-vs-lasso--demo.png)
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 comparison matrix stacking in row 0 and 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.](../assets/ml/ridge-vs-lasso--exercise.png)
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 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 : zero sparsity, heavy damping on the largest weights.
- Lasso applies additive shrinkage : exact zeros for , constant shift on large weights.
- Ridge and Lasso tie in magnitude right at (where both shrink to magnitude
1.0); Lasso stays larger above and smaller below it.