Repository navigation
BUG: Fix NegativeBinomialP hessian for p other than 1 and 2 - #10737
Merged
bashtage merged 1 commit intoNov 15, 2026
Merged
Conversation
The derivative of the alpha score with respect to the regression parameters in NegativeBinomialP.hessian and hessian_factor had the term p * a4 / a2 where the derivative of a1 / a2 gives a4 / a2, with a4 = (2 - parameterization) * a1 / mu. Both agree only if 2 - parameterization is 0 or 1, i.e. for p = 2 and p = 1. With p = 1.5 the standard errors of the constant and of alpha were about 19% too large, and for p larger than 2 the hessian was not invertible. The hessian now agrees with the numerical hessian of loglike for all p.
bhaskargurram-ai
marked this pull request as draft
November 11, 2026 15:27
bhaskargurram-ai
marked this pull request as ready for review
November 11, 2026 15:39
1 task done
Member
|
Thanks. |
This was referenced Nov 15, 2026
bashtage
added a commit
that referenced
this pull request
Nov 16, 2026
The release note of #10737 had no :pr: role, sat in the middle of its list and quoted "about 19% too large", which is a single simulated sample. Move it to the end of the list and say what users see: bse, cov_params, conf_int, p-values and the HC0 covariance change for p other than 1 and 2, fit(method="newton") converges, score_test with a tuple exog_extra changes, p=1 and p=2 are unchanged. The numbers are from 20 simulated samples of 3000 counts (bse of the constant and alpha 1.2 to 2.2 times the numerical bse at p=1.5, 1.1 to 1.3 at p=1.8, within 8% at p=1.2, nan at p=2.5 and 3). Add versionchanged 0.15.1 to hessian and hessian_factor, document the three returned factors of hessian_factor (it returned a tuple, not a matrix), and document p as a float in the class docstring with the variance function mu + alpha * mu**p. NegativeBinomialP does not restrict p to a range. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
added a commit
that referenced
this pull request
Nov 16, 2026
The test added in #10737 compared only the cross block of hessian_factor with the hessian. Mutating the other two factors (hfbb: 1 + a4 -> 1, hfaa: dropping the a1 * a3 / a2**2 term) left all 9 tests green. Compare the block of the regression parameters and the alpha element too, and explain the atol of the numerical Hessian comparison (error of approx_hess up to 5e-7 times the largest entry, smallest entries 4e-4 to 1e-2 of it). Parametrize test_negbinp_bse_p15 with p=2.5, where bse is nan with the old cross derivative, and assert that the newton fit converges and that bse is finite. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
added a commit
that referenced
this pull request
Nov 16, 2026
DOC: Follow up #10737 on the NegativeBinomialP Hessian, with tests
bashtage
added a commit
that referenced
this pull request
Nov 16, 2026
The bullet for the truncated count score of #10736 was in the middle of the list of the discrete models, without the PR role, and did not say what users see. Move it to the end of the list, add the PR number, and state that params, bse and llf of the fitted models change for every p other than p=1 of the generalized Poisson and p=2 of the negative binomial model, with the example of the default generalized Poisson (alpha 0.099 -> 0.219, llf -3582.06 -> -3554.15 for 3000 simulated counts, measured on main before the PR and with the PR). The bullet stays in Bug Fixes like the sibling bullets of #10723 and #10737 for the Hessians of the discrete models. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This was referenced Nov 16, 2026
bashtage
added a commit
that referenced
this pull request
Nov 16, 2026
The release note of #10744 named only cov_type="cluster". Every covariance type that sets df_resid_inference changes: "cluster" (also two-way clustering), "hac-panel" and "hac-groupsum" for all models and "cluster-crv3" and "cluster-jk" for linear models, unless df_correction=False. With use_t=False nothing changes. Checked with seeded OLS, GLM, Logit and Poisson fits with 10 clusters on main before and after #10744. The entry stays in Bug Fixes with the neighbouring #10742, #10737 and #10736. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The cross derivative of the regression parameters and
alphainNegativeBinomialP.hessianandhessian_factoris wrong whenpis not 1 or 2. WithQ = 2 - p(pin the code),a1 = mu**Q / alphaanda4 = Q * a1 / mu, the derivative ofa1 / a2contributesa4 / a2, but the code hadQ * a4 / a2. The two agree only for Q = 0 or 1, i.e.p=2andp=1, the cases in the tests. The other blocks of the Hessian are correct.With 3000 simulated NB2 counts, the relative error of the Hessian at the MLE (against the complex-step derivative of
score) was 0.16, 0.31, 0.25, 1.8 and 6.1 forp= 1.2, 1.5, 1.8, 2.5 and 3. Withp=1.5thebseof the constant and ofalphawere 19% too large (p=1.8: 9%). Withp=2.5andp=3the Hessian was not invertible, sobsewas nan with aHessianInversionWarning.The fix changes this one term in
hessianand inhessian_factor. Afterwards the Hessian agrees with the complex-step derivative ofscoreto 5e-15 and withapprox_hess3oflogliketo 2.4e-7 for all thesep, andbseagrees with the numericalbse.Tests:
test_negbinp_hessian_numdiffcompareshessianwithapprox_hessofloglike(with an offset) forpin 0.5, 1, 1.2, 1.5, 1.8, 2, 2.5, 3, and checks the cross term ofhessian_factoragainsthessian.test_negbinp_bse_p15comparesbseof a newton fit withp=1.5with the numericalbse. All fail on main exceptp=1andp=2.NumPy's guide.
AI Disclosure
Contributions must comply with the statsmodels AI Policy.
Tick exactly one of the following. If AI tools were used, replace the
<...>placeholders in the second option with the tool name(s) and how the tool's output was
used.
Tool(s):
Claude Code.Used for:
drafting the tests and this PR description.I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.