Lasso Regularization (L1 Regularization) › Math and Optimality Conditions

Michael BrenndoerferApril 19, 20259 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 35Lasso Regularization (L1 Regularization) › Math and Optimality Conditions
Reading section 3 of 11 · Math and Optimality Conditions · 9 minSection 3 of 11 · 9 min

Book contents

Section 3 of 11 · 9 min read

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:

min⁡β0,β1,…,βp{∑i=1n(yi−β0−∑j=1pβjxij)2+λ∑j=1p∣βj∣}\min_{\beta_0, \beta_1, \ldots, \beta_p} \left\{ \sum_{i=1}^n \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right)^2 + \lambda \sum_{j=1}^p |\beta_j| \right\}

To find the minimum, we set the partial derivatives (where they exist) to zero. The absolute value term ∣βj∣|\beta_j| is not differentiable at βj=0\beta_j = 0, 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 βj=0\beta_j = 0.

For βj>0\beta_j > 0: The partial derivative with respect to βj\beta_j is:

∂∂βj[∑i=1n(yi−β0−∑j=1pβjxij)2+λ∑j=1p∣βj∣]=0\frac{\partial}{\partial \beta_j} \left[ \sum_{i=1}^n \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right)^2 + \lambda \sum_{j=1}^p |\beta_j| \right] = 0

Step 1: Differentiate the SSE term

The first term ∑i=1n(yi−β0−∑j=1pβjxij)2\sum_{i=1}^n \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right)^2 is the SSE. When we take the partial derivative with respect to βj\beta_j, we use the chain rule:

∂∂βj[∑i=1n(yi−β0−∑j=1pβjxij)2]=∑i=1n2(yi−β0−∑j=1pβjxij)⋅∂∂βj(yi−β0−∑j=1pβjxij)\frac{\partial}{\partial \beta_j} \left[ \sum_{i=1}^n \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right)^2 \right] = \sum_{i=1}^n 2 \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right) \cdot \frac{\partial}{\partial \beta_j} \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right)

Step 2: Differentiate the inner expression

The derivative of the inner expression with respect to βj\beta_j is:

∂∂βj(yi−β0−∑j=1pβjxij)=−xij\frac{\partial}{\partial \beta_j} \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right) = -x_{ij}

This is because:

  • yiy_i and β0\beta_0 are constants with respect to βj\beta_j (derivative = 0)
  • ∑j=1pβjxij\sum_{j=1}^p \beta_j x_{ij} becomes xijx_{ij} when differentiated with respect to βj\beta_j (all other terms become 0)

Step 3: Differentiate the penalty term

Let's solve for the partial derivative step by step when βj>0\beta_j > 0. Start with what we need to differentiate:

∂∂βj[λ∑j=1p∣βj∣]\frac{\partial}{\partial \beta_j} \left[ \lambda \sum_{j=1}^p |\beta_j| \right]

Apply the constant multiple rule

Since λ\lambda is a constant, we can pull it out:

=λ⋅∂∂βj[∑j=1p∣βj∣]= \lambda \cdot \frac{\partial}{\partial \beta_j} \left[ \sum_{j=1}^p |\beta_j| \right]

Focus on the relevant term. When taking the derivative with respect to βj\beta_j, only the jj-th term in the sum matters:

=λ⋅∂∂βj[∣βj∣]= \lambda \cdot \frac{\partial}{\partial \beta_j} [|\beta_j|]

Handle the absolute value. When βj>0\beta_j > 0, we have ∣βj∣=βj|\beta_j| = \beta_j (the absolute value doesn't change positive numbers):

=λ⋅∂∂βj[βj]= \lambda \cdot \frac{\partial}{\partial \beta_j} [\beta_j]

Take the derivative

The derivative of βj\beta_j with respect to itself is 1:

=λ⋅1=λ= \lambda \cdot 1 = \lambda

Final result for βj>0\beta_j > 0:

∂∂βj[λ∑j=1p∣βj∣]=λ\frac{\partial}{\partial \beta_j} \left[ \lambda \sum_{j=1}^p |\beta_j| \right] = \lambda

For βj<0\beta_j < 0: The partial derivative becomes:

−2∑i=1nxij(yi−β0−∑j=1pβjxij)−λ=0-2 \sum_{i=1}^n x_{ij} \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right) - \lambda = 0

