Neural Sync Active
Regularization Theory
Registry Synced
Regularization Theory
660 words
3 min read
Reading compass
Now Β· π― Learning Objectives
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 βΞΈj2ββ€t
By Lagrangian duality, this is equivalent to:
The geometry: The constraint βΞΈj2ββ€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: ββ£ΞΈjββ£β€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/Ξ±) β Gaussian prior on weights
- Lasso = ΞΈβΌLaplace(0,1/Ξ±) β 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=I:
As Ξ± increases: bias increases, variance decreases. The total MSE (biasΒ² + variance) has a minimum at some intermediate Ξ±.
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(ΞΈ)=(yβXΞΈ)T(yβXΞΈ)+Ξ±ΞΈTΞΈSet gradient to 0: β=β2XT(yβXΞΈ)+2Ξ±ΞΈ=0 βXTy+XTXΞΈ+Ξ±ΞΈ=0 (XTX+Ξ±I)ΞΈ=XTy ΞΈ=(XTX+Ξ±I)β1XTyThis is always invertible because adding Ξ±I makes the matrix positive definite (even if XTX was singular). Q2: Why can't Lasso be solved in closed form?The L1 penalty ββ£ΞΈjββ£ is not differentiable at ΞΈ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: Var(ΞΈ^ridgeβ)=Ο2(XTX+Ξ±I)β1XTX(XTX+Ξ±I)β1For orthogonal X (where XTX=I): Var(ΞΈ^ridgeβ)=(1+Ξ±)2Ο2βCompare to OLS: Var(ΞΈ^OLSβ)=Ο2. The Ridge variance is smaller by factor 1/(1+Ξ±)2. Join Discord PreviousCourse OverviewNextNaive Bayes