Example #1
0
 def func2():
     dist = Distribution('bar')
     rng = TransformedDensityRejection(dist,
                                       domain=(10, 100),
                                       random_state=2)
     try:
         rng.rvs(100000)
     except ValueError as e:
         errors['err2'] = e.args[0]
Example #2
0
def test_rvs_size(size):
    # As the `rvs` method is present in the base class and shared between
    # all the classes, we can just test with one of the methods.
    rng = TransformedDensityRejection(StandardNormal())
    if size is None:
        assert np.isscalar(rng.rvs(size))
    else:
        if np.isscalar(size):
            size = (size, )
        assert rng.rvs(size).shape == size
Example #3
0
    def test_bad_dist(self):
        # Empty distribution
        class dist:
            ...

        msg = r"`pdf` required but not found."
        with pytest.raises(ValueError, match=msg):
            TransformedDensityRejection(dist)

        # dPDF not present in dist
        class dist:
            pdf = lambda x: 1 - x * x  # noqa: E731

        msg = r"`dpdf` required but not found."
        with pytest.raises(ValueError, match=msg):
            TransformedDensityRejection(dist)
Example #4
0
    def test_bad_dpdf(self, dpdf, err, msg):
        class dist:
            pass

        dist.pdf = lambda x: x
        dist.dpdf = dpdf
        with pytest.raises(err, match=msg):
            TransformedDensityRejection(dist, domain=(1, 10))
Example #5
0
    def test_bad_pdf(self, pdf, err, msg):
        class dist:
            pass

        dist.pdf = pdf
        dist.dpdf = lambda x: 1  # an arbitrary dPDF
        with pytest.raises(err, match=msg):
            TransformedDensityRejection(dist)
Example #6
0
 def test_ppf_hat(self, u):
     # Increase the `max_squeeze_hat_ratio` so the ppf_hat is more
     # accurate.
     rng = TransformedDensityRejection(StandardNormal(),
                                       max_squeeze_hat_ratio=0.9999,
                                       max_intervals=10000)
     # 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_hat(u)
         expected = stats.norm.ppf(u)
     assert_allclose(res, expected, rtol=1e-3, atol=1e-5)
     assert res.shape == expected.shape
Example #7
0
    def test_bad_construction_points_array(self):
        # empty array
        construction_points = []
        with pytest.raises(ValueError,
                           match=r"`construction_points` must "
                           r"either be a "
                           r"scalar or a non-empty array."):
            TransformedDensityRejection(
                StandardNormal(), construction_points=construction_points)

        # construction_points not monotonically increasing
        construction_points = [1, 1, 1, 1, 1, 1]
        with pytest.warns(RuntimeWarning,
                          match=r"33 : starting points not "
                          r"strictly monotonically "
                          r"increasing"):
            TransformedDensityRejection(
                StandardNormal(), construction_points=construction_points)

        # construction_points containing nans
        construction_points = [np.nan, np.nan, np.nan]
        with pytest.raises(UNURANError,
                           match=r"50 : bad construction "
                           r"points."):
            TransformedDensityRejection(
                StandardNormal(), construction_points=construction_points)

        # construction_points out of domain
        construction_points = [-10, 10]
        with pytest.warns(RuntimeWarning,
                          match=r"50 : starting point out of "
                          r"domain"):
            TransformedDensityRejection(
                StandardNormal(),
                domain=(-3, 3),
                construction_points=construction_points)
Example #8
0
 def test_bad_variant(self):
     msg = r"Invalid option for the `variant`"
     with pytest.raises(ValueError, match=msg):
         TransformedDensityRejection(StandardNormal(), variant='foo')
Example #9
0
 def test_bad_c(self, c):
     msg = r"`c` must either be -0.5 or 0."
     with pytest.raises(ValueError, match=msg):
         TransformedDensityRejection(StandardNormal(), c=-1.)
Example #10
0
 def test_bad_construction_points_scalar(self, construction_points):
     with pytest.raises(ValueError,
                        match=r"`construction_points` must be "
                        r"a positive integer."):
         TransformedDensityRejection(
             StandardNormal(), construction_points=construction_points)
Example #11
0
 def test_inf_nan_domains(self, domain, err, msg):
     with pytest.raises(err, match=msg):
         TransformedDensityRejection(StandardNormal(), domain=domain)
Example #12
0
 def test_basic(self, dist, mv_ex):
     with suppress_warnings() as sup:
         # filter the warnings thrown by UNU.RAN
         sup.filter(RuntimeWarning)
         rng = TransformedDensityRejection(dist, random_state=42)
     check_cont_samples(rng, dist, mv_ex)
Example #13
0
def test_set_random_state():
    rng1 = TransformedDensityRejection(StandardNormal(), random_state=123)
    rng2 = TransformedDensityRejection(StandardNormal())
    rng2.set_random_state(123)
    assert_equal(rng1.rvs(100), rng2.rvs(100))
    rng = TransformedDensityRejection(StandardNormal(), random_state=123)
    rvs1 = rng.rvs(100)
    rng.set_random_state(123)
    rvs2 = rng.rvs(100)
    assert_equal(rvs1, rvs2)