Skip to content

Commit 3b61e16

Browse files
MAINT: stats: Improve precision of argus.sf. (scipy#21326)
Replace (1-x**2) with (1 - x)*(1 + x) to avoid loss of precision when x is near 1.
1 parent 6f6d066 commit 3b61e16

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

scipy/stats/_continuous_distns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11467,7 +11467,7 @@ def _cdf(self, x, chi):
1146711467
return 1.0 - self._sf(x, chi)
1146811468

1146911469
def _sf(self, x, chi):
11470-
return _argus_phi(chi * np.sqrt(1 - x**2)) / _argus_phi(chi)
11470+
return _argus_phi(chi * np.sqrt((1 - x)*(1 + x))) / _argus_phi(chi)
1147111471

1147211472
def _rvs(self, chi, size=None, random_state=None):
1147311473
chi = np.asarray(chi)

scipy/stats/tests/test_distributions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9242,6 +9242,17 @@ def test_sf_small_chi(self, chi, expected):
92429242
x = np.array([0.1, 0.5, 0.9])
92439243
assert_allclose(stats.argus.sf(x, chi), expected, rtol=1e-14)
92449244

9245+
# Expected values were computed with mpmath.
9246+
@pytest.mark.parametrize(
9247+
'x, chi, expected',
9248+
[(0.9999999, 0.25, 9.113252974162428e-11),
9249+
(0.9999999, 3.0, 6.616650419714568e-10),
9250+
(0.999999999, 2.5, 4.130195911418939e-13),
9251+
(0.999999999, 10.0, 2.3788319094393724e-11)])
9252+
def test_sf_near_1(self, x, chi, expected):
9253+
sf = stats.argus.sf(x, chi)
9254+
assert_allclose(sf, expected, rtol=5e-15)
9255+
92459256
# Expected values were computed with mpmath (code: see gh-13370).
92469257
@pytest.mark.parametrize(
92479258
'chi, expected',

0 commit comments

Comments
 (0)