Repository navigation
DOC: Follow up #10736 on the truncated count score, with tests - #10750
Merged
bashtage merged 3 commits intoNov 16, 2026
Merged
Conversation
The bullet for the truncated count score of statsmodels#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 statsmodels#10723 and statsmodels#10737 for the Hessians of the discrete models. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Follow-up to statsmodels#10736. The default fit test of the truncated generalized Poisson only checked that the optimizer had stopped, which is true for any score. It now compares llf and params with the maximum of the same likelihood computed in R (own log-likelihood, numDeriv, optim), with the R call in a comment. The test of the hurdle model passed also if the model ignored p, because loglike and score were then both consistent with p=2, so it checks that the truncated model of the hurdle has p=1. The score test of the truncated models gets cases with an offset, which the models for the truncated counts have to receive, and a comment for the tolerance (finite difference error). Fix the blank lines before the helper. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
marked this pull request as ready for review
November 15, 2026 22:52
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 #10736 (the score of the truncated count models used the default
pfor the probability of the truncated counts). Its review left the points below. The code oftruncated_model.pyis unchanged.NumPy's guide.
Changes
DOC:Release note: moved from the middle to the end of the 'Generalized linear models and discrete models' bug-fix list,:pr:10736`` added, and rewritten to say what a user sees. The score was incorrect for everypexcept `p=1` of the generalized Poisson and `p=2` of the negative binomial model (this includes the default `p=2` of `TruncatedLFGeneralizedPoisson`, and `HurdleCountModel` with `dist="negbin"` and `p` other than 2), `fit` stopped before the maximum with a `ConvergenceWarning`, and so `params`, `bse` and `llf` of these models change for valid input. Measured on the code before BUG: Pass p to the submodels in the truncated count score #10736 and now (3000 simulated negative binomial counts, zeros dropped by the model): the default `TruncatedLFGeneralizedPoisson` `alpha` changes from 0.0989 to 0.2192 and `llf` from -3582.06 to -3554.15, and the `ConvergenceWarning` is gone. Fits for the generalized Poisson with `p=1`, the negative binomial with `p=2` and the hurdle model with `p=2` are identical before and now. The hurdle score was wrong for `p=1` and `p=1.5` (largest absolute error against the numerical gradient 34.4 and 18.8) and is exact to about 2e-7 for all of them now. The note stays under 'Bug Fixes' next to BUG: Fix cross derivatives in ZeroInflatedPoisson hessian #10723 and BUG: Fix NegativeBinomialP hessian for p other than 1 and 2 #10737, although `AGENTS.md` puts changed results under 'Behavior changes'; move it if you prefer.TST:test_truncated_genpoisson_default_fitassertedconvergedand a small numerical gradient, that is where the optimizer stops. It now compares with the maximum of the zero-truncated generalized Poisson (Consul-Famoye,p=2) log-likelihood computed in R 4.6.1 (optimwith L-BFGS-B and then Nelder-Mead on the log-likelihood written in the test comment, withnumDerivgradients):llf-641.290608843211 tortol=1e-8(the fit matches to 8e-12) andparamstortol=1e-3, because the log-likelihood is flat there and both optimizers stop at a gradient of about 3e-5 (the fit matches R to 6e-5 in the parameters).TST:test_hurdle_negbin_p1_score_numericalalso asserts thatpreaches the model of the truncated part (model.model2.model_main.parameterization == 1), because the score and the log-likelihood are consistent with each other also for the defaultp.TST:test_score_numerical_pgainsuse_offsetand four cases with an offset (rng.normal(scale=0.2)): the submodels that build the truncated probabilities inscore_obsneed the offset as well aspandexposure. Thertol=1e-6has its reason in a comment: it is the accuracy of the centered finite difference, and the worst elementwise relative error over all 12 cases is 2.2e-7. A missing blank line before_simulate_negbin_countsis added.Mutation checks (the tree mutated, each restored afterwards): removing
p=pinHurdleCountModel._initializefails the hurdle test; removing**kwdsinscore_obsfails 12 tests, including the default fit and the hurdle test; removing theoffset=line inscore_obsfails the four new offset cases and the two existing testsTestTruncatedNegBinSt::test_offsetandTestTruncatedLFPoissonSt::test_offset.Not changed
fitandfit_regularizedbuild the model for the start values withoutp(the start values are only start values),score_obsreads the init keywords on every call, and_prob_nonzeroand the predicted mean are wrong forpother than 2 and ignorepzero(a separate defect inpredict, not in the score).Tests
statsmodels/discrete/tests/test_truncated_model.py: 87 passed, 1 xfailed on Python 3.13, and 88 passed on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1; the xfail passes there);test_predict.py: 30 passed on Python 3.13.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: Pass p to the submodels in the truncated count score #10736, 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