Ridge Regression
Throughout this page we assume the linear regression model
\(\boldsymbol{y} = X\boldsymbol{\beta} + \boldsymbol{\epsilon}\) with i.i.d. Gaussian noise
\(\boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \sigma^2 I)\), where \(X \in \mathbb{R}^{n \times d}\) is the design matrix and
\(\boldsymbol{\beta} \in \mathbb{R}^d\) is the parameter vector. As established in our
linear regression page, the
maximum likelihood estimator
under this model coincides with the
least-squares solution
given by the normal equations:
\[
\widehat{\boldsymbol{\beta}}_{\text{LS}} = (X^\top X)^{-1} X^\top \boldsymbol{y} = \widehat{\boldsymbol{\beta}}_{\text{MLE}},
\]
valid whenever \(X^\top X\) is invertible.
This estimator is unbiased for a fixed design, since \(\mathbb{E}[\boldsymbol{\epsilon}] = \mathbf{0}\), and it is reliable when
\(X^\top X\) is well-conditioned. In high-dimensional settings with \(d \gt n\), however, the \(d \times d\) matrix \(X^\top X\) has rank
at most \(n\) and is singular, and even for \(d \leq n\) nearly collinear features make it ill-conditioned. The objective surface is then
flat or nearly flat in some directions, so the solution is non-unique or highly sensitive to noise. Coefficients that track the noise fit
the training data at the expense of new data, a failure known as overfitting. Regularization addresses it by accepting
some bias in exchange for a reduction in variance, a principle we will formalize in the
bias-variance decomposition below.
To mitigate this issue, we add a penalty term to the objective. This technique is called regularization,
and the modified version of linear regression is known as ridge regression:
\[
\begin{align*}
\widehat{\boldsymbol{\beta}}_{\text{ridge}}
&= \arg \min_{\boldsymbol{\beta}} \|\boldsymbol{y} - X\boldsymbol{\beta}\|_2^2 + \underbrace{\lambda \|\boldsymbol{\beta}\|_2^2}_{\text{squared } \ell_2 \text{-norm regularizer}} \\\\
&= \arg \min_{\boldsymbol{\beta}} \boldsymbol{y}^\top \boldsymbol{y} - 2\boldsymbol{\beta}^\top X^\top \boldsymbol{y} + \boldsymbol{\beta}^\top (X^\top X + \lambda I) \boldsymbol{\beta} \\\\
&=: \arg \min_{\boldsymbol{\beta}} J(\boldsymbol{\beta}).
\end{align*}
\]
Setting the gradient to zero,
\[
\nabla_{\boldsymbol{\beta}} J(\boldsymbol{\beta}) = -2 X^\top \boldsymbol{y} + 2(X^\top X + \lambda I) \boldsymbol{\beta} = \mathbf{0},
\]
yields the closed-form solution
\[
\widehat{\boldsymbol{\beta}}_{\text{ridge}} = (X^\top X + \lambda I)^{-1} X^\top \boldsymbol{y},
\]
where \(\lambda \gt 0\) is the regularization parameter controlling the strength of the penalty.
The inverse in this formula exists because \(X^\top X + \lambda I\) is
positive definite.
For \(\boldsymbol{\beta} \neq \mathbf{0}\),
\[
\boldsymbol{\beta}^\top (X^\top X + \lambda I)\boldsymbol{\beta}
= \|X\boldsymbol{\beta}\|_2^2 + \lambda\|\boldsymbol{\beta}\|_2^2
\geq \lambda\|\boldsymbol{\beta}\|_2^2 \gt 0,
\]
so \((X^\top X + \lambda I)\boldsymbol{\beta} = \mathbf{0}\) forces \(\boldsymbol{\beta} = \mathbf{0}\).
The stationary point is also the unique global minimizer. Expanding the quadratic form below and using
\((X^\top X + \lambda I)\widehat{\boldsymbol{\beta}}_{\text{ridge}} = X^\top \boldsymbol{y}\) shows that
\[
\begin{align*}
J(\boldsymbol{\beta})
&= J(\widehat{\boldsymbol{\beta}}_{\text{ridge}}) \\\\
&\quad + (\boldsymbol{\beta} - \widehat{\boldsymbol{\beta}}_{\text{ridge}})^\top (X^\top X + \lambda I)(\boldsymbol{\beta} - \widehat{\boldsymbol{\beta}}_{\text{ridge}}),
\end{align*}
\]
and the last term is positive for every \(\boldsymbol{\beta} \neq \widehat{\boldsymbol{\beta}}_{\text{ridge}}\).
Theorem: Ridge Regression Solution
Ridge regression augments the least-squares objective with a squared \(\ell_2\)-norm penalty
on the parameter vector:
\[
\widehat{\boldsymbol{\beta}}_{\text{ridge}} = \arg \min_{\boldsymbol{\beta}} \|\boldsymbol{y} - X\boldsymbol{\beta}\|_2^2 + \lambda \|\boldsymbol{\beta}\|_2^2
= (X^\top X + \lambda I)^{-1} X^\top \boldsymbol{y},
\]
where \(\lambda \gt 0\) is the regularization parameter. The addition of \(\lambda I\) guarantees that
\(X^\top X + \lambda I\) is positive definite and hence invertible, even when
\(X^\top X\) is singular.
The hyperparameter \(\lambda \gt 0\) controls the strength of the regularization:
- As \(\lambda \to 0\), if \(X^\top X\) is invertible, the solution approaches the least-squares solution:
\(\widehat{\boldsymbol{\beta}}_{\text{ridge}} \to \widehat{\boldsymbol{\beta}}_{\text{LS}}\). If \(X^\top X\) is singular, it approaches instead the
minimum-norm least-squares solution
\(X^\dagger \boldsymbol{y}\), a limit we state without proof.
- As \(\lambda \to \infty\), the solution is pushed toward zero: \(\widehat{\boldsymbol{\beta}}_{\text{ridge}} \to \mathbf{0}\), leading to high bias but low variance.
Demo: Ridge Regression
Note that the demo solves both problems via QR factorization, so the \(\lambda = 0\) case is genuine ordinary least squares (OLS) with
no hidden regularization. OLS is sometimes truly singular, for instance when the number of coefficients exceeds the number of training
points. In that case the demo reports the failure honestly while ridge with \(\lambda \gt 0\) remains well-posed, which illustrates the
invertibility guarantee in the theorem above. This demo and the cross-validation demo below draw uniform noise, whereas our model
assumes Gaussian noise.
After running the demo above, we observe that at high polynomial degrees OLS has lower training error than ridge with \(\lambda \gt 0\),
but often has higher test error and much larger coefficients than ridge with a suitable \(\lambda\). In such cases, the least-squares fit
memorizes the training data too closely and fails to generalize well to new, unseen data.
In contrast, by penalizing large weights, ridge regression may have slightly higher training error but often generalizes better to
new data for a well-chosen \(\lambda\). This tradeoff between fitting the training data and maintaining model simplicity is known
as the bias-variance tradeoff. For least squares, raising the polynomial degree never increases the training
error, but after a certain point, the test error typically begins to increase. This turning point highlights the
danger of overfitting.
Cross Validation
To build a model that generalizes well and select an appropriate regularization strength (\(\lambda\)),
we employ K-fold cross-validation (CV) on the training set. The training data is partitioned into \(K\)
folds of equal (or nearly equal) size. For each fold \(k \in \{1, \ldots, K\}\), the model is trained on the \(K - 1\) other folds and
validated on the \(k\)th fold. This process is repeated \(K\) times, so that each data point in the training set is used once for
validation. The average validation error is used to select the hyperparameter \(\lambda\). Finally, the model is evaluated on the
held-out test set.
Here, we demonstrate how K-fold CV is used to tune the regularization parameter \(\lambda \gt 0\) in ridge regression.
A special case of K-fold CV is Leave-One-Out Cross-Validation (LOOCV), where the number of folds \(K\) equals the number
of data points in the training set. That is, the model is trained on all training data points except one, which is used for validation, and
this process is repeated for each data point. Because each model is trained on nearly all the data, LOOCV tends to produce a low-bias
estimate of the test error of a model trained on the full training set. However, for a general learner, LOOCV requires one fit per training
point and is expensive for large datasets. In contrast, smaller values of \(K\) (such as 5-10) offer a good tradeoff between bias,
variance, and computational cost, which makes them a popular choice in practice.
In the demo, each thin line in the error plot is a single fold's validation curve, and the bold line is their average. The spread of the
thin lines shows how much the validation error varies from fold to fold. Varying \(K\) changes the number of curves and their dispersion.
Bias-Variance Tradeoff in Ridge Regression
Whereas mean squared error
measures the discrepancy between an estimator and the parameter it estimates, here we examine the closely related
discrepancy between a model's predictions and the true response. The estimator's mean squared error splits
into variance and squared bias. The prediction error carries a third term, the noise in the response itself.
We assume the probabilistic generative model:
\[
\boldsymbol{x}_i \sim P_X, \quad \boldsymbol{y} = X\boldsymbol{\beta} + \boldsymbol{\epsilon}, \quad \boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \sigma^2 I),
\]
and write \(\mathcal{D} = \{(\boldsymbol{x}_i, y_i)\}_{i=1}^n\) for the training sample, with \(\mathbb{E}_{\mathcal{D}}\) denoting expectation
over the random training set. At a fixed query point \(\boldsymbol{x} \in \mathbb{R}^d\), a fresh response is
\(y = \boldsymbol{x}^\top \boldsymbol{\beta} + \epsilon_0\), where \(\epsilon_0 \sim \mathcal{N}(0, \sigma^2)\) is
independent of \(\mathcal{D}\), so that \(\mathbb{E}[y \mid \boldsymbol{x}] = \boldsymbol{x}^\top \boldsymbol{\beta}\).
The prediction error then decomposes as follows.
Theorem: Bias-Variance Decomposition for Ridge Prediction Error
Under the linear-Gaussian model above, the expected squared prediction error of the ridge estimator
\(\widehat{\boldsymbol{\beta}}_{\text{ridge}}\) at a query point \(\boldsymbol{x}\) decomposes as
\[
\begin{align*}
&\mathbb{E}_{y, \mathcal{D} \mid \boldsymbol{x}} \!\left[ \left(y - \boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}} \right)^2 \mid \boldsymbol{x} \right] \\\\
&= \underbrace{\sigma^2}_{\text{Noise}}
+ \underbrace{\Big(\boldsymbol{x}^\top \boldsymbol{\beta} - \mathbb{E}_{\mathcal{D}}\!\left[\boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}} \mid \boldsymbol{x}\right] \Big)^2}_{(\text{Bias})^2}
+ \underbrace{\mathbb{E}_{\mathcal{D}}\!\left[\Big(\boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}} - \mathbb{E}_{\mathcal{D}}\!\left[\boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}} \mid \boldsymbol{x}\right] \Big)^2 \mid \boldsymbol{x}\right]}_{\text{Variance}}. \tag{1}
\end{align*}
\]
Derivation:
Adding and subtracting \(\mathbb{E}[y \mid \boldsymbol{x}] = \boldsymbol{x}^\top \boldsymbol{\beta}\) inside the squared error and expanding yields
\[
\begin{align*}
&\mathbb{E}_{y, \mathcal{D} \mid \boldsymbol{x}} \!\left[ \left(y - \boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}} \right)^2 \mid \boldsymbol{x} \right] \\\\
&= \mathbb{E}_{y \mid \boldsymbol{x}}\!\left[\left(y - \mathbb{E}[y \mid \boldsymbol{x}]\right)^2 \mid \boldsymbol{x}\right]
+ \mathbb{E}_{\mathcal{D}}\!\left[\left(\mathbb{E}[y \mid \boldsymbol{x}] - \boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}}\right)^2 \mid \boldsymbol{x}\right]
\end{align*}
\]
(the cross term vanishes because \(y - \mathbb{E}[y \mid \boldsymbol{x}] = \epsilon_0\) has mean zero and is independent of
\(\mathcal{D}\), hence of \(\widehat{\boldsymbol{\beta}}_{\text{ridge}}\)).
The first term equals \(\sigma^2\). The second admits a further bias-variance split via the identity
\(\mathbb{E}[(Z - c)^2] = (\mathbb{E}[Z] - c)^2 + \operatorname{Var}[Z]\), obtained by expanding
\((Z - \mathbb{E}[Z] + \mathbb{E}[Z] - c)^2\), applied with
\(Z = \boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}}\) and \(c = \boldsymbol{x}^\top \boldsymbol{\beta}\), giving (1).
Closed Form Under Orthogonal Design
To make the tradeoff concrete, hold the design matrix fixed with \(X^\top X = n I\), so that the
randomness in \(\mathcal{D}\) enters only through \(\boldsymbol{\epsilon}\). This corresponds to
standardized, orthogonal predictors, a common simplifying assumption that isolates the shrinkage effect
of \(\lambda\) without the confounding influence of correlated features. Substituting
\(\boldsymbol{y} = X\boldsymbol{\beta} + \boldsymbol{\epsilon}\) into the ridge solution:
\[
\begin{align*}
\widehat{\boldsymbol{\beta}}_{\text{ridge}} &= (X^\top X + \lambda I)^{-1} X^\top (X\boldsymbol{\beta} + \boldsymbol{\epsilon}) \\\\
&= (n+\lambda)^{-1} I \cdot (n\boldsymbol{\beta} + X^\top \boldsymbol{\epsilon}) \\\\
&= \frac{n}{n+\lambda}\boldsymbol{\beta} + \frac{1}{n+\lambda} X^\top \boldsymbol{\epsilon},
\end{align*}
\]
which exhibits the pure shrinkage form \(\widehat{\boldsymbol{\beta}}_{\text{ridge}} = \frac{n}{n+\lambda} \widehat{\boldsymbol{\beta}}_{\text{LS}}\). Therefore,
\[
\boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}} = \underbrace{\frac{n}{n+\lambda}\boldsymbol{x}^\top \boldsymbol{\beta}}_{\text{deterministic}} + \underbrace{\frac{1}{n+\lambda}(X\boldsymbol{x})^\top \boldsymbol{\epsilon}}_{\text{random}}.
\]
Since \(\mathbb{E}[\boldsymbol{\epsilon}] = \mathbf{0}\), the squared bias term in (1) is
\[
\left(\boldsymbol{x}^\top \boldsymbol{\beta} - \frac{n}{n+\lambda}\boldsymbol{x}^\top \boldsymbol{\beta}\right)^2
= \frac{\lambda^2}{(n+\lambda)^2}(\boldsymbol{x}^\top \boldsymbol{\beta})^2.
\]
The variance term is the variance of the random part. Because the entries of \(\boldsymbol{\epsilon}\) are
independent with variance \(\sigma^2\), a fixed combination \(\boldsymbol{a}^\top \boldsymbol{\epsilon}\) has variance
\(\sum_i a_i^2 \sigma^2 = \boldsymbol{a}^\top (\sigma^2 I) \boldsymbol{a}\), so
\[
\begin{align*}
\operatorname{Var}\!\left[\frac{1}{n+\lambda}(X\boldsymbol{x})^\top \boldsymbol{\epsilon}\right]
&= \frac{1}{(n+\lambda)^2}(X\boldsymbol{x})^\top (\sigma^2 I)(X\boldsymbol{x}) \\\\
&= \frac{\sigma^2}{(n+\lambda)^2}\boldsymbol{x}^\top (X^\top X) \boldsymbol{x}
= \frac{\sigma^2 n}{(n+\lambda)^2}\|\boldsymbol{x}\|_2^2.
\end{align*}
\]
Combining all three terms:
\[
\mathbb{E}_{y, \mathcal{D} \mid \boldsymbol{x}}\!\left[\left(y - \boldsymbol{x}^\top \widehat{\boldsymbol{\beta}}_{\text{ridge}}\right)^2 \mid \boldsymbol{x}\right]
= \sigma^2 + \frac{\lambda^2}{(n+\lambda)^2}(\boldsymbol{x}^\top \boldsymbol{\beta})^2 + \frac{\sigma^2 n}{(n+\lambda)^2}\|\boldsymbol{x}\|_2^2.
\]
This result makes the tradeoff explicit. As \(\lambda\) increases, the bias term
\(\frac{\lambda^2}{(n+\lambda)^2}(\boldsymbol{x}^\top \boldsymbol{\beta})^2\) grows when \(\boldsymbol{x}^\top \boldsymbol{\beta} \neq 0\),
while the variance term \(\frac{\sigma^2 n}{(n+\lambda)^2}\|\boldsymbol{x}\|_2^2\) shrinks when \(\boldsymbol{x} \neq \mathbf{0}\).
Differentiating the total error in \(\lambda\) gives
\[
\frac{2n\left(\lambda(\boldsymbol{x}^\top \boldsymbol{\beta})^2 - \sigma^2\|\boldsymbol{x}\|_2^2\right)}{(n+\lambda)^3},
\]
which is negative at \(\lambda = 0\) for every \(\boldsymbol{x} \neq \mathbf{0}\). A small positive \(\lambda\) therefore gives a smaller
expected prediction error at \(\boldsymbol{x}\) than least squares. When \(\boldsymbol{x}^\top \boldsymbol{\beta} \neq 0\), the error is
minimized at \(\lambda^* = \sigma^2\|\boldsymbol{x}\|_2^2 / (\boldsymbol{x}^\top \boldsymbol{\beta})^2\). This optimum depends on the
unknown \(\boldsymbol{\beta}\), so in practice \(\lambda\) is selected via cross-validation.
Insight: The Necessity of the Gap
The bias-variance decomposition sets up a tempting question: why not send \(\lambda \to 0\) and recover the unbiased least-squares
estimator? The decomposition tells us we trade bias for variance, but pushes the question one step further: what would happen if we
could drive the training error to exactly zero? In an overparameterized regime where \(d \gg n\) and \(X\) has rank \(n\), zero
training error is achievable. The model interpolates \(\mathcal{D}\), including the noise term \(\boldsymbol{\epsilon}\), rather than
distilling the underlying signal \(X\boldsymbol{\beta}\).
Regularization is, structurally, the imposition of a deliberate gap between the model's fit
and the training data. The penalty \(\lambda \|\boldsymbol{\beta}\|_2^2\) prevents the optimizer
from chasing every fluctuation in the noise, and so trades a controlled increase in bias for a
reduction in variance. The bias term in the decomposition above is not a defect to be eliminated. The derivative computed
above shows that, in the orthogonal design, some bias is worth accepting at every query point
\(\boldsymbol{x} \neq \mathbf{0}\).
Put more sharply, under the classical bias-variance picture, a learning algorithm that
closes the gap between its predictions and the noisy training labels completely is no longer
learning the signal. It is reconstructing the noise. (Heavily overparameterized models can
sometimes generalize despite interpolation, a phenomenon studied under the names
benign overfitting and double descent. The classical principle remains the
baseline.)
The same need for a productive gap recurs throughout machine learning, under names
such as early stopping, dropout, weight decay, and the implicit regularization of stochastic
gradient descent. Each is a mechanism for engineering a gap that lets the model see past the
noise. In the classical picture, generalization requires the model to be slightly wrong about the training data in order
to be right about the underlying distribution.
Lasso Regression
Ridge regression shrinks the coefficient vector toward zero as \(\lambda\) grows but, except by coincidence,
sets no coefficient exactly to zero.
In many applications we want the model to identify and discard uninformative variables automatically,
particularly when most features are irrelevant. This motivates replacing the squared \(\ell_2\)
penalty with an \(\ell_1\) penalty.
Definition: Lasso Regression
The Least Absolute Shrinkage and Selection Operator (Lasso)
solves
\[
\widehat{\boldsymbol{\beta}}_{\text{lasso}} = \arg \min_{\boldsymbol{\beta}} \|\boldsymbol{y} - X\boldsymbol{\beta}\|_2^2 + \lambda \|\boldsymbol{\beta}\|_1,
\]
where \(\|\boldsymbol{\beta}\|_1 = \sum_{j=1}^d |\beta_j|\) is the \(\ell_1\)-norm and
\(\lambda \gt 0\) controls the regularization strength.
The key property of Lasso is that it performs automatic feature selection. The \(\ell_1\) penalty can set coefficients
\(\beta_j\) to exactly zero, effectively dropping the corresponding features from the model. Geometrically, this occurs
because the \(\ell_1\) ball has corners, unlike the smooth \(\ell_2\) ball. The ball enters because a Lasso solution
\(\widehat{\boldsymbol{\beta}}_{\text{lasso}}\) also minimizes the squared loss over
\(\{\boldsymbol{\beta} : \|\boldsymbol{\beta}\|_1 \leq \|\widehat{\boldsymbol{\beta}}_{\text{lasso}}\|_1\}\). A point of that ball with a
smaller loss would also have a smaller penalized objective. The level sets of the loss function are therefore more likely to intersect this
ball at a point where one or more coordinates vanish, such as a vertex.
Unlike the ridge objective, the Lasso objective is not differentiable where some \(\beta_j = 0\), so a minimizer with some \(\beta_j = 0\)
cannot be found by setting a gradient to zero, and in general no closed form exists. In practice, Lasso is solved by coordinate descent or
proximal gradient methods that handle the non-smoothness via the
subdifferential of the \(\ell_1\)-norm.
After obtaining the Lasso solution, a common practice is to re-fit OLS on the selected features. Specifically, define
the active set
\[
S = \{j : \widehat{\beta}_{\text{lasso}, j} \neq 0\},
\]
and then solve the unregularized problem restricted to these features:
\[
\tilde{\boldsymbol{\beta}}_S = \arg \min_{\boldsymbol{\beta}_S} \sum_{i=1}^n (y_i - \boldsymbol{x}_{i,S}^\top \boldsymbol{\beta}_S)^2,
\]
setting \(\tilde{\beta}_j = 0\) for \(j \notin S\). This two-stage procedure re-estimates the selected coefficients without the \(\ell_1\)
penalty. When the columns of \(X\) are linearly dependent, the Lasso minimizer need not be unique (splitting a coefficient evenly between
two identical columns leaves the objective unchanged), and \(S\) then depends on which minimizer the solver returns.
Lasso's feature selection is fundamentally different from
dimensionality reduction via PCA.
Lasso selects a subset of the original variables. This preserves interpretability but
potentially misses signals that are spread across many weakly predictive features.
PCA, by contrast, constructs new variables, linear combinations of the originals, that capture directions of
maximal variance. This improves numerical stability for correlated features at the cost of direct
interpretability. When both compression and sparsity are needed, one can combine the two approaches by
reducing to a moderate number of principal components and then applying Lasso.