Skip to content

DOC: Follow up #10736 on the truncated count score, with tests - #10750

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-truncated-score-followup
Nov 16, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-truncated-score-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10736 (the score of the truncated count models used the default p for the probability of the truncated counts). Its review left the points below. The code of truncated_model.py is unchanged.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    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 every p except `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_fit asserted converged and 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 (optim with L-BFGS-B and then Nelder-Mead on the log-likelihood written in the test comment, with numDeriv gradients): llf -641.290608843211 to rtol=1e-8 (the fit matches to 8e-12) and params to rtol=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_numerical also asserts that p reaches 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 default p.
  • TST: test_score_numerical_p gains use_offset and four cases with an offset (rng.normal(scale=0.2)): the submodels that build the truncated probabilities in score_obs need the offset as well as p and exposure. The rtol=1e-6 has 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_counts is added.

Mutation checks (the tree mutated, each restored afterwards): removing p=p in HurdleCountModel._initialize fails the hurdle test; removing **kwds in score_obs fails 12 tests, including the default fit and the hurdle test; removing the offset= line in score_obs fails the four new offset cases and the two existing tests TestTruncatedNegBinSt::test_offset and TestTruncatedLFPoissonSt::test_offset.

Not changed

  • fit and fit_regularized build the model for the start values without p (the start values are only start values), score_obs reads the init keywords on every call, and _prob_nonzero and the predicted mean are wrong for p other than 2 and ignore pzero (a separate defect in predict, 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 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: 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

bashtage and others added 2 commits November 15, 2026 18:45
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
bashtage marked this pull request as ready for review November 15, 2026 22:52
@bashtage
bashtage merged commit e75f302 into statsmodels:main Nov 16, 2026
10 checks passed
@bashtage
bashtage deleted the wip-truncated-score-followup branch November 20, 2026 10:56
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