Speed

The Soft-Threshold Rule

Solve the 1D Lasso objective in closed form with the soft-threshold operator and see the dead zone appear around zero.

The problem

Iterating subgradients revealed why 12(w−z)2+λ∣w∣\frac{1}{2}(w - z)^2 + \lambda |w| chatters near zero, yet we do not need iterative steps in one dimension at all. Because J(w)J(w) is a quadratic on w>0w > 0, another quadratic on w<0w < 0, and has subdifferential (0−z)+[−λ,+λ](0 - z) + [-\lambda, +\lambda] at w=0w = 0, we can solve the optimality condition 0∈(w−z)+λ ∂∣w∣0 \in (w - z) + \lambda \,\partial |w| in exact closed form.

Checking the three sign regions yields the soft-threshold operator S(z,λ)S(z, \lambda):

  • When z>λz > \lambda, setting (w−z)+λ=0(w - z) + \lambda = 0 gives w=z−λ>0w = z - \lambda > 0.
  • When z<−λz < -\lambda, setting (w−z)−λ=0(w - z) - \lambda = 0 gives w=z+λ<0w = z + \lambda < 0.
  • When ∣z∣≤λ|z| \le \lambda, 00 lies inside [−z−λ,−z+λ][-z - \lambda, -z + \lambda], pinning w=0w = 0.

All three branches collapse into one expression:

S(z,λ)=sign⁡(z)max⁡(∣z∣−λ,0) S(z, \lambda) = \operatorname{sign}(z)\max(|z| - \lambda, 0)

Work through the numbers

Apply S(z,λ)S(z, \lambda) at lam = 1.0 to four unconstrained coordinates z = [3.0, 1.2, 0.5, -2.4].

Take magnitudes first: |z| = [3.0, 1.2, 0.5, 2.4]. Subtract lam = 1.0 from every magnitude to get [2.0, 0.2, -0.5, 1.4]. Now clamp negative values at zero via max⁡(⋅,0)\max(\cdot, 0): the third entry -0.5 sits inside the dead zone ∣0.5∣≤1.0|0.5| \le 1.0, so it clamps to 0.0, giving shrunk magnitudes [2.0, 0.2, 0.0, 1.4]. Finally restore original signs [+1, +1, +1, -1] to obtain S(z, 1.0) = [2.0, 0.2, 0.0, -1.4].

Look at what happens if you omit the zero clamp and evaluate sign⁡(z)(∣z∣−λ)\operatorname{sign}(z)(|z| - \lambda) directly: the third coordinate z=0.5z = 0.5 overshoots the origin and flips sign to +1⋅(0.5−1.0)=−0.5+1 \cdot (0.5 - 1.0) = -0.5, producing [2.0, 0.2, -0.5, -1.4]. Raising lam to 1.5 widens the dead zone [−1.5,1.5][-1.5, 1.5] so 1.2 also hits zero, yielding [1.5, 0.0, 0.0, -0.9].

The live model

The canvas draws the 45-degree reference line w=zw = z (gray), the threshold fences z=−λz = -\lambda and z=+λz = +\lambda (orange), and the piecewise-linear soft-threshold transfer curve S(z,λ)S(z, \lambda) (blue), marking the four input coordinates as dots (green when zeroed). Slide lam from 0.0 to 3.2 to watch the flat dead zone widen and swallow coordinates one by one.

Piecewise-linear soft-threshold transfer curve with dead zone [-1, 1], mapping [3.0, 1.2, 0.5, -2.4] to [2.0, 0.2, 0.0, -1.4].
let lam = scene.parameter("lam", 1.0, 0.0, 3.2, 0.2);
let mag = (input.abs() - lam).relu();
let answer = input.sign() * mag;
out.set("shrunk_mag", mag).set("soft", answer);
{
  "shrunk_mag": [
    2,
    0.19999999999999996,
    0,
    1.4
  ],
  "soft": [
    2,
    0.19999999999999996,
    0,
    -1.4
  ]
}

Your turn

Write the closed-form soft-threshold operator S(input,λ)S(\text{input}, \lambda) so answer returns the shrunk weight vector for any input and lam. On the default vector the starter returns input unchanged ([3.0, 1.2, 0.5, -2.4] where the page computed [2.0, 0.2, 0.0, -1.4]).

Starter leaves the input vector unthresholded, returning [3.0, 1.2, 0.5, -2.4] instead of [2.0, 0.2, 0.0, -1.4].
let lam = scene.parameter("lam", 1.0, 0.0, 3.2, 0.2);
let answer = input;
out.set("soft", answer);
{
  "soft": [
    3,
    1.2,
    0.5,
    -2.4
  ]
}

Evaluate the closed-form soft-threshold operator at penalty lam for every coordinate in input. On the default vector the starter returns [3.0, 1.2, 0.5, -2.4] where the page computed [2.0, 0.2, 0.0, -1.4].

Read the result carefully

Do not confuse soft thresholding with hard thresholding (w=zw = z if ∣z∣>λ|z| > \lambda, else 00). Hard thresholding jumps discontinuously from 00 to ±λ\pm\lambda at the boundary, making tiny perturbations in zz trigger large jumps in predictions. Soft thresholding is continuous everywhere: outside [−λ,λ][-\lambda, \lambda], every surviving coordinate is pulled inward by λ\lambda so the two outer ramps meet the flat zero segment without a gap.

Recap

  • The exact minimizer of 12(w−z)2+λ∣w∣\frac{1}{2}(w - z)^2 + \lambda |w| is S(z,λ)=sign⁡(z)max⁡(∣z∣−λ,0)S(z, \lambda) = \operatorname{sign}(z)\max(|z| - \lambda, 0).
  • Inputs inside the dead zone [−λ,λ][-\lambda, \lambda] map to exact 0.0; inputs outside shrink toward zero by λ\lambda.
  • Clamping ∣z∣−λ|z| - \lambda at zero prevents penalty pull from pushing a small weight past the origin into the opposite sign.