Ridge Regularization (L2 Regularization) › Summation Notation

Michael BrenndoerferJune 6, 20257 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 34Ridge Regularization (L2 Regularization) › Summation Notation
Reading section 3 of 9 · Summation Notation · 7 minSection 3 of 9 · 7 min

Book contents

Section 3 of 9 · 7 min read

Ridge Regularization (L2 Regularization) › Summation NotationOne section · Sections in this chapter3 / 9 ↓
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

As for LASSO, we can write the Ridge regression objective function as a summation:

min⁡β{∑i=1n(yi−y^i)2+λ∑j=1pβj2}\min_{\beta} \left\{ \sum_{i=1}^n (y_i - \hat{y}_i)^2 + \lambda \sum_{j=1}^p \beta_j^2 \right\}

where:

  • yiy_i = actual (observed) value for observation ii
  • y^i=β0+β1xi1+β2xi2+⋯+βpxip\hat{y}_i = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \cdots + \beta_p x_{ip} = predicted value for observation ii
  • λ\lambda = regularization parameter (non-negative; λ≥0\lambda \geq 0)
  • β0\beta_0 = intercept term (not penalized in Ridge regression)
  • βj\beta_j = coefficient for feature jj (where j=1,2,…,pj = 1, 2, \ldots, p)
  • xijx_{ij} = value of feature jj for observation ii
  • nn = number of observations (sample size)
  • pp = number of features (predictors)

This notation should look familiar from the previous section on LASSO. Rather than the L1 penalty, we have the L2 penalty, which is the sum of the squared coefficients.

∑j=1pβj2=∥β∥22\sum_{j=1}^p \beta_j^2 = \|\beta\|_2^2

This is the squared L2 norm (also called the L2 norm squared) of the coefficient vector β\boldsymbol{\beta}, where ∥β∥2=∑j=1pβj2\|\boldsymbol{\beta}\|_2 = \sqrt{\sum_{j=1}^p \beta_j^2} is the L2 norm itself.

Matrix Notation

However, because matrix computations are much faster than summation, we can also write the Ridge regression objective function in matrix notation:

min⁡β{∣∣y−Xβ∣∣22+λ∣∣β∣∣22}\min_{\beta} \left\{ ||\mathbf{y} - \mathbf{X}\boldsymbol{\beta}||_2^2 + \lambda ||\boldsymbol{\beta}||_2^2 \right\}

where:

  • y\mathbf{y} = n×1n \times 1 vector of target values (observed responses)
  • X\mathbf{X} = n×pn \times p design matrix of features (each row is an observation, each column is a feature)
  • β\boldsymbol{\beta} = p×1p \times 1 vector of coefficients (parameters to estimate)
  • ∣∣⋅∣∣2||\cdot||_2 = L2 norm (Euclidean norm), defined as ∣∣v∣∣2=∑ivi2||\mathbf{v}||_2 = \sqrt{\sum_i v_i^2}
  • ∣∣⋅∣∣22||\cdot||_2^2 = squared L2 norm, defined as ∣∣v∣∣22=∑ivi2||\mathbf{v}||_2^2 = \sum_i v_i^2

Closed-Form Solution

Because we are not encountering issues of non-differentiability, we can find the closed-form solution for the Ridge regression objective function. Taking the derivative with respect to β\boldsymbol{\beta} and setting it to zero:

∂∂β[∣∣y−Xβ∣∣22+λ∣∣β∣∣22]=0\frac{\partial}{\partial \boldsymbol{\beta}} \left[ ||\mathbf{y} - \mathbf{X}\boldsymbol{\beta}||_2^2 + \lambda ||\boldsymbol{\beta}||_2^2 \right] = 0

This yields the Ridge estimator:

β^Ridge=(XTX+λI)−1XTy\hat{\boldsymbol{\beta}}_{Ridge} = (\mathbf{X}^T\mathbf{X} + \lambda \mathbf{I})^{-1} \mathbf{X}^T\mathbf{y}

