コード例 #1
0
 def test_negative_guide_factor_raises_warning(self):
     # This occurs from the UNU.RAN wrapper automatically.
     # however it already gives a useful warning
     # Here we just test that a warning is raised.
     pv = [0.1, 0.3, 0.6]
     urng = np.random.default_rng()
     with pytest.warns(RuntimeWarning):
         DiscreteGuideTable(pv, random_state=urng, guide_factor=-1)
コード例 #2
0
    def test_ppf(self, u):
        n, p = 4, 0.1
        dist = stats.binom(n, p)
        rng = DiscreteGuideTable(dist, random_state=42)

        # 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.binom.ppf(u, n, p)
        assert_equal(res.shape, expected.shape)
        assert_equal(res, expected)
コード例 #3
0
    def test_basic(self, distname, params):
        if distname in self.basic_fail_dists:
            msg = ("DGT 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]):
            # DGT only works with finite domain. So, skip the distributions
            # with infinite tails.
            pytest.skip("DGT only works with a finite domain.")

        k = np.arange(domain[0], domain[1] + 1)
        pv = dist.pmf(k)
        mv_ex = dist.stats('mv')
        rng = DiscreteGuideTable(dist, random_state=42)
        check_discr_samples(rng, pv, mv_ex)
コード例 #4
0
 def test_inf_domain(self, domain):
     with pytest.raises(ValueError, match=r"must be finite"):
         DiscreteGuideTable(stats.binom(10, 0.2), domain=domain)
コード例 #5
0
 def test_bad_pv(self, pv, msg):
     with pytest.raises(ValueError, match=msg):
         DiscreteGuideTable(pv)
コード例 #6
0
 def test_guide_factor_zero_raises_warning(self):
     pv = [0.1, 0.3, 0.6]
     urng = np.random.default_rng()
     with pytest.warns(RuntimeWarning):
         DiscreteGuideTable(pv, random_state=urng, guide_factor=0)