From 77236617d37150e726be0b02c3b780080cdb8eda Mon Sep 17 00:00:00 2001 From: warren Date: Fri, 25 Nov 2022 16:37:39 -0500 Subject: [PATCH] TST: stats: Make check_cdf_ppf a bit more robust. [skip circle] --- scipy/stats/tests/test_discrete_basic.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/scipy/stats/tests/test_discrete_basic.py b/scipy/stats/tests/test_discrete_basic.py index 0b69ece31112..86ea2af660c2 100644 --- a/scipy/stats/tests/test_discrete_basic.py +++ b/scipy/stats/tests/test_discrete_basic.py @@ -184,9 +184,20 @@ def test_isf_with_loc(dist, args): def check_cdf_ppf(distfn, arg, supp, msg): + # supp is assumed to be an array of integers in the support of distfn + # (but not necessarily all the integers in the support). + # This test assumes that the PMF of any value in the support of the + # distribution is greater than 1e-8. + # cdf is a step function, and ppf(q) = min{k : cdf(k) >= q, k integer} - npt.assert_array_equal(distfn.ppf(distfn.cdf(supp, *arg), *arg), + cdf_supp = distfn.cdf(supp, *arg) + # In very rare cases, the finite precision calculation of ppf(cdf(supp)) + # can produce an array in which an element is off by one. We nudge the + # CDF values down by 10 ULPs help to avoid this. + cdf_supp0 = cdf_supp - 10*np.spacing(cdf_supp) + npt.assert_array_equal(distfn.ppf(cdf_supp0, *arg), supp, msg + '-roundtrip') + # Repeat the same calculation, but with the CDF values decreased by 1e-8. npt.assert_array_equal(distfn.ppf(distfn.cdf(supp, *arg) - 1e-8, *arg), supp, msg + '-roundtrip') @@ -195,7 +206,6 @@ def check_cdf_ppf(distfn, arg, supp, msg): supp1 = supp[supp < _b] npt.assert_array_equal(distfn.ppf(distfn.cdf(supp1, *arg) + 1e-8, *arg), supp1 + distfn.inc, msg + ' ppf-cdf-next') - # -1e-8 could cause an error if pmf < 1e-8 def check_pmf_cdf(distfn, arg, distname):