Ridge Regularization (L2 Regularization) › Example

Michael BrenndoerferJune 6, 20255 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 34Ridge Regularization (L2 Regularization) › Example
Reading section 5 of 9 · Example · 5 minSection 5 of 9 · 5 min

Book contents

Section 5 of 9 · 5 min read

Ridge Regularization (L2 Regularization) › ExampleOne section · Sections in this chapter5 / 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

Let's work through a simple mathematical example to understand how Ridge regression works. We remind ourselves of 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}

We'll use a small dataset with 3 observations and 2 features (excluding scaling to make the example easier to follow). Given following data:

  • n=3n = 3 observations
  • p=2p = 2 features
  • λ=1\lambda = 1 (regularization parameter)
X=[122331],y=[586]\mathbf{X} = \begin{bmatrix} 1 & 2 \\ 2 & 3 \\ 3 & 1 \end{bmatrix}, \mathbf{y} = \begin{bmatrix} 5 \\ 8 \\ 6 \end{bmatrix}

Let's walk through the Ridge regression calculation step by step.

Step 1: Calculate XTX\mathbf{X}^T\mathbf{X}
First, we compute the matrix multiplication of the transpose of X\mathbf{X} and X\mathbf{X}. This gives us the sum of squares and cross-products matrix of the features, which is used in both OLS and Ridge regression.

XTX=[123231][122331]=[14111114]\mathbf{X}^T\mathbf{X} = \begin{bmatrix} 1 & 2 & 3 \\ 2 & 3 & 1 \end{bmatrix} \begin{bmatrix} 1 & 2 \\ 2 & 3 \\ 3 & 1 \end{bmatrix} = \begin{bmatrix} 14 & 11 \\ 11 & 14 \end{bmatrix}

To verify: The (1,1)(1,1) element is 12+22+32=1+4+9=141^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14. The (1,2)(1,2) element is 1⋅2+2⋅3+3⋅1=2+6+3=111 \cdot 2 + 2 \cdot 3 + 3 \cdot 1 = 2 + 6 + 3 = 11.

Note: This matrix is symmetric, meaning Xij=XjiX_{ij} = X_{ji} for all i,ji, j. This is always true for XTX\mathbf{X}^T\mathbf{X}.

Step 2: Add regularization term λI\lambda \mathbf{I}
Next, we add the regularization term λI\lambda \mathbf{I} to the matrix. This penalizes large coefficients and helps prevent overfitting by making the matrix more stable. The resulting matrix is better conditioned, a term from numerical linear algebra that means small changes in input tend to produce small changes in output.

XTX+λI=[14111114]+[1001]=[15111115]\mathbf{X}^T\mathbf{X} + \lambda \mathbf{I} = \begin{bmatrix} 14 & 11 \\ 11 & 14 \end{bmatrix} + \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 15 & 11 \\ 11 & 15 \end{bmatrix}

Step 3: Calculate XTy\mathbf{X}^T\mathbf{y}
We then compute the product of the transpose of X\mathbf{X} and the target vector y\mathbf{y}. This operation measures how each feature in X\mathbf{X} is correlated with the target variable y\mathbf{y}. It forms the right-hand side of the normal equations used in both OLS and Ridge regression.

XTy=[123231][586]=[3940]\mathbf{X}^T\mathbf{y} = \begin{bmatrix} 1 & 2 & 3 \\ 2 & 3 & 1 \end{bmatrix} \begin{bmatrix} 5 \\ 8 \\ 6 \end{bmatrix} = \begin{bmatrix} 39 \\ 40 \end{bmatrix}

To verify: The first element is 1⋅5+2⋅8+3⋅6=5+16+18=391 \cdot 5 + 2 \cdot 8 + 3 \cdot 6 = 5 + 16 + 18 = 39. The second element is 2⋅5+3⋅8+1⋅6=10+24+6=402 \cdot 5 + 3 \cdot 8 + 1 \cdot 6 = 10 + 24 + 6 = 40.

Step 4: Find the inverse and solve
Now, we invert the regularized matrix from Step 2 to solve the linear system and find the coefficients.

(XTX+λI)−1=115⋅15−11⋅11[15−11−1115]=1104[15−11−1115](\mathbf{X}^T\mathbf{X} + \lambda \mathbf{I})^{-1} = \frac{1}{15 \cdot 15 - 11 \cdot 11} \begin{bmatrix} 15 & -11 \\ -11 & 15 \end{bmatrix} = \frac{1}{104} \begin{bmatrix} 15 & -11 \\ -11 & 15 \end{bmatrix}