We have ∣βj∣=−βj|\beta_j| = -\beta_j when βj<0\beta_j < 0, so:

∂∂βj[λ∑j=1p∣βj∣]=λ⋅∂∂βj[−βj]=−λ\frac{\partial}{\partial \beta_j} \left[ \lambda \sum_{j=1}^p |\beta_j| \right] = \lambda \cdot \frac{\partial}{\partial \beta_j} [-\beta_j] = -\lambda

This gives us the negative sign in front of λ\lambda in the final equation.

Step 4: Combine the results

Putting it all together:

∑i=1n2(yi−β0−∑j=1pβjxij)⋅(−xij)+λ=0\sum_{i=1}^n 2 \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right) \cdot (-x_{ij}) + \lambda = 0

Step 5: Simplify

−2∑i=1nxij(yi−β0−∑j=1pβjxij)+λ=0-2 \sum_{i=1}^n x_{ij} \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right) + \lambda = 0
Subgradient at βj=0\beta_j = 0

At βj=0\beta_j = 0, the derivative of ∣βj∣|\beta_j| is not defined, but its subgradient is the interval [−1,1][-1, 1]. The optimality condition becomes:

−2∑i=1nxij(yi−β0−∑j=1pβjxij)+λsj=0,sj∈[−1,1].-2 \sum_{i=1}^n x_{ij} \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right) + \lambda s_j = 0, \quad s_j \in [-1, 1].
  • The first term, −2∑i=1nxij(yi−β0−∑j=1pβjxij)-2 \sum_{i=1}^n x_{ij} \left( y_i - \beta_0 - \sum_{j=1}^p \beta_j x_{ij} \right), is the gradient of the sum of squared errors (SSE) with respect to βj\beta_j. This measures how much the loss would decrease (or increase) if we changed βj\beta_j a little bit.
  • The second term, λsj\lambda s_j, comes from the L1 penalty. Here, sjs_j is not a regular derivative, but a subgradient of the absolute value function at zero.

What is a subgradient?
For most values of βj\beta_j, the derivative of ∣βj∣|\beta_j| is either +1+1 (if βj>0\beta_j > 0) or −1-1 (if βj<0\beta_j < 0). However, at βj=0\beta_j = 0, 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 −1-1 and 11. That is, when βj=0\beta_j = 0, sjs_j can be any number in the interval [−1,1][-1, 1].

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 λ\lambda, then the optimal solution is to set βj=0\beta_j = 0. In this case, the equation can still be satisfied by choosing an appropriate sjs_j in [−1,1][-1, 1].

In other words:

  • If the data "wants" to pull βj\beta_j away from zero (the gradient is large), but not enough to overcome the penalty, then the best solution is to keep βj=0\beta_j = 0.
  • If the gradient is strong enough (in magnitude) to overcome λ\lambda, then βj\beta_j 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 βj=0\beta_j = 0 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.

Out[2]:
Visualization
Geometric visualization showing why LASSO produces sparse solutions: elliptical SSE contours intersect L1 diamond constraint at sharp corners where coefficients become zero, while L2 circle constraint allows non-zero coefficients.
Geometric interpretation of LASSO (L1) vs Ridge (L2) regularization. The plot shows why LASSO produces sparse solutions by illustrating the constraint regions in coefficient space. The elliptical contours represent levels of constant Sum of Squared Errors (SSE), with inner contours having lower error. The diamond-shaped region (blue) is the L1 constraint |β₁| + |β₂| ≤ t, while the circular region (red) is the L2 constraint β₁² + β₂² ≤ t. The optimal solution occurs where the SSE contours first touch the constraint region. Because the L1 constraint has sharp corners at the axes, the contours typically intersect at these corners, setting one or more coefficients exactly to zero (shown by the blue star). In contrast, the smooth L2 constraint rarely intersects at the axes, so Ridge regression (red star) shrinks coefficients but doesn't eliminate them. This geometric property is the fundamental reason LASSO performs automatic feature selection.

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.