Speed

At and Beyond the Threshold

Compute the exact all-zero boundary max |X^T y| and verify that every penalty at or beyond lam_max yields w = 0.

The problem

How large must λ\lambda become before Lasso drops every feature and predicts zero everywhere? You do not need to solve a path of models by trial and error to find out: the subgradient optimality condition at w=0w = 0 answers the question in one matrix-vector product.

At w=0w = 0, the residual is −y-y, the smooth fit gradient is −X⊤y-X^\top y, and the L1 subdifferential is the hypercube [−λ,+λ]d[-\lambda, +\lambda]^d. Zero lies inside −X⊤y+[−λ,+λ]d-X^\top y + [-\lambda, +\lambda]^d if and only if ∣xj⊤y∣≤λ|x_j^\top y| \le \lambda for every column jj. Therefore the exact all-zero threshold—even when columns of XX are correlated—is the infinity norm

λmax⁡=∥X⊤y∥∞=max⁡j∣xj⊤y∣ \lambda_{\max} = \|X^\top y\|_\infty = \max_{j} |x_j^\top y|

Work through the numbers

Inspect the orthogonal four-row [x_1, x_2, y] table (1, 1, -1.5), (1, -1, -2.5), (-1, 1, 2.5), and (-1, -1, 1.5) with s1=s2=4.0s_1 = s_2 = 4.0.

Dot each column with yy: c1=−1.5−2.5−2.5−1.5=−8.0c_1 = -1.5 - 2.5 - 2.5 - 1.5 = -8.0 and c2=−1.5+2.5+2.5−1.5=2.0c_2 = -1.5 + 2.5 + 2.5 - 1.5 = 2.0. Taking absolute values before maximizing gives lam_max = max(|-8.0|, |2.0|) = 8.0. Taking the raw maximum max⁡(−8.0,2.0)=2.0\max(-8.0, 2.0) = 2.0 misses the dominant negative correlation completely.

Compare three penalty values around lam_max = 8.0:

  • Below (lam = 5.0): w=[S(−8.0,5.0)/4.0,S(2.0,5.0)/4.0]=[−0.75,0.0]w = [S(-8.0, 5.0) / 4.0, S(2.0, 5.0) / 4.0] = [-0.75, 0.0].
  • At threshold (lam = 8.0): S(−8.0,8.0)=0.0S(-8.0, 8.0) = 0.0 and S(2.0,8.0)=0.0S(2.0, 8.0) = 0.0, returning [8.0, 0.0, 0.0].
  • Beyond (lam = 11.0): both magnitudes still sit inside [−11.0,11.0][-11.0, 11.0], so [8.0, 0.0, 0.0] stays completely unchanged.

The live model

The left panel compares column correlation magnitudes ∣c1∣=8|c_1| = 8 and ∣c2∣=2|c_2| = 2 against the horizontal orange threshold line at lam, marking lam_max in purple; the right panel plots ∣w2(λ)∣|w_2(\lambda)| hitting zero at λ=2\lambda = 2 and ∣w1(λ)∣|w_1(\lambda)| hitting zero at λ=8\lambda = 8. Slide lam from 5.0 through 8.0 up to 12.0 to see the solution freeze at [0, 0] the instant lam reaches lam_max.

Correlation magnitude bars |c1| = 8 and |c2| = 2 met by threshold line lam = 8 = lam_max, where both Lasso paths reach exact zero [8.0, 0.0, 0.0].
let lam = scene.parameter("lam", 8.0, 0.0, 12.0, 1.0);
let x1 = input.select(1, 0);
let x2 = input.select(1, 1);
let ys = input.select(1, 2);
let s1 = x1.square().sum();
let s2 = x2.square().sum();
let c1 = (x1 * ys).sum();
let c2 = (x2 * ys).sum();
let c_vec = torch.cat(torch.list().add(c1.reshape(1)).add(c2.reshape(1)), 0);
let lam_max = c_vec.abs().max();
let w1 = (c1.sign() * (c1.abs() - lam).relu()).div(s1);
let w2 = (c2.sign() * (c2.abs() - lam).relu()).div(s2);
let parts = torch.list().add(lam_max.reshape(1));
let parts2 = parts.add(w1.reshape(1)).add(w2.reshape(1));
let answer = torch.cat(parts2, 0);
out.set("corrs", c_vec).set("threshold_state", answer);
{
  "corrs": [
    -8,
    2
  ],
  "threshold_state": [
    8,
    0,
    0
  ]
}

Your turn

Compute [lam_max, w_1, w_2] for the orthogonal [x_1, x_2, y] table input at penalty lam. On the default table the starter reports [2.0, 0.0, 0.0] where the page computed [8.0, 0.0, 0.0].

Starter reports lam_max = 2.0 instead of 8.0 on correlations [-8, 2].
let lam = scene.parameter("lam", 8.0, 0.0, 12.0, 1.0);
let x1 = input.select(1, 0);
let x2 = input.select(1, 1);
let ys = input.select(1, 2);
let s1 = x1.square().sum();
let s2 = x2.square().sum();
let c1 = (x1 * ys).sum();
let c2 = (x2 * ys).sum();
let c_vec = torch.cat(torch.list().add(c1.reshape(1)).add(c2.reshape(1)), 0);
let lam_max = c_vec.max();
let w1 = (c1.sign() * (c1.abs() - lam).relu()).div(s1);
let w2 = (c2.sign() * (c2.abs() - lam).relu()).div(s2);
let parts = torch.list().add(lam_max.reshape(1));
let parts2 = parts.add(w1.reshape(1)).add(w2.reshape(1));
let answer = torch.cat(parts2, 0);
out.set("corrs", c_vec).set("threshold_state", answer);
{
  "corrs": [
    -8,
    2
  ],
  "threshold_state": [
    2,
    0,
    0
  ]
}

Return [lam_max, w_1, w_2] for the orthogonal [x_1, x_2, y] table input at penalty lam. On the default table the starter reports [2.0, 0.0, 0.0] where the page computed [8.0, 0.0, 0.0].

Read the result carefully

Regularization path libraries do not guess an upper bound for λ\lambda: they compute λmax⁡=∥X⊤y∥∞\lambda_{\max} = \|X^\top y\|_\infty first (divided by the row count when their objective averages the squared error) and build a descending grid from λmax⁡\lambda_{\max} down to a small fraction of it, such as 0.001 λmax⁡0.001\,\lambda_{\max}, because testing two values above λmax⁡\lambda_{\max} wastes computation on identical zero vectors. Also keep the norm duality straight: an L1 penalty on ww induces an L-infinity (max⁡j∣cj∣\max_j |c_j|) threshold on X⊤yX^\top y, never the sum ∣c1∣+∣c2∣|c_1| + |c_2|.

Recap

  • Every Lasso weight equals exact zero if and only if λ≥λmax⁡=max⁡j∣xj⊤y∣\lambda \ge \lambda_{\max} = \max_j |x_j^\top y|.
  • Always take absolute values of xj⊤yx_j^\top y before maximizing so strong negative correlations set λmax⁡\lambda_{\max} properly.
  • At λ=λmax⁡\lambda = \lambda_{\max} the last surviving coordinate reaches 0.0, and for all λ>λmax⁡\lambda > \lambda_{\max} the solution remains [0, ..., 0].