Skip to content

Commit 6f6d066

Browse files
BUG: stats: Allow betaprime._ppf to accept scalars. (scipy#21322)
* TST: stats: test betaprime._ppf with scalar arrays. * BUG: Allow betaprime._ppf to handle scalar inputs. * STY: stats: appease linter --------- Co-authored-by: Lucas Colley <[email protected]>
1 parent 06e9a54 commit 6f6d066

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

scipy/stats/_continuous_distns.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,16 +1035,20 @@ def _sf(self, x, a, b):
10351035

10361036
def _ppf(self, p, a, b):
10371037
p, a, b = np.broadcast_arrays(p, a, b)
1038-
# by default, compute compute the ppf by solving the following:
1038+
# By default, compute the ppf by solving the following:
10391039
# p = beta._cdf(x/(1+x), a, b). This implies x = r/(1-r) with
10401040
# r = beta._ppf(p, a, b). This can cause numerical issues if r is
1041-
# very close to 1. in that case, invert the alternative expression of
1041+
# very close to 1. In that case, invert the alternative expression of
10421042
# the cdf: p = beta._sf(1/(1+x), b, a).
10431043
r = stats.beta._ppf(p, a, b)
10441044
with np.errstate(divide='ignore'):
10451045
out = r / (1 - r)
1046-
i = (r > 0.9999)
1047-
out[i] = 1/stats.beta._isf(p[i], b[i], a[i]) - 1
1046+
rnear1 = r > 0.9999
1047+
if np.isscalar(r):
1048+
if rnear1:
1049+
out = 1/stats.beta._isf(p, b, a) - 1
1050+
else:
1051+
out[rnear1] = 1/stats.beta._isf(p[rnear1], b[rnear1], a[rnear1]) - 1
10481052
return out
10491053

10501054
def _munp(self, n, a, b):
@@ -4880,7 +4884,7 @@ def _stats(self, mu):
48804884
def fit(self, data, *args, **kwds):
48814885
method = kwds.get('method', 'mle')
48824886

4883-
if (isinstance(data, CensoredData) or type(self) == wald_gen
4887+
if (isinstance(data, CensoredData) or isinstance(self, wald_gen)
48844888
or method.lower() == 'mm'):
48854889
return super().fit(data, *args, **kwds)
48864890

scipy/stats/tests/test_distributions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4635,6 +4635,13 @@ def test_ppf(self, p, a, b, expected):
46354635
def test_ppf_gh_17631(self, x, a, b, p):
46364636
assert_allclose(stats.betaprime.ppf(p, a, b), x, rtol=2e-14)
46374637

4638+
def test__ppf(self):
4639+
# Verify that _ppf supports scalar arrays.
4640+
a = np.array(1.0)
4641+
b = np.array(1.0)
4642+
p = np.array(0.5)
4643+
assert_allclose(stats.betaprime._ppf(p, a, b), 1.0, rtol=5e-16)
4644+
46384645
@pytest.mark.parametrize(
46394646
'x, a, b, expected',
46404647
cdf_vals + [

0 commit comments

Comments
 (0)