示例#1
0
    def test_bad_pmf(self, pmf, err, msg):
        class dist:
            pass

        dist.pmf = pmf
        with pytest.raises(err, match=msg):
            DiscreteAliasUrn(dist, domain=(1, 10))
示例#2
0
    def test_bad_args(self):
        msg = (r"`domain` must be provided when the "
               r"probability vector is not available.")

        class dist:
            def pmf(self, x):
                return x

        with pytest.raises(ValueError, match=msg):
            DiscreteAliasUrn(dist)
示例#3
0
 def test_sampling_with_pv(self, pv):
     pv = np.asarray(pv, dtype=np.float64)
     rng = DiscreteAliasUrn(pv, random_state=123)
     rvs = rng.rvs(100_000)
     pv = pv / pv.sum()
     variates = np.arange(0, len(pv))
     # test if the first few moments match
     m_expected = np.average(variates, weights=pv)
     v_expected = np.average((variates - m_expected)**2, weights=pv)
     mv_expected = m_expected, v_expected
     check_discr_samples(rng, pv, mv_expected)
示例#4
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())
示例#5
0
 def test_basic(self, distname, params):
     if distname in self.basic_fail_dists:
         msg = ("DAU fails on these probably because of large domains "
                "and small computation errors in PMF.")
         pytest.skip(msg)
     if not isinstance(distname, str):
         dist = distname
     else:
         dist = getattr(stats, distname)
     dist = dist(*params)
     domain = dist.support()
     if not np.isfinite(domain[1] - domain[0]):
         # DAU only works with finite domain. So, skip the distributions
         # with infinite tails.
         pytest.skip("DAU only works with a finite domain.")
     k = np.arange(domain[0], domain[1] + 1)
     pv = dist.pmf(k)
     mv_ex = dist.stats('mv')
     rng = DiscreteAliasUrn(dist, random_state=42)
     check_discr_samples(rng, pv, mv_ex)
示例#6
0
 def test_bad_urn_factor(self):
     with pytest.warns(RuntimeWarning, match=r"relative urn size < 1."):
         DiscreteAliasUrn([0.5, 0.5], urn_factor=-1)
示例#7
0
 def test_inf_domain(self, domain):
     with pytest.raises(ValueError, match=r"must be finite"):
         DiscreteAliasUrn(stats.binom(10, 0.2), domain=domain)
示例#8
0
 def test_bad_pv(self, pv, msg):
     with pytest.raises(ValueError, match=msg):
         DiscreteAliasUrn(pv)