Ejemplo n.º 1
0
    def test_basic_all_scipy_dists(self, distname, shapes):
        slow_dists = {'ksone', 'kstwo', 'levy_stable', 'skewnorm'}
        fail_dists = {
            'beta', 'gausshyper', 'geninvgauss', 'ncf', 'nct', 'norminvgauss',
            'genhyperbolic', 'studentized_range', 'vonmises', 'kappa4',
            'invgauss', 'wald'
        }

        if distname in slow_dists:
            pytest.skip("Distribution is too slow")
        if distname in fail_dists:
            # specific reasons documented in gh-13319
            # https://github.com/scipy/scipy/pull/13319#discussion_r626188955
            pytest.xfail("Fails - usually due to inaccurate CDF/PDF")

        np.random.seed(0)

        dist = getattr(stats, distname)(*shapes)

        with np.testing.suppress_warnings() as sup:
            sup.filter(RuntimeWarning, "overflow encountered")
            sup.filter(RuntimeWarning, "divide by zero")
            sup.filter(RuntimeWarning, "invalid value encountered")
            fni = NumericalInverseHermite(dist)

        x = np.random.rand(10)
        p_tol = np.max(np.abs(dist.ppf(x) - fni.ppf(x)) / np.abs(dist.ppf(x)))
        u_tol = np.max(np.abs(dist.cdf(fni.ppf(x)) - x))

        assert p_tol < 1e-8
        assert u_tol < 1e-12
Ejemplo n.º 2
0
def test_with_scipy_distribution():
    # test if the setup works with SciPy's rv_frozen distributions
    dist = stats.norm()
    urng = np.random.default_rng(0)
    rng = NumericalInverseHermite(dist, random_state=urng)
    u = np.linspace(0, 1, num=100)
    check_cont_samples(rng, dist, dist.stats())
    assert_allclose(dist.ppf(u), rng.ppf(u))
    # test if it works with `loc` and `scale`
    dist = stats.norm(loc=10., scale=5.)
    rng = NumericalInverseHermite(dist, random_state=urng)
    check_cont_samples(rng, dist, dist.stats())
    assert_allclose(dist.ppf(u), rng.ppf(u))
    # check for discrete distributions
    dist = stats.binom(10, 0.2)
    rng = DiscreteAliasUrn(dist, random_state=urng)
    domain = dist.support()
    pv = dist.pmf(np.arange(domain[0], domain[1] + 1))
    check_discr_samples(rng, pv, dist.stats())
Ejemplo n.º 3
0
 def test_ppf(self, u):
     dist = StandardNormal()
     rng = NumericalInverseHermite(dist, u_resolution=1e-12)
     # Older versions of NumPy throw RuntimeWarnings for comparisons
     # with nan.
     with suppress_warnings() as sup:
         sup.filter(RuntimeWarning, "invalid value encountered in greater")
         sup.filter(RuntimeWarning, "invalid value encountered in "
                    "greater_equal")
         sup.filter(RuntimeWarning, "invalid value encountered in less")
         sup.filter(RuntimeWarning, "invalid value encountered in "
                    "less_equal")
         res = rng.ppf(u)
         expected = stats.norm.ppf(u)
     assert_allclose(res, expected, rtol=1e-9, atol=3e-10)
     assert res.shape == expected.shape