Find the Index of the Largest Absolute Residual in an OLS Fit

Quick Overview

Write a Python function that fits ordinary least squares with an intercept and returns the index of the observation with the largest absolute residual. It tests residual computation, careful handling of sign and return types, and how leverage affects raw residuals as an outlier signal.

Find the Index of the Largest Absolute Residual in an OLS Fit

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def index_of_largest_residual(X: np.ndarray, y: np.ndarray) -> int`. `X` has shape `(n, k)` with no intercept column and `y` has shape `(n,)`. Fit OLS with an intercept and return the 0-based index of the observation with the largest absolute residual. Requirements: - Fit OLS including an intercept term (add a column of ones or use a library that does). - Residuals are `e = y - y_hat` from the fitted model. - Return `int(np.argmax(np.abs(e)))` as a plain Python `int` (not a NumPy scalar or the residual value). `numpy` (for example `np.linalg.lstsq`) or `statsmodels` may be used. ```hint Direction of the miss The outlier may sit above or below the fitted line. Make sure your comparison treats both directions the same way. ``` ### Constraints & Assumptions - `X` is `(n, k)` and holds predictors only; `y` is `(n,)`. - The grader's fixed input has a unique maximizer (no ties), with one deliberate large-residual outlier. ### What a Strong Answer Covers - Adding the intercept column and solving least squares for the coefficients. - Computing residuals on the same rows and taking the index of the largest absolute value. - Returning a plain Python `int` index, not the residual or a NumPy scalar. - The limits of raw residuals for spotting outliers, including how leverage can shrink an outlier's own residual. ### Follow-up Questions - Why is the raw residual a poor measure for flagging outliers? - If two observations tied for the largest absolute residual, which index would your function return? - How would you flag every outlier in the data, not only the single largest one?

Overview: Write a Python function that fits ordinary least squares with an intercept and returns the index of the observation with the largest absolute residual. It tests residual computation, careful handling of sign and return types, and how leverage affects raw residuals as an outlier signal.

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

Implement def index_of_largest_residual(X: np.ndarray, y: np.ndarray) -> int. X has shape (n, k) with no intercept column and y has shape (n,). Fit OLS with an intercept and return the 0-based index of the observation with the largest absolute residual.

Requirements:

  • Fit OLS including an intercept term (add a column of ones or use a library that does).
  • Residuals are e = y - y_hat from the fitted model.
  • Return int(np.argmax(np.abs(e))) as a plain Python int (not a NumPy scalar or the residual value).

numpy (for example np.linalg.lstsq) or statsmodels may be used.

Constraints & Assumptions

  • X is (n, k) and holds predictors only; y is (n,) .
  • The grader's fixed input has a unique maximizer (no ties), with one deliberate large-residual outlier.

What a Strong Answer Covers Guidance

  • Adding the intercept column and solving least squares for the coefficients.
  • Computing residuals on the same rows and taking the index of the largest absolute value.
  • Returning a plain Python int index, not the residual or a NumPy scalar.
  • The limits of raw residuals for spotting outliers, including how leverage can shrink an outlier's own residual.

Follow-up Questions Guidance

  • Why is the raw residual a poor measure for flagging outliers?
  • If two observations tied for the largest absolute residual, which index would your function return?
  • How would you flag every outlier in the data, not only the single largest one?