Compute the OLS residual vector for a regression fit with an intercept

Quick Overview

Implement a function that fits ordinary least squares with an intercept on a feature matrix that has no ones column and returns the raw residual vector in row order. It tests building the design matrix, solving least squares stably, and knowing the properties and diagnostic uses of OLS residuals.

Compute the OLS residual vector for a regression fit with an intercept

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def compute_residuals(X: np.ndarray, y: np.ndarray) -> np.ndarray`. Given a float `X` of shape `(n, k)` with no intercept column and `y` of shape `(n,)`, fit OLS with an intercept and return the residuals `e = y - y_hat` as a 1-D float array of shape `(n,)`. - Fit with an intercept even though `X` has no ones column. - Element `i` equals `y[i]` minus the fitted value for row `i`, in the original row order. - Return raw residuals. Do not standardize them, scale them or take absolute values. Any fitting method is accepted (the normal equations, `np.linalg.lstsq`, `sklearn.linear_model.LinearRegression`, `statsmodels.OLS`) as long as the numbers match. ```hint Where the intercept goes Find a way to make the intercept one more coefficient in the same least squares solve, and keep every row in its original position. ``` ### Constraints & Assumptions - `X` is a float array of shape `(n, k)` without an intercept column, and `y` has shape `(n,)`. - The output has shape `(n,)` and lines up row for row with `y`. ### What a Strong Answer Covers - Adding the intercept to the fit even though `X` lacks a ones column. - Solving the least squares problem with a numerically stable method and computing the fitted values row by row. - Returning raw residuals of shape `(n,)` in the original row order, with no scaling. - Properties of OLS residuals with an intercept that can serve as a quick correctness check. ### Follow-up Questions - How would you compute standardized (studentized) residuals instead? - Why do high-leverage points tend to have small raw residuals? - How would you use the residuals to check the assumptions of the linear model?

Overview: Implement a function that fits ordinary least squares with an intercept on a feature matrix that has no ones column and returns the raw residual vector in row order. It tests building the design matrix, solving least squares stably, and knowing the properties and diagnostic uses of OLS residuals.

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

Implement def compute_residuals(X: np.ndarray, y: np.ndarray) -> np.ndarray. Given a float X of shape (n, k) with no intercept column and y of shape (n,), fit OLS with an intercept and return the residuals e = y - y_hat as a 1-D float array of shape (n,).

  • Fit with an intercept even though X has no ones column.
  • Element i equals y[i] minus the fitted value for row i , in the original row order.
  • Return raw residuals. Do not standardize them, scale them or take absolute values.

Any fitting method is accepted (the normal equations, np.linalg.lstsq, sklearn.linear_model.LinearRegression, statsmodels.OLS) as long as the numbers match.

Constraints & Assumptions

  • X is a float array of shape (n, k) without an intercept column, and y has shape (n,) .
  • The output has shape (n,) and lines up row for row with y .

What a Strong Answer Covers Guidance

  • Adding the intercept to the fit even though X lacks a ones column.
  • Solving the least squares problem with a numerically stable method and computing the fitted values row by row.
  • Returning raw residuals of shape (n,) in the original row order, with no scaling.
  • Properties of OLS residuals with an intercept that can serve as a quick correctness check.

Follow-up Questions Guidance

  • How would you compute standardized (studentized) residuals instead?
  • Why do high-leverage points tend to have small raw residuals?
  • How would you use the residuals to check the assumptions of the linear model?