Repository navigation
BUG: Fix exposure in truncated and zero-inflated count models - #10505
Conversation
7929951 to
8bb5347
Compare
bashtage
left a comment
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
8bb5347 to
97c7a35
Compare
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.
97c7a35 to
fbccf7a
Compare
|
Thanks for the review! Pushed the changes:
Also rebased on main to fix the conflict in truncated_model.py. |
|
Thanks. |
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:
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.
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.