Skip to content

BUG: HC2/HC3 standard errors are arbitrary (or inf) when an observation has leverage 1, e.g. a single-observation dummy #10659

Open

Description

@dani2annotti

Describe the bug

When an observation has leverage h_ii = 1 (a dummy that is 1 for a single observation, an event or outlier dummy, a fixed effect with one observation), its residual is exactly 0 in exact arithmetic, and HC2/HC3 divide that residual by (1 - h_ii) = 0. In floating point both the residual and 1 - h_ii are rounding noise (~1e-15 and ~1e-16), so the standard error of that coefficient is whatever the rounding produces: a finite number with no warning, or inf with a generic divide by zero warning.

In the example below, the same model gives an HC3 standard error of 0.133, 2.29, 2.41, 4.80 or inf for the dummy, decided by the last bits of floating point; in seeds 0, 2, 4 and 5 there is no warning at all. 1 - h is even negative in seed 0. Since t-statistics, p-values and confidence intervals are built from these, inference on that coefficient is arbitrary.

Single-observation dummies and singleton groups are common in applied work, which is one reason reghdfe (Correia 2015) drops singletons by default.

Code Sample, a copy-pastable example

python
import numpy as np
import statsmodels
import statsmodels.api as sm

for seed in range(6):
    rng = np.random.default_rng(seed)
    n = 50
    x = rng.normal(size=n)
    d = np.zeros(n); d[7] = 1.0          # dummy for a single observation -> leverage h_77 = 1
    y = 1 + 2 * x + 3 * d + rng.normal(size=n)
    X = sm.add_constant(np.column_stack([x, d]))
    res = sm.OLS(y, X).fit()
    h = res.get_influence().hat_matrix_diag[7]
    print(f"seed {seed}: resid_7 {res.resid[7]: .2e}  1-h {1 - h: .2e}  "
          f"HC2 se(dummy) {res.get_robustcov_results('HC2').bse[2]:.4g}  "
          f"HC3 se(dummy) {res.get_robustcov_results('HC3').bse[2]:.4g}")
print(statsmodels.__version__, np.__version__)

Expected Output

For an observation with leverage h_ii = 1 (here the single-observation dummy), the HC2/HC3 variance of the affected coefficient is undefined: the residual is 0 and the correction divides by (1 - h_ii) = 0. I would expect statsmodels to detect h_ii close to 1 (e.g. 1 - h_ii < 1e-10) and either:

  • return NaN for the affected standard errors, with a warning that names the observation(s) with leverage 1, or
  • raise an informative error,

and in any case give the same result for every seed. Today the result depends on floating-point rounding: 0.133, 2.29, 2.41, 4.80 or inf for the same model, mostly with no warning.

HC0/HC1 are not affected by the division, but a warning about leverage-1 observations would also help there, since that coefficient is identified by a single observation.

Happy to open a PR with the check and tests.

Actual Output

seed 0: resid_7 -2.66e-15  1-h -4.44e-16  HC2 se(dummy) 0.2252  HC3 se(dummy) 2.411
seed 1: resid_7 -8.88e-16  1-h  0.00e+00  HC2 se(dummy) inf  HC3 se(dummy) inf
seed 2: resid_7  1.78e-15  1-h  3.33e-16  HC2 se(dummy) 0.1503  HC3 se(dummy) 2.291
seed 3: resid_7  1.33e-15  1-h  1.11e-16  HC2 se(dummy) inf  HC3 se(dummy) inf
seed 4: resid_7  2.66e-15  1-h  2.22e-16  HC2 se(dummy) 0.1468  HC3 se(dummy) 4.802
seed 5: resid_7  0.00e+00  1-h  0.00e+00  HC2 se(dummy) 0.1304  HC3 se(dummy) 0.1331
0.15.0 2.3.5

Versions

INSTALLED VERSIONS
------------------
Python: 3.13.16.final.0
OS: Linux 6.18.44-fc-v185 #1 SMP PREEMPT_DYNAMIC @0 x86_64
byteorder: little
LC_ALL: None
LANG: None

statsmodels
===========

Installed: 0.15.0 (/usr/local/lib/python3.13/dist-packages/statsmodels)

Required Dependencies
=====================

