Speed

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 Jridge(w)=12(w−z)2+12λw2J_{\text{ridge}}(w) = \frac{1}{2}(w - z)^2 + \frac{1}{2}\lambda w^2 so its minimizer is z/(1+λ)z / (1 + \lambda) (the Ridge book wrote unhalved SSE+λw2\text{SSE} + \lambda w^2, multiplying both terms by 22). Because the L2 slope λw\lambda w flattens to zero as w→0w \to 0, any nonzero signal zz pulls the Ridge weight slightly away from zero.

Lasso (*Least Absolute Shrinkage and Selection Operator*) swaps 12w2\frac{1}{2}w^2 for the absolute value ∣w∣|w|. With unconstrained target zz (written source), the 1D Lasso objective is

J(w)=12(w−z)2+λ∣w∣ J(w) = \frac{1}{2}(w - z)^2 + \lambda |w|

Unlike 12w2\frac{1}{2}w^2, ∣w∣|w| meets the origin in a sharp V-corner with slope +λ+\lambda on the right and −λ-\lambda on the left.

Work through the numbers

Set source = 2.0 and lam = 1.0, and evaluate J(w)=12(w−2)2+1.0 ∣w∣J(w) = \frac{1}{2}(w - 2)^2 + 1.0\,|w| 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 w=−1.0w = -1.0, fit is 0.5(−3)2=4.50.5(-3)^2 = 4.5 and ∣−1.0∣=1.0|-1.0| = 1.0, giving J(-1.0) = 5.5; at w=0.0w = 0.0, 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 12(w−2)2+12(1.0)w2\frac{1}{2}(w - 2)^2 + \frac{1}{2}(1.0)w^2, which yields [5.0, 2.0, 1.25, 1.0, 1.25, 2.0, 5.0]: L2 charges more at w=3.0w = 3.0 (4.5 vs 3.0, total 5.0 vs 3.5) yet charges far less near zero at w=0.5w = 0.5 (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 J(w)J(w) in blue, the V-shaped penalty λ∣w∣\lambda |w| 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 w=0w = 0 swallow the valley minimum, then move source negative to see the mirror image.

1D Lasso objective curve and V-shaped L1 penalty across seven candidate weights, hitting minimum 1.5 at w = 1.0 for source = 2.0 and lam = 1.0.
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 12(w−source)2+λ∣w∣\frac{1}{2}(w - \text{source})^2 + \lambda |w| 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].
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 12\frac{1}{2} factor in front of (w−z)2(w - z)^2 is a scaling convention so its derivative is w−zw - z rather than 2(w−z)2(w - z); omitting 12\frac{1}{2} halves the effective threshold λ\lambda. Second, ∣w∣|w| is mandatory on negative weights: writing λw\lambda w without magnitude rewards negative weights instead of penalizing them, sending J(w)→−∞J(w) \to -\infty as ww decreases.

Recap

  • Lasso replaces the smooth half-squared penalty 12λw2\frac{1}{2}\lambda w^2 with the L1 penalty λ∣w∣\lambda |w|.
  • For ∣w∣>2|w| > 2, ∣w∣|w| costs less than 12w2\frac{1}{2}w^2; near zero (∣w∣<1|w| < 1), ∣w∣|w| costs more and keeps unit slope 11 while the slope ∣w∣|w| of 12w2\frac{1}{2}w^2 fades to zero.
  • Because λ∣w∣\lambda |w| maintains slope ±λ\pm\lambda right up to w=0w = 0, a sufficiently large λ\lambda traps the exact minimum at the corner w=0w = 0.