Skip to content

BUG: Fix cross derivatives in ZeroInflatedPoisson hessian - #10723

Merged
bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/zip-hessian-cross-block
Nov 11, 2026
Merged

bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/zip-hessian-cross-block

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown
Contributor

ZeroInflatedPoisson.hessian with logit inflation (the default) returns zeros for the second derivatives between the inflation and the count parameters. _hessian_inflate puts that block in the upper triangle, and hessian then 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 of log P is -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 / P instead of * f / P**2. For y > 0 the cross derivative is zero.

This makes bse wrong. In 20 simulated designs (n = 3000, 2 to 5 count and 1 to 3 inflation parameters, some with offset or exposure) the worst bse per fit differed from the bse of a numerical Hessian by 18% (median) and up to 35%. For the randhie data in the tests, bse differs from the reference values by up to 0.36%, which only passes because test_bse uses atol=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:

  • hessian against approx_hess3 of loglike: max relative error 1.6e-6 at the MLE and 7.5e-7 at perturbed parameters (same 20 designs).
  • bse against the numerical bse: median 9e-7, max 1.5e-5 (before: 0.18 / 0.35).
  • randhie bse against 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_numdiff compares hessian with approx_hess at fixed parameters and bse at the MLE with the numerical bse, without offset, with offset and with exposure. test_bse of the three ZIP logit classes now uses rtol=1e-4 without atol. 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.

  • closes #xxxx
  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    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.

  • No AI tools were used to develop this pull request.
  • AI tools were 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.

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.
@bashtage
bashtage merged commit f4e3626 into statsmodels:main Nov 11, 2026
14 checks passed
@bashtage

Copy link
Copy Markdown
Member

Thanks.

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
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants