Compute the minimum-norm OLS solution and rank for a rank-deficient design matrix

Quick Overview

Fit ordinary least squares with an intercept on a design matrix that has an exactly collinear column, and return the minimum-norm coefficient vector and the rank. It tests the pseudoinverse and SVD view of regression, numerical rank, and why the normal equations cannot be inverted here.

Compute the minimum-norm OLS solution and rank for a rank-deficient design matrix

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Fit OLS with an intercept on a rank-deficient design. `X` is `float64` of shape `(n, p)` (no ones column) with an exactly collinear column, so `Xd = [ones, X]` has rank below `p + 1` and `Xd.T @ Xd` is singular. Return the minimum-norm least-squares solution and the rank. ```python import numpy as np def min_norm_coefficients(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, int]: ... ``` The function must: - Return a 2-tuple `(beta, rank)`. - Return `beta` as an ndarray of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`, the least-squares minimizer of smallest L2 norm. It should match `np.linalg.pinv(Xd) @ y` and `np.linalg.lstsq(Xd, y, rcond=None)[0]`. - Return `rank` as a native Python `int` (not a NumPy scalar), equal to `np.linalg.matrix_rank(Xd)`. - Avoid calling `np.linalg.inv` on the normal-equations matrix. The matrix is singular, so the call either raises `LinAlgError` or returns meaningless, enormous values. ```hint Where the freedom is Any two least-squares minimizers differ by a vector in the null space of `Xd`. Think about which matrix decomposition separates that null space from the directions that affect the fit. ``` ```hint Rank in floating point An exactly collinear column rarely produces a singular value that is exactly zero after rounding. Decide how you count the rank so it agrees with the cutoff your solver uses. ``` ### Constraints & Assumptions - `X` is `float64` with shape `(n, p)`, has no intercept column, and contains at least one column that is an exact linear combination of the other columns of `Xd` (including the ones column). - `y` has shape `(n,)`. - NumPy is available. ### What a Strong Answer Covers - Explains why the normal equations have infinitely many solutions here and why inverting `Xd.T @ Xd` is not an option. - Uses the pseudoinverse, an SVD, or `lstsq` to return the minimum-norm minimizer in the required order and shape. - Computes the rank with a tolerance and returns it as a Python `int`. - Explains why the chosen solution has the smallest norm and which quantities (fitted values, residuals) are unique even though `beta` is not. ### Follow-up Questions - Why does the pseudoinverse pick the minimum-norm solution? - How would ridge regression change the situation? - How do you choose the tolerance for the numerical rank? - Does the minimum-norm coefficient vector change if you rescale one of the collinear columns?

Overview: Fit ordinary least squares with an intercept on a design matrix that has an exactly collinear column, and return the minimum-norm coefficient vector and the rank. It tests the pseudoinverse and SVD view of regression, numerical rank, and why the normal equations cannot be inverted here.

General logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Fit OLS with an intercept on a rank-deficient design. X is float64 of shape (n, p) (no ones column) with an exactly collinear column, so Xd = [ones, X] has rank below p + 1 and Xd.T @ Xd is singular. Return the minimum-norm least-squares solution and the rank.

import numpy as np


def min_norm_coefficients(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, int]:
    ...

The function must:

  • Return a 2-tuple (beta, rank) .
  • Return beta as an ndarray of shape (p+1,) ordered [intercept, b1, ..., bp] , the least-squares minimizer of smallest L2 norm. It should match np.linalg.pinv(Xd) @ y and np.linalg.lstsq(Xd, y, rcond=None)[0] .
  • Return rank as a native Python int (not a NumPy scalar), equal to np.linalg.matrix_rank(Xd) .
  • Avoid calling np.linalg.inv on the normal-equations matrix. The matrix is singular, so the call either raises LinAlgError or returns meaningless, enormous values.

Constraints & Assumptions

  • X is float64 with shape (n, p) , has no intercept column, and contains at least one column that is an exact linear combination of the other columns of Xd (including the ones column).
  • y has shape (n,) .
  • NumPy is available.

What a Strong Answer Covers Guidance

  • Explains why the normal equations have infinitely many solutions here and why inverting Xd.T @ Xd is not an option.
  • Uses the pseudoinverse, an SVD, or lstsq to return the minimum-norm minimizer in the required order and shape.
  • Computes the rank with a tolerance and returns it as a Python int .
  • Explains why the chosen solution has the smallest norm and which quantities (fitted values, residuals) are unique even though beta is not.

Follow-up Questions Guidance

  • Why does the pseudoinverse pick the minimum-norm solution?
  • How would ridge regression change the situation?
  • How do you choose the tolerance for the numerical rank?
  • Does the minimum-norm coefficient vector change if you rescale one of the collinear columns?