Repository navigation
Description
Describe the bug
When running statsmodels.stats.rates.test_poisson_2indep on data with count2=0, the function raises a ZeroDivisionError instead of computing the test statistics.
Code Sample, a copy-pastable example if possible
>>> from statsmodels.stats.rates import test_poisson_2indep
/home/gereon/.local/lib/python3.10/site-packages/statsmodels/compat/pandas.py:65: FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead.
from pandas import Int64Index as NumericIndex
>>> test_poisson_2indep(1, 1, 0, 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/gereon/.local/lib/python3.10/site-packages/statsmodels/stats/rates.py", line 140, in test_poisson_2indep
ratio = rates[0] / rates[1]
ZeroDivisionError: float division by zero
>>> test_poisson_2indep(0, 4, 1, 1)
<class 'statsmodels.stats.base.HolderTuple'>
statistic = -2.0
pvalue = 0.04550026389635839
distribution = 'normal'
method = 'score'
alternative = 'two-sided'
rates = (0.0, 1.0)
ratio = 0.0
ratio_null = 1
tuple = (-2.0, 0.04550026389635839)Expected Output
I believe the test is symmetric, so
>>> test_poisson_2indep(1, 1, 0, 4)
<class 'statsmodels.stats.base.HolderTuple'>
statistic = -2.0
pvalue = 0.04550026389635839
distribution = 'normal'
method = 'score'
alternative = 'two-sided'
rates = (1.0, 0.0)
ratio = 0.0
ratio_null = 1
tuple = (-2.0, 0.04550026389635839)
Output of import statsmodels.api as sm; sm.show_versions()
INSTALLED VERSIONS
Python: 3.10.4.final.0
OS: Linux 5.10.114-1-MANJARO #1 SMP PREEMPT Mon May 9 07:52:55 UTC 2022 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
statsmodels
Installed: 0.13.2 (/home/gereon/.local/lib/python3.10/site-packages/statsmodels)
Required Dependencies
cython: 0.29.28 (/usr/lib/python3.10/site-packages/Cython)
numpy: 1.22.3 (/usr/lib/python3.10/site-packages/numpy)
scipy: 1.8.1 (/home/gereon/.local/lib/python3.10/site-packages/scipy)
pandas: 1.4.2 (/usr/lib/python3.10/site-packages/pandas)
dateutil: 2.8.2 (/usr/lib/python3.10/site-packages/dateutil)
patsy: 0.5.2 (/home/gereon/.local/lib/python3.10/site-packages/patsy)
Optional Dependencies
matplotlib: 3.5.2 (/usr/lib/python3.10/site-packages/matplotlib)
backend: GTK3Agg
cvxopt: Not installed
joblib: 1.1.0 (/usr/lib/python3.10/site-packages/joblib)
Developer Tools
IPython: 8.3.0 (/usr/lib/python3.10/site-packages/IPython)
jinja2: 3.0.3 (/usr/lib/python3.10/site-packages/jinja2)
sphinx: 4.5.0 (/usr/lib/python3.10/site-packages/sphinx)
pygments: 2.12.0 (/usr/lib/python3.10/site-packages/pygments)
pytest: 7.1.2 (/usr/lib/python3.10/site-packages/pytest)
virtualenv: 20.11.0 (/usr/lib/python3.10/site-packages/virtualenv)
Activity
Anaphory commentedon Jun 16, 2022
The offending line is this one.
statsmodels/statsmodels/stats/rates.py
Line 141 in 96c41cd
If I am right about it being symmetric, it should boil down to
but there may be things in the one-sided tests in particular that I am forgetting.
josef-pkt commentedon Jun 16, 2022
zero division should be a warning, but Python raises.
We should use numpy in cases like
ratio = rates[0] / rates[1]The problem is still present in my current PR #8166
score test works even for zero counts,
(using my branch)
I think the fix should be something like the following which works automatically for scalar and vectorized rates
np.float64(1) * rate1 / rate2edit
using
np.divideis simplerjosef-pkt commentedon Jun 16, 2022
current behavior in #8166
several RuntimeWarnings
It looks like we got continuity with respect to count around zero
Note, nan in exact-cond and cond-midp statistics are by design, not available
josef-pkt commentedon Jun 17, 2022
wald-log is messier, 3 divisions
also, we don't use asarray in the functions
josef-pkt commentedon Jun 17, 2022
asarrayit isIt works and it still returns a scalar (nulmpy scalar)
change will be in PR #8166 (fixing main would just cause messy merge conflicts)
ENH\REF: use asarray closes statsmodels#8313 remove ratio_null except…