Prior of Lasso and Ridge
From a Bayesian Maximum a Posteriori (MAP) perspective under Gaussian observation noise, Ridge regression assumes a zero-mean Gaussian prior on the weights while Lasso assumes a zero-mean Laplace prior. The Gaussian log-prior produces the quadratic L2 penalty that shrinks coefficients smoothly, whereas the sharp peak of the Laplace prior at zero produces the L1 penalty whose MAP mode drives weak weights to exact zero.
Study Card: Priors of Lasso and Ridge Regression
Direct Answer
In Bayesian linear regression with Gaussian observation noise , , finding the Maximum a Posteriori (MAP) parameter estimate maximizes the log posterior , which is equivalent to minimizing the negative log-likelihood plus the negative log-prior:
- Ridge Regression (Gaussian Prior): Placing independent zero-mean Gaussian priors yields:
corresponding to an penalty with regularization strength .
- Lasso Regression (Laplace Prior): Placing independent zero-mean Laplace priors yields:
corresponding to an penalty with .
Notice an important Bayesian subtlety: sparsity occurs specifically at the MAP mode due to the non-differentiable cusp of at zero (whereas the full Bayesian posterior mean under a continuous Laplace prior is non-sparse).
Key Terms
- Prior Distribution: Probability distribution encoding beliefs about parameter values before observing data.
- Laplace Distribution: Double-exponential distribution with a sharp peak at zero and heavier tails than a Gaussian of matched variance, yielding the penalty under MAP estimation.
- Gaussian Distribution: Smooth, bell-shaped distribution whose quadratic negative log-density produces the penalty under MAP estimation.
- Maximum a Posteriori (MAP) Estimation: Point estimation maximizing ; reduces to MLE under an improper flat prior.
- Regularization: Constraining parameter estimates via priors (Bayesian view) or explicit norm penalties (frequentist view) to reduce variance.
Example
Consider predicting house prices from dozens of candidate features (square footage, bedrooms, wall color, zip code). If domain belief holds that only a few features truly drive price—matching a sharply peaked Laplace prior—Lasso’s MAP solution sets irrelevant feature coefficients to exact zero. If instead most features contribute modest correlated signals—matching a Gaussian prior—Ridge shrinks all weights smoothly toward zero without discarding variables.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Lasso, Ridge
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from scipy.stats import laplace, norm
# 1. Compare Laplace (Lasso) vs. Gaussian (Ridge) prior densities
x = np.linspace(-5, 5, 1000)
laplace_dist = laplace.pdf(x, loc=0, scale=1)
gaussian_dist = norm.pdf(x, loc=0, scale=1)
plt.figure(figsize=(10, 6))
plt.plot(x, laplace_dist, label="Laplace Prior (Lasso / L1)")
plt.plot(x, gaussian_dist, label="Gaussian Prior (Ridge / L2)")
plt.title("Prior Distributions for Lasso and Ridge")
plt.xlabel("Coefficient Value")
plt.ylabel("Probability Density")
plt.legend()
plt.grid(True)
plt.show()
# 2. Illustrative Regression showing sparse MAP vs. dense shrinkage
X, y = make_regression(n_samples=100, n_features=10, n_informative=2, random_state=42, noise=0.5)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
lasso = Lasso(alpha=0.1) # alpha controls L1 regularization strength
lasso.fit(X_train, y_train)
lasso_preds = lasso.predict(X_test)
ridge = Ridge(alpha=0.1) # alpha controls L2 regularization strength
ridge.fit(X_train, y_train)
ridge_preds = ridge.predict(X_test)
print("Lasso Coefficients (sparse due to L1 / Laplace MAP):", np.round(lasso.coef_, 4))
print("Ridge Coefficients (dense shrinkage due to L2 / Gaussian MAP):", np.round(ridge.coef_, 4))
Related Concepts
- Regularization & Hyperparameter ( / ): In the MAP view, stronger regularization corresponds to a narrower prior variance ( or ) relative to observation noise .
- Bayesian Inference vs. MAP: Full Bayesian inference integrates over the entire posterior to quantify parameter uncertainty, whereas MAP returns only a single mode.
- Elastic Net Prior: Corresponds to a prior whose log-density combines L1 and L2 terms (a Gaussian-Laplace product prior).
- Sparsity & Feature Selection: Why the diamond constraint / Laplace cusp enables automatic feature selection in high-dimensional settings.