Skip to content

Commit

Permalink
TST: stats: Make check_cdf_ppf a bit more robust.
Browse files Browse the repository at this point in the history
[skip circle]
  • Loading branch information
WarrenWeckesser committed Nov 26, 2022
1 parent 16357de commit 7723661
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions scipy/stats/tests/test_discrete_basic.py
Expand Up @@ -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')

Expand All @@ -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):
Expand Down

0 comments on commit 7723661

Please sign in to comment.