Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEP: add deprecation warning for binom_test #16307

Merged
merged 7 commits into from Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions scipy/stats/_morestats.py
Expand Up @@ -22,6 +22,7 @@
from ._distn_infrastructure import rv_generic
from ._hypotests import _get_wilcoxon_distr
from ._axis_nan_policy import _axis_nan_policy_factory
from .._lib.deprecation import _deprecated


__all__ = ['mvsdist',
Expand Down Expand Up @@ -2645,16 +2646,20 @@ def levene(*samples, center='median', proportiontocut=0.05):
return LeveneResult(W, pval)


@_deprecated("'binom_test' is deprecated in favour of"
" 'binomtest' from version 1.7.0 and will"
" be removed in Scipy 1.12.0.")
def binom_test(x, n=None, p=0.5, alternative='two-sided'):
"""Perform a test that the probability of success is p.

tupui marked this conversation as resolved.
Show resolved Hide resolved
Note: `binom_test` is deprecated; it is recommended that `binomtest`
be used instead.

This is an exact, two-sided test of the null hypothesis
that the probability of success in a Bernoulli experiment
is `p`.

.. deprecated:: 1.10.0
'binom_test' is deprecated in favour of 'binomtest' and will
be removed in Scipy 1.12.0.

Parameters
----------
x : int or array_like
Expand Down
46 changes: 20 additions & 26 deletions scipy/stats/tests/test_morestats.py
Expand Up @@ -728,10 +728,27 @@ def test_1d_input(self):
assert_raises(ValueError, stats.levene, g1, x)


class TestBinomP:
"""Tests for stats.binom_test."""
class TestBinomTestP:
"""
Tests for stats.binomtest as a replacement for deprecated stats.binom_test.
"""
@staticmethod
def binom_test_func(x, n=None, p=0.5, alternative='two-sided'):
# This processing of x and n is copied from from binom_test.
x = np.atleast_1d(x).astype(np.int_)
if len(x) == 2:
n = x[1] + x[0]
x = x[0]
elif len(x) == 1:
x = x[0]
if n is None or n < x:
raise ValueError("n must be >= x")
n = np.int_(n)
else:
raise ValueError("Incorrect length for x.")

binom_test_func = staticmethod(stats.binom_test)
tupui marked this conversation as resolved.
Show resolved Hide resolved
result = stats.binomtest(x, n, p=p, alternative=alternative)
return result.pvalue

def test_data(self):
pval = self.binom_test_func(100, 250)
Expand Down Expand Up @@ -772,29 +789,6 @@ def test_boost_overflow_raises(self):
assert_raises(OverflowError, self.binom_test_func, 5.0, 6, p=sys.float_info.min)


class TestBinomTestP(TestBinomP):
"""
Tests for stats.binomtest as a replacement for stats.binom_test.
"""
@staticmethod
def binom_test_func(x, n=None, p=0.5, alternative='two-sided'):
# This processing of x and n is copied from from binom_test.
x = np.atleast_1d(x).astype(np.int_)
if len(x) == 2:
n = x[1] + x[0]
x = x[0]
elif len(x) == 1:
x = x[0]
if n is None or n < x:
raise ValueError("n must be >= x")
n = np.int_(n)
else:
raise ValueError("Incorrect length for x.")

result = stats.binomtest(x, n, p=p, alternative=alternative)
return result.pvalue


class TestBinomTest:
"""Tests for stats.binomtest."""

Expand Down
30 changes: 22 additions & 8 deletions scipy/stats/tests/test_stats.py
Expand Up @@ -5952,6 +5952,22 @@ def test_masked_3d_array(self):
assert_equal(gstd_actual.mask, mask)


@pytest.mark.parametrize('alternative', ['two-sided', 'greater', 'less'])
def test_binom_test_deprecation(alternative):
deprecation_msg = ("'binom_test' is deprecated in favour of"
" 'binomtest' from version 1.7.0 and will"
" be removed in Scipy 1.12.0.")
num = 10
rng = np.random.default_rng(156114182869662948677852568516310985853)
X = rng.integers(10, 100, (num,))
N = X + rng.integers(0, 100, (num,))
P = rng.uniform(0, 1, (num,))
for x, n, p in zip(X, N, P):
with pytest.warns(DeprecationWarning, match=deprecation_msg):
res = stats.binom_test(x, n, p, alternative=alternative)
assert res == stats.binomtest(x, n, p, alternative=alternative).pvalue


def test_binomtest():
# precision tests compared to R for ticket:986
pp = np.concatenate((np.linspace(0.1, 0.2, 5),
Expand All @@ -5968,10 +5984,9 @@ def test_binomtest():
2.6102587134694721e-006]

for p, res in zip(pp, results):
assert_approx_equal(stats.binom_test(x, n, p), res,
assert_approx_equal(stats.binomtest(x, n, p).pvalue, res,
significant=12, err_msg='fail forp=%f' % p)

assert_approx_equal(stats.binom_test(50, 100, 0.1),
assert_approx_equal(stats.binomtest(50, 100, 0.1).pvalue,
5.8320387857343647e-024,
significant=12)

Expand All @@ -5995,16 +6010,15 @@ def test_binomtest2():
1.000000000, 0.753906250, 0.343750000, 0.109375000, 0.021484375,
0.001953125]
]

for k in range(1, 11):
res1 = [stats.binom_test(v, k, 0.5) for v in range(k + 1)]
res1 = [stats.binomtest(v, k, 0.5).pvalue for v in range(k + 1)]
tupui marked this conversation as resolved.
Show resolved Hide resolved
assert_almost_equal(res1, res2[k-1], decimal=10)


def test_binomtest3():
# test added for issue #2384
# test when x == n*p and neighbors
res3 = [stats.binom_test(v, v*k, 1./k)
res3 = [stats.binomtest(v, v*k, 1./k).pvalue
for v in range(1, 11) for k in range(2, 11)]
assert_equal(res3, np.ones(len(res3), int))

Expand Down Expand Up @@ -6086,9 +6100,9 @@ def test_binomtest3():
0.736270323773157, 0.737718376096348
])

res4_p1 = [stats.binom_test(v+1, v*k, 1./k)
res4_p1 = [stats.binomtest(v+1, v*k, 1./k).pvalue
for v in range(1, 11) for k in range(2, 11)]
res4_m1 = [stats.binom_test(v-1, v*k, 1./k)
res4_m1 = [stats.binomtest(v-1, v*k, 1./k).pvalue
for v in range(1, 11) for k in range(2, 11)]

assert_almost_equal(res4_p1, binom_testp1, decimal=13)
Expand Down