Quiz 2

Regularization Theory

660 words
3 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Regularization Theory ## 🎯 Learning Objectives - Derive Ridge and Lasso from constrained optimization - Explain the geometry of L1 vs L2 regularization - Understand the Bayesian interpretation (prior distributions) - Implement cross-validated regularized models ## 📖 Core Content ### 1.1 Intuition: Why Regulariza...

Regularization Theory

🎯 Learning Objectives

  • Derive Ridge and Lasso from constrained optimization
  • Explain the geometry of L1 vs L2 regularization
  • Understand the Bayesian interpretation (prior distributions)
  • Implement cross-validated regularized models

📖 Core Content

1.1 Intuition: Why Regularization Works

Think of model coefficients like a budget. Without regularization, the model can spend its "coefficient budget" however it likes — putting all its money on one feature if that minimizes training error. Regularization imposes a tax on large coefficients. The model must now pay a penalty for spending too much on any one feature, forcing it to distribute its budget more evenly or eliminate features entirely.

1.2 Ridge Regression as Constrained Optimization

Standard linear regression: minimize MSE. Ridge regression: minimize MSE subject to θj2t\sum \theta_j^2 \leq t By Lagrangian duality, this is equivalent to:
minθMSE(θ)+αθj2\min_{\theta} MSE(\theta) + \alpha \sum \theta_j^2
The geometry: The constraint θj2t\sum \theta_j^2 \leq t is a circle/sphere in parameter space. The unconstrained minimum of MSE likely lies outside this circle. The constrained optimum is where the MSE contours first touch the circle — which is always on the boundary (not at the origin).

1.3 Lasso Geometry

Lasso constraint: θjt\sum |\theta_j| \leq t — a diamond/square. The key difference: The diamond has corners at the axes. If the MSE contours touch at a corner, that coefficient is exactly zero. This is why Lasso produces sparse solutions. (Diagram)

1.4 Bayesian Interpretation

  • Ridge = θN(0,σ2/α)\theta \sim \mathcal{N}(0, \sigma^2/\alpha) — Gaussian prior on weights
  • Lasso = θLaplace(0,1/α)\theta \sim \text{Laplace}(0, 1/\alpha) — Laplace prior on weights The Gaussian prior concentrates probability near zero but allows any value. The Laplace prior has a sharp peak at zero and heavy tails — this gives the sparsity property.

1.5 Bias-Variance Decomposition for Ridge

For Ridge regression with orthogonal features XTX=IX^T X = I:
θ^ridge=θ1+α\hat{\theta}_{ridge} = \frac{\theta}{1 + \alpha} Bias(θ^ridge)=αθ1+αBias(\hat{\theta}_{ridge}) = -\frac{\alpha\theta}{1 + \alpha} Var(θ^ridge)=σ2(1+α)2Var(\hat{\theta}_{ridge}) = \frac{\sigma^2}{(1 + \alpha)^2}
As α\alpha increases: bias increases, variance decreases. The total MSE (bias² + variance) has a minimum at some intermediate α\alpha.

1.6 Implementation

python
# runnable
import numpy as np
from sklearn.linear_model import RidgeCV, LassoCV, ElasticNetCV
from sklearn.datasets import make_regression
X, y = make_regression(n_samples=200, n_features=20, noise=0.5, random_state=42)
# Ridge with built-in CV
ridge_cv = RidgeCV(alphas=np.logspace(-3, 3, 20), cv=5)
ridge_cv.fit(X, y)
print(f"Ridge best α: {ridge_cv.alpha_:.3f}")
# Lasso with built-in CV
lasso_cv = LassoCV(alphas=np.logspace(-3, 3, 20), cv=5, random_state=42)
lasso_cv.fit(X, y)
print(f"Lasso best α: {lasso_cv.alpha_:.3f}")
print(f"Non-zero coefficients: {np.sum(lasso_cv.coef_ != 0)} / {len(lasso_cv.coef_)}")
# Elastic Net with built-in CV
elastic_cv = ElasticNetCV(l1_ratio=[0.1, 0.3, 0.5, 0.7, 0.9],
                          alphas=np.logspace(-3, 3, 20), cv=5, random_state=42)
elastic_cv.fit(X, y)
print(f"Elastic Net best α: {elastic_cv.alpha_:.3f}, l1_ratio: {elastic_cv.l1_ratio_:.2f}")

📝 Practice Questions

Q1: Show that Ridge has a closed-form solution.
J(θ)=(yXθ)T(yXθ)+αθTθJ(\theta) = (y - X\theta)^T(y - X\theta) + \alpha \theta^T \theta
Set gradient to 0: =2XT(yXθ)+2αθ=0\nabla = -2X^T(y - X\theta) + 2\alpha\theta = 0 XTy+XTXθ+αθ=0-X^T y + X^T X \theta + \alpha\theta = 0 (XTX+αI)θ=XTy(X^T X + \alpha I)\theta = X^T y θ=(XTX+αI)1XTy\theta = (X^T X + \alpha I)^{-1} X^T y
This is always invertible because adding αI\alpha I makes the matrix positive definite (even if XTXX^T X was singular). Q2: Why can't Lasso be solved in closed form?
The L1 penalty θj\sum |\theta_j| is not differentiable at θj=0\theta_j = 0. There's a "kink" at zero where the derivative doesn't exist. This requires iterative optimization methods like coordinate descent or subgradient methods. Q3: Prove that Ridge reduces the variance of coefficient estimates.
From the closed form θ^ridge=(XTX+αI)1XTy\hat{\theta}_{ridge} = (X^T X + \alpha I)^{-1} X^T y: Var(θ^ridge)=σ2(XTX+αI)1XTX(XTX+αI)1Var(\hat{\theta}_{ridge}) = \sigma^2 (X^T X + \alpha I)^{-1} X^T X (X^T X + \alpha I)^{-1}
For orthogonal X (where XTX=IX^T X = I): Var(θ^ridge)=σ2(1+α)2Var(\hat{\theta}_{ridge}) = \frac{\sigma^2}{(1+\alpha)^2}
Compare to OLS: Var(θ^OLS)=σ2Var(\hat{\theta}_{OLS}) = \sigma^2. The Ridge variance is smaller by factor 1/(1+α)21/(1+\alpha)^2. Join Discord PreviousCourse OverviewNextNaive Bayes
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.