Tutorials › Regression Analysis (Undergraduate) › Effective Degrees of Freedom for Ridge

Ridge and Lasso Regression · Tutorial 689 of 991

Effective Degrees of Freedom for Ridge

Learn how ridge’s smoother matrix leads to effective degrees of freedom and how each singular direction contributes as the penalty changes.

Advanced 10 min read

What You'll Learn

  • Define ridge effective degrees of freedom using the trace of the fitted-value smoother.
  • Separate the unpenalized intercept from the centered predictor contribution.
  • Derive the singular-value formula for ridge effective degrees of freedom.
  • Handle rank-deficient designs and the zero-penalty case correctly.
  • Calculate and interpret how effective degrees of freedom change with the penalty.
  • Distinguish effective degrees of freedom from the number of nonzero coefficients.

Why Ridge Has Effective, Rather Than Ordinary, Degrees of Freedom

In ordinary least squares, the fitted values are a projection of the observed response onto the space spanned by the model columns. The trace of that projection matrix equals the dimension of the space, so it counts the fitted model’s degrees of freedom. Ridge regression changes the fitted values by shrinking contributions from predictor directions. Its smoother matrix is generally not a projection, and its trace need not be an integer. That trace provides a useful measure of how much flexibility the ridge fit uses.

The key is to define the model and penalty precisely. Here, the predictors are centered, the intercept is not penalized, and the ridge penalty is \(\lambda\) times the sum of squared slope coefficients. For \(\lambda>0\), the ridge fit is a linear function of the response. Its smoother matrix makes it possible to quantify the fit’s complexity, while a singular-value decomposition reveals which predictor directions contribute most.

Definition: For a fitted-value rule \(\hat{y}=S_\lambda y\), the effective degrees of freedom of that rule are \(\operatorname{tr}(S_\lambda)\), the trace of its smoother matrix. This tutorial uses an unpenalized intercept and centered predictor columns, so the intercept contributes one degree of freedom.

The Ridge Model and Its Smoother Matrix

Let \(y\) be the \(n\)-vector of observed responses and let \(X\) be the \(n\times p\) design matrix of centered predictor columns. With an intercept, write the ridge objective as

$$ \min_{\beta_0,\boldsymbol{\beta}} \left\{ \sum_{i=1}^n (y_i-\beta_0-\mathbf{x}_i^\mathsf{T}\boldsymbol{\beta})^2 +\lambda\boldsymbol{\beta}^\mathsf{T}\boldsymbol{\beta} \right\},\qquad \lambda\geq 0. $$

Here \(\beta_0\) is the intercept, \(\boldsymbol{\beta}\) is the \(p\)-vector of slopes, and \(\lambda\) is the penalty parameter for this stated objective. Since the columns of \(X\) are centered, the intercept estimate is \(\bar{y}\); equivalently, the slope fit can be obtained by fitting the centered response \(y_c=y-\bar{y}\mathbf{1}\). This separates the intercept from the predictor contribution.

For \(\lambda>0\), the matrix \(X^\mathsf{T}X+\lambda I_p\) is invertible, even if \(X\) is rank deficient. The centered ridge fitted values are \(X(X^\mathsf{T}X+\lambda I_p)^{-1}X^\mathsf{T}y_c\). Adding back the intercept gives the full fitted-value smoother matrix:

$$ S_\lambda=\frac{\mathbf{1}\mathbf{1}^\mathsf{T}}{n} +X(X^\mathsf{T}X+\lambda I_p)^{-1}X^\mathsf{T}, \qquad \lambda>0. $$

The first term maps \(y\) to its mean in every observation, and the second maps the centered response to the ridge predictor fit. Because \(X\) is centered, its columns are orthogonal to \(\mathbf{1}\), so these contributions are separated. The intercept term has trace one. At \(\lambda=0\), do not use the displayed inverse formula unless \(X^\mathsf{T}X\) is known to be invertible. For any rank, the zero-penalty predictor fit is instead the least-squares projection onto \(\operatorname{col}(X)\).

Deriving the Singular-Value Formula