Note: In mathematics, the inverse of a 2×22 \times 2 matrix [abcd]\begin{bmatrix} a & b \\ c & d \end{bmatrix} is given by 1ad−bc[d−b−ca]\frac{1}{ad-bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}, provided ad−bc≠0ad-bc \neq 0. For larger matrices, the process involves more advanced linear algebra techniques such as Gaussian elimination or using determinants and adjugates.

In practice, when working with code, you can invert matrices using libraries like NumPy. For example, in Python, you can use np.linalg.inv(matrix) to compute the inverse of a matrix. However, for solving systems like Ridge regression, it's often more numerically stable to use np.linalg.solve() or specialized solvers rather than explicitly computing the inverse.

np.linalg.inv(A) explicitly computes the inverse of a matrix and then multiplies it by bb, which is slower and can amplify numerical errors. In contrast, np.linalg.solve(A, b) directly solves the system Ax=bA x = b using efficient factorizations without forming the inverse, making it both faster and more numerically stable.

Step 5: Calculate Ridge coefficients
Finally, we multiply the inverse matrix by XTy\mathbf{X}^T\mathbf{y} to obtain the Ridge regression coefficients. These coefficients are shrunk compared to the OLS solution due to the regularization.

β^Ridge=1104[15−11−1115][3940]=1104[145171]=[1.3941.644]\hat{\boldsymbol{\beta}}_{Ridge} = \frac{1}{104} \begin{bmatrix} 15 & -11 \\ -11 & 15 \end{bmatrix} \begin{bmatrix} 39 \\ 40 \end{bmatrix} = \frac{1}{104} \begin{bmatrix} 145 \\ 171 \end{bmatrix} = \begin{bmatrix} 1.394 \\ 1.644 \end{bmatrix}

To verify the matrix multiplication:

  • First element: 15⋅39+(−11)⋅40=585−440=14515 \cdot 39 + (-11) \cdot 40 = 585 - 440 = 145
  • Second element: (−11)⋅39+15⋅40=−429+600=171(-11) \cdot 39 + 15 \cdot 40 = -429 + 600 = 171

Then dividing by 104: 145/104≈1.394145/104 \approx 1.394 and 171/104≈1.644171/104 \approx 1.644.

If we compare it with OLS which has no regularization (λ=0\lambda = 0), the OLS solution would be:

β^OLS=(XTX)−1XTy\hat{\boldsymbol{\beta}}_{OLS} = (\mathbf{X}^T\mathbf{X})^{-1} \mathbf{X}^T\mathbf{y}

Let's calculate this step by step:

First, compute the determinant: 14⋅14−11⋅11=196−121=7514 \cdot 14 - 11 \cdot 11 = 196 - 121 = 75

(XTX)−1=175[14−11−1114](\mathbf{X}^T\mathbf{X})^{-1} = \frac{1}{75} \begin{bmatrix} 14 & -11 \\ -11 & 14 \end{bmatrix}

Then multiply by XTy\mathbf{X}^T\mathbf{y}:

β^OLS=175[14−11−1114][3940]=175[106131]=[1.4131.747]\hat{\boldsymbol{\beta}}_{OLS} = \frac{1}{75} \begin{bmatrix} 14 & -11 \\ -11 & 14 \end{bmatrix} \begin{bmatrix} 39 \\ 40 \end{bmatrix} = \frac{1}{75} \begin{bmatrix} 106 \\ 131 \end{bmatrix} = \begin{bmatrix} 1.413 \\ 1.747 \end{bmatrix}

where 14⋅39−11⋅40=546−440=10614 \cdot 39 - 11 \cdot 40 = 546 - 440 = 106 and −11⋅39+14⋅40=−429+560=131-11 \cdot 39 + 14 \cdot 40 = -429 + 560 = 131.

Comparison:

  • Ridge coefficients: [1.3941.644]\begin{bmatrix} 1.394 \\ 1.644 \end{bmatrix}
  • OLS coefficients: [1.4131.747]\begin{bmatrix} 1.413 \\ 1.747 \end{bmatrix}

Notice how Ridge shrunk both coefficients toward zero compared to OLS. The first coefficient decreased from 1.413 to 1.394, and the second from 1.747 to 1.644.