Compute internally studentized residuals for an OLS fit with NumPy

Quick Overview

Implement internally studentized residuals for an OLS fit with an intercept using NumPy, matching what statsmodels reports. It tests leverage and the hat matrix diagonal, residual degrees of freedom, and computing leverages without building an n by n matrix.

Compute internally studentized residuals for an OLS fit with NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def standardized_residuals(X: np.ndarray, y: np.ndarray) -> np.ndarray`. Given `X` of shape `(n, k)` (no intercept column) and `y` of shape `(n,)`, fit OLS with an intercept and return the internally studentized residuals $$r_i = \frac{e_i}{s \sqrt{1 - h_{ii}}}$$ as a 1-D float array of shape `(n,)`. - $e_i$ is the raw residual of observation $i$. - $h_{ii} = \tilde x_i^\top (\tilde X^\top \tilde X)^{-1} \tilde x_i$ is the leverage, computed from the intercept-augmented design $\tilde X = [\mathbf{1} \mid X]$, where $\tilde x_i$ is row $i$ of $\tilde X$. - $s = \sqrt{\text{RSS} / (n - k - 1)}$. - Divide by both $s$ and $\sqrt{1 - h_{ii}}$. - The result must match `results.get_influence().resid_studentized_internal` from statsmodels. For example: ```python X = np.array([[1.0], [2.0], [3.0], [4.0], [5.0]]) y = np.array([1.1, 1.9, 3.2, 3.8, 5.3]) r = standardized_residuals(X, y) # r.shape == (5,) # r is approximately [0.6783, -0.6666, 0.6715, -1.4870, 1.2210] ``` The fitted line is $y = -0.03 + 1.03x$, the residuals are 0.10, -0.13, 0.14, -0.29 and 0.18, and RSS is 0.163 with 3 residual degrees of freedom. The leverages are 0.6, 0.3, 0.2, 0.3 and 0.6, so the end points get the largest correction. ```hint Only the diagonal You need the diagonal of the hat matrix, not the matrix itself. Look for a factorization of the design matrix that gives each leverage as a quantity computed from one row. ``` ```hint Count the intercept The intercept is a fitted parameter too. Check that it is in both the design you factor and the residual degrees of freedom. ``` ### Constraints & Assumptions - Use NumPy only. statsmodels is not required, though it is useful for checking the result. - Assume $n > k + 1$ and that $\tilde X^\top \tilde X$ is invertible. ### What a Strong Answer Covers - Building the intercept-augmented design and fitting OLS with a numerically stable solver. - Why the raw residuals have unequal variances, and how both $s$ and the leverage term correct for that. - Residual degrees of freedom that account for the intercept, so the result matches statsmodels. - The time and memory cost of computing the leverages. - Edge cases such as an observation with leverage 1, or a nearly singular design. ### Follow-up Questions - How do externally studentized residuals differ, and when are they preferred? - How would you compute all leverages efficiently without forming the full $n \times n$ hat matrix? - How do studentized residuals and leverage combine into Cook's distance?

Overview: Implement internally studentized residuals for an OLS fit with an intercept using NumPy, matching what statsmodels reports. It tests leverage and the hat matrix diagonal, residual degrees of freedom, and computing leverages without building an n by n matrix.

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

Implement def standardized_residuals(X: np.ndarray, y: np.ndarray) -> np.ndarray. Given X of shape (n, k) (no intercept column) and y of shape (n,), fit OLS with an intercept and return the internally studentized residuals

ri=eis1−hiir_i = \frac{e_i}{s \sqrt{1 - h_{ii}}}

as a 1-D float array of shape (n,).

  • eie_i is the raw residual of observation ii .
  • hii=x~i⊤(X~⊤X~)−1x~ih_{ii} = \tilde x_i^\top (\tilde X^\top \tilde X)^{-1} \tilde x_i is the leverage, computed from the intercept-augmented design X~=[1∣X]\tilde X = [\mathbf{1} \mid X] , where x~i\tilde x_i is row ii of X~\tilde X .
  • s=RSS/(n−k−1)s = \sqrt{\text{RSS} / (n - k - 1)} .
  • Divide by both ss and 1−hii\sqrt{1 - h_{ii}} .
  • The result must match results.get_influence().resid_studentized_internal from statsmodels.

For example:

X = np.array([[1.0], [2.0], [3.0], [4.0], [5.0]])
y = np.array([1.1, 1.9, 3.2, 3.8, 5.3])
r = standardized_residuals(X, y)
# r.shape == (5,)
# r is approximately [0.6783, -0.6666, 0.6715, -1.4870, 1.2210]

The fitted line is y=−0.03+1.03xy = -0.03 + 1.03x, the residuals are 0.10, -0.13, 0.14, -0.29 and 0.18, and RSS is 0.163 with 3 residual degrees of freedom. The leverages are 0.6, 0.3, 0.2, 0.3 and 0.6, so the end points get the largest correction.

Constraints & Assumptions

  • Use NumPy only. statsmodels is not required, though it is useful for checking the result.
  • Assume n>k+1n > k + 1 and that X~⊤X~\tilde X^\top \tilde X is invertible.

What a Strong Answer Covers Guidance

  • Building the intercept-augmented design and fitting OLS with a numerically stable solver.
  • Why the raw residuals have unequal variances, and how both ss and the leverage term correct for that.
  • Residual degrees of freedom that account for the intercept, so the result matches statsmodels.
  • The time and memory cost of computing the leverages.
  • Edge cases such as an observation with leverage 1, or a nearly singular design.

Follow-up Questions Guidance

  • How do externally studentized residuals differ, and when are they preferred?
  • How would you compute all leverages efficiently without forming the full n×nn \times n hat matrix?
  • How do studentized residuals and leverage combine into Cook's distance?