Compute OLS leverage values from the hat matrix diagonal

Quick Overview

Write a NumPy function that returns the leverage of every observation, the diagonal of the hat matrix, for an OLS fit with an intercept, matching statsmodels. It tests the projection view of regression, numerical stability, and computing a matrix diagonal without building the full matrix.

Compute OLS leverage values from the hat matrix diagonal

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `leverage_values(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 diagonal $h_{ii}$ of the hat matrix $H = \tilde{X}(\tilde{X}^\top\tilde{X})^{-1}\tilde{X}^\top$, where $\tilde{X}$ is `X` with a column of ones prepended. ```python def leverage_values(X: np.ndarray, y: np.ndarray) -> np.ndarray: ... ``` The result must: - be a 1-D float `np.ndarray` of shape `(n,)` in input row order, - match `results.get_influence().hat_matrix_diag` from statsmodels for the same model, - come from computing only the diagonal where possible, without building the full `(n, n)` matrix. ```python X = np.array([[1.0], [2.0], [3.0], [10.0]]) y = np.array([1.0, 2.0, 3.0, 4.0]) h = leverage_values(X, y) # h is approximately [0.43, 0.33, 0.27, 0.97] # every value is between 0 and 1, h.sum() is 2 (k + 1) up to rounding, # and the far-out row (x = 10) has the largest leverage ``` ```hint Only the diagonal Each diagonal entry of H depends on a single row of the design. Look for a way to get all of them from arrays of size n by (k + 1), for example through a factorization of the design. ``` ### Constraints & Assumptions - `X` has shape `(n, k)` with no intercept column, so the design has k + 1 columns. - `y` has shape `(n,)` and does not affect the result. - Agreement with statsmodels is to floating-point tolerance, not bit-for-bit. ### What a Strong Answer Covers - Prepends the ones column so the diagonal belongs to a model with an intercept. - Computes only the diagonal, with time and memory that grow linearly in n. - Returns a float array of shape `(n,)` in input order that agrees with statsmodels to floating-point tolerance. - Uses sanity checks such as the values summing to k + 1 and each value lying between 1/n and 1. ### Follow-up Questions - How can you get the diagonal of H without forming the full matrix, and why is a QR factorization more stable than inverting $\tilde{X}^\top\tilde{X}$? - Why is high leverage worth flagging, and what threshold is common? - What happens if `X` already contains a constant column, and would your function still match statsmodels?

Overview: Write a NumPy function that returns the leverage of every observation, the diagonal of the hat matrix, for an OLS fit with an intercept, matching statsmodels. It tests the projection view of regression, numerical stability, and computing a matrix diagonal without building the full matrix.

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

Implement leverage_values(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 diagonal hiih_{ii} of the hat matrix H=X~(X~⊤X~)−1X~⊤H = \tilde{X}(\tilde{X}^\top\tilde{X})^{-1}\tilde{X}^\top, where X~\tilde{X} is X with a column of ones prepended.

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

The result must:

  • be a 1-D float np.ndarray of shape (n,) in input row order,
  • match results.get_influence().hat_matrix_diag from statsmodels for the same model,
  • come from computing only the diagonal where possible, without building the full (n, n) matrix.
X = np.array([[1.0], [2.0], [3.0], [10.0]])
y = np.array([1.0, 2.0, 3.0, 4.0])
h = leverage_values(X, y)
# h is approximately [0.43, 0.33, 0.27, 0.97]
# every value is between 0 and 1, h.sum() is 2 (k + 1) up to rounding,
# and the far-out row (x = 10) has the largest leverage

Constraints & Assumptions

  • X has shape (n, k) with no intercept column, so the design has k + 1 columns.
  • y has shape (n,) and does not affect the result.
  • Agreement with statsmodels is to floating-point tolerance, not bit-for-bit.

What a Strong Answer Covers Guidance

  • Prepends the ones column so the diagonal belongs to a model with an intercept.
  • Computes only the diagonal, with time and memory that grow linearly in n.
  • Returns a float array of shape (n,) in input order that agrees with statsmodels to floating-point tolerance.
  • Uses sanity checks such as the values summing to k + 1 and each value lying between 1/n and 1.

Follow-up Questions Guidance

  • How can you get the diagonal of H without forming the full matrix, and why is a QR factorization more stable than inverting X~⊤X~\tilde{X}^\top\tilde{X} ?
  • Why is high leverage worth flagging, and what threshold is common?
  • What happens if X already contains a constant column, and would your function still match statsmodels?