Repository navigation
BUG: Fix cross derivatives in ZeroInflatedPoisson hessian - #10723
Merged
bashtage merged 1 commit intoNov 11, 2026
Merged
Conversation
The analytic hessian of ZeroInflatedPoisson with logit inflation stored the inflation-count cross block in the upper triangle, which was then overwritten when the lower triangle was mirrored, so the cross derivatives were zero. The dropped block was also missing a factor f / P, where f is the probability of a zero in the count model and P the probability of a zero in the mixture. Put the cross block in the lower triangle and use the correct derivative. Standard errors now agree with those from a numerical hessian, and the bse of the randhie example agree with the reference values to 3e-5 instead of 4e-3.
Member
|
Thanks. |
4 of 5 tasks
bashtage
added a commit
that referenced
this pull request
Nov 11, 2026
…note The release note for #10723 had no :pr: role and claimed that the standard errors were off by more than 10%, which holds only for some designs. Measured on the code before the fix (logit inflation, newton fit, bse compared with the numerical Hessian at the MLE): about 9 to 10% too small for the inflation and the count intercept in simulated data with nobs = 3000 (three seeds), and 0.04 to 0.4% (without offset) and up to 0.6% (with offset) too small for the RandHIE data of the tests. State that and mention that cov_params, conf_int and p-values change with bse. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
added a commit
that referenced
this pull request
Nov 11, 2026
…test #10723 tested the hessian with a single design (k_inflate = 2, k_exog = 3). Parametrize test_zip_hessian_numdiff over the inflation design: constant only (exog_infl=None, k_inflate = 1), constant and one or two regressors (k_inflate = 2, 3; the latter is a square cross block that is not symmetric) and exog_infl = exog. Dropping the transpose of the cross block is now caught for the square case instead of only by a shape error. exog_infl = exog does not catch it: the block is symmetric there. Also compare the analytic score with a numerical gradient of the loglike. Add a provenance comment to the two RandHIE zero-inflated Poisson logit results that #10723 compares at rtol=1e-4: they agree with pscl 1.5.9 zeroinfl to 2.1e-5 (bse) and 1.4e-5 (params), relative. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
added a commit
that referenced
this pull request
Nov 11, 2026
TST: Follow up #10723, more inflation designs in the ZIP hessian test
This was referenced Nov 15, 2026
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 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.
ZeroInflatedPoisson.hessianwith logit inflation (the default) returns zeros for the second derivatives between the inflation and the count parameters._hessian_inflateputs that block in the upper triangle, andhessianthen overwrites the upper triangle with the lower one, which was never filled. The block itself was also wrong: for an observation with y = 0 the derivative oflog Pis-w (1 - w) f / P**2 * score_main * z, where f is the probability of a zero in the Poisson part and P the probability of a zero in the mixture. The code had/ Pinstead of* f / P**2. For y > 0 the cross derivative is zero.This makes
bsewrong. In 20 simulated designs (n = 3000, 2 to 5 count and 1 to 3 inflation parameters, some with offset or exposure) the worstbseper fit differed from thebseof a numerical Hessian by 18% (median) and up to 35%. For the randhie data in the tests,bsediffers from the reference values by up to 0.36%, which only passes becausetest_bseusesatol=1e-3.The fix puts the cross block in the lower triangle before it is mirrored and uses the correct derivative. ZINB, ZIGP and probit inflation are not affected, they have no analytic Hessian and use
approx_hess.Checks after the fix:
hessianagainstapprox_hess3ofloglike: max relative error 1.6e-6 at the MLE and 7.5e-7 at perturbed parameters (same 20 designs).bseagainst the numericalbse: median 9e-7, max 1.5e-5 (before: 0.18 / 0.35).bseagainst the reference values: 2.5e-5 (before 3.6e-3), and 4.4e-6 for the offset case (before 5.8e-3).Tests:
test_zip_hessian_numdiffcompareshessianwithapprox_hessat fixed parameters andbseat the MLE with the numericalbse, without offset, with offset and with exposure.test_bseof the three ZIP logit classes now usesrtol=1e-4withoutatol. All six fail on main.xref #7632, where the correctness of the ZI hessian code was questioned. This PR only fixes the hessian, no refactoring.
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.