Ridge Coefficient Path via a Single SVD

Quick Overview

Compute the full ridge regression coefficient path, intercepts and effective degrees of freedom for a grid of penalties from one SVD of the centred design matrix in NumPy. It tests the SVD form of ridge, an unpenalised intercept, and correct behavior on rank-deficient data with duplicated columns.

Ridge Coefficient Path via a Single SVD

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `ridge_svd_path(X, y, lambdas)` minimising $\lVert y - b_0 - Xb \rVert_2^2 + \lambda \lVert b \rVert_2^2$ (intercept unpenalised, no 1/n scaling) for every $\lambda$ in the grid. Compute one SVD of the column-centred `X` and apply the shrinkage factors $d^2/(d^2+\lambda)$. Return `(coefs, intercepts, dof)` with shapes `(n_lambdas, p)`, `(n_lambdas,)` and `(n_lambdas,)`. - `intercept = mean(y) - coefs @ mean(X, axis=0)` - `dof` is the effective degrees of freedom, $\sum_i d_i^2/(d_i^2+\lambda)$. - Rows follow the input order of `lambdas` (no sorting). - Singular values at or below `1e-12 * d.max()` are dropped, so $\lambda = 0$ on a rank-deficient `X` gives the finite minimum-norm solution and a `dof` equal to the rank. Plotting the coefficient paths (coefs against $\log \lambda$) is expected but not graded. Example: ```text Input: X = [[1.0], [2.0], [3.0]], y = [1.0, 2.0, 4.0], lambdas = [1.0, 0.0, 4.0] Output: coefs -> [[1.0], [1.5], [0.5]] intercepts -> [1/3, -2/3, 4/3] dof -> [2/3, 1.0, 1/3] ``` The centred column is `[-1, 0, 1]`, so $d^2 = 2$ and the centred $X^\top y$ is 3, which makes each coefficient ${3/(2+\lambda)}$ and each dof ${2/(2+\lambda)}$. The intercept is ${7/3}$ minus twice the coefficient. ```hint Where the SVD enters Write the ridge normal equations for the centred data and substitute $X_c = U D V^\top$. Look at what $(X_c^\top X_c + \lambda I)^{-1} X_c^\top$ becomes in that basis. ``` ```hint Rank deficiency At $\lambda = 0$ a zero singular value would mean dividing by zero. Ask which components of the solution that singular value can affect, and what the minimum-norm solution does with them. ``` ### Constraints & Assumptions - `X` is `(n, p)`, `y` has length `n`, and all values are finite. - Every $\lambda \ge 0$, and the grid may be unsorted. - `X` may contain duplicated (collinear) columns. ### What a Strong Answer Covers - Centring so the intercept stays unpenalised, and recovering the intercept for each $\lambda$. - The ridge solution written in the SVD basis, and why one factorisation serves the whole grid. - Rank deficiency and $\lambda = 0$: the singular-value cutoff and the value of `dof` it produces. - A vectorised computation that keeps rows in input order, with its time and space cost. - What the solution does with duplicated columns. ### Follow-up Questions - How would you pick $\lambda$ using the same SVD? - What does a duplicated feature do to the ridge solution? - Why should features usually be standardised before ridge?

Overview: Compute the full ridge regression coefficient path, intercepts and effective degrees of freedom for a grid of penalties from one SVD of the centred design matrix in NumPy. It tests the SVD form of ridge, an unpenalised intercept, and correct behavior on rank-deficient data with duplicated columns.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement ridge_svd_path(X, y, lambdas) minimising ∥y−b0−Xb∥22+λ∥b∥22\lVert y - b_0 - Xb \rVert_2^2 + \lambda \lVert b \rVert_2^2 (intercept unpenalised, no 1/n scaling) for every λ\lambda in the grid. Compute one SVD of the column-centred X and apply the shrinkage factors d2/(d2+λ)d^2/(d^2+\lambda). Return (coefs, intercepts, dof) with shapes (n_lambdas, p), (n_lambdas,) and (n_lambdas,).

  • intercept = mean(y) - coefs @ mean(X, axis=0)
  • dof is the effective degrees of freedom, ∑idi2/(di2+λ)\sum_i d_i^2/(d_i^2+\lambda) .
  • Rows follow the input order of lambdas (no sorting).
  • Singular values at or below 1e-12 * d.max() are dropped, so λ=0\lambda = 0 on a rank-deficient X gives the finite minimum-norm solution and a dof equal to the rank.

Plotting the coefficient paths (coefs against log⁡λ\log \lambda) is expected but not graded.

Example:

Input:  X = [[1.0], [2.0], [3.0]], y = [1.0, 2.0, 4.0], lambdas = [1.0, 0.0, 4.0]
Output: coefs      -> [[1.0], [1.5], [0.5]]
        intercepts -> [1/3, -2/3, 4/3]
        dof        -> [2/3, 1.0, 1/3]

The centred column is [-1, 0, 1], so d2=2d^2 = 2 and the centred X⊤yX^\top y is 3, which makes each coefficient 3/(2+λ){3/(2+\lambda)} and each dof 2/(2+λ){2/(2+\lambda)}. The intercept is 7/3{7/3} minus twice the coefficient.

Constraints & Assumptions

  • X is (n, p) , y has length n , and all values are finite.
  • Every λ≥0\lambda \ge 0 , and the grid may be unsorted.
  • X may contain duplicated (collinear) columns.

What a Strong Answer Covers Guidance

  • Centring so the intercept stays unpenalised, and recovering the intercept for each λ\lambda .
  • The ridge solution written in the SVD basis, and why one factorisation serves the whole grid.
  • Rank deficiency and λ=0\lambda = 0 : the singular-value cutoff and the value of dof it produces.
  • A vectorised computation that keeps rows in input order, with its time and space cost.
  • What the solution does with duplicated columns.

Follow-up Questions Guidance

  • How would you pick λ\lambda using the same SVD?
  • What does a duplicated feature do to the ridge solution?
  • Why should features usually be standardised before ridge?