Sandwich mle #1225

merged 4 commits into from Dec 20, 2013


None yet

2 participants


next round of sandwich robust covariance matrices

sandwiches for MLE, mainly discrete models
similar to PR #1189 which added it to Regression models

#863 for cluster robust based on score
#1158 main overview issue with link to other issues


  • generalize to take score, hessian_inv DONE
    • added unit test for Poisson and NegativeBinomial for cluster so far
    • I made it backwards compatible, it still accepts results instance, but requires tuple for (jac, hessian_inv)
  • no use of wresid for WLS, GLS yet
  • add wrapper function that dispatches to individual sandwiches, like the list in RegressionResults.get_robustcov_results, to intended as single point of entry for all sandwiches
  • integrate with discrete results classes
  • make additional results use robust statistics, e.g .wald_null instead of LL-Null in summary see #1104

Options, Design Problems

  • degrees of freedom and small sample corrections: Stata uses a different correction for cluster robust after Count regression models than after linear regression models. (small number of group adjustment but no nobs-k_vars adjustment)
  • not checked yet: Which HCx is Stata's robust after Poisson.
  • should we rename jac to score_obs?
  • add cov_type option consistently to fit across all models where it applies.
  • still to figure out: what's the easiest way to get the sandwiches for GLM? need the score equivalent, or WLS results (?)
  • also open non- or limited heteroscedasticity robust cov: AC, SUR structure, variations on (X'uu'X = Sigma * X'X)
    (only example so far is replicating OLS in GMM).
    These cases cannot use score directly, but need X, u in X*u separately, AFAICS.
  • demean score? I just saw this in PR #1214 and don't remember what I read (demeaning by cluster?).
    (Bruce Hansen recommends demeaning for GMM which is the default that I use. Stata uses no demeaning as default in GMM.)

test failure on TravisCI, I only ran the test suite for the directly affected directories
typo missing .shape
It also means missing test coverage because cov_white is not used yet, since we have HCx as equivalent

ERROR: statsmodels.sandbox.panel.tests.test_random_panel.test_short_panel
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/", line 197, in runTest
  File "/usr/local/lib/python2.7/dist-packages/statsmodels-0.6.0-py2.7-linux-x86_64.egg/statsmodels/sandbox/panel/tests/", line 143, in test_short_panel
    cov_white = sw.cov_white_simple(mod2.res_pooled)
  File "/usr/local/lib/python2.7/dist-packages/statsmodels-0.6.0-py2.7-linux-x86_64.egg/statsmodels/stats/", line 633, in cov_white_simple
    nobs, k_params = xu
ValueError: too many values to unpack

Coverage Status

Coverage remained the same when pulling 4fc34e6 on josef-pkt:sandwich_mle into a14d989 on statsmodels:master.


rebased and force pushed


Coverage Status

Coverage remained the same when pulling 4de0201 on josef-pkt:sandwich_mle into 8b0f906 on statsmodels:master.


green, merging as part 1 for getting robust covariances for mle, still needs integration with discrete models

@josef-pkt josef-pkt merged commit 903bc22 into statsmodels:master Dec 20, 2013

1 check passed

default The Travis CI build passed
@josef-pkt josef-pkt deleted the josef-pkt:sandwich_mle branch Dec 20, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment