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 , Ridge regression shrinks small coordinates asymptotically toward zero via : 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 lands on exact floating-point 0.0, dropping out of completely. The surviving indices form the active set, and its size 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 exceed
1.0:w_1 = +2.2,w_2 = -1.1, andw_3 = +0.4. - Coordinates satisfy and : 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 , so appending the active count produces [2.2, -1.1, 0.4, 0.0, 0.0, 3.0].
Watch out when computing with sign operations: taking and summing gives 3.0, whereas summing signed lets cancel 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 .
The live model
For each of the five coordinates, the canvas draws the raw signal in gray beside the Lasso weight 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.](../assets/ml/exact-sparsity--demo.png)
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 , 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.

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 from the L1 norm . On w = [2.2, -1.1, 0.4, 0.0, 0.0], ||w||_1 = 2.2 + 1.1 + 0.4 = 3.7 while . Minimizing directly is combinatorially hard because has zero slope almost everywhere and jumps at zero; Lasso minimizes convex as the tightest convex relaxation that still produces genuine sparsity at its kink.
Recap
- Soft thresholding sets every coordinate with to exact
0.0, pruning features rather than merely damping them. - The active-set size equals because and for every nonzero weight.
- Increasing
lamshrinks surviving weights linearly while stepping monotonically down toward0.