Find the index of the highest-leverage observation in OLS

Quick Overview

Write a NumPy function that fits OLS with an intercept and returns the index of the observation with the largest diagonal entry of the hat matrix. It tests whether you separate leverage from residual size and can compute the hat diagonal without building an n by n matrix.

Find the index of the highest-leverage observation in OLS

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `index_of_max_leverage(X, y)`. `X` is a NumPy array of 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 leverage $h_{ii}$. ```python def index_of_max_leverage(X: np.ndarray, y: np.ndarray) -> int: ... ``` Prepend an intercept column, so the design $Z$ has k + 1 columns. The leverage values are the diagonal of the hat matrix $H = Z(Z^\top Z)^{-1}Z^\top$, or any equivalent computation of that diagonal. Return `int(np.argmax(h))` as a plain Python `int`: the index, not the leverage value. ```python X = np.array([[1.0], [2.0], [3.0], [50.0]]) y = np.array([1.1, 2.0, 2.9, 4.2]) index_of_max_leverage(X, y) # -> 3 ``` ```hint Look at what H is built from Write out which arrays the hat matrix depends on. Then think about what a fitted line does to the residual of a point it is pulled toward. ``` ### Constraints & Assumptions - `X` has shape `(n, k)` with no intercept column, and `y` has shape `(n,)`. - The test input has one point far out in predictor space, so the maximizer is unique. - That point is deliberately not the observation with the largest absolute residual. - The return value is a plain Python `int`. ### What a Strong Answer Covers - Builds the intercept-augmented design with k + 1 columns. - Computes the hat-matrix diagonal from the predictors alone, and either avoids the n×n matrix or states its cost. - Returns the argmax index as a plain Python `int`, not the leverage value or a NumPy scalar. - Explains why the observation with the largest residual is the wrong answer. ### Follow-up Questions - How can you get the diagonal of H without forming the full n×n matrix? - Why prefer a QR or SVD decomposition over explicitly inverting $Z^\top Z$? - What does a leverage near 1 imply, and how does it relate to the sum of the leverage values?

Overview: Write a NumPy function that fits OLS with an intercept and returns the index of the observation with the largest diagonal entry of the hat matrix. It tests whether you separate leverage from residual size and can compute the hat diagonal without building an n by n matrix.

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

Implement index_of_max_leverage(X, y). X is a NumPy array of 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 leverage hiih_{ii}.

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

Prepend an intercept column, so the design ZZ has k + 1 columns. The leverage values are the diagonal of the hat matrix H=Z(Z⊤Z)−1Z⊤H = Z(Z^\top Z)^{-1}Z^\top, or any equivalent computation of that diagonal. Return int(np.argmax(h)) as a plain Python int: the index, not the leverage value.

X = np.array([[1.0], [2.0], [3.0], [50.0]])
y = np.array([1.1, 2.0, 2.9, 4.2])
index_of_max_leverage(X, y)  # -> 3

Constraints & Assumptions

  • X has shape (n, k) with no intercept column, and y has shape (n,) .
  • The test input has one point far out in predictor space, so the maximizer is unique.
  • That point is deliberately not the observation with the largest absolute residual.
  • The return value is a plain Python int .

What a Strong Answer Covers Guidance

  • Builds the intercept-augmented design with k + 1 columns.
  • Computes the hat-matrix diagonal from the predictors alone, and either avoids the n×n matrix or states its cost.
  • Returns the argmax index as a plain Python int , not the leverage value or a NumPy scalar.
  • Explains why the observation with the largest residual is the wrong answer.

Follow-up Questions Guidance

  • How can you get the diagonal of H without forming the full n×n matrix?
  • Why prefer a QR or SVD decomposition over explicitly inverting Z⊤ZZ^\top Z ?
  • What does a leverage near 1 imply, and how does it relate to the sum of the leverage values?