Fit OLS with an intercept by Cholesky-solving the normal equations in NumPy

Quick Overview

Implement ordinary least squares with an intercept in NumPy by forming the normal equations and solving them with a Cholesky factorization and two triangular solves. It tests linear algebra fluency, cost analysis, and what fails on ill-conditioned or rank-deficient designs.

Fit OLS with an intercept by Cholesky-solving the normal equations in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def cholesky_ols(X: np.ndarray, y: np.ndarray) -> np.ndarray` that fits ordinary least squares (OLS) with an intercept by Cholesky-solving the normal equations. `X` is float64 with shape `(n, p)` and has no intercept column, and `y` is float64 with shape `(n,)`. The design matrix `Xd = [ones, X]` has full column rank, so `Xd.T @ Xd` is symmetric positive-definite. The function should: - Prepend the ones column, then form `G = Xd.T @ Xd` and `b = Xd.T @ y`. - Factor `L = np.linalg.cholesky(G)` (lower-triangular, `G = L @ L.T`). - Solve `L @ z = b` (`lower=True`), then `L.T @ beta = z` (`lower=False`), using `scipy.linalg.solve_triangular` for both. - Return an `np.ndarray` of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. For example: ```python X = np.array([[1.0], [2.0], [3.0]]) y = np.array([2.0, 4.0, 6.0]) cholesky_ols(X, y) # -> array([0.0, 2.0]) up to floating-point rounding, as [intercept, slope] ``` ```hint Which factor goes where `np.linalg.cholesky` returns the lower factor. Work out which of `L` and `L.T` belongs in each of the two solves and which `lower` flag each call needs. ``` ```hint Test against a reference A noiseless line should come back exactly up to rounding. On random data, compare your result with `np.linalg.lstsq` using `np.allclose`. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n, p)` and no intercept column; `y` is float64 with shape `(n,)`. - `Xd = [ones, X]` has full column rank, so `G` is symmetric positive-definite and its Cholesky factor exists. No rank-deficiency handling is required. - NumPy and SciPy are available. ### What a Strong Answer Covers - A design matrix with the intercept column first, and `G` and `b` formed without any explicit matrix inverse. - The forward solve on `L` and the back solve on `L.T`, with the matching `lower` flags. - Time and memory cost in terms of `n` and `p`. - Why full column rank makes `G` positive-definite, and what the factorization does when that assumption fails. - The conditioning cost of forming `G`. ### Follow-up Questions - Why use `scipy.linalg.solve_triangular` instead of `np.linalg.solve` for the two solves? - What is the numerical downside of the normal-equations route? - How would you handle a rank-deficient `Xd`?

Overview: Implement ordinary least squares with an intercept in NumPy by forming the normal equations and solving them with a Cholesky factorization and two triangular solves. It tests linear algebra fluency, cost analysis, and what fails on ill-conditioned or rank-deficient designs.

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

Implement def cholesky_ols(X: np.ndarray, y: np.ndarray) -> np.ndarray that fits ordinary least squares (OLS) with an intercept by Cholesky-solving the normal equations. X is float64 with shape (n, p) and has no intercept column, and y is float64 with shape (n,). The design matrix Xd = [ones, X] has full column rank, so Xd.T @ Xd is symmetric positive-definite.

The function should:

  • Prepend the ones column, then form G = Xd.T @ Xd and b = Xd.T @ y .
  • Factor L = np.linalg.cholesky(G) (lower-triangular, G = L @ L.T ).
  • Solve L @ z = b ( lower=True ), then L.T @ beta = z ( lower=False ), using scipy.linalg.solve_triangular for both.
  • Return an np.ndarray of shape (p+1,) ordered [intercept, b1, ..., bp] .

For example:

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

Constraints & Assumptions

  • X is float64 with shape (n, p) and no intercept column; y is float64 with shape (n,) .
  • Xd = [ones, X] has full column rank, so G is symmetric positive-definite and its Cholesky factor exists. No rank-deficiency handling is required.
  • NumPy and SciPy are available.

What a Strong Answer Covers Guidance

  • A design matrix with the intercept column first, and G and b formed without any explicit matrix inverse.
  • The forward solve on L and the back solve on L.T , with the matching lower flags.
  • Time and memory cost in terms of n and p .
  • Why full column rank makes G positive-definite, and what the factorization does when that assumption fails.
  • The conditioning cost of forming G .

Follow-up Questions Guidance

  • Why use scipy.linalg.solve_triangular instead of np.linalg.solve for the two solves?
  • What is the numerical downside of the normal-equations route?
  • How would you handle a rank-deficient Xd ?