Zero Inflated Negative Binomial model does not converge

Open
#9,156 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
numpy, pandas, python
Domain
data

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from statsmodels/statsmodels

All issues in statsmodels/statsmodels

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.