cython: Not installed
numpy: 2.3.5 (/usr/local/lib/python3.13/dist-packages/numpy)
scipy: 1.16.3 (/usr/local/lib/python3.13/dist-packages/scipy)
pandas: 2.3.3 (/usr/local/lib/python3.13/dist-packages/pandas)
    dateutil: 2.9.0.post0 (/root/.local/lib/python3.13/site-packages/dateutil)
patsy: 1.0.3 (/usr/local/lib/python3.13/dist-packages/patsy)

Optional Dependencies
=====================

matplotlib: 3.10.9 (/usr/local/lib/python3.13/dist-packages/matplotlib)
    backend: agg 
cvxopt: Not installed
joblib: 1.6.0 (/usr/local/lib/python3.13/dist-packages/joblib)

Developer Tools
================

IPython: 9.18.0 (/usr/local/lib/python3.13/dist-packages/IPython)
    jinja2: 3.1.6 (/root/.local/lib/python3.13/site-packages/jinja2)
sphinx: Not installed
    pygments: 2.21.0 (/usr/local/lib/python3.13/dist-packages/pygments)
pytest: 9.2.0 (/usr/local/lib/python3.13/dist-packages/pytest)
virtualenv: Not installed

Checklist

  • I have searched the issue tracker for a similar issue and did not find one.

  • I have confirmed this bug exists on the latest released version of statsmodels, or on the main branch.

Activity

  1. dani2annotti commented on Nov 2, 2026

    @dani2annotti
    Author

    A correction to my report above: I wrote that HC0/HC1 are not affected. They don't divide by 1 - h_ii, but they are affected, and in a worse way. The only observation that identifies the dummy has a zero residual, so the robust variance of that coefficient gets no information from it and comes out far too small. In size terms (true dummy effect = 0, 1000 seeds, n = 40), HC0 and HC1 reject at 5% far more often than HC2/HC3:

    import numpy as np, statsmodels.api as sm, warnings
    warnings.simplefilter("ignore")
    rej = {c: 0 for c in ("nonrobust", "HC0", "HC1", "HC2", "HC3")}
    N = 1000
    for s in range(N):
        r = np.random.default_rng(s); n = 40
        x = r.normal(size=n); d = np.zeros(n); d[7] = 1.0
        y = 1 + 0.5 * x + r.normal(size=n)          # the dummy has no effect
        X = np.column_stack([np.ones(n), x, d])
        for c in rej:
            p = sm.OLS(y, X).fit(cov_type=c).pvalues[2]
            rej[c] += bool(np.isfinite(p) and p < 0.05)
    print({c: v / N for c, v in rej.items()})
    # {'nonrobust': 0.041, 'HC0': 0.693, 'HC1': 0.678, 'HC2': 0.605, 'HC3': 0.205}

    (statsmodels 0.15.0, NumPy 2.5.3.) The classical test is close to nominal. HC0/HC1 are stable across seeds, so nothing looks wrong to the user, which makes them the more dangerous case. I'd suggest the leverage check cover HC0-HC3 and return NaN for the coefficients identified only by leverage-one observations (or warn and name the observations), not only for HC2/HC3.

  2. aniyow07 commented on Nov 7, 2026

    @aniyow07

    I would like to work on this.

  3. markjayson13 commented on Nov 11, 2026

    @markjayson13

    I agree that HC2/HC3 should not return a seed-dependent finite standard error for a leverage-1 coefficient, and the follow-up on HC0/HC1 is important: those estimators do not divide by 1 − h_ii, but the identifying observation has a zero residual, so the robust variance of that coefficient is far too small and the 5% test over-rejects badly.

    This is the usual singleton-dummy / singleton-group problem in fixed-effects work. Point estimates of the other coefficients are unchanged if those observations are dropped, but the robust SEs are not, which is why pyfixest drops singleton fixed effects by default (fixef_rm="singleton") and why reghdfe does the same, as you already note.

    For statsmodels, I would also vote for covering HC0–HC3: return NaN for coefficients identified only by leverage-one observations, and warn while naming those observations, rather than only special-casing the HC2/HC3 division by zero. I am not claiming the PR; just adding the applied-econometrics framing in case it helps the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions