def test_setup_no_bounds(self): dist = StandardNormal() u_bound = np.exp(-0.5) * np.sqrt(2) v_max = 1.0 # no bound rng = NaiveRatioUniforms(dist, random_state=7303) check_cont_samples(rng, dist, (0, 1)) # just v_max rng = NaiveRatioUniforms(dist, v_max=v_max, random_state=7303) check_cont_samples(rng, dist, (0, 1)) # just one u-bound is missing msg = 'Only one of the values of u_min and u_max is None.' with pytest.warns(RuntimeWarning, match=msg): rng = NaiveRatioUniforms(dist, u_max=u_bound, random_state=7303) check_cont_samples(rng, dist, (0, 1))
def test_rv_generation_r(self, r): # test different values of r # note: u_max, u_min are attained at the points -/+ sqrt((r+1)/r) dist = StandardNormal() y = np.sqrt((r + 1) / r) u = (dist.pdf(y))**(r / (r + 1)) * y v_max = (dist.pdf(0))**(1 / (r + 1)) rng = NaiveRatioUniforms(dist, r=r, u_min=-u, u_max=u, v_max=v_max, random_state=7303) check_cont_samples(rng, dist, (0, 1))
def test_rv_generation_default(self): # use default settings (r=1, center=0) # exponential distribution class Exponential(): def pdf(self, x): return np.exp(-x) def cdf(self, x): return 1 - np.exp(-x) dist = Exponential() rng = NaiveRatioUniforms(dist, v_max=1, u_min=0, u_max=2 * np.exp(-1), random_state=76525) check_cont_samples(rng, dist, (1, 1))
def test_rv_generation_mode_shift(self, p): # test mode shift with Gamma(p) distribution, mode=p-1 # note: the pdf is bounded only if p >= 1 # note: u_max, u_min can be computed explicitly def u_bound(x, p, m): if x < 0: return 0 return (x - m) * x**((p - 1) / 2) * math.exp(-x / 2) dist = Gamma(p) m = p - 1 h = (p + 1 + m) / 2 k = np.sqrt(h**2 - m * (p - 1)) u_min, u_max = u_bound(h - k, p, m), u_bound(h + k, p, m) v_max = np.sqrt(dist.pdf(m)) rng = NaiveRatioUniforms(dist, center=m, u_min=u_min, u_max=u_max, v_max=v_max, random_state=357) check_cont_samples(rng, dist, (p, p))
def test_exceptions(self): dist = StandardNormal() # need u_min < u_max msg = r"`u_min` must be smaller than `u_max`." with pytest.raises(ValueError, match=msg): NaiveRatioUniforms(dist, v_max=1, u_min=3, u_max=1) with pytest.raises(ValueError, match=msg): NaiveRatioUniforms(dist, v_max=1, u_min=1, u_max=1) # need v_max > 0 msg = r'`v_max` must be positive.' with pytest.raises(ValueError, match=msg): NaiveRatioUniforms(dist, v_max=-1, u_min=1, u_max=3) with pytest.raises(ValueError, match=msg): NaiveRatioUniforms(dist, v_max=0, u_min=1, u_max=3) # need r > 0 msg = r'`r` must be positive.' with pytest.raises(ValueError, match=msg): NaiveRatioUniforms(dist, r=0, v_max=1, u_min=1, u_max=3) with pytest.raises(ValueError, match=msg): NaiveRatioUniforms(dist, r=-0.24, v_max=1, u_min=1, u_max=3)