You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't have time to check right now, maybe later today.
Just some general comments based on my experience with this:
Check the scaling of variables. There should not be large values in absolute terms because of the exponential transformation. Also, try to start with method='nm' and if you get something closer to convergence, use it with method='bfgs'.
I was struggling for a while with this, but it is still not very robust in some cases with Poisson and NegativeBinomial.
There was also a problem in some recent scipy versions where 'bfgs' did not have a robust enough step-size choice. That has been fixed in scipy, AFAIR.
In some cases using GLM NegativeBinomial with irls and fixed nbin shape parameter works better for convergence.
(One possible solution that seems to help in many cases is to standardize the exog. But the automatic support for this is still in prototype state.)
to clarify: I ran into many convergence issues with NegativeBinomial, but AFAIR, I haven't seen that specific traceback before, usually I got nans, IIRC. (or maybe I had displaying the zero division warnings turned off.)
Can you try to see if Poisson works?
In the current master version (I don't remember when I changed this), I use the Poisson estimate for the start_params, but it also failed on badly scaled exog.
Poisson doesn't get the extra complication from the nbin dispersion parameter, but can still run into exp overflow problems.
However, given the traceback llf = coeff + size*np.log(prob) + endog*np.log(1-prob)
I think we need an additional safeguard for prob = 0 or 1. I didn't look at the code yet where this happens.
(a suspicion:
the prob in the traceback looks like a perfect separation/prediction corner case, which I've never really seen before, #1391, and maybe #1512
Similar behavior will apply to NegativeBinomial as to Poisson
)
Does the last summary() for NegativeBinomial mean that you managed to get it to converge?
The results look good and close to the Poisson mean parameters.
BTW: model.fit(cov_type='HC0') will produce correct standard errors for Poisson with dispersion.
(It corrects for general heteroscedasticity rather than just for dispersion.)
NB converged since the beginning. I don't understand all the warnings: why? Bug?
(I explain it better: I'm trying to publish some results and I'd like to be sure about my results. Moreover, I'm trying to contribute)
where can I see the cov_types documentation? Very interesting! Thanks
Ok, then I misunderstood.
It is possible that during the optimization some parameter values are tried out that produce invalid results. However, several of the scipy optimizers are robust to some of these problems. So, there might be warnings during the optimization even though the final result is good. If the message is "converged", then the optimization didn't find any problems at the final solution.
One way to cross-check the results is to look at the score, i.e. first order condition of the optimization, (or moment condition or estimating equation.
e.g. model.score(results.params)
which should be close to zero.
Usually gradient or hessian based method like newton have better final local convergence, but might get thrown off away from the neighborhood of the optimum.
Note, score is a method of the model, but only the result instance knows the estimated parameters.
I'm looking at this again. I hadn't paid attention to the start_params in the original example.
alpha (i.e. params[-1]) cannot be zero in NegativeBinomial(P). The loglikelihood and derivatives cannot be evaluated with alpha equal to zero or in the close neighborhood of zero.
So the start_params for alpha has to be strictly positive, and the data needs to be overdispersed relative to Poisson so that parameter estimate don't try to converge to alpha <= 0.
Note, alpha in NegativeBinomial does not use a log-link, params[-1] is the actual alpha.
NegativeBinomialP has a use_transparams fit option, but, AFAIR, that is not fully implemented as user option.
(not verified, and does not work with all optimizers)
Activity
josef-pkt commentedon Dec 22, 2016
I don't have time to check right now, maybe later today.
Just some general comments based on my experience with this:
Check the scaling of variables. There should not be large values in absolute terms because of the exponential transformation. Also, try to start with
method='nm'and if you get something closer to convergence, use it withmethod='bfgs'.I was struggling for a while with this, but it is still not very robust in some cases with Poisson and NegativeBinomial.
There was also a problem in some recent scipy versions where 'bfgs' did not have a robust enough step-size choice. That has been fixed in scipy, AFAIR.
In some cases using GLM NegativeBinomial with irls and fixed nbin shape parameter works better for convergence.
(One possible solution that seems to help in many cases is to standardize the exog. But the automatic support for this is still in prototype state.)
josef-pkt commentedon Dec 22, 2016
to clarify: I ran into many convergence issues with NegativeBinomial, but AFAIR, I haven't seen that specific traceback before, usually I got nans, IIRC. (or maybe I had displaying the zero division warnings turned off.)
denadai2 commentedon Dec 22, 2016
I tried also this, but of course I have warnings. It's not clear to me how to perform a good regression and be "sure" to have good results
(thanks)
josef-pkt commentedon Dec 22, 2016
Can you try to see if Poisson works?
In the current master version (I don't remember when I changed this), I use the Poisson estimate for the start_params, but it also failed on badly scaled exog.
Poisson doesn't get the extra complication from the nbin dispersion parameter, but can still run into exp overflow problems.
However, given the traceback
llf = coeff + size*np.log(prob) + endog*np.log(1-prob)I think we need an additional safeguard for prob = 0 or 1. I didn't look at the code yet where this happens.
josef-pkt commentedon Dec 22, 2016
(a suspicion:
the
probin the traceback looks like a perfect separation/prediction corner case, which I've never really seen before,#1391, and maybe #1512
Similar behavior will apply to NegativeBinomial as to Poisson
)
denadai2 commentedon Dec 22, 2016
Poisson works without warnings, but I have to say that there is overdispersion so I need the NB model...
NB (with warnings):
josef-pkt commentedon Dec 22, 2016
Does the last summary() for NegativeBinomial mean that you managed to get it to converge?
The results look good and close to the Poisson mean parameters.
BTW:
model.fit(cov_type='HC0')will produce correct standard errors for Poisson with dispersion.(It corrects for general heteroscedasticity rather than just for dispersion.)
denadai2 commentedon Dec 22, 2016
NB converged since the beginning. I don't understand all the warnings: why? Bug?
(I explain it better: I'm trying to publish some results and I'd like to be sure about my results. Moreover, I'm trying to contribute)
where can I see the cov_types documentation? Very interesting! Thanks
josef-pkt commentedon Dec 22, 2016
Ok, then I misunderstood.
It is possible that during the optimization some parameter values are tried out that produce invalid results. However, several of the scipy optimizers are robust to some of these problems. So, there might be warnings during the optimization even though the final result is good. If the message is "converged", then the optimization didn't find any problems at the final solution.
One way to cross-check the results is to look at the score, i.e. first order condition of the optimization, (or moment condition or estimating equation.
e.g.
model.score(results.params)which should be close to zero.
Usually gradient or hessian based method like
newtonhave better final local convergence, but might get thrown off away from the neighborhood of the optimum.Note,
scoreis a method of the model, but only theresultinstance knows the estimated parameters.I don't find the docstring for the
cov_typeright now.A draft for a more elaborate explanation is here https://github.com/josef-pkt/misc/blob/robust_cov/notebooks/doc_robust_cov.ipynb
There were still some changes after 0.6.1, so I'm don't remember exactly what was available then. Also, while writing the
doc_robust_cov.ipynbI worked through some API/naming inconsistencies that are fixed in master, but not reflected in that notebook.The original version was a results method, but now the recommended and safer pattern is as a
fitkeyword. All models have mostly the same options, except HC1, HC2 and HC3 are only for OLS/WLS. In the other models all HCx are HC0.http://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.RegressionResults.get_robustcov_results.html#statsmodels.regression.linear_model.RegressionResults.get_robustcov_results
denadai2 commentedon Dec 24, 2016
super thanks
josef-pkt commentedon Jan 1, 2017
I'm labeling this as FAQ issue.
I'm keeping the bug label for now to check the
0 log 0case in NegativeBinomial.denadai2 commentedon Mar 3, 2017
thanks
bashtage commentedon May 10, 2019
Only a problem if user provides start params, otherwise works without warnings.
josef-pkt commentedon Dec 21, 2021
I'm looking at this again. I hadn't paid attention to the start_params in the original example.
alpha(i.e. params[-1]) cannot be zero in NegativeBinomial(P). The loglikelihood and derivatives cannot be evaluated with alpha equal to zero or in the close neighborhood of zero.So the start_params for alpha has to be strictly positive, and the data needs to be overdispersed relative to Poisson so that parameter estimate don't try to converge to alpha <= 0.
Note, alpha in NegativeBinomial does not use a log-link,
params[-1]is the actual alpha.NegativeBinomialP has a
use_transparamsfit option, but, AFAIR, that is not fully implemented as user option.(not verified, and does not work with all optimizers)