Closed-Form Ridge Regression with Unpenalized Intercept

Quick Overview

Implement closed-form ridge regression with an unpenalized intercept in NumPy, plus the effective degrees of freedom of the fit. Tests how centring handles the intercept, numerically stable solving for ill-conditioned or rank-deficient features, and the minimum-norm solution at zero penalty.

Closed-Form Ridge Regression with Unpenalized Intercept

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement `ridge_fit(X, y, alpha, rcond=1e-12)` returning `(coef, intercept)` that minimises $\lVert y - Xw - b\mathbf{1} \rVert^2 + \alpha \lVert w \rVert^2$. The intercept `b` is unpenalized, which matches scikit-learn's `Ridge`. Also implement `effective_degrees_of_freedom(X, alpha, rcond=1e-12)`, the trace of the ridge hat matrix on column-centred `X`. - Centre `X` and `y`, then set `intercept = mean(y) - mean(X) @ coef`. - Treat singular values of the centred `X` with `s <= rcond * s.max()` as zero. At `alpha = 0`, return the minimum-norm least-squares solution, which is finite even when `X` is rank-deficient. - The fit must stay accurate for ill-conditioned `X`. Do not explicitly invert $X^\top X$. - `alpha < 0` raises `ValueError` in both functions. - Return `coef` as an array of shape `(p,)`, and the intercept and the degrees of freedom as Python `float` values. Example: ```python X = [[0.0], [1.0], [2.0]] y = [1.0, 3.0, 5.0] ridge_fit(X, y, 2.0) # (array([1.0]), 2.0) effective_degrees_of_freedom(X, 2.0) # 0.5 X = [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]] # duplicated column ridge_fit(X, y, 2.0) # (array([2/3, 2/3]), 5/3) ``` ```hint Get rid of the intercept first Set the derivative of the objective with respect to `b` to zero and see what the remaining problem looks like on centred data. ``` ```hint Avoid the normal equations A decomposition of the centred `X` itself never forms $X^\top X$, and it lets you decide direction by direction which parts of the matrix are numerically zero. ``` ```hint The rank-deficient case With a duplicated column and `alpha = 0`, infinitely many coefficient vectors fit equally well. Work out which one has the smallest norm and how your decomposition produces it. ``` ### Constraints & Assumptions - `X` has shape `(n, p)`, `y` has length `n`, and all values are finite. - `X` may have duplicated, collinear or nearly collinear columns. - At `alpha = 0`, the degrees of freedom equal the numerical rank of the centred `X`. - Use NumPy only. ### What a Strong Answer Covers - Why centring removes the unpenalized intercept from the penalized problem, and how the intercept is recovered afterwards. - A numerically stable solver that works on the centred `X` directly, with the `rcond` truncation rule applied as specified. - The `alpha = 0` rank-deficient case: the minimum-norm solution, and degrees of freedom equal to the numerical rank. - The effective degrees of freedom formula, the `ValueError` checks, and the required return types. - Time and space complexity of the decomposition. ### Follow-up Questions - How would you efficiently fit a whole path of `alpha` values? - How would you choose `alpha` without refitting n times for leave-one-out cross-validation? - Why not just solve $(X^\top X + \alpha I) w = X^\top y$ directly?

Overview: Implement closed-form ridge regression with an unpenalized intercept in NumPy, plus the effective degrees of freedom of the fit. Tests how centring handles the intercept, numerically stable solving for ill-conditioned or rank-deficient features, and the minimum-norm solution at zero penalty.

PracHub logo
General
Nov 25, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Implement ridge_fit(X, y, alpha, rcond=1e-12) returning (coef, intercept) that minimises ∥y−Xw−b1∥2+α∥w∥2\lVert y - Xw - b\mathbf{1} \rVert^2 + \alpha \lVert w \rVert^2. The intercept b is unpenalized, which matches scikit-learn's Ridge. Also implement effective_degrees_of_freedom(X, alpha, rcond=1e-12), the trace of the ridge hat matrix on column-centred X.

  • Centre X and y , then set intercept = mean(y) - mean(X) @ coef .
  • Treat singular values of the centred X with s <= rcond * s.max() as zero. At alpha = 0 , return the minimum-norm least-squares solution, which is finite even when X is rank-deficient.
  • The fit must stay accurate for ill-conditioned X . Do not explicitly invert X⊤XX^\top X .
  • alpha < 0 raises ValueError in both functions.
  • Return coef as an array of shape (p,) , and the intercept and the degrees of freedom as Python float values.

Example:

X = [[0.0], [1.0], [2.0]]
y = [1.0, 3.0, 5.0]
ridge_fit(X, y, 2.0)                  # (array([1.0]), 2.0)
effective_degrees_of_freedom(X, 2.0)  # 0.5

X = [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]]  # duplicated column
ridge_fit(X, y, 2.0)                  # (array([2/3, 2/3]), 5/3)

Constraints & Assumptions

  • X has shape (n, p) , y has length n , and all values are finite.
  • X may have duplicated, collinear or nearly collinear columns.
  • At alpha = 0 , the degrees of freedom equal the numerical rank of the centred X .
  • Use NumPy only.

What a Strong Answer Covers Guidance

  • Why centring removes the unpenalized intercept from the penalized problem, and how the intercept is recovered afterwards.
  • A numerically stable solver that works on the centred X directly, with the rcond truncation rule applied as specified.
  • The alpha = 0 rank-deficient case: the minimum-norm solution, and degrees of freedom equal to the numerical rank.
  • The effective degrees of freedom formula, the ValueError checks, and the required return types.
  • Time and space complexity of the decomposition.

Follow-up Questions Guidance

  • How would you efficiently fit a whole path of alpha values?
  • How would you choose alpha without refitting n times for leave-one-out cross-validation?
  • Why not just solve (X⊤X+αI)w=X⊤y(X^\top X + \alpha I) w = X^\top y directly?