Fit OLS with an intercept from a QR factorization without forming the normal equations

Quick Overview

Implement ordinary least squares with an intercept in NumPy from the reduced QR factorization of the design matrix and one triangular back substitution. It tests the derivation from the normal equations, numerical stability, and how to handle rank-deficient inputs.

Fit OLS with an intercept from a QR factorization without forming the normal equations

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def qr_ols(X: np.ndarray, y: np.ndarray) -> np.ndarray` that fits ordinary least squares (OLS) with an intercept via QR factorization. `X` is float64 of shape `(n, p)` without a ones column, and `y` is float64 of shape `(n,)`. The function should: - Prepend a ones column to build `Xd` of shape `(n, p+1)`. - Take the reduced QR: `Q, R = np.linalg.qr(Xd)` (`Q` is `(n, p+1)`, upper-triangular `R` is `(p+1, p+1)`). - Solve `R @ beta = Q.T @ y`, for example with `scipy.linalg.solve_triangular(R, Q.T @ y, lower=False)`. - Never form `X^T X` or call a normal-equations solver. - Return an `np.ndarray` of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. For example: ```python X = np.array([[0.0], [1.0], [2.0], [3.0]]) y = np.array([1.0, 3.0, 2.0, 5.0]) qr_ols(X, y) # -> array([1.1, 1.1]) up to floating-point rounding, as [intercept, slope] ``` ```hint Where the normal equations go Substitute `Xd = Q @ R` into the least-squares condition and use what `Q.T @ Q` equals for the reduced factorization. ``` ```hint Mind the QR mode The shapes of `Q` and `R` depend on the `mode` argument of `np.linalg.qr`. Check that `R` is square before passing it to a triangular solver. ``` ### Constraints & Assumptions - `n > p+1` and `Xd` has full column rank (no rank-deficiency handling needed). - `numpy` and `scipy` are available. ### What a Strong Answer Covers - A design matrix with the intercept column first, and the reduced QR with a square `R`. - The derivation of `R @ beta = Q.T @ y` from the least-squares problem, including why `R` is invertible. - A triangular back substitution with no explicit inverse and no `Xd.T @ Xd`. - The cost of the method and how it compares with the normal equations. - The stability argument in terms of the condition number of `Xd`. ### Follow-up Questions - Why is QR more stable than solving the normal equations? - How would you handle a rank-deficient or nearly singular `X`? - `np.linalg.qr` can return `R` with negative diagonal entries. Does that change `beta`?

Overview: Implement ordinary least squares with an intercept in NumPy from the reduced QR factorization of the design matrix and one triangular back substitution. It tests the derivation from the normal equations, numerical stability, and how to handle rank-deficient inputs.

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

Implement def qr_ols(X: np.ndarray, y: np.ndarray) -> np.ndarray that fits ordinary least squares (OLS) with an intercept via QR factorization. X is float64 of shape (n, p) without a ones column, and y is float64 of shape (n,).

The function should:

  • Prepend a ones column to build Xd of shape (n, p+1) .
  • Take the reduced QR: Q, R = np.linalg.qr(Xd) ( Q is (n, p+1) , upper-triangular R is (p+1, p+1) ).
  • Solve R @ beta = Q.T @ y , for example with scipy.linalg.solve_triangular(R, Q.T @ y, lower=False) .
  • Never form X^T X or call a normal-equations solver.
  • Return an np.ndarray of shape (p+1,) ordered [intercept, b1, ..., bp] .

For example:

X = np.array([[0.0], [1.0], [2.0], [3.0]])
y = np.array([1.0, 3.0, 2.0, 5.0])
qr_ols(X, y)  # -> array([1.1, 1.1]) up to floating-point rounding, as [intercept, slope]

Constraints & Assumptions

  • n > p+1 and Xd has full column rank (no rank-deficiency handling needed).
  • numpy and scipy are available.

What a Strong Answer Covers Guidance

  • A design matrix with the intercept column first, and the reduced QR with a square R .
  • The derivation of R @ beta = Q.T @ y from the least-squares problem, including why R is invertible.
  • A triangular back substitution with no explicit inverse and no Xd.T @ Xd .
  • The cost of the method and how it compares with the normal equations.
  • The stability argument in terms of the condition number of Xd .

Follow-up Questions Guidance

  • Why is QR more stable than solving the normal equations?
  • How would you handle a rank-deficient or nearly singular X ?
  • np.linalg.qr can return R with negative diagonal entries. Does that change beta ?