Lasso Regularization (L1 Regularization) › Math and Optimality Conditions
Machine Learning from ScratchAll chapters Lasso Regularization (L1 Regularization) › Math and Optimality ConditionsOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's look at how LASSO finds the optimal coefficients, starting with the optimization process. LASSO solves the following optimization problem, here defined in summation form:
To find the minimum, we set the partial derivatives (where they exist) to zero. The absolute value term is not differentiable at , so we use subgradients there. We'll use the chain rule for the differentiable part. The main reason why there is no closed-form solution for LASSO is because the absolute value term is not differentiable at .
For : The partial derivative with respect to is:
Step 1: Differentiate the SSE term
The first term is the SSE. When we take the partial derivative with respect to , we use the chain rule:
Step 2: Differentiate the inner expression
The derivative of the inner expression with respect to is:
This is because:
- and are constants with respect to (derivative = 0)
- becomes when differentiated with respect to (all other terms become 0)
Step 3: Differentiate the penalty term
Let's solve for the partial derivative step by step when . Start with what we need to differentiate:
Apply the constant multiple rule
Since is a constant, we can pull it out:
Focus on the relevant term. When taking the derivative with respect to , only the -th term in the sum matters:
Handle the absolute value. When , we have (the absolute value doesn't change positive numbers):
Take the derivative
The derivative of with respect to itself is 1:
Final result for :
For : The partial derivative becomes:
We have when , so:
This gives us the negative sign in front of in the final equation.
Step 4: Combine the results
Putting it all together:
Step 5: Simplify
At , the derivative of is not defined, but its subgradient is the interval . The optimality condition becomes:
- The first term, , is the gradient of the sum of squared errors (SSE) with respect to . This measures how much the loss would decrease (or increase) if we changed a little bit.
- The second term, , comes from the L1 penalty. Here, is not a regular derivative, but a subgradient of the absolute value function at zero.
What is a subgradient?
For most values of , the derivative of is either (if ) or (if ). However, at , the absolute value function has a "kink" and is not differentiable there. In this case, instead of a single derivative, we use the concept of a subgradient, which is any value between and . That is, when , can be any number in the interval .
Why does this matter for LASSO?
This subgradient condition is what allows LASSO to set coefficients exactly to zero. If the (unpenalized) gradient, the first term above, is not large enough in magnitude to "overcome" the regularization strength , then the optimal solution is to set . In this case, the equation can still be satisfied by choosing an appropriate in .
In other words:
- If the data "wants" to pull away from zero (the gradient is large), but not enough to overcome the penalty, then the best solution is to keep .
- If the gradient is strong enough (in magnitude) to overcome , then will be nonzero, and the solution will be found where the gradient and penalty exactly balance.
This is the mathematical reason why LASSO can produce sparse solutions: the subgradient at zero allows the optimality condition to be satisfied with for some features, effectively removing them from the model.
Geometric Interpretation: Why LASSO Produces Sparse Solutions
To understand why LASSO sets coefficients exactly to zero while Ridge regression does not, we can visualize the geometry of the optimization problem. The following plot shows how the constraint regions differ between L1 (LASSO) and L2 (Ridge) regularization.

The key insight from this visualization is that the L1 constraint region (diamond) has sharp corners at the coordinate axes. When the SSE contours expand outward from the OLS solution, they are much more likely to first touch the constraint region at one of these corners, where at least one coefficient is exactly zero. This is why LASSO naturally performs feature selection.
In contrast, the L2 constraint region (circle) is smooth everywhere, so the SSE contours typically touch it at a point where both coefficients are non-zero. Ridge regression therefore shrinks coefficients toward zero but rarely sets them exactly to zero.
This geometric property holds in higher dimensions as well: the L1 constraint in p-dimensional space has 2p corners on the coordinate axes, giving LASSO many opportunities to set coefficients to zero. The L2 constraint, being a hypersphere, remains smooth in all dimensions.