From Ridge to Lasso
Replace the smooth squared penalty with the V-shaped absolute-value penalty and inspect how the corner at zero reshapes the objective.
The problem
Throughout this book we pair the half-squared fit error with the half-squared Ridge penalty so its minimizer is (the Ridge book wrote unhalved , multiplying both terms by ). Because the L2 slope flattens to zero as , any nonzero signal pulls the Ridge weight slightly away from zero.
Lasso (*Least Absolute Shrinkage and Selection Operator*) swaps for the absolute value . With unconstrained target (written source), the 1D Lasso objective is
Unlike , meets the origin in a sharp V-corner with slope on the right and on the left.
Work through the numbers
Set source = 2.0 and lam = 1.0, and evaluate across seven candidate weights w = [-1.0, 0.0, 0.5, 1.0, 1.5, 2.0, 3.0] before deriving any closed-form rule.
At , fit is and , giving J(-1.0) = 5.5; at , J(0.0) = 2.0. Stepping positive gives J(0.5) = 1.625, J(1.0) = 0.5 + 1.0 = 1.5, J(1.5) = 1.625, J(2.0) = 2.0, and J(3.0) = 3.5. The full objective vector is [5.5, 2.0, 1.625, 1.5, 1.625, 2.0, 3.5], hitting minimum 1.5 at w = 1.0.
Contrast that grid with the smooth Ridge objective , which yields [5.0, 2.0, 1.25, 1.0, 1.25, 2.0, 5.0]: L2 charges more at (4.5 vs 3.0, total 5.0 vs 3.5) yet charges far less near zero at (0.125 vs 0.5, total 1.25 vs 1.625). Raising lam to 2.5 makes the corner decisive: J(0.0) = 2.0 beats both J(0.5) = 2.375 and J(-0.5) = 4.375, pinning the true minimum right at w = 0.0.
The live model
The canvas plots the total Lasso objective in blue, the V-shaped penalty in orange, and the evaluated candidate grid points as circles. Slide lam from 1.0 up to 2.5 to watch the V-corner at swallow the valley minimum, then move source negative to see the mirror image.

let source = scene.parameter("source", 2.0, -2.5, 2.5, 0.5);
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let fit_term = (input - source).square() * 0.5;
let pen = input.abs() * lam;
let answer = fit_term + pen;
out.set("fit", fit_term).set("penalty", pen).set("objective", answer);
{
"fit": [
4.5,
2,
1.125,
0.5,
0.125,
0,
0.5
],
"penalty": [
1,
0,
0.5,
1,
1.5,
2,
3
],
"objective": [
5.5,
2,
1.625,
1.5,
1.625,
2,
3.5
]
}Your turn
Evaluate the 1D Lasso objective at every candidate weight in input. On the default grid the starter returns [5.0, 2.0, 1.25, 1.0, 1.25, 2.0, 5.0] where the page computed [5.5, 2.0, 1.625, 1.5, 1.625, 2.0, 3.5].
![Candidate objective values when the half-squared penalty replaces absolute value, producing [5.0, 2.0, 1.25, 1.0, 1.25, 2.0, 5.0] instead of [5.5, 2.0, 1.625, 1.5, 1.625, 2.0, 3.5].](../assets/ml/from-ridge-to-lasso--exercise.png)
let source = scene.parameter("source", 2.0, -2.5, 2.5, 0.5);
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let fit_term = (input - source).square() * 0.5;
let pen = input.square() * (0.5 * lam);
let answer = fit_term + pen;
out.set("fit", fit_term).set("penalty", pen).set("objective", answer);
{
"fit": [
4.5,
2,
1.125,
0.5,
0.125,
0,
0.5
],
"penalty": [
0.5,
0,
0.125,
0.5,
1.125,
2,
4.5
],
"objective": [
5,
2,
1.25,
1,
1.25,
2,
5
]
}Evaluate the 1D Lasso objective at every candidate weight in input for source and lam. On the default grid the starter returns [5.0, 2.0, 1.25, 1.0, 1.25, 2.0, 5.0] where the page computed [5.5, 2.0, 1.625, 1.5, 1.625, 2.0, 3.5].
Read the result carefully
Two details matter whenever you write down a Lasso objective. First, the factor in front of is a scaling convention so its derivative is rather than ; omitting halves the effective threshold . Second, is mandatory on negative weights: writing without magnitude rewards negative weights instead of penalizing them, sending as decreases.
Recap
- Lasso replaces the smooth half-squared penalty with the L1 penalty .
- For , costs less than ; near zero (), costs more and keeps unit slope while the slope of fades to zero.
- Because maintains slope right up to , a sufficiently large traps the exact minimum at the corner .