Rank-Aware OLS Solver Dispatch

Quick Overview

Implement an OLS fit with an intercept in NumPy that checks the design matrix rank first and then picks the normal equations or the pseudo-inverse. It tests rank deficiency, minimum-norm solutions, and how numerical tolerance affects a full-rank check on nearly collinear data.

Rank-Aware OLS Solver Dispatch

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def robust_ols(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, str]` fitting OLS with an intercept. `X` is float64 `(n, p)` without a ones column, `y` is float64 `(n,)`. Prepend a ones column to form `Xd` of shape `(n, p+1)` and choose the solver from `np.linalg.matrix_rank(Xd)`. The function should: - If `rank == p+1`: compute `beta = np.linalg.solve(Xd.T @ Xd, Xd.T @ y)` and report method `"solve"`. - Otherwise: compute `beta = np.linalg.pinv(Xd) @ y` and report method `"pinv"`. - Decide the branch from the rank up front. Do not catch `LinAlgError` from a failing `inv()`. - Return `(beta, method)`: `beta` is an ndarray of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`, and `method` is exactly `"solve"` or `"pinv"` (lowercase). Example: ```python X = np.array([[1.0], [2.0], [3.0]]) y = np.array([2.0, 4.0, 6.0]) beta, method = robust_ols(X, y) # Xd rank == 2 (full) -> method == "solve" # beta ~= [0.0, 2.0] # [intercept, slope] ``` ```hint Which matrix to test Run the rank test on `Xd` itself. Think about what happens to small singular values if you test `Xd.T @ Xd` instead. ``` ```hint Count the columns The intercept column counts toward the rank. Also consider what the rank can be when `Xd` has fewer rows than columns. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n, p)` and contains no intercept column. - `y` is float64 with shape `(n,)`. - `Xd` may be rank-deficient, for example when two features are identical or a feature is constant. - `method` must be one of the two lowercase strings `"solve"` and `"pinv"`. ### What a Strong Answer Covers - Builds `Xd` with the ones column first and compares the rank of `Xd` against `p+1`, computed once before any solver runs. - Uses `np.linalg.solve` on the normal equations in the full-rank branch and `np.linalg.pinv` on `Xd` in the other, with no exception handling around either. - Returns `beta` in `[intercept, b1, ..., bp]` order with the exact method string. - Explains why the pseudo-inverse gives the minimum-norm solution when many least-squares solutions exist. - Recognizes that `matrix_rank` uses a tolerance, so a full-rank verdict does not guarantee that the normal equations are well conditioned. ### Follow-up Questions - Why prefer `np.linalg.solve` over forming `inv(Xd.T @ Xd)` explicitly? - When rank is deficient, why does `pinv` give the minimum-norm solution? - How would you make the full-rank test reliable when columns are nearly collinear?

Overview: Implement an OLS fit with an intercept in NumPy that checks the design matrix rank first and then picks the normal equations or the pseudo-inverse. It tests rank deficiency, minimum-norm solutions, and how numerical tolerance affects a full-rank check on nearly collinear data.

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

Implement def robust_ols(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, str] fitting OLS with an intercept. X is float64 (n, p) without a ones column, y is float64 (n,). Prepend a ones column to form Xd of shape (n, p+1) and choose the solver from np.linalg.matrix_rank(Xd).

The function should:

  • If rank == p+1 : compute beta = np.linalg.solve(Xd.T @ Xd, Xd.T @ y) and report method "solve" .
  • Otherwise: compute beta = np.linalg.pinv(Xd) @ y and report method "pinv" .
  • Decide the branch from the rank up front. Do not catch LinAlgError from a failing inv() .
  • Return (beta, method) : beta is an ndarray of shape (p+1,) ordered [intercept, b1, ..., bp] , and method is exactly "solve" or "pinv" (lowercase).

Example:

X = np.array([[1.0], [2.0], [3.0]])
y = np.array([2.0, 4.0, 6.0])
beta, method = robust_ols(X, y)
# Xd rank == 2 (full) -> method == "solve"
# beta ~= [0.0, 2.0]   # [intercept, slope]

Constraints & Assumptions

  • X is float64 with shape (n, p) and contains no intercept column.
  • y is float64 with shape (n,) .
  • Xd may be rank-deficient, for example when two features are identical or a feature is constant.
  • method must be one of the two lowercase strings "solve" and "pinv" .

What a Strong Answer Covers Guidance

  • Builds Xd with the ones column first and compares the rank of Xd against p+1 , computed once before any solver runs.
  • Uses np.linalg.solve on the normal equations in the full-rank branch and np.linalg.pinv on Xd in the other, with no exception handling around either.
  • Returns beta in [intercept, b1, ..., bp] order with the exact method string.
  • Explains why the pseudo-inverse gives the minimum-norm solution when many least-squares solutions exist.
  • Recognizes that matrix_rank uses a tolerance, so a full-rank verdict does not guarantee that the normal equations are well conditioned.

Follow-up Questions Guidance

  • Why prefer np.linalg.solve over forming inv(Xd.T @ Xd) explicitly?
  • When rank is deficient, why does pinv give the minimum-norm solution?
  • How would you make the full-rank test reliable when columns are nearly collinear?