Skip to content

Commit

Permalink
BUG: address the overflow bug of inverse sf of gamma when input proba…
Browse files Browse the repository at this point in the history
…bility is very small
  • Loading branch information
zoj613 committed Mar 14, 2021
1 parent cf68d4a commit 30d8703
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
3 changes: 2 additions & 1 deletion scipy/stats/_continuous_distns.py
Expand Up @@ -3578,7 +3578,8 @@ def f_small_q(q, mu, k):
"""select the start values for the newton using the quantiles of
the gamma (if right tail is required) or normal distribution"""
if upper:
return gamma.isf(q, 1 / mu, scale=mu * mu)
mu3 = mu ** 3
return sc.gammainccinv(1 / mu3, q) / mu3
return mu / (_norm_ppf(q) ** 2),

k = 1.5 * mu
Expand Down
12 changes: 5 additions & 7 deletions scipy/stats/tests/test_distributions.py
Expand Up @@ -1894,14 +1894,12 @@ def test_ppf_sf(self):

# tests if algorithm does not diverge for small probabilities.
assert np.isclose(stats.invgauss.ppf(0.00013, mu=1 / 3) * 3,
0.15039762631802803, atol=1e-08)
0.15039762631802803, atol=1e-17)
# test if it returns right tail values accurately
assert np.isclose(stats.invgauss.isf(1e-16, mu=1.05) / 0.7,
98.47905825943425, atol=1e-08)
# test if it overflows for input 1e-17 or less
with pytest.warns(RuntimeWarning,
match="invalid value encountered in multiply"):
assert_equal(stats.invgauss.isf(1e-17, mu=1.05) / 0.7, np.nan)
assert np.isclose(stats.invgauss.isf(1e-17, mu=1.05) / 0.7,
105.42079072389444, atol=1e-14)
assert np.isclose(stats.invgauss.isf(1e-50, mu=1.05) / 0.7,
339.3560284841935, atol=1e-13)
# test if correct out is returned for boundary values
with np.errstate(invalid='ignore'):
# because probabilities must be in the interval [0, 1] a
Expand Down

0 comments on commit 30d8703

Please sign in to comment.