HC3-Robust 95% Confidence Intervals for OLS Coefficients

Quick Overview

Write a Python function that fits OLS with the HC3 heteroskedasticity-consistent covariance in statsmodels and returns 95% confidence intervals for the intercept and slopes. It tests how robust standard errors feed into interval estimates, which critical value a robust fit uses, and what the covariance choice leaves unchanged.

HC3-Robust 95% Confidence Intervals for OLS Coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `robust_coef_conf_int(X, y)` in Python with statsmodels. It fits OLS with the HC3 robust covariance and returns the 95% confidence intervals for the coefficients. ```python def robust_coef_conf_int(X: np.ndarray, y: np.ndarray) -> np.ndarray: ... ``` The function must: - Prepend a constant with `sm.add_constant(X)`. - Fit with `sm.OLS(y, Xc).fit(cov_type='HC3')`. - Return `results.conf_int(alpha=0.05)` as an `np.ndarray` of shape `(p+1, 2)`. - Make row `i` equal to `[lower_i, upper_i]`, with rows ordered `[intercept, b1, ..., bp]`. Example with a single predictor and 14 observations: ```python X.shape == (14, 1) out = robust_coef_conf_int(X, y) out.shape == (2, 2) # [[lo_intercept, hi_intercept], [lo_b1, hi_b1]] ``` ```hint Pieces of an interval Write each interval as an estimate plus or minus a critical value times a standard error. Then check which of those pieces the `cov_type` argument changes. ``` ```hint Critical value Find out which reference distribution statsmodels uses for intervals after a robust fit, and what that means with only 14 observations. ``` ### Constraints & Assumptions - `numpy` is imported as `np` and `statsmodels.api` as `sm`. - `X` has shape `(n, p)` and `y` has shape `(n,)`. Inputs are clean numeric arrays. - The example has n = 14 and p = 1. ### What a Strong Answer Covers - A fit with the HC3 covariance and intervals returned as an array with the required shape and row order. - How each interval is built from the coefficient, a critical value and the HC3 standard error. - Which reference distribution the robust fit uses and how it affects interval width at n = 14. - Why the point estimates match a non-robust fit while the intervals do not. ### Follow-up Questions - How do HC0, HC1, HC2 and HC3 differ, and why prefer HC3 here? - Would robust covariance change the point estimates or just the intervals? - How would you rebuild the same intervals by hand from `params` and the HC3 standard errors, and how would you switch them to the t distribution?

Overview: Write a Python function that fits OLS with the HC3 heteroskedasticity-consistent covariance in statsmodels and returns 95% confidence intervals for the intercept and slopes. It tests how robust standard errors feed into interval estimates, which critical value a robust fit uses, and what the covariance choice leaves unchanged.

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

Implement robust_coef_conf_int(X, y) in Python with statsmodels. It fits OLS with the HC3 robust covariance and returns the 95% confidence intervals for the coefficients.

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

The function must:

  • Prepend a constant with sm.add_constant(X) .
  • Fit with sm.OLS(y, Xc).fit(cov_type='HC3') .
  • Return results.conf_int(alpha=0.05) as an np.ndarray of shape (p+1, 2) .
  • Make row i equal to [lower_i, upper_i] , with rows ordered [intercept, b1, ..., bp] .

Example with a single predictor and 14 observations:

X.shape == (14, 1)
out = robust_coef_conf_int(X, y)
out.shape == (2, 2)   # [[lo_intercept, hi_intercept], [lo_b1, hi_b1]]

Constraints & Assumptions

  • numpy is imported as np and statsmodels.api as sm .
  • X has shape (n, p) and y has shape (n,) . Inputs are clean numeric arrays.
  • The example has n = 14 and p = 1.

What a Strong Answer Covers Guidance

  • A fit with the HC3 covariance and intervals returned as an array with the required shape and row order.
  • How each interval is built from the coefficient, a critical value and the HC3 standard error.
  • Which reference distribution the robust fit uses and how it affects interval width at n = 14.
  • Why the point estimates match a non-robust fit while the intervals do not.

Follow-up Questions Guidance

  • How do HC0, HC1, HC2 and HC3 differ, and why prefer HC3 here?
  • Would robust covariance change the point estimates or just the intervals?
  • How would you rebuild the same intervals by hand from params and the HC3 standard errors, and how would you switch them to the t distribution?