def test_uniform(): l = Symbol('l', real=True) w = Symbol('w', positive=True, finite=True) X = Uniform('x', l, l + w) assert simplify(E(X)) == l + w/2 assert simplify(variance(X)) == w**2/12 # With numbers all is well X = Uniform('x', 3, 5) assert P(X < 3) == 0 and P(X > 5) == 0 assert P(X < 4) == P(X > 4) == Rational(1, 2)
def test_cdf(): X = Normal('x', 0, 1) d = cdf(X) assert P(X < 1) == d(1) assert d(0) == Rational(1, 2) d = cdf(X, X > 0) # given X>0 assert d(0) == 0 Y = Exponential('y', 10) d = cdf(Y) assert d(-5) == 0 assert P(Y > 3) == 1 - d(3) pytest.raises(ValueError, lambda: cdf(X + Y)) Z = Exponential('z', 1) f = cdf(Z) z = Symbol('z') assert f(z) == Piecewise((1 - exp(-z), z >= 0), (0, True)) U = Uniform('x', 3, 5) u = cdf(U) assert u(z) == z/2 - Rational(3, 2)
def test_uniform_P(): """This stopped working because SingleContinuousPSpace.compute_density no longer calls integrate on a DiracDelta but rather just solves directly. integrate used to call UniformDistribution.expectation which special-cased subsed out the Min and Max terms that Uniform produces. I decided to regress on this class for general cleanliness (and I suspect speed) of the algorithm. """ l = Symbol('l', real=True) w = Symbol('w', positive=True, finite=True) X = Uniform('x', l, l + w) assert P(X < l) == 0 and P(X > l + w) == 0
def test_prefab_sampling(): N = Normal('X', 0, 1) L = LogNormal('L', 0, 1) E = Exponential('Ex', 1) P = Pareto('P', 1, 3) W = Weibull('W', 1, 1) U = Uniform('U', 0, 1) B = Beta('B', 2, 5) G = Gamma('G', 1, 3) variables = [N, L, E, P, W, U, B, G] niter = 10 for var in variables: for i in range(niter): assert sample(var) in var.pspace.domain.set