Speed

Negative Source Keeps Its Sign

Fit 1D Lasso on negatively correlated data and verify that the Lasso slope shrinks upward toward zero while keeping its negative sign.

The problem

Beginner implementations of Lasso often write max⁡(c−λ,0)\max(c - \lambda, 0) as a shortcut for thresholding a correlation c=x⊤yc = x^\top y. That shortcut works on textbook examples where c>0c > 0, and silently destroys every negatively correlated feature by clamping c<0c < 0 straight to zero even at tiny λ\lambda.

From data rows (xi,yi)(x_i, y_i), minimizing 12∑i=1n(xiw−yi)2+λ∣w∣\frac{1}{2}\sum_{i=1}^n (x_i w - y_i)^2 + \lambda |w| uses sxx=∑i=1nxi2s_{xx} = \sum_{i=1}^n x_i^2, sxy=∑i=1nxiyis_{xy} = \sum_{i=1}^n x_i y_i, and wols=sxy/sxxw_{\text{ols}} = s_{xy} / s_{xx} to give

wlasso=S(sxy,λ)sxx=sign⁡(sxy)max⁡(∣sxy∣−λ,0)sxx w_{\text{lasso}} = \frac{S(s_{xy}, \lambda)}{s_{xx}} = \frac{\operatorname{sign}(s_{xy})\max(|s_{xy}| - \lambda, 0)}{s_{xx}}

When sxy<−λs_{xy} < -\lambda, the penalty pulls ww upward toward zero (wols+λ/sxxw_{\text{ols}} + \lambda / s_{xx}), preserving negative sign until λ≥∣sxy∣\lambda \ge |s_{xy}|.

Work through the numbers

Consider the four-row [x, y] table (1.0, -3.0), (2.0, -4.0), (-1.0, 2.0), and (-2.0, 3.5) with penalty lam = 6.0.

Sum squared inputs first: sxx=1+4+1+4=10.0s_{xx} = 1 + 4 + 1 + 4 = 10.0. Sum signed cross-products next: sxy=−3.0−8.0−2.0−7.0=−20.0s_{xy} = -3.0 - 8.0 - 2.0 - 7.0 = -20.0. The unpenalized least-squares slope is wols=−20.0/10.0=−2.0w_{\text{ols}} = -20.0 / 10.0 = -2.0.

Now apply S(sxy,λ)S(s_{xy}, \lambda) at lam = 6.0. Because ∣sxy∣=20.0>6.0|s_{xy}| = 20.0 > 6.0, shrunk magnitude is 20.0−6.0=14.020.0 - 6.0 = 14.0, and sign⁡(−20.0)=−1\operatorname{sign}(-20.0) = -1 gives S(−20.0,6.0)=−14.0S(-20.0, 6.0) = -14.0. Dividing by sxx=10.0s_{xx} = 10.0 yields wlasso=−14.0/10.0=−1.4w_{\text{lasso}} = -14.0 / 10.0 = -1.4, so [w_ols, w_lasso] is [-2.0, -1.4]. Slope -2.0 moved up by +0.6 toward 0.0 while staying negative; clamping max⁡(−20.0−6.0,0)\max(-20.0 - 6.0, 0) instead outputs 0.0.

The live model

The scatter plot displays the four data points in blue, the unpenalized steep negative line y=wolsxy = w_{\text{ols}} x in gray, and the Lasso line y=wlassoxy = w_{\text{lasso}} x in green. Drag lam from 0.0 toward 24.0: as lam rises, the green line rotates counterclockwise (flattening from slope -2.0 through -1.4 toward 0.0) without ever flipping to a positive slope, then locks horizontally onto y=0y = 0 once λ≥20.0\lambda \ge 20.0.

Scatter of four negatively correlated points with OLS line slope -2.0 and Lasso line flattened upward to slope -1.4 at lam = 6.
let lam = scene.parameter("lam", 6.0, 0.0, 24.0, 2.0);
let xs = input.select(1, 0);
let ys = input.select(1, 1);
let sxx = xs.square().sum();
let sxy = (xs * ys).sum();
let w_ols = sxy.div(sxx);
let shrunk = sxy.sign() * (sxy.abs() - lam).relu();
let w_lasso = shrunk.div(sxx);
let pair = torch.list().add(w_ols.reshape(1)).add(w_lasso.reshape(1));
let answer = torch.cat(pair, 0);
out.set("sxy", sxy).set("slopes", answer);
{
  "sxy": -20,
  "slopes": [
    -2,
    -1.4
  ]
}

Your turn

Compute [w_ols, w_lasso] from the [x, y] data matrix input at penalty lam. On the default negatively correlated table the starter returns [-2.0, 0.0] where the page computed [-2.0, -1.4].

Starter erases the negative correlation, returning [-2.0, 0.0] instead of [-2.0, -1.4].
let lam = scene.parameter("lam", 6.0, 0.0, 24.0, 2.0);
let xs = input.select(1, 0);
let ys = input.select(1, 1);
let sxx = xs.square().sum();
let sxy = (xs * ys).sum();
let w_ols = sxy.div(sxx);
let shrunk = (sxy - lam).relu();
let w_lasso = shrunk.div(sxx);
let pair = torch.list().add(w_ols.reshape(1)).add(w_lasso.reshape(1));
let answer = torch.cat(pair, 0);
out.set("sxy", sxy).set("slopes", answer);
{
  "sxy": -20,
  "slopes": [
    -2,
    0
  ]
}

Return [w_ols, w_lasso] for the zero-intercept [x, y] table input at penalty lam. On the default negatively correlated table the starter returns [-2.0, 0.0] where the page computed [-2.0, -1.4].

Read the result carefully

Pay attention to where sxxs_{xx} enters the formula. Writing S(sxy,λ)/sxxS(s_{xy}, \lambda) / s_{xx} is algebraically identical to S(wols,λ/sxx)S(w_{\text{ols}}, \lambda / s_{xx}) (S(−2.0,0.6)=−1.4S(-2.0, 0.6) = -1.4), because sxx>0s_{xx} > 0 factors cleanly out of max⁡(∣sxy∣−λ,0)\max(|s_{xy}| - \lambda, 0). What is not equivalent is thresholding wolsw_{\text{ols}} by unscaled λ\lambda: S(−2.0,6.0)S(-2.0, 6.0) compares −2.0-2.0 against 6.06.0 instead of 0.60.6, falsely zeroing the feature ten times too early whenever sxx=10s_{xx} = 10.

Recap

  • On a zero-intercept column xx, wols=(x⊤y)/(x⊤x)w_{\text{ols}} = (x^\top y) / (x^\top x) and wlasso=S(x⊤y,λ)/(x⊤x)w_{\text{lasso}} = S(x^\top y, \lambda) / (x^\top x).
  • Negative correlations (x⊤y<−λx^\top y < -\lambda) shrink upward toward zero (wols+λ/sxxw_{\text{ols}} + \lambda / s_{xx}), retaining negative sign until λ≥∣x⊤y∣\lambda \ge |x^\top y|.
  • Always threshold x⊤yx^\top y by λ\lambda before dividing by x⊤xx^\top x (or equivalently threshold wolsw_{\text{ols}} by λ/(x⊤x)\lambda / (x^\top x)).