Classical vs HC3 Robust Standard Errors from One OLS Fit
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `nonrobust_vs_hc3_se(X, y)` in Python with statsmodels. The function adds an intercept with `sm.add_constant`, fits OLS once, and returns `(params, se_nonrobust, se_hc3)`. Point estimates are unaffected by the covariance choice: only the standard errors differ.
```python
def nonrobust_vs_hc3_se(X: np.ndarray, y: np.ndarray) -> tuple:
...
```
The function must:
- Add an intercept with `sm.add_constant(X)` and fit one OLS model.
- Return `params`, the coefficient array of shape `(p+1,)`, ordered `[intercept, b1, ..., bp]`.
- Return `se_nonrobust`, the classical standard errors from a default `fit()` (`results.bse`).
- Return `se_hc3`, the HC3 robust standard errors (`results.HC3_se`), also of shape `(p+1,)`. A separate `fit(cov_type='HC3')` is not required.
Example on a fixed, strongly heteroskedastic dataset with 14 observations and one feature:
```python
X.shape == (14, 1)
params, se_nb, se_hc3 = nonrobust_vs_hc3_se(X, y)
params.shape == (2,) # [intercept, b1]
# params are the same whichever covariance estimator is used
# se_hc3 != se_nb because the errors are strongly heteroskedastic
```
```hint What the covariance choice touches
Write down the OLS coefficient formula and check whether the error variance appears in it. Then write the variance of the coefficients and see where it does.
```
```hint Leverage
HC3 rescales each squared residual using that observation's leverage $h_{ii}$. Think about which points in a sample of 14 get the largest adjustment.
```
### 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 with no missing values.
- The test dataset has n = 14 and p = 1.
### What a Strong Answer Covers
- One OLS fit with an intercept, returning coefficients in the order `[intercept, b1, ..., bp]` and both standard-error vectors from the same results object.
- Why the coefficients do not depend on the covariance estimator.
- The classical and HC3 variance formulas and the exact place where they differ.
- How leverage enters HC3 and why it matters at this sample size.
- Edge cases such as an existing constant column in `X` or a point with leverage near 1.
### Follow-up Questions
- When would you prefer HC3 over HC0 or HC1?
- What does heteroskedasticity do to classical OLS inference?
- How would you get HC3-based t-statistics, p-values and confidence intervals from statsmodels, and which reference distribution does it use for them by default?
- When would you switch to weighted least squares instead of keeping OLS with HC3 standard errors?
Overview: Implement a Python function that fits one OLS model with statsmodels and returns the coefficients, the classical standard errors and the HC3 heteroskedasticity-consistent standard errors. It tests your grasp of sandwich covariance estimators, leverage corrections and what heteroskedasticity does to regression inference.