Compare HC0, HC1, HC2 and HC3 Robust Standard Errors from One OLS Fit

Quick Overview

Write a Python function that fits one OLS model with statsmodels and returns the HC0, HC1, HC2 and HC3 heteroskedasticity-consistent standard errors side by side in a DataFrame. It tests how the sandwich estimators weight squared residuals, how leverage enters the small-sample corrections, and which orderings between them are guaranteed.

Compare HC0, HC1, HC2 and HC3 Robust Standard Errors from One OLS Fit

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `hc_family_se(X, y)` in Python. It fits one OLS of `y` on `sm.add_constant(X)` and returns the four heteroskedasticity-consistent standard errors side by side, one row per coefficient. ```python def hc_family_se(X: np.ndarray, y: np.ndarray) -> pd.DataFrame: ... ``` The function must: - Fit exactly one OLS model with `sm.OLS(...).fit()`. - Return a `pd.DataFrame` with exactly four columns, `HC0`, `HC1`, `HC2` and `HC3`, in that order. - Keep the default `RangeIndex`, with rows ordered `[intercept, b1, ..., bp]`. - Fill each column with the matching `results.HCx_se` values. The test data is a fixed `X` of shape `(14, 1)` and a heteroskedastic `y` with one high-leverage point. On that data, the standard errors of the coefficient on `x` must satisfy `HC0 <= HC1 <= HC2 <= HC3`. ```hint Shared ingredients All four estimators use the same residuals and the same hat matrix. Write each one as a weight applied to $e_i^2$ and compare the weights. ``` ```hint Which inequalities are guaranteed Compare the weights pairwise. Ask which comparisons hold for any data and which depend on where the large residuals sit. ``` ### Constraints & Assumptions - `numpy` is imported as `np`, `pandas` as `pd` and `statsmodels.api` as `sm`. - `X` has shape `(n, p)` and `y` has shape `(n,)`. Inputs are clean numeric arrays. - The test data has n = 14, p = 1 and one high-leverage point. ### What a Strong Answer Covers - A single fit whose results object supplies all four standard-error vectors, returned with the required column order and index. - The weight each estimator places on a squared residual, including the role of the leverage $h_{ii}$. - Which of the inequalities hold for any data and which depend on the data. - Why the full ordering holds for the slope on the test data. ### Follow-up Questions - How do HC1, HC2 and HC3 differ from HC0? - When would you prefer HC3 over HC0 in practice? - How would you compute HC0 by hand with numpy and confirm it matches `results.HC0_se`? - What happens to HC2 and HC3 when one observation has leverage close to 1?

Overview: Write a Python function that fits one OLS model with statsmodels and returns the HC0, HC1, HC2 and HC3 heteroskedasticity-consistent standard errors side by side in a DataFrame. It tests how the sandwich estimators weight squared residuals, how leverage enters the small-sample corrections, and which orderings between them are guaranteed.

General logo
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement hc_family_se(X, y) in Python. It fits one OLS of y on sm.add_constant(X) and returns the four heteroskedasticity-consistent standard errors side by side, one row per coefficient.

def hc_family_se(X: np.ndarray, y: np.ndarray) -> pd.DataFrame:
    ...

The function must:

  • Fit exactly one OLS model with sm.OLS(...).fit() .
  • Return a pd.DataFrame with exactly four columns, HC0 , HC1 , HC2 and HC3 , in that order.
  • Keep the default RangeIndex , with rows ordered [intercept, b1, ..., bp] .
  • Fill each column with the matching results.HCx_se values.

The test data is a fixed X of shape (14, 1) and a heteroskedastic y with one high-leverage point. On that data, the standard errors of the coefficient on x must satisfy HC0 <= HC1 <= HC2 <= HC3.

Constraints & Assumptions

  • numpy is imported as np , pandas as pd and statsmodels.api as sm .
  • X has shape (n, p) and y has shape (n,) . Inputs are clean numeric arrays.
  • The test data has n = 14, p = 1 and one high-leverage point.

What a Strong Answer Covers Guidance

  • A single fit whose results object supplies all four standard-error vectors, returned with the required column order and index.
  • The weight each estimator places on a squared residual, including the role of the leverage hiih_{ii} .
  • Which of the inequalities hold for any data and which depend on the data.
  • Why the full ordering holds for the slope on the test data.

Follow-up Questions Guidance

  • How do HC1, HC2 and HC3 differ from HC0?
  • When would you prefer HC3 over HC0 in practice?
  • How would you compute HC0 by hand with numpy and confirm it matches results.HC0_se ?
  • What happens to HC2 and HC3 when one observation has leverage close to 1?