Compute Pearson residuals for an OLS fit with NumPy

Quick Overview

Implement Pearson residuals for an OLS fit with an intercept in NumPy, matching the values statsmodels reports. It tests the residual variance estimate, degrees of freedom that count the intercept, and how these residuals differ from studentized residuals that adjust for leverage.

Compute Pearson residuals for an OLS fit with NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def pearson_residuals(X: np.ndarray, y: np.ndarray) -> np.ndarray`. Fit OLS of `y` on `X` (shape `(n, k)`, no intercept column) with an added intercept, and return the Pearson residuals $p_i = e_i / \sqrt{\text{scale}}$, the same values as statsmodels `results.resid_pearson`. - Prepend an intercept column, so the design matrix has $k + 1$ columns. - The raw residuals are `e = y - X_design @ beta`. - Use a single divisor for all observations: $\text{scale} = \text{RSS} / (n - k - 1)$, with no leverage term. These are not internally studentized residuals. - Return a 1-D float `np.ndarray` of shape `(n,)`. 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]) p = pearson_residuals(X, y) # p is approximately [0.4290, -0.5577, 0.6006, -1.2441, 0.7722] ``` The fitted line is $y = -0.03 + 1.03x$ with residuals 0.10, -0.13, 0.14, -0.29 and 0.18. RSS is 0.163 with 3 degrees of freedom, so every residual is divided by the same $\sqrt{0.163 / 3} \approx 0.2331$. ```hint One divisor Every observation shares the same divisor here. Work out which variance estimate statsmodels calls the scale of an OLS fit, and how many fitted parameters its degrees of freedom subtract. ``` ### Constraints & Assumptions - Assume $n > k + 1$. - No statsmodels dependency is required. ### What a Strong Answer Covers - Prepending the intercept and solving least squares with a stable method. - One common divisor, the residual variance estimate, with degrees of freedom that count the intercept. - The output shape and dtype the task asks for. - How these residuals differ from internally studentized residuals, and what that means for comparing observations. ### Follow-up Questions - How do internally studentized residuals differ from these? - What if $X^\top X$ is singular or ill-conditioned? - In a GLM such as logistic regression, what does a Pearson residual divide by?

Overview: Implement Pearson residuals for an OLS fit with an intercept in NumPy, matching the values statsmodels reports. It tests the residual variance estimate, degrees of freedom that count the intercept, and how these residuals differ from studentized residuals that adjust for leverage.

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

Implement def pearson_residuals(X: np.ndarray, y: np.ndarray) -> np.ndarray. Fit OLS of y on X (shape (n, k), no intercept column) with an added intercept, and return the Pearson residuals pi=ei/scalep_i = e_i / \sqrt{\text{scale}}, the same values as statsmodels results.resid_pearson.

  • Prepend an intercept column, so the design matrix has k+1k + 1 columns.
  • The raw residuals are e = y - X_design @ beta .
  • Use a single divisor for all observations: scale=RSS/(n−k−1)\text{scale} = \text{RSS} / (n - k - 1) , with no leverage term. These are not internally studentized residuals.
  • Return a 1-D float np.ndarray of shape (n,) .

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])
p = pearson_residuals(X, y)
# p is approximately [0.4290, -0.5577, 0.6006, -1.2441, 0.7722]

The fitted line is y=−0.03+1.03xy = -0.03 + 1.03x with residuals 0.10, -0.13, 0.14, -0.29 and 0.18. RSS is 0.163 with 3 degrees of freedom, so every residual is divided by the same 0.163/3≈0.2331\sqrt{0.163 / 3} \approx 0.2331.

Constraints & Assumptions

  • Assume n>k+1n > k + 1 .
  • No statsmodels dependency is required.

What a Strong Answer Covers Guidance

  • Prepending the intercept and solving least squares with a stable method.
  • One common divisor, the residual variance estimate, with degrees of freedom that count the intercept.
  • The output shape and dtype the task asks for.
  • How these residuals differ from internally studentized residuals, and what that means for comparing observations.

Follow-up Questions Guidance

  • How do internally studentized residuals differ from these?
  • What if X⊤XX^\top X is singular or ill-conditioned?
  • In a GLM such as logistic regression, what does a Pearson residual divide by?