Skip to content

BUG: Fix exposure in truncated and zero-inflated count models - #10505

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
devsummar:fix-8842-exposure
Nov 22, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
devsummar:fix-8842-exposure

Conversation

@devsummar

Copy link
Copy Markdown
Contributor

The offset part of #8842 was fixed in #8845, but exposure is still broken in the truncated count models. CountModel stores log(exposure), and the truncated models passed that to their main model, which logged it again. For exposure < 1 the loglike is nan, and TruncatedLFPoisson silently returns wrong params. For NB and GP, fitting with exposure raises "truth value of an array is ambiguous" because predict does elif exposure != 0, and the which="prob" branch calls predict again with the logged exposure. The zero-inflated NB/GP models hit the same thing in predict(which="prob").

Changes:

  • truncated models pass exposure on the original scale to model_main
  • NBP/GP predict always log a user-supplied exposure (same as Poisson), and which="prob" reuses linpred
  • ZI NB/GP _predict_prob pass exp(exposure), since their exposure is already logged

Tests check that exposure=e and offset=log(e) give the same loglike, score, params and bse for all three truncated models, check the zero-truncated Poisson loglike against the closed form, and cover array exposure in NBP/GP predict and ZI predict(which="prob"). All new tests fail on main.

One behaviour change: an explicit exposure=0 in NBP/GP predict now gives log(0) like Poisson, instead of being treated as no exposure.

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): Grok Bot.
    Used for: debugging, drafting the implementation and tests.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NegativeBinomialP and GeneralizedPoisson fixes look right. ZeroInflatedPoisson has the same problem, one test can flake, and a release note is missing (it should also mention that exposure=0 now gives log(0), as in Poisson).

"model_class",
[sm.ZeroInflatedNegativeBinomialP, sm.ZeroInflatedGeneralizedPoisson],
)
def test_predict_prob_exposure(model_class):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZeroInflatedPoisson._predict_prob (count_model.py, the self.model_main.predict(params_main, exog, offset=offset) line) does not pass the exposure either, so predict(which="prob") differs from the offset=np.log(exposure) model by about 0.27. Please fix it and add ZeroInflatedPoisson to this test.

warnings.simplefilter("ignore", ConvergenceWarning)
res_offset = mod_offset.fit(maxiter=300, disp=0)
res_exposure = mod_exposure.fit(maxiter=300, disp=0)
assert_allclose(res_exposure.params, res_offset.params, rtol=1e-6)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For TruncatedLFGeneralizedPoisson the BFGS fit does not converge in my runs, so comparing the parameters of two fits at rtol=1e-6 can flake across platforms. Please keep the log-likelihood and score comparisons and compare the fits only for Poisson and NegativeBinomialP.

ZeroInflatedPoisson._predict_prob called model_main.predict without the
exposure, so predict(which="prob") did not match the offset=log(exposure)
model. It now passes exp(exposure) like the ZI NB/GP versions, and
ZeroInflatedPoisson is added to test_predict_prob_exposure.

The bfgs fit of TruncatedLFGeneralizedPoisson does not converge on the test
data, so the fitted params are now only compared for Poisson and
NegativeBinomialP. loglike and score are still compared for all three.

Add a release note, including that exposure=0 in NBP/GP predict now gives
log(0) like Poisson.
@devsummar

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Pushed the changes:

  • ZeroInflatedPoisson._predict_prob now passes the exposure too (same exp(exposure) handling as the ZI NB/GP ones), and I added ZeroInflatedPoisson to test_predict_prob_exposure. Those three are the only ZI models, so nothing else needed the same fix.
  • Split the truncated test: loglike and score are still compared for all three models, the fit comparison now only runs for Poisson and NegativeBinomialP.
  • Added a release note to version0.15.1.rst, including the exposure=0 -> log(0) change in NBP/GP predict.

Also rebased on main to fix the conflict in truncated_model.py.

@bashtage
bashtage merged commit c5567a1 into statsmodels:main Nov 22, 2026
12 of 13 checks passed
@bashtage

Copy link
Copy Markdown
Member

Thanks.

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.

BUG: offset in truncated model raises valueerror

2 participants