Ridge Effective Degrees of Freedom via SVD

Quick Overview

Compute the effective degrees of freedom of ridge regression for a scalar penalty or a whole grid from the singular values of the design matrix, without inverting any matrix. It tests the SVD form of the ridge hat matrix, numerical rank thresholds, and careful input validation in NumPy.

Ridge Effective Degrees of Freedom via SVD

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `ridge_effective_df(X, lam)` returning $\text{df}(\lambda) = \sum_j d_j^2 / (d_j^2 + \lambda)$, where $d_j$ are the singular values of the 2-D matrix `X` (used as given: no centring, no intercept). Do not invert any matrix. Example: ```python X = np.array([[3., 0.], [0., 4.], [0., 0.]]) ridge_effective_df(X, 1.0) # 9/10 + 16/17 ≈ 1.8412 (float) ridge_effective_df(X, [0.0, 1.0]) # array([2.0, 1.8412...]) ``` ```hint Hat matrix in the SVD basis Substitute $X = U D V^\top$ into the ridge hat matrix $X (X^\top X + \lambda I)^{-1} X^\top$ and look at its eigenvalues. ``` ### Constraints & Assumptions - A scalar `lam` returns a Python `float`. A 1-D `lam` returns an `np.ndarray` of the same length. - Singular values at or below `max(n, p) * eps * max(d)` count as zero and contribute 0, so df(0) equals the numerical rank of `X`. - A negative or non-finite `lam`, a non-2-D `X`, or a `lam` with more than one dimension raises `ValueError`. - `X` may be tall, wide, or rank-deficient. Results must match the trace of the hat matrix to `rtol=1e-7`. ### What a Strong Answer Covers - Why the trace of the ridge hat matrix depends only on the singular values of `X`. - A computation that needs no matrix inverse and no singular vectors. - The round-off threshold and what it does to df(0) for a rank-deficient `X`. - Input validation and return types for scalar and 1-D `lam`, with a whole grid handled in one vectorized pass. - Time and space cost compared with forming the hat matrix. ### Follow-up Questions - Given a target df, how would you find the matching $\lambda$? - How does centring `X` and fitting an unpenalised intercept change the answer? - Why use the SVD instead of computing `trace(X (XᵀX + λI)⁻¹ Xᵀ)` directly?

Overview: Compute the effective degrees of freedom of ridge regression for a scalar penalty or a whole grid from the singular values of the design matrix, without inverting any matrix. It tests the SVD form of the ridge hat matrix, numerical rank thresholds, and careful input validation in NumPy.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement ridge_effective_df(X, lam) returning df(λ)=∑jdj2/(dj2+λ)\text{df}(\lambda) = \sum_j d_j^2 / (d_j^2 + \lambda), where djd_j are the singular values of the 2-D matrix X (used as given: no centring, no intercept). Do not invert any matrix.

Example:

X = np.array([[3., 0.], [0., 4.], [0., 0.]])
ridge_effective_df(X, 1.0)  # 9/10 + 16/17 ≈ 1.8412 (float)
ridge_effective_df(X, [0.0, 1.0])  # array([2.0, 1.8412...])

Constraints & Assumptions

  • A scalar lam returns a Python float . A 1-D lam returns an np.ndarray of the same length.
  • Singular values at or below max(n, p) * eps * max(d) count as zero and contribute 0, so df(0) equals the numerical rank of X .
  • A negative or non-finite lam , a non-2-D X , or a lam with more than one dimension raises ValueError .
  • X may be tall, wide, or rank-deficient. Results must match the trace of the hat matrix to rtol=1e-7 .

What a Strong Answer Covers Guidance

  • Why the trace of the ridge hat matrix depends only on the singular values of X .
  • A computation that needs no matrix inverse and no singular vectors.
  • The round-off threshold and what it does to df(0) for a rank-deficient X .
  • Input validation and return types for scalar and 1-D lam , with a whole grid handled in one vectorized pass.
  • Time and space cost compared with forming the hat matrix.

Follow-up Questions Guidance

  • Given a target df, how would you find the matching λ\lambda ?
  • How does centring X and fitting an unpenalised intercept change the answer?
  • Why use the SVD instead of computing trace(X (XᵀX + λI)⁻¹ Xᵀ) directly?