Repository navigation
Description
edit same bug as #4757
Hi,
I am modelling a discrete skewed distribution with several of the available methods in statsmodels, namely: Poisson, Negative Binomial, Zero Inflated Poisson, and Zero Inflated Negative Binomial. What I am doing is training with 60% of my samples, and testing the model with the remaining 40%. For this 40%, I obtain the RMSE to have a performance metric for each method.
There are no problems with the Poisson and the Negative Binomial: I use the .fit() method on my training samples, and the .predict() method on the test samples. However, I observed that for the Zero-Inflated models, I cannot predict for the testing set, as you can see in this snippet:
print("Model: Zero Inflated Poisson")
zip_mod = sm.ZeroInflatedPoisson(ytrain, xtrain).fit(method="newton", maxiter=50)
**zip_mean_pred = zip_mod.predict(xtest)**
zip_ppf_obs = stats.poisson.ppf(q=0.95, mu=zip_mean_pred)
zip_rmse = np.sqrt(mean_squared_error(ytest, zip_ppf_obs))
print("RMSE Zero-Inflated Poisson", zip_rmse)
print("Model: Zero Inflated Neg. Binomial")
zinb_mod = sm.ZeroInflatedNegativeBinomialP(ytrain, xtrain).fit(method="newton", maxiter=50)
zinb_pred = zinb_mod.predict(xtest)
zinb_rmse = np.sqrt(mean_squared_error(ytest, zinb_pred))
print("RMSE Zero-Inflated Negative Binomial: ", zinb_rmse)
The error that I get says:
Traceback (most recent call last):
File "/home/irene/.../four_models.py", line 569, in <module>
test_poi_nb_zip_zinb_tiny_subset(meta_m, m)
File "/home/irene/.../four_models.py", line 325, in test_poi_nb_zip_zinb_tiny_subset
**zinb_pred = zinb_mod.predict(xtest)**
File "/home/irene/.conda/envs/defaultpy34/lib/python2.7/site-packages/statsmodels/base/model.py", line 854, in predict
**kwargs)
File "/home/irene/.conda/envs/defaultpy34/lib/python2.7/site-packages/statsmodels/discrete/count_model.py", line 444, in predict
prob_zero = (1 - prob_main) + prob_main * np.exp(llf)
**ValueError: operands could not be broadcast together with shapes (480,) (320,)**
Perhaps I am doing something wrong, but I do not understand why these two methods require a test sample for the predict method as big as the one used in the fitting. Is there any way to pass to these methods a test array with a different size than the training? I tried training and testing with the same dataset and it works.
Thanks a lot for all the work! :-)
Activity
Irene-GM commentedon Aug 21, 2018
I forgot to mention that the same procedure that you see here for the zero inflated models is applied above it to the normal poisson and negative binomial with not problem. Thanks.
josef-pkt commentedon Aug 21, 2018
Hi, there is a good chance that this is a bug. Given that the models are new, there might be cases that are not included in the test suite and that could be buggy.
Can you make a reproducible example?
It can have random number, just so I can reproduce the shape mismatch.
(temporarily prio-high, at least until I know what's going on.)
josef-pkt commentedon Aug 21, 2018
Based on a quick look at the source.
If exog_infl is not specified in predict, then the one of the model is used which will conflict with the predict exog.
Try to specify
exog_infl.In the case of constant inflation probability as in your case, you need
exog_infl=np.ones(len(exog_predict))josef-pkt commentedon Aug 21, 2018
the same is true for
exposure, which also uses the model exposure if it is available but no predict exposure is specified.Irene-GM commentedon Aug 21, 2018
Thanks for the quick response! I made the following dummy code causing the error I was mentioning, I hope it is useful to trace the bug in the code.
Irene-GM commentedon Aug 21, 2018
If I add to the
.predict()methods theexog_inflas follows:I get the following error:
The
.shapeof thenp.ones(len(xtest))is(320,)just in case it is important.josef-pkt commentedon Aug 21, 2018
I just tried it. exog_infl in predict needs to be 2dim
zip_mean_pred = zip_mod.predict(xtest, exog_infl=np.ones((len(xtest), 1)))this works, but the example has convergence problems or hessian inversion problem if I use method='nm' as optimizer. (The data doesn't generate anything close to ZIP or ZINB)
general aside:
Estimation of a NegativBinomial(P) model and ZI variant needs overdispersion relative to Poisson in the data. If there is no overdispersion, then the loglike and derivative computation end with a zero division. Poisson is the limit of NegativeBinomial as overdispersion disappears, but computationally we need to stay away from that boundary case.
GeneralizedPoisson and it's ZI version allow for overdispersion, no excess dispersion and a small amount of underdispersion, i.e. the Poisson special case is in the interior of the GP parameter space.
(In the zi version excess dispersion refers to the non-zero inflated part.)
Irene-GM commentedon Aug 22, 2018
I tried your fix and it works!
It gives some warnings as you said, like this:
But it processes my testing dataset. Tried the "nm" and "newton" optimizers.
So now i can compute the RMSE:
Thanks for the super fast reaction, much appreciated!
PS - Regarding this "overdispersion", is there any paper/manual you recommend to learn more about it? :-)
josef-pkt commentedon Aug 22, 2018
If you do a google search for "overdispersion" or "overdispersion poisson", then you will find some introductory explanation,
e.g. I never read this
http://biometry.github.io/APES//LectureNotes/2016-JAGS/Overdispersion/OverdispersionJAGS.html
Zero-inflation can cause overdispersion if we just estimate a poisson model. If we estimate a zero-inflated model, then the zero inflation is handled separately but there can still be a (over-, under-) dispersion issue for the not inflated part.
I don't have directly a reference because I haven't looked at it recently, and most of my references are more technical for specific issues (e.g. what we don't have yet is testing zero-inflated Poisson versus zero-inflated NegativeBinomial)
Here are links to two of my notebooks which are a mix of something I started to write for "public" consumption, and then often ended up investigating some specific problems. Those were written when I reviewed and finished up the count model PR and I'm not sure those notebooks are up to date with current statsmodels.
https://gist.github.com/josef-pkt/c932904296270d75366a24ee92a4eb2f
https://gist.github.com/josef-pkt/b528c099592bdb0239e6e750d365f7fc