Let \(r=\operatorname{rank}(X)\), and write a reduced singular-value decomposition using only the positive singular values:

$$ X=U_rD_rV_r^\mathsf{T},\qquad D_r=\operatorname{diag}(d_1,\ldots,d_r),\qquad d_j>0. $$

The columns of \(U_r\) are orthonormal vectors in observation space, and those of \(V_r\) are orthonormal vectors in predictor space. Substituting this decomposition into the predictor part of the smoother and using \(V_r^\mathsf{T}V_r=I_r\) gives

$$ \begin{aligned} X(X^\mathsf{T}X+\lambda I_p)^{-1}X^\mathsf{T} &=U_rD_r(D_r^2+\lambda I_r)^{-1}D_rU_r^\mathsf{T}\\ &=U_r\operatorname{diag}\left( \frac{d_1^2}{d_1^2+\lambda},\ldots, \frac{d_r^2}{d_r^2+\lambda} \right)U_r^\mathsf{T},\qquad \lambda>0. \end{aligned} $$

Taking the trace uses the fact that for compatible matrices, \(\operatorname{tr}(AB)=\operatorname{tr}(BA)\). The trace of the intercept term is one, and each diagonal entry in the singular-direction representation contributes its shrinkage weight. Therefore:

Result: With centered predictors and an unpenalized intercept, ridge effective degrees of freedom are
$$ df_{\mathrm{eff}}(\lambda) =1+\sum_{j=1}^{r}\frac{d_j^2}{d_j^2+\lambda}, \qquad \lambda>0. $$
At \(\lambda=0\), the predictor smoother is the orthogonal projection \(XX^+\) onto \(\operatorname{col}(X)\), where \(X^+\) is the Moore–Penrose pseudoinverse. Its trace is \(r\), so \(df_{\mathrm{eff}}(0)=1+r\).

The zero-penalty result also follows by taking the limit in the singular-value formula: each positive singular value contributes \(d_j^2/(d_j^2+0)=1\). Singular directions with zero singular value are not included in the sum, since they do not belong to the column space of \(X\). Thus the zero-penalty statement remains valid when \(X\) is rank deficient.

Worked Calculations

Worked Example: Two Singular Directions at Two Penalties

Hypothetical setup. Suppose centered predictors have positive singular values \(d_1=3\) and \(d_2=1\), and there is an unpenalized intercept. The design has rank \(r=2\). Calculate effective degrees of freedom at \(\lambda=1\) and \(\lambda=4\).

For \(\lambda=1\), the singular-value formula gives

$$ df_{\mathrm{eff}}(1) =1+\frac{3^2}{3^2+1}+\frac{1^2}{1^2+1} =1+\frac{9}{10}+\frac{1}{2} =2.400. $$

At \(\lambda=4\), the calculation is

$$ df_{\mathrm{eff}}(4) =1+\frac{9}{9+4}+\frac{1}{1+4} =1+\frac{9}{13}+\frac{1}{5} \approx 1.892. $$

The penalty increase reduces the predictor contribution from \(1.400\) to about \(0.8923\), while the intercept continues to contribute one. The result is not a count of retained predictors: it is the trace-based effective flexibility of the ridge fit.

Worked Example: A Rank-Deficient Design at Zero and Positive Penalty

Hypothetical setup. Let \(n=4\), let \(z=(-1,0,1,0)^\mathsf{T}\), and suppose the two centered predictor columns are identical: \(X=[z,z]\). Since \(z^\mathsf{T}z=2\), the matrix \(X^\mathsf{T}X\) is

$$ X^\mathsf{T}X= \begin{pmatrix}2&2\\2&2\end{pmatrix}. $$

This matrix has rank one, with its single positive eigenvalue equal to \(4\). Therefore \(X\) has rank \(r=1\) and one positive singular value with \(d_1^2=4\). At \(\lambda=2\),

$$ df_{\mathrm{eff}}(2) =1+\frac{4}{4+2} =1+\frac{2}{3} \approx 1.667. $$

