예제 #1
0
 def test_ppf(self, u):
     dist = StandardNormal()
     rng = NumericalInversePolynomial(dist, u_resolution=1e-14)
     # 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-11, atol=1e-11)
     assert res.shape == expected.shape
예제 #2
0
    def test_bad_pdf(self, pdf, err, msg):
        class dist:
            pass

        dist.pdf = pdf
        with pytest.raises(err, match=msg):
            NumericalInversePolynomial(dist, domain=[0, 5])
예제 #3
0
    def test_bad_args(self):
        dist = StandardNormal()
        rng = NumericalInversePolynomial(dist)
        msg = r"CDF is not available."
        with pytest.raises(ValueError, match=msg):
            rng.cdf([1, 2, 3])
        msg = r"`sample_size` must be greater than or equal to 1000."
        with pytest.raises(ValueError, match=msg):
            rng.u_error(10)

        class Distribution:
            def pdf(self, x):
                return np.exp(-0.5 * x * x)

        dist = Distribution()
        rng = NumericalInversePolynomial(dist)
        msg = r"Exact CDF required but not found."
        with pytest.raises(ValueError, match=msg):
            rng.u_error()
예제 #4
0
 def test_u_error(self):
     dist = StandardNormal()
     rng = NumericalInversePolynomial(dist, u_resolution=1e-10)
     max_error, mae = rng.u_error()
     assert max_error < 1e-10
     assert mae <= max_error
     rng = NumericalInversePolynomial(dist, u_resolution=1e-14)
     max_error, mae = rng.u_error()
     assert max_error < 1e-14
     assert mae <= max_error
예제 #5
0
 def test_basic_all_scipy_dists(self, distname, params):
     if distname in self.very_slow_dists:
         pytest.skip(f"PINV too slow for {distname}")
     if distname in self.fail_dists:
         pytest.skip(f"PINV fails for {distname}")
     if distname in self.unbounded_pdf_fail_dists:
         pytest.skip("PINV fails for unbounded PDFs.")
     if distname in self.fatal_fail_dists:
         pytest.xfail(f"PINV segmentation faults for {distname}")
     dist = (getattr(stats, distname)
             if isinstance(distname, str) else distname)
     dist = dist(*params)
     with suppress_warnings() as sup:
         sup.filter(RuntimeWarning)
         rng = NumericalInversePolynomial(dist, random_state=42)
     check_cont_samples(rng, dist, [dist.mean(), dist.var()])
예제 #6
0
 def test_bad_max_intervals(self, max_intervals):
     msg = (r"`max_intervals` must be an integer in the range "
            r"\[100, 1000000\].")
     with pytest.raises(ValueError, match=msg):
         NumericalInversePolynomial(StandardNormal(),
                                    max_intervals=max_intervals)
예제 #7
0
 def test_bad_u_resolution(self, u_resolution):
     msg = r"`u_resolution` must be between 1e-15 and 1e-5."
     with pytest.raises(ValueError, match=msg):
         NumericalInversePolynomial(StandardNormal(),
                                    u_resolution=u_resolution)
예제 #8
0
    def test_bad_orders(self, order):
        dist = StandardNormal()

        msg = r"`order` must be an integer in the range \[3, 17\]."
        with pytest.raises(ValueError, match=msg):
            NumericalInversePolynomial(dist, order=order)
예제 #9
0
 def test_inf_nan_domains(self, domain, err, msg):
     with pytest.raises(err, match=msg):
         NumericalInversePolynomial(StandardNormal(), domain=domain)
예제 #10
0
 def test_basic(self, dist, mv_ex):
     rng = NumericalInversePolynomial(dist, random_state=42)
     check_cont_samples(rng, dist, mv_ex)