Skip to content

TST: Follow up #10723, more inflation designs in the ZIP hessian test - #10729

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-zip-hessian-followup
Nov 11, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-zip-hessian-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10723 (the analytic Hessian of ZeroInflatedPoisson with logit inflation, xref #7632). Its review left the points below. The code of count_model.py is unchanged.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Changes

  • DOC: Release note: added :pr:10723`` (there is no :issue:, because REF: review and refactor for computational efficiency in ZeroInflated models #7632 is a general refactoring issue that BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723 does not close), and replaced "could be off by more than 10%", which depends on the design, by what was measured. On the code before BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723, `bse` of a Newton fit against the standard errors from a numerical Hessian at the MLE: with simulated data (n = 3000, three seeds) too small by 9.3 to 10.3% for the inflation intercept and the count intercept and by at most 0.7% for the other parameters; with the RandHIE data of the tests, compared with the stored reference values, too small by 0.04 to 0.36% (0.07 to 0.58% for the offset variant). On the fixed code the difference is 0 for the simulations and at most 2.5e-5 for RandHIE. The note now says "about 10% for the two intercepts" in the simulated example and "up to 0.6%" for RandHIE, and that `cov_params`, `conf_int` and the p-values change with `bse`.
  • TST: test_zip_hessian_numdiff had one inflation design (a constant and one regressor, k_inflate = 2 and k_exog = 3), so the layout of the cross block, hess_arr[k_infl:, :k_infl] = hess_arr_infl[:, k_infl:].T, was only exercised for a 2 x 3 block. It is now parametrized over the inflation design: exog_infl=None (constant only, k_inflate = 1), the old design (2), a constant and two regressors (3, a square block that is not symmetric) and exog_infl equal to exog (3, a symmetric block), each with no offset, an offset and an exposure (12 cases, nobs 1000, about 0.5 s in total). The analytic score is checked against a centered numerical gradient of loglike (rtol=1e-6), because the cross block of the Hessian is the derivative of that score.
  • TST: Mutation checks of _hessian_logit and hessian against the 12 cases: dropping the .T fails 9 of them (the three const and the three old-design cases fail with a broadcasting ValueError, the three square non-symmetric ones with wrong values; the three exog cases pass, because there the cross block is symmetric, which a comment in the test says); putting the block back in the upper triangle, flipping its sign and restoring the old factor / pmf instead of * pmf_main / pmf**2 each fail all 12.
  • TST: Provenance of the stored RandHIE references: zero_inflated_poisson_logit and zero_inflated_poisson_offset in results_discrete.py had no note where the numbers come from, and BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723 relies on them at rtol=1e-4. R 4.6.1 with pscl 1.5.9 reproduces them: zeroinfl(y ~ idp + lpi + fmde | lncoins, dist="poisson", link="logit") to 2.1e-5 in bse and 1.4e-5 in the parameters (relative, limited by the stored digits) and the same call with + offset(hlthf) to 7.2e-6 and 1.5e-6. One comment above each entry says so. This does not say that the numbers came from R; it says that R gives them.

Not changed

Tests

  • statsmodels/discrete/tests/test_count_model.py, test_predict.py and test_margins.py (slow tests included): 176 passed, 1 xfailed on Python 3.13; test_count_model.py on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1): 105 passed.
  • ruff check and git diff --check pass.

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 (Claude Sonnet 5.5).
    Used for: reviewing BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723, and writing and checking the follow-up changes.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

bashtage and others added 2 commits November 11, 2026 10:04
…note

The release note for statsmodels#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]>
…test

statsmodels#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 statsmodels#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
bashtage marked this pull request as ready for review November 11, 2026 10:21
@bashtage
bashtage merged commit 27e7475 into statsmodels:main Nov 11, 2026
13 of 14 checks passed
@bashtage
bashtage deleted the wip-zip-hessian-followup branch November 20, 2026 10:57
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.

1 participant