At \(\lambda=0\), \(X^\mathsf{T}X\) is singular, so an ordinary inverse cannot be used. Instead the predictor smoother is the projection onto the one-dimensional space spanned by \(z\); its trace is one. Including the intercept gives \(df_{\mathrm{eff}}(0)=1+1=2\). The duplicate columns do not create a second independent predictor direction.

Worked Example: Strong and Weak Singular Directions

Hypothetical setup. Consider a centered design with three positive squared singular values \(16\), \(4\), and \(1\), plus an unpenalized intercept. Compare \(\lambda=3\) with \(\lambda=12\).

At \(\lambda=3\), the directions contribute \(16/19\), \(4/7\), and \(1/4\), respectively:

$$ df_{\mathrm{eff}}(3) =1+\frac{16}{19}+\frac{4}{7}+\frac{1}{4} \approx 2.664. $$

At \(\lambda=12\), their contributions are \(16/28\), \(4/16\), and \(1/13\):

$$ df_{\mathrm{eff}}(12) =1+\frac{16}{28}+\frac{4}{16}+\frac{1}{13} \approx 1.898. $$

The direction with squared singular value \(1\) contributes only \(0.07692\) at \(\lambda=12\), while the direction with squared singular value \(16\) contributes \(0.5714\). For the same penalty, directions with larger squared singular values retain a larger share of their unpenalized contribution.

How the Penalty Changes Effective Degrees of Freedom

Each predictor direction contributes the fraction \(d_j^2/(d_j^2+\lambda)\). For \(\lambda\geq 0\), this contribution is one at zero penalty, lies strictly between zero and one for positive penalty, and approaches zero as the penalty grows without bound. The intercept remains unpenalized under the model defined here. For \(\lambda>0\), differentiating the sum makes the direction of change explicit:

$$ \frac{d}{d\lambda}df_{\mathrm{eff}}(\lambda) =-\sum_{j=1}^{r}\frac{d_j^2}{(d_j^2+\lambda)^2}<0 \qquad\text{if }r>0. $$

Thus increasing \(\lambda\) strictly decreases effective degrees of freedom when the predictor design has positive rank. At zero penalty, the value is \(1+r\); as \(\lambda\) tends to infinity, it approaches one, the intercept contribution. If there are no predictor directions, \(r=0\), it is one for every penalty. Without an intercept, the corresponding predictor-only formula omits the leading one.

Effective degrees of freedom can be fractional because ridge shrinks directions rather than simply retaining or discarding them. The trace summarizes the total influence of the response on its fitted values. It is useful for describing model flexibility, but it is not the number of nonzero slope coefficients and should not be interpreted as such. For a fixed design and penalty, it depends on the design’s singular values; changing predictor scaling or the penalty convention changes the numerical setup and must be accounted for when comparing values.

Common Mistake

A common error is to substitute \(\lambda=0\) into \((X^\mathsf{T}X+\lambda I_p)^{-1}\) even when the design is rank deficient. In that case \(X^\mathsf{T}X\) is singular and the inverse does not exist. The correction is to use the projection onto \(\operatorname{col}(X)\), equivalently \(XX^+\), at zero penalty; its trace is \(r\). Another related error is to count every predictor column as a separate direction. When columns are dependent, only the rank \(r\) counts at zero penalty, and only the positive singular values appear in the ridge formula.

Key takeaway: For centered predictors and an unpenalized intercept, ridge effective degrees of freedom equal one plus the sum of \(d_j^2/(d_j^2+\lambda)\) over the positive singular values. At zero penalty, use the least-squares projection, whose predictor trace is the design rank.

Check Your Understanding

Use the stated convention of centered predictors and an unpenalized intercept.

  1. A design has rank three. What is its effective degrees of freedom at \(\lambda=0\), including the intercept?
  2. For a positive singular value \(d\), explain why its ridge contribution is between zero and one when \(\lambda>0\).
  3. A design has positive squared singular values \(9\) and \(1\). Calculate effective degrees of freedom at \(\lambda=2\).
  4. What matrix should define the predictor smoother at \(\lambda=0\) when \(X\) is rank deficient, and what is the trace of that smoother?
  5. As \(\lambda\) tends to infinity, what value does effective degrees of freedom approach in this intercept model, and why?