OLS Fitted Values via Normal Equation

Quick Overview

Implement ordinary least squares with an intercept in NumPy and return the in-sample fitted values by solving the normal equation. It tests building a design matrix, solving a linear system without an explicit inverse, and knowing where the normal equation loses numerical precision.

OLS Fitted Values via Normal Equation

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fitted_values(X: np.ndarray, y: np.ndarray) -> np.ndarray` that fits OLS with an intercept and returns in-sample fitted values. `X` is float64 `(n, p)` without a ones column, `y` is float64 `(n,)`. The function should: - Prepend a ones column to form `Xd` of shape `(n, p+1)`. - Solve `(Xdᵀ Xd) beta = Xdᵀ y` for `beta`. - Return `Xd @ beta` as an `np.ndarray` of shape `(n,)` in row order (not the coefficients, not the residuals). Example: ```python X = np.array([[1.0], [2.0], [3.0]]) y = np.array([2.0, 4.0, 6.0]) fitted_values(X, y) # -> array([2., 4., 6.]) (perfect fit, yhat == y) ``` ```hint Solving the system NumPy can solve a square linear system directly, so you never need to form a matrix inverse. Check the shape of what you return against the requirements. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n, p)` and contains no intercept column. - `y` is float64 with shape `(n,)`. - `Xd` is guaranteed full column rank, so `Xdᵀ Xd` is invertible. ### What a Strong Answer Covers - Builds `Xd` with the ones column first so the intercept is estimated and the shapes line up. - Solves the normal equation with a linear solver instead of an explicit inverse, and says why. - Returns `Xd @ beta` as a 1-D array of length `n` in row order. - States the time and memory cost in terms of `n` and `p`. - Recognizes that the normal equation loses precision on ill-conditioned designs. ### Follow-up Questions - Why prefer `np.linalg.lstsq` or a QR solve over the normal equation? - How would you also return residuals or `R²` from the same fit? - What happens if `Xd` is not full column rank, and how would you change the function to handle it?

Overview: Implement ordinary least squares with an intercept in NumPy and return the in-sample fitted values by solving the normal equation. It tests building a design matrix, solving a linear system without an explicit inverse, and knowing where the normal equation loses numerical precision.

PracHub logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def fitted_values(X: np.ndarray, y: np.ndarray) -> np.ndarray that fits OLS with an intercept and returns in-sample fitted values. X is float64 (n, p) without a ones column, y is float64 (n,).

The function should:

  • Prepend a ones column to form Xd of shape (n, p+1) .
  • Solve (Xdᵀ Xd) beta = Xdᵀ y for beta .
  • Return Xd @ beta as an np.ndarray of shape (n,) in row order (not the coefficients, not the residuals).

Example:

X = np.array([[1.0], [2.0], [3.0]])
y = np.array([2.0, 4.0, 6.0])
fitted_values(X, y)  # -> array([2., 4., 6.])  (perfect fit, yhat == y)

Constraints & Assumptions

  • X is float64 with shape (n, p) and contains no intercept column.
  • y is float64 with shape (n,) .
  • Xd is guaranteed full column rank, so Xdᵀ Xd is invertible.

What a Strong Answer Covers Guidance

  • Builds Xd with the ones column first so the intercept is estimated and the shapes line up.
  • Solves the normal equation with a linear solver instead of an explicit inverse, and says why.
  • Returns Xd @ beta as a 1-D array of length n in row order.
  • States the time and memory cost in terms of n and p .
  • Recognizes that the normal equation loses precision on ill-conditioned designs.

Follow-up Questions Guidance

  • Why prefer np.linalg.lstsq or a QR solve over the normal equation?
  • How would you also return residuals or R² from the same fit?
  • What happens if Xd is not full column rank, and how would you change the function to handle it?