where:

  • β^Ridge\hat{\boldsymbol{\beta}}_{Ridge} = Ridge regression coefficient estimates
  • XT\mathbf{X}^T = transpose of the design matrix
  • XTX\mathbf{X}^T\mathbf{X} = p×pp \times p matrix (sum of squares and cross-products of features)
  • λ\lambda = regularization parameter
  • I\mathbf{I} = p×pp \times p identity matrix (diagonal matrix with 1s on the diagonal and 0s elsewhere)
  • (⋅)−1(\cdot)^{-1} = matrix inverse operation
Identity Matrix

The identity matrix is a square matrix with ones on the diagonal and zeros elsewhere. For example, the identity matrix of size 3 is:

I=[100010001]\mathbf{I} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{bmatrix}

Mathematical Properties

  • Bias-Variance Tradeoff: As λ\lambda increases, bias increases but variance decreases
  • Shrinkage: All coefficients are shrunk toward zero. In the special case of orthonormal features (where XTX=I\mathbf{X}^T\mathbf{X} = \mathbf{I}), each coefficient is multiplied by the shrinkage factor 11+λ\frac{1}{1+\lambda}, meaning β^Ridge=11+λβ^OLS\hat{\beta}_{Ridge} = \frac{1}{1+\lambda}\hat{\beta}_{OLS}
  • Stability: The term λI\lambda \mathbf{I} ensures (XTX+λI)(\mathbf{X}^T\mathbf{X} + \lambda \mathbf{I}) is always invertible, even when XTX\mathbf{X}^T\mathbf{X} is singular (non-invertible)
  • Regularization Path: As λ→0\lambda \to 0, Ridge approaches OLS (β^Ridge→β^OLS\hat{\boldsymbol{\beta}}_{Ridge} \to \hat{\boldsymbol{\beta}}_{OLS}); as λ→∞\lambda \to \infty, all coefficients approach zero (β^Ridge→0\hat{\boldsymbol{\beta}}_{Ridge} \to \mathbf{0})
Out[4]:
Visualization
Log-scale bias-variance tradeoff plot with blue training error and coral test error across 50 lambda values, a dashed teal line at the optimal lambda 9.103, a highlighted minimum-test-error point, and labeled overfitting and underfitting regions.
Bias-variance tradeoff in Ridge regression. As λ increases, training error rises (model becomes more biased) while model variance decreases. The test error forms a U-shaped curve, with the optimal λ minimizing test error by balancing bias and variance. Too small λ leads to overfitting (high variance), while too large λ leads to underfitting (high bias).

This visualization illustrates the fundamental bias-variance tradeoff in Ridge regression. When λ is very small (left side), the model is close to OLS and may overfit the training data, resulting in low training error but high test error due to high variance. As λ increases, the training error rises because the model becomes more constrained (increased bias), but the test error initially decreases as variance is reduced. The optimal λ (green line) achieves the best balance, minimizing test error. Beyond this point, further increasing λ causes underfitting, where the model becomes too simple and both training and test errors increase due to excessive bias.

We skipped over the derivation of the closed-form solution for Ridge regression, but it is similar to the derivation for LASSO, but easier since we are not encountering issues of non-differentiability, making the math more straightforward.

Regularization Path

To better understand how Ridge regression affects multiple coefficients simultaneously, let's visualize the regularization path.how each coefficient changes as we increase λ.

Out[5]:
Visualization
Log-scale Ridge regularization path with eight labeled coefficient curves beta one through beta eight, each shrinking smoothly toward a dashed zero reference line as lambda increases from 0.01 to 10,000.
Regularization path showing multiple coefficients shrink smoothly toward zero as λ increases. Unlike LASSO, no coefficient reaches exactly zero.

This plot reveals a key characteristic of Ridge regression: as λ increases, all coefficients shrink smoothly and continuously toward zero, but do not reach exactly zero. This is fundamentally different from LASSO, where coefficients can be driven to exactly zero. The smooth shrinkage means Ridge maintains all features in the model, which can be advantageous when all features are believed to be relevant or when dealing with highly correlated features.