Speed

Exact Sparsity

Soft-threshold a five-feature vector to see exact zeros appear and count the active set without canceling negative signs.

The problem

Under the book's half-squared penalty 12λwj2\frac{1}{2}\lambda w_j^2, Ridge regression shrinks small coordinates asymptotically toward zero via zj/(1+λ)z_j / (1 + \lambda): at lam = 1.0, a weak signal z_5 = 0.3 becomes 0.3 / 2.0 = 0.15, and even at lam = 99 it stays 0.003. Every feature remains in the model, so predicting a new row still requires reading and multiplying every column.

Lasso achieves exact sparsity: every coordinate with ∣zj∣≤λ|z_j| \le \lambda lands on exact floating-point 0.0, dropping out of XwX w completely. The surviving indices {j:wj≠0}\{j : w_j \neq 0\} form the active set, and its size k=∥w∥0=∑j=1d1(wj≠0)k = \|w\|_0 = \sum_{j=1}^d \mathbf{1}(w_j \neq 0) measures how many features the model actually uses.

Work through the numbers

Take five orthonormal feature correlations z = [3.2, -2.1, 1.4, -0.8, 0.3] at lam = 1.0.

Compare each magnitude |z| = [3.2, 2.1, 1.4, 0.8, 0.3] against the threshold lam = 1.0:

  • Coordinates 1,2,31, 2, 3 exceed 1.0: w_1 = +2.2, w_2 = -1.1, and w_3 = +0.4.
  • Coordinates 4,54, 5 satisfy ∣−0.8∣≤1.0|-0.8| \le 1.0 and ∣0.3∣≤1.0|0.3| \le 1.0: both clamp to exact 0.0.

The sparse weight vector is w = [2.2, -1.1, 0.4, 0.0, 0.0]. Counting nonzero entries gives ∥w∥0=1+1+1+0+0=3\|w\|_0 = 1 + 1 + 1 + 0 + 0 = 3, so appending the active count produces [2.2, -1.1, 0.4, 0.0, 0.0, 3.0].

Watch out when computing ∥w∥0\|w\|_0 with sign operations: taking sign⁡(∣w∣)=[1,1,1,0,0]\operatorname{sign}(|w|) = [1, 1, 1, 0, 0] and summing gives 3.0, whereas summing signed sign⁡(w)=[+1,−1,+1,0,0]\operatorname{sign}(w) = [+1, -1, +1, 0, 0] lets −1-1 cancel +1+1 and misreports 1.0. Stepping lam from 0.0 through 0.5, 1.0, 1.5, 2.5, 3.5 drops the active count along the staircase 5→4→3→2→1→05 \to 4 \to 3 \to 2 \to 1 \to 0.

The live model

For each of the five coordinates, the canvas draws the raw signal zjz_j in gray beside the Lasso weight wjw_j in blue, marking exact zeros with green dots on the horizontal axis. Slide lam upward from 0.0 to 3.5 to watch weak coordinates snap cleanly onto the axis one by one as the active count steps down to 0.

Paired bars for five coordinates [3.2, -2.1, 1.4, -0.8, 0.3] soft-thresholded at lam = 1.0 to [2.2, -1.1, 0.4, 0.0, 0.0] with active count 3.
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let w = input.sign() * (input.abs() - lam).relu();
let active = w.abs().sign().sum();
let parts = torch.list().add(w).add(active.reshape(1));
let answer = torch.cat(parts, 0);
out.set("weights", w).set("active", active).set("summary", answer);
{
  "weights": [
    2.2,
    -1.1,
    0.3999999999999999,
    0,
    0
  ],
  "active": 3,
  "summary": [
    2.2,
    -1.1,
    0.3999999999999999,
    0,
    0,
    3
  ]
}

Your turn

Soft-threshold input at lam and append the active-set count ∥w∥0\|w\|_0, returning [w_1, ..., w_d, active]. On the default vector [3.2, -2.1, 1.4, -0.8, 0.3] at lam = 1.0 the starter reports active count 1 where the page computed 3.

Starter cancels negative and positive active signs, reporting active count 1 instead of 3.
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let w = input.sign() * (input.abs() - lam).relu();
let active = w.sign().sum();
let parts = torch.list().add(w).add(active.reshape(1));
let answer = torch.cat(parts, 0);
out.set("weights", w).set("active", active).set("summary", answer);
{
  "weights": [
    2.2,
    -1.1,
    0.3999999999999999,
    0,
    0
  ],
  "active": 1,
  "summary": [
    2.2,
    -1.1,
    0.3999999999999999,
    0,
    0,
    1
  ]
}

Soft-threshold input at lam and append the active-set count to form [w_1, ..., w_d, active]. On the default vector the starter returns active count 1 where the page computed 3.

Read the result carefully

Distinguish the L0 count ∥w∥0\|w\|_0 from the L1 norm ∥w∥1=∑j=1d∣wj∣\|w\|_1 = \sum_{j=1}^d |w_j|. On w = [2.2, -1.1, 0.4, 0.0, 0.0], ||w||_1 = 2.2 + 1.1 + 0.4 = 3.7 while ∥w∥0=3\|w\|_0 = 3. Minimizing ∥w∥0\|w\|_0 directly is combinatorially hard because 1(wj≠0)\mathbf{1}(w_j \neq 0) has zero slope almost everywhere and jumps at zero; Lasso minimizes convex ∥w∥1\|w\|_1 as the tightest convex relaxation that still produces genuine ∥w∥0\|w\|_0 sparsity at its kink.

Recap

  • Soft thresholding sets every coordinate with ∣zj∣≤λ|z_j| \le \lambda to exact 0.0, pruning features rather than merely damping them.
  • The active-set size ∥w∥0\|w\|_0 equals ∑j=1dsign⁡(∣wj∣)\sum_{j=1}^d \operatorname{sign}(|w_j|) because sign⁡(0)=0\operatorname{sign}(0) = 0 and sign⁡(∣wj∣)=1\operatorname{sign}(|w_j|) = 1 for every nonzero weight.
  • Increasing lam shrinks surviving weights linearly while stepping ∥w∥0\|w\|_0 monotonically down toward 0.