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 as a shortcut for thresholding a correlation . That shortcut works on textbook examples where , and silently destroys every negatively correlated feature by clamping straight to zero even at tiny .
From data rows , minimizing uses , , and to give
When , the penalty pulls upward toward zero (), preserving negative sign until .
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: . Sum signed cross-products next: . The unpenalized least-squares slope is .
Now apply at lam = 6.0. Because , shrunk magnitude is , and gives . Dividing by yields , 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 instead outputs 0.0.
The live model
The scatter plot displays the four data points in blue, the unpenalized steep negative line in gray, and the Lasso line 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 once .

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].](../assets/ml/negative-source-keeps-its-sign--exercise.png)
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 enters the formula. Writing is algebraically identical to (), because factors cleanly out of . What is not equivalent is thresholding by unscaled : compares against instead of , falsely zeroing the feature ten times too early whenever .
Recap
- On a zero-intercept column , and .
- Negative correlations () shrink upward toward zero (), retaining negative sign until .
- Always threshold by before dividing by (or equivalently threshold by ).