Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7d1e9af

Browse filesBrowse files
authored
ENH: stats.truncexpon: improve sf/isf precision (scipy#18449)
* ENH: stats.truncexpon: improve sf/isf precision
1 parent 3d4ccd7 commit 7d1e9af
Copy full SHA for 7d1e9af

File tree

Expand file treeCollapse file tree

3 files changed

+33
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-1
lines changed

‎scipy/stats/_continuous_distns.py

Copy file name to clipboardExpand all lines: scipy/stats/_continuous_distns.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9125,6 +9125,12 @@ def _cdf(self, x, b):
91259125
def _ppf(self, q, b):
91269126
return -sc.log1p(q*sc.expm1(-b))
91279127

9128+
def _sf(self, x, b):
9129+
return (np.exp(-b) - np.exp(-x))/sc.expm1(-b)
9130+
9131+
def _isf(self, q, b):
9132+
return -np.log(np.exp(-b) - q * sc.expm1(-b))
9133+
91289134
def _munp(self, n, b):
91299135
# wrong answer with formula, same as in continuous.pdf
91309136
# return sc.gamman+1)-sc.gammainc1+n, b)

‎scipy/stats/tests/test_distributions.py

Copy file name to clipboardExpand all lines: scipy/stats/tests/test_distributions.py
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3871,6 +3871,19 @@ def test_cdf_ppf(self, x, p, a, b, c):
38713871
ppf = stats.genexpon.ppf(p, a, b, c)
38723872
assert_allclose(ppf, x, rtol=1e-14)
38733873

3874+
class TestTruncexpon:
3875+
3876+
# reference values were computed via the reference distribution
3877+
# mp.dps = 50
3878+
# TruncExpon(b=100).sf(99.999999)
3879+
3880+
@pytest.mark.parametrize("x, b, ref",
3881+
[(19.999999, 20, 2.0611546593828472e-15),
3882+
(99.999999, 100, 3.7200778266671455e-50)])
3883+
def test_sf_isf(self, x, b, ref):
3884+
assert_allclose(stats.truncexpon.sf(x, b), ref, rtol=1e-10)
3885+
assert_allclose(stats.truncexpon.isf(ref, b), x, rtol=1e-12)
3886+
38743887

38753888
class TestExponpow:
38763889
def test_tail(self):

‎scipy/stats/tests/test_generation/reference_distributions.py

Copy file name to clipboardExpand all lines: scipy/stats/tests/test_generation/reference_distributions.py
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,24 @@ def _pdf(self, x, alpha, beta):
297297
a = mp.pi**-1 * alpha * mp.exp(mp.sqrt(alpha**2 - beta**2))
298298
return a * q**-1 * mp.besselk(1, alpha*q) * mp.exp(beta*x)
299299

300+
300301
class StudentT(ReferenceDistribution):
301302

302303
def __init(self, *, df):
303-
super().__init(df=df)
304+
super().__init__(df=df)
304305

305306
def _pdf(self, x, df):
306307
return (mp.gamma((df + mp.one)/2)/(mp.sqrt(df * mp.pi) * mp.gamma(df/2))
307308
* (mp.one + x*x/df)**(-(df + mp.one)/2))
309+
310+
311+
class TruncExpon(ReferenceDistribution):
312+
313+
def __init__(self, *, b):
314+
super().__init__(b=b)
315+
316+
def _pdf(self, x, b):
317+
return -mp.exp(-x)/mp.expm1(-b)
318+
319+
def _sf(self, x, b):
320+
return (mp.exp(-b) - mp.exp(-x))/mp.expm1(-b)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.