Skip to content

BUG: Pass p to the submodels in the truncated count score - #10736

Merged
bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/truncated-score-p
Nov 15, 2026
Merged

bashtage merged 1 commit into
statsmodels:mainfrom
bhaskargurram-ai:fix/truncated-score-p

Conversation

@bhaskargurram-ai

@bhaskargurram-ai bhaskargurram-ai commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

TruncatedLFGeneric.score_obs creates one untruncated model for each truncated count to get the score of the truncation probability, but it does not pass p. These models use the default of their class (p=1 for GeneralizedPoisson, p=2 for NegativeBinomialP), while loglike uses the p of the model. So score is wrong for

  • TruncatedLFGeneralizedPoisson with its default p=2 (and any p other than 1),
  • TruncatedLFNegativeBinomialP with p other than 2,
  • HurdleCountModel(dist="negbin", p=...) with p other than 2.

With 3000 simulated NB2 counts (zeros dropped), TruncatedLFGeneralizedPoisson(endog, exog).fit() stops with a ConvergenceWarning at llf -3582.06 and alpha 0.099. The maximum, found with Nelder-Mead and Powell on loglike only, is at llf -3554.15 with alpha 0.219. For TruncatedLFNegativeBinomialP(p=1) it is -3566.23 instead of -3555.57. The relative error of score against the numerical derivative of loglike was between 0.5 and 2.8 for the affected truncated models.

The fix passes the p of model_main to these models. Afterwards score agrees with approx_fprime of loglike to 1.2e-7 or better for GP and NBP with p = 1, 1.5, 2, truncation 0 and 1, and for the hurdle model with p=1, and the default fits reach the same llf and params as the derivative-free optimization.

Tests: test_score_numerical_p (GP and NBP, p = 1, 1.5, 2, truncation 0 and 1), test_hurdle_negbin_p1_score_numerical and test_truncated_genpoisson_default_fit. 8 of the 10 fail on main; GP with p=1 and NBP with p=2 pass on main, as expected.

#10505 changes the exposure= argument of the same constructor call. Whichever is merged second needs a one-line rebase there.

  • 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.

TruncatedLFGeneric.score_obs creates one untruncated model per
truncated count to get the score of the truncation probability, but it
did not pass p, so these models used the default p of the untruncated
class. The score was wrong for TruncatedLFGeneralizedPoisson with its
default p=2, for TruncatedLFNegativeBinomialP with p other than 2 and
for HurdleCountModel with dist="negbin" and p other than 2, and fit
stopped early with a ConvergenceWarning.

Pass the p of model_main to these models. The score now agrees with
the numerical derivative of loglike.
@bhaskargurram-ai
bhaskargurram-ai marked this pull request as draft November 11, 2026 15:28
@bhaskargurram-ai
bhaskargurram-ai marked this pull request as ready for review November 11, 2026 15:38
@bashtage

Copy link
Copy Markdown
Member

Thanks.

@bashtage
bashtage merged commit a049d9e into statsmodels:main Nov 15, 2026
14 of 15 checks passed
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]>
bashtage added a commit that referenced this pull request Nov 16, 2026
Follow-up to #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 added a commit that referenced this pull request Nov 16, 2026
DOC: Follow up #10736 on the truncated count score, with tests
bashtage added a commit that referenced this pull request Nov 16, 2026
The release note of #10744 named only cov_type="cluster". Every covariance
type that sets df_resid_inference changes: "cluster" (also two-way
clustering), "hac-panel" and "hac-groupsum" for all models and "cluster-crv3"
and "cluster-jk" for linear models, unless df_correction=False. With
use_t=False nothing changes. Checked with seeded OLS, GLM, Logit and Poisson
fits with 10 clusters on main before and after #10744.

The entry stays in Bug Fixes with the neighbouring #10742, #10737 and #10736.

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