Compute OLS coefficients with an intercept using the normal equation

Quick Overview

Implement ordinary least squares in NumPy by prepending an intercept column and solving the normal equation for the coefficient vector. It tests the closed-form OLS derivation, coefficient ordering, complexity, and why solving the linear system is preferred to forming an explicit matrix inverse.

Compute OLS coefficients with an intercept using the normal equation

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement the function below, returning the OLS coefficients $\beta = (X_d^\top X_d)^{-1} X_d^\top y$, where $X_d$ is `X` with a ones column prepended. Inputs are a `float64` array `X` of shape `(n, p)` and `y` of shape `(n,)`. ```python import numpy as np def normal_equation(X: np.ndarray, y: np.ndarray) -> np.ndarray: ... ``` The function must: - Return shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. - Produce the right numbers by any equivalent route (`np.linalg.inv`, `np.linalg.solve`, `lstsq`, `pinv`). Only the numbers are graded. For example: ```python X = np.array([[1.0], [2.0], [3.0]]) y = np.array([2.0, 4.0, 6.0]) normal_equation(X, y) # -> array([0., 2.]) up to rounding: intercept 0, slope 2 ``` ```hint Order of the columns Decide where the ones column goes in the design matrix, since that fixes which entry of the result is the intercept. ``` ### Constraints & Assumptions - `X` is `float64` with shape `(n, p)`; `y` has shape `(n,)`. - `Xd` has full column rank. ### What a Strong Answer Covers - Builds the design matrix by prepending a ones column so the intercept is element 0. - Forms $X_d^\top X_d$ and $X_d^\top y$ and solves the system without needing an explicit inverse. - Returns a 1-D array of length `p + 1` and checks it against the example. - States the time and space complexity and why full column rank makes the solution unique. ### Follow-up Questions - Why prefer `np.linalg.solve` over `np.linalg.inv`? - What breaks when `Xd` is not full column rank? - Why can `lstsq` or a QR factorization on `Xd` be more accurate than solving the normal equations at all? - What would you do instead if `p` were in the tens of thousands?

Overview: Implement ordinary least squares in NumPy by prepending an intercept column and solving the normal equation for the coefficient vector. It tests the closed-form OLS derivation, coefficient ordering, complexity, and why solving the linear system is preferred to forming an explicit matrix inverse.

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

Implement the function below, returning the OLS coefficients β=(Xd⊤Xd)−1Xd⊤y\beta = (X_d^\top X_d)^{-1} X_d^\top y, where XdX_d is X with a ones column prepended. Inputs are a float64 array X of shape (n, p) and y of shape (n,).

import numpy as np


def normal_equation(X: np.ndarray, y: np.ndarray) -> np.ndarray:
    ...

The function must:

  • Return shape (p+1,) ordered [intercept, b1, ..., bp] .
  • Produce the right numbers by any equivalent route ( np.linalg.inv , np.linalg.solve , lstsq , pinv ). Only the numbers are graded.

For example:

X = np.array([[1.0], [2.0], [3.0]])
y = np.array([2.0, 4.0, 6.0])
normal_equation(X, y)  # -> array([0., 2.]) up to rounding: intercept 0, slope 2

Constraints & Assumptions

  • X is float64 with shape (n, p) ; y has shape (n,) .
  • Xd has full column rank.

What a Strong Answer Covers Guidance

  • Builds the design matrix by prepending a ones column so the intercept is element 0.
  • Forms Xd⊤XdX_d^\top X_d and Xd⊤yX_d^\top y and solves the system without needing an explicit inverse.
  • Returns a 1-D array of length p + 1 and checks it against the example.
  • States the time and space complexity and why full column rank makes the solution unique.

Follow-up Questions Guidance

  • Why prefer np.linalg.solve over np.linalg.inv ?
  • What breaks when Xd is not full column rank?
  • Why can lstsq or a QR factorization on Xd be more accurate than solving the normal equations at all?
  • What would you do instead if p were in the tens of thousands?