Zero Inflated Negative Binomial model does not converge
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the supplied Python example and start at ZeroInflatedNegativeBinomialP.fit, following the warning paths in discrete_model.py and count_model.py. Determine why optimization produces invalid values or fails to converge across seeds; done means the behavior is understood and the reported convergence problem has a verified resolution or regression case.
Written by the indexing model from the issue text.
Description
hi!
I would like to refer to @jfhawkin comment and ask if you could look at the convergence of ZINB model because it seems to not be able to converge for zero-inflated count data.
Here is a small example I made:
import pandas as pd
import numpy as np
import statsmodels.api as sm
from scipy.stats import nbinom
### Generate random data
size = 100 # size of the data
n = 10 # number of successes for negative binomial
p = 0.5 # probability of success for negative binomial and Poisson
zero_prob = 0.5 # probability of zero for zero-inflated distributions
zinb_data = nbinom.rvs(n, p, size=size)
zinb_data[np.random.random(size) < zero_prob] = 0
df = pd.DataFrame({
'nb': np.nan,
'zinb': zinb_data,
})
x = df['zinb']
intercept = sm.add_constant(np.ones(len(x)))
mle_zinb = sm.ZeroInflatedNegativeBinomialP(x, intercept).fit(method='bfgs', maxiter=5000, gtol=1e-12)
In my tests, I've noticed that depending on the seed sometimes model is able to converge.
I use this setting of the model because I'm trying to replicate the result from zeroinfl() method in R.
I appreciate your time, please, let me know if the example is not clear, I would be happy to explain it better.
Thank you!
OUTPUT:
gtol=1e-12)= sm.ZeroInflatedNegativeBinomialP(x, intercept).fit(method='bfgs', maxiter=5000
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in log
a1 * np.log(a1) + y * np.log(mu) -
/python3.10/site-packages/statsmodels/discrete/count_model.py:167: RuntimeWarning: invalid value encountered in log
llf[zero_idx] = (np.log(w[zero_idx] +
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: invalid value encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:4325: RuntimeWarning: overflow encountered in exp
return np.exp(linpred)
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in multiply
a1 * np.log(a1) + y * np.log(mu) -
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: divide by zero encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3975: RuntimeWarning: invalid value encountered in multiply
dparams = (a4 * dgterm -
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in log
a1 * np.log(a1) + y * np.log(mu) -
python3.10/site-packages/statsmodels/discrete/count_model.py:167: RuntimeWarning: invalid value encountered in log
llf[zero_idx] = (np.log(w[zero_idx] +
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: invalid value encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: invalid value encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in log
a1 * np.log(a1) + y * np.log(mu) -
python3.10/site-packages/statsmodels/discrete/count_model.py:167: RuntimeWarning: invalid value encountered in log
llf[zero_idx] = (np.log(w[zero_idx] +
Warning: Desired error not necessarily achieved due to precision loss.
Current function value: nan
Iterations: 16
Function evaluations: 127
Gradient evaluations: 127
python3.10/site-packages/statsmodels/base/model.py:595: HessianInversionWarning: Inverting hessian failed, no bse or cov_params available
warnings.warn('Inverting hessian failed, no bse or cov_params '
python3.10/site-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
warnings.warn("Maximum Likelihood optimization failed to "
- Dominant language
- Python
- Stars
- 11.6k
- Forks
- 3.6k
- Avg merge
- 7h 37m
- Merged PRs (30d)
- 96
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from statsmodels/statsmodels
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
statsmodels/statsmodels#10271 ·
-
type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
statsmodels/statsmodels#10269 ·
-
Documentation
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
statsmodels/statsmodels#10266 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
statsmodels/statsmodels#9627 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
statsmodels/statsmodels#9293 · 1 comment ·
All issues in statsmodels/statsmodels
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100