Repository navigation
TST: Follow up #10723, more inflation designs in the ZIP hessian test - #10729
Merged
Merged
Conversation
…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]>
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.
Follow up to #10723 (the analytic Hessian of
ZeroInflatedPoissonwith logit inflation, xref #7632). Its review left the points below. The code ofcount_model.pyis unchanged.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_numdiffhad one inflation design (a constant and one regressor,k_inflate = 2andk_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) andexog_inflequal toexog(3, a symmetric block), each with no offset, an offset and an exposure (12 cases, nobs 1000, about 0.5 s in total). The analyticscoreis checked against a centered numerical gradient ofloglike(rtol=1e-6), because the cross block of the Hessian is the derivative of that score.TST:Mutation checks of_hessian_logitandhessianagainst the 12 cases: dropping the.Tfails 9 of them (the threeconstand the three old-design cases fail with a broadcastingValueError, the three square non-symmetric ones with wrong values; the threeexogcases 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/ pmfinstead of* pmf_main / pmf**2each fail all 12.TST:Provenance of the stored RandHIE references:zero_inflated_poisson_logitandzero_inflated_poisson_offsetinresults_discrete.pyhad no note where the numbers come from, and BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723 relies on them atrtol=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 inbseand 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
ZeroInflatedGeneralizedPoissonandZeroInflatedNegativeBinomialP(numerical Hessian), the three copies of thetest_bseoverride in the test classes, and thecloses #xxxxline of the body of BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723.Tests
statsmodels/discrete/tests/test_count_model.py,test_predict.pyandtest_margins.py(slow tests included): 176 passed, 1 xfailed on Python 3.13;test_count_model.pyon Python 3.10 (NumPy 1.23.5, SciPy 1.8.1): 105 passed.ruff checkandgit diff --checkpass.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 (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