def test_cdf(self, x): 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.cdf(x) expected = stats.norm.cdf(x) assert_allclose(res, expected, rtol=1e-11, atol=1e-11) assert res.shape == expected.shape
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])
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}") 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()])
def test_bad_args(self): dist = StandardNormal() rng = NumericalInversePolynomial(dist) 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()
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
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)
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)
def test_inf_nan_domains(self, domain, err, msg): with pytest.raises(err, match=msg): NumericalInversePolynomial(StandardNormal(), domain=domain)
def test_basic(self, dist, mv_ex): rng = NumericalInversePolynomial(dist, random_state=42) check_cont_samples(rng, dist, mv_ex)