Repository navigation
Closed
Milestone
Description
I have this error and my fit doesn't work. I don't know the reason (same problem in negativebinomial). Stats,model 0.8.0
import pandas as pd
features = ['pp']
df = pd.read_csv('bug.csv')
X = (df[features] - df[features].mean())/df[features].std()
y = df['num'].values
X_sm = sm.add_constant(X[features].copy())
model = sm.Poisson(y, X_sm, ) #
res = model.fit()
Thanks
bug.csv.zip
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels/discrete/discrete_model.py:901: RuntimeWarning: overflow encountered in exp
/ return stats.poisson.cdf(y, np.exp(X))
//Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels/discrete/discrete_model.py:1175: RuntimeWarning: overflow encountered in exp
/ L = np.exp(np.dot(X,params) + exposure + offset)
//Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels/discrete/discrete_model.py:1112: RuntimeWarning: overflow encountered in exp
/ L = np.exp(np.dot(X,params) + offset + exposure)
//Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/stats/_discrete_distns.py:451: RuntimeWarning: invalid value encountered in greater_equal
/ return mu >= 0
//Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels/base/optimizer.py:230: RuntimeWarning: invalid value encountered in greater
/ oldparams) > tol)):
//Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/stats/_distn_infrastructure.py:875: RuntimeWarning: invalid value encountered in greater
/ return (self.a < x) & (x < self.b)
//Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/stats/_distn_infrastructure.py:875: RuntimeWarning: invalid value encountered in less
/ return (self.a < x) & (x < self.b)
//Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/stats/_distn_infrastructure.py:1814: RuntimeWarning: invalid value encountered in less_equal
/ cond2 = cond0 & (x <= self.a)
Activity
denadai2 commentedon Mar 3, 2017
It seems (from my debug) that in _fit_newton, score(oldparams) gives nan everywhere at a certain interaction
josef-pkt commentedon Mar 3, 2017
sounds like the same issue as in #3337
(we get many of those warnings now in the test run, which AFAIR were not printed before some recent version changes.)
Does the optimization converge in your case or not?
denadai2 commentedon Mar 3, 2017
yes :)
josef-pkt commentedon Mar 3, 2017
That looks bad.
converged=Trueis completely misleading.denadai2 commentedon Mar 3, 2017
True that. Let me know how I can help. It seems that the iteration goes well until a point where it goes crazy. In this case iteration 2 :)
josef-pkt commentedon Mar 3, 2017
You could try
model.fit(method='nm', maxiter=5000, maxfun=5000I hope I remember the maxiter and maxfun keywords correctly.
From looking at the numbers in the csv files I guess they are not very close to a Poisson distribution, looking at raw numbers is not very "precise".
Main thing to check is whether GLM with default IRLS fit manages to converge to a reasonable looking result in this case.
(for the future: In the next release we should be able to use better trust region newton method provided by scipy, which might manage to converge in some of these cases.)
Given that you are already standardizing the explanatory variable, scaling itself cannot be a source for the convergence failure anymore.
denadai2 commentedon Mar 3, 2017
I'm using a Poisson fit just because it is simpler to see whether there is a bug (imho). I'll use a Negative Binomial, which has the same np.exp error.
However, with maxiter + maxfun I have the same warnings + it seems it converges but the llnull is nan:
Is it normal?
PS: is standardization a problem?
josef-pkt commentedon Mar 3, 2017
ll-null is nan is also a convergence failure, but there is no way for users to affect it, #3476
I converged actually pretty fast, 89 iterations is good for "nm".
The problem I think is that the gradient in your example is very large, and most gradient based optimization methods jump out of the range that doesn't result in overflow. This happens with our newton and it happened with bfgs and several others. bfgs has been fixed in the latest scipy releases, so it should work better now.
My latest take on this is #3521. our newton works well in nice cases, but we need something more convenient for the difficult optimization cases.
josef-pkt commentedon Mar 3, 2017
@denadai2 Is the data in your csv file for public use, i.e. can we include it in statsmodels as a test case?
Also, when you have a converged result from Poisson, then you can use it also as a start_params for NegativeBinomial, after adding an initial value for the dispersion coefficient, e.g. 0.5.
josef-pkt commentedon Mar 3, 2017
cross-link to list of optimization failures #1649
denadai2 commentedon Mar 3, 2017
Yes you can use the csv as test case :)
However I have some doubts/issues:
PS: u deserve beers :D
josef-pkt commentedon Mar 3, 2017
about ll-null: there is currently no workaround to prevent the convergence failure and nan. In the mailing list thread that is linked in #3476 describes a bit how users could compute them but no fully coded recipe.
Note,
converged=Trueas reported by the scipy optimizers and all nans is a serious bug, because it is misleading. The other bugs require ongoing efforts to reduce convergence failures in "not so nice" cases.josef-pkt commentedon Mar 4, 2017
brief check of the example
in the Poisson example
with scipy 0.17.0 bfgs converges
with statsmodels master using recently merged method="minimize" option
To get llnull, I changed the method to "nm" in
DiscreteResults.llnulldenadai2 commentedon Mar 4, 2017
Ok I'm using last version (master) of statsmodels and scipy 0.18.1.
Poisson with const + pp works IF I modify DiscreteResults.llnull.
Now, before adding all the variables I have, we move to the Negative Binomial:
I add
And the result is that the llnullmodel does not fit (even with the method='nm')
I naively try also to put method="minimize", min_method='dogleg' to fit llnull in DiscreteResults and I have:
denadai2 commentedon Mar 4, 2017
To add more info for you: if I directly use
I have
josef-pkt commentedon Mar 4, 2017
I will try to look at it later today, but I'm going on a 4 day skiing trip tomorrow and I don't know if I will have time until later next week.
What I was trying to do is to add access to the llnull options
and compute the constant for the llnull model directly
here's a gist with the notebook https://gist.github.com/josef-pkt/b45d0b5ceb3b692c17af5f77a657e639
For trying out the llnull options I was just working from the "outside" which is messy, and needs proper access like
set_llnull_options.denadai2 commentedon Mar 4, 2017
thx
denadai2 commentedon Mar 5, 2017
What if llnull() passes "good" start_params from the full model, to fit the null model?
denadai2 commentedon Mar 10, 2017
in def llnull(self) I solved with:
BUT I'm not sure it crashes with other models, so I can't push it. moreover, maybe it is just a silly trick
josef-pkt commentedon Mar 10, 2017
do you use mean exog or mean of endog?
link.inverse(endog.mean())is the right estimate for a constant only, but we need to handle offset and exposure, and MNLogit has multivariate endog which might need to be special cased.start = [np.mean(model.exog)]might only work by chance in the example.denadai2 commentedon Mar 21, 2017
endog! anyway, I'll try to push it ASAP, then you can help me testing it :)