def test_sampling_methods(): distribs_random = [DiscreteUniform("D", list(range(5)))] distribs_scipy = [Hypergeometric("H", 1, 1, 1)] distribs_pymc3 = [BetaBinomial("B", 1, 1, 1)] size = 5 with ignore_warnings(UserWarning): for X in distribs_random: sam = X.pspace.distribution._sample_random(size) for i in range(size): assert sam[i] in X.pspace.domain.set scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests for _sample_scipy.') else: with ignore_warnings(UserWarning): for X in distribs_scipy: sam = X.pspace.distribution._sample_scipy(size) for i in range(size): assert sam[i] in X.pspace.domain.set pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 not installed. Abort tests for _sample_pymc3.') else: with ignore_warnings(UserWarning): for X in distribs_pymc3: sam = X.pspace.distribution._sample_pymc3(size) for i in range(size): assert sam[i] in X.pspace.domain.set
def test_sample_scipy(): distribs_scipy = [ FiniteRV('F', {1: S.Half, 2: Rational(1, 4), 3: Rational(1, 4)}), DiscreteUniform("Y", list(range(5))), Die("D"), Bernoulli("Be", 0.3), Binomial("Bi", 5, 0.4), BetaBinomial("Bb", 2, 1, 1), Hypergeometric("H", 1, 1, 1), Rademacher("R") ] size = 3 numsamples = 5 scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests for _sample_scipy.') else: with ignore_warnings(UserWarning): h_sample = list(sample(Hypergeometric("H", 1, 1, 1), size=size, numsamples=numsamples)) assert len(h_sample) == numsamples for X in distribs_scipy: samps = next(sample(X, size=size)) samps2 = next(sample(X, size=(2, 2))) for sam in samps: assert sam in X.pspace.domain.set for i in range(2): for j in range(2): assert samps2[i][j] in X.pspace.domain.set
def test_sample_scipy(): p = S(2) / 3 x = Symbol('x', integer=True, positive=True) pdf = p * (1 - p)**(x - 1) # pdf of Geometric Distribution distribs_scipy = [ DiscreteRV(x, pdf, set=S.Naturals), Geometric('G', 0.5), Logarithmic('L', 0.5), NegativeBinomial('N', 5, 0.4), Poisson('P', 1), Skellam('S', 1, 1), YuleSimon('Y', 1), Zeta('Z', 2) ] size = 3 numsamples = 5 scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests for _sample_scipy.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed z_sample = list( sample(Zeta("G", 7), size=size, numsamples=numsamples)) assert len(z_sample) == numsamples for X in distribs_scipy: samps = next(sample(X, size=size, library='scipy')) samps2 = next(sample(X, size=(2, 2), library='scipy')) for sam in samps: assert sam in X.pspace.domain.set for i in range(2): for j in range(2): assert samps2[i][j] in X.pspace.domain.set
def test_ContinuousMarkovChain(): T1 = Matrix([[S(-2), S(2), S.Zero], [S.Zero, S.NegativeOne, S.One], [Rational(3, 2), Rational(3, 2), S(-3)]]) C1 = ContinuousMarkovChain('C', [0, 1, 2], T1) assert C1.limiting_distribution() == ImmutableMatrix([[Rational(3, 19), Rational(12, 19), Rational(4, 19)]]) T2 = Matrix([[-S.One, S.One, S.Zero], [S.One, -S.One, S.Zero], [S.Zero, S.One, -S.One]]) C2 = ContinuousMarkovChain('C', [0, 1, 2], T2) A, t = C2.generator_matrix, symbols('t', positive=True) assert C2.transition_probabilities(A)(t) == Matrix([[S.Half + exp(-2*t)/2, S.Half - exp(-2*t)/2, 0], [S.Half - exp(-2*t)/2, S.Half + exp(-2*t)/2, 0], [S.Half - exp(-t) + exp(-2*t)/2, S.Half - exp(-2*t)/2, exp(-t)]]) with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert P(Eq(C2(1), 1), Eq(C2(0), 1), evaluate=False) == Probability(Eq(C2(1), 1), Eq(C2(0), 1)) assert P(Eq(C2(1), 1), Eq(C2(0), 1)) == exp(-2)/2 + S.Half assert P(Eq(C2(1), 0) & Eq(C2(2), 1) & Eq(C2(3), 1), Eq(P(Eq(C2(1), 0)), S.Half)) == (Rational(1, 4) - exp(-2)/4)*(exp(-2)/2 + S.Half) assert P(Not(Eq(C2(1), 0) & Eq(C2(2), 1) & Eq(C2(3), 2)) | (Eq(C2(1), 0) & Eq(C2(2), 1) & Eq(C2(3), 2)), Eq(P(Eq(C2(1), 0)), Rational(1, 4)) & Eq(P(Eq(C2(1), 1)), Rational(1, 4))) is S.One assert E(C2(Rational(3, 2)), Eq(C2(0), 2)) == -exp(-3)/2 + 2*exp(Rational(-3, 2)) + S.Half assert variance(C2(Rational(3, 2)), Eq(C2(0), 1)) == ((S.Half - exp(-3)/2)**2*(exp(-3)/2 + S.Half) + (Rational(-1, 2) - exp(-3)/2)**2*(S.Half - exp(-3)/2)) raises(KeyError, lambda: P(Eq(C2(1), 0), Eq(P(Eq(C2(1), 1)), S.Half))) assert P(Eq(C2(1), 0), Eq(P(Eq(C2(5), 1)), S.Half)) == Probability(Eq(C2(1), 0)) TS1 = MatrixSymbol('G', 3, 3) CS1 = ContinuousMarkovChain('C', [0, 1, 2], TS1) A = CS1.generator_matrix assert CS1.transition_probabilities(A)(t) == exp(t*A) C3 = ContinuousMarkovChain('C', [Symbol('0'), Symbol('1'), Symbol('2')], T2) assert P(Eq(C3(1), 1), Eq(C3(0), 1)) == exp(-2)/2 + S.Half assert P(Eq(C3(1), Symbol('1')), Eq(C3(0), Symbol('1'))) == exp(-2)/2 + S.Half
def test_auto_point_vel_shortest_path(): t = dynamicsymbols._t q1, q2, u1, u2 = dynamicsymbols('q1 q2 u1 u2') B = ReferenceFrame('B') P = Point('P') P.set_vel(B, u1 * B.x) P1 = Point('P1') P1.set_pos(P, q2 * B.y) P1.set_vel(B, q1 * B.z) P2 = Point('P2') P2.set_pos(P1, q1 * B.z) P3 = Point('P3') P3.set_pos(P2, 10 * q1 * B.y) P4 = Point('P4') P4.set_pos(P3, q1 * B.x) O = Point('O') O.set_vel(B, u2 * B.y) O1 = Point('O1') O1.set_pos(O, q2 * B.z) P4.set_pos(O1, q1 * B.x + q2 * B.z) with warnings.catch_warnings( ): #There are two possible paths in this point tree, thus a warning is raised warnings.simplefilter('error') with ignore_warnings(UserWarning): assert P4.vel( B) == q1.diff(t) * B.x + u2 * B.y + 2 * q2.diff(t) * B.z
def test_sample_discrete(): X = Geometric('X', S.Half) scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests') with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(X)) in X.pspace.domain.set samps = next(sample( X, size=2)) # This takes long time if ran without scipy for samp in samps: assert samp in X.pspace.domain.set libraries = ['scipy', 'numpy', 'pymc3'] for lib in libraries: try: imported_lib = import_module(lib) if imported_lib: s0, s1, s2 = [], [], [] s0 = list(sample(X, numsamples=10, library=lib, seed=0)) s1 = list(sample(X, numsamples=10, library=lib, seed=0)) s2 = list(sample(X, numsamples=10, library=lib, seed=1)) assert s0 == s1 assert s1 != s2 except NotImplementedError: continue
def test_unevaluated_CompoundDist(): # these tests need to be removed once they work with evaluation as they are currently not # evaluated completely in sympy. R = Rayleigh('R', 4) X = Normal('X', 3, R) ans = ''' Piecewise(((-sqrt(pi)*sinh(x/4 - 3/4) + sqrt(pi)*cosh(x/4 - 3/4))/( 8*sqrt(pi)), Abs(arg(x - 3)) <= pi/4), (Integral(sqrt(2)*exp(-(x - 3) **2/(2*R**2))*exp(-R**2/32)/(32*sqrt(pi)), (R, 0, oo)), True))''' assert streq(density(X)(x), ans) expre = ''' Integral(X*Integral(sqrt(2)*exp(-(X-3)**2/(2*R**2))*exp(-R**2/32)/(32* sqrt(pi)),(R,0,oo)),(X,-oo,oo))''' with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert streq(E(X, evaluate=False).rewrite(Integral), expre) X = Poisson('X', 1) Y = Poisson('Y', X) Z = Poisson('Z', Y) exprd = Sum( exp(-Y) * Y**x * Sum( exp(-1) * exp(-X) * X**Y / (factorial(X) * factorial(Y)), (X, 0, oo)) / factorial(x), (Y, 0, oo)) assert density(Z)(x) == exprd N = Normal('N', 1, 2) M = Normal('M', 3, 4) D = Normal('D', M, N) exprd = ''' Integral(sqrt(2)*exp(-(N-1)**2/8)*Integral(exp(-(x-M)**2/(2*N**2))*exp (-(M-3)**2/32)/(8*pi*N),(M,-oo,oo))/(4*sqrt(pi)),(N,-oo,oo))''' assert streq(density(D, evaluate=False)(x), exprd)
def test_Sample(): X = Die('X', 6) Y = Normal('Y', 0, 1) z = Symbol('z', integer=True) scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(X)) in [1, 2, 3, 4, 5, 6] assert isinstance(next(sample(X + Y)), float) assert P(X + Y > 0, Y < 0, numsamples=10).is_number assert E(X + Y, numsamples=10).is_number assert E(X**2 + Y, numsamples=10).is_number assert E((X + Y)**2, numsamples=10).is_number assert variance(X + Y, numsamples=10).is_number raises(TypeError, lambda: P(Y > z, numsamples=5)) assert P(sin(Y) <= 1, numsamples=10) == 1 assert P(sin(Y) <= 1, cos(Y) < 1, numsamples=10) == 1 assert all(i in range(1, 7) for i in density(X, numsamples=10)) assert all(i in range(4, 7) for i in density(X, X > 3, numsamples=10))
def test_sample_pymc3(): distribs_pymc3 = [ Beta("B", 1, 1), Cauchy("C", 1, 1), Normal("N", 0, 1), Gamma("G", 2, 7), GaussianInverse("GI", 1, 1), Exponential("E", 2), LogNormal("LN", 0, 1), Pareto("P", 1, 1), ChiSquared("CS", 2), Uniform("U", 0, 1) ] size = 3 pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 is not installed. Abort tests for _sample_pymc3.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_pymc3: samps = next(sample(X, size=size, library='pymc3')) for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: next(sample(Chi("C", 1), library='pymc3')))
def test_sample_numpy(): distribs_numpy = [ Beta("B", 1, 1), Normal("N", 0, 1), Gamma("G", 2, 7), Exponential("E", 2), LogNormal("LN", 0, 1), Pareto("P", 1, 1), ChiSquared("CS", 2), Uniform("U", 0, 1) ] size = 3 numpy = import_module('numpy') if not numpy: skip('Numpy is not installed. Abort tests for _sample_numpy.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_numpy: samps = next(sample(X, size=size, library='numpy')) for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: next(sample(Chi("C", 1), library='numpy'))) raises( NotImplementedError, lambda: Chi("C", 1).pspace.distribution.sample(library='tensorflow'))
def test_sample_continuous(): z = Symbol('z') Z = ContinuousRV(z, exp(-z), set=Interval(0, oo)) assert density(Z)(-1) == 0 scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(Z)) in Z.pspace.domain.set sym, val = list(Z.pspace.sample().items())[0] assert sym == Z and val in Interval(0, oo) libraries = ['scipy', 'numpy', 'pymc3'] for lib in libraries: try: imported_lib = import_module(lib) if imported_lib: s0, s1, s2 = [], [], [] s0 = list(sample(Z, numsamples=10, library=lib, seed=0)) s1 = list(sample(Z, numsamples=10, library=lib, seed=0)) s2 = list(sample(Z, numsamples=10, library=lib, seed=1)) assert s0 == s1 assert s1 != s2 except NotImplementedError: continue
def test_unevaluated_CompoundDist(): # these tests need to be removed once they work with evaluation as they are currently not # evaluated completely in sympy. R = Rayleigh('R', 4) X = Normal('X', 3, R) _k = Dummy('k') exprd = Piecewise((exp(S(3)/4 - x/4)/8, 2*Abs(arg(x - 3)) <= pi/2), (sqrt(2)*Integral(exp(-(_k**4 + 16*(x - 3)**2)/(32*_k**2)), (_k, 0, oo))/(32*sqrt(pi)), True)) assert (density(X)(x).simplify()).dummy_eq(exprd.simplify()) expre = Integral(_k*Integral(sqrt(2)*exp(-_k**2/32)*exp(-(_k - 3)**2/(2*_k**2) )/(32*sqrt(pi)), (_k, 0, oo)), (_k, -oo, oo)) with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert E(X, evaluate=False).rewrite(Integral).dummy_eq(expre) X = Poisson('X', 1) Y = Poisson('Y', X) Z = Poisson('Z', Y) exprd = exp(-1)*Sum(exp(-Y)*Y**x*Sum(exp(-X)*X**Y/(factorial(X)*factorial(Y) ), (X, 0, oo)), (Y, 0, oo))/factorial(x) assert density(Z)(x).simplify() == exprd N = Normal('N', 1, 2) M = Normal('M', 3, 4) D = Normal('D', M, N) exprd = Integral(sqrt(2)*exp(-(_k - 1)**2/8)*Integral(exp(-(-_k + x )**2/(2*_k**2))*exp(-(_k - 3)**2/32)/(8*pi*_k) , (_k, -oo, oo))/(4*sqrt(pi)), (_k, -oo, oo)) assert density(D, evaluate=False)(x).dummy_eq(exprd)
def test_lognormal_sampling(): # Right now, only density function and sampling works scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed for i in range(3): X = LogNormal('x', i, 1) assert next(sample(X)) in X.pspace.domain.set size = 5 with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed samps = next(sample(X, size=size)) for samp in samps: assert samp in X.pspace.domain.set
def test_given_sample(): X = Die('X', 6) scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(X, X > 5)) == 6
def test_sampling_methods(): distribs_numpy = [ Geometric('G', 0.5), Poisson('P', 1), Zeta('Z', 2) ] distribs_scipy = [ Geometric('G', 0.5), Logarithmic('L', 0.5), Poisson('P', 1), Skellam('S', 1, 1), YuleSimon('Y', 1), Zeta('Z', 2) ] distribs_pymc3 = [ Geometric('G', 0.5), Poisson('P', 1), ] size = 3 numpy = import_module('numpy') if not numpy: skip('Numpy is not installed. Abort tests for _sample_numpy.') else: with ignore_warnings(UserWarning): for X in distribs_numpy: samps = X.pspace.distribution._sample_numpy(size) for samp in samps: assert samp in X.pspace.domain.set scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests for _sample_scipy.') else: with ignore_warnings(UserWarning): for X in distribs_scipy: samps = next(sample(X, size=size)) for samp in samps: assert samp in X.pspace.domain.set pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 is not installed. Abort tests for _sample_pymc3.') else: with ignore_warnings(UserWarning): for X in distribs_pymc3: samps = X.pspace.distribution._sample_pymc3(size) for samp in samps: assert samp in X.pspace.domain.set
def test_ignore_continues_after_warning(): with warnings.catch_warnings(record=True) as w: finished = False with ignore_warnings(UserWarning): warnings.warn('this is the warning message') finished = True assert finished assert len(w) == 0
def test_sampling_gamma_inverse(): scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests for sampling of gamma inverse.') X = GammaInverse("x", 1, 1) with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(X)) in X.pspace.domain.set
def test_Logarithmic(): p = S.Half x = Logarithmic('x', p) assert E(x) == -p / ((1 - p) * log(1 - p)) assert variance(x) == -1/log(2)**2 + 2/log(2) assert E(2*x**2 + 3*x + 4) == 4 + 7 / log(2) with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert isinstance(E(x, evaluate=False), Expectation)
def test_given(): X = Die('X', 6) assert density(X, X > 5) == {S(6): S.One} assert where(X > 2, X > 5).as_boolean() == Eq(X.symbol, 6) scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') with ignore_warnings(UserWarning): assert next(sample(X, X > 5)) == 6
def test_differentiate_finite(): x, y, h = symbols('x y h') f = Function('f') with ignore_warnings(SymPyDeprecationWarning): res0 = differentiate_finite(f(x, y) + exp(42), x, y, evaluate=True) xm, xp, ym, yp = [ v + sign * S.Half for v, sign in product([x, y], [-1, 1]) ] ref0 = f(xm, ym) + f(xp, yp) - f(xm, yp) - f(xp, ym) assert (res0 - ref0).simplify() == 0 g = Function('g') with ignore_warnings(SymPyDeprecationWarning): res1 = differentiate_finite(f(x) * g(x) + 42, x, evaluate=True) ref1 = (-f(x - S.Half) + f(x + S.Half))*g(x) + \ (-g(x - S.Half) + g(x + S.Half))*f(x) assert (res1 - ref1).simplify() == 0 res2 = differentiate_finite(f(x) + x**3 + 42, x, points=[x - 1, x + 1]) ref2 = (f(x + 1) + (x + 1)**3 - f(x - 1) - (x - 1)**3) / 2 assert (res2 - ref2).simplify() == 0 raises(TypeError, lambda: differentiate_finite(f(x) * g(x), x, pints=[x - 1, x + 1])) res3 = differentiate_finite(f(x) * g(x).diff(x), x) ref3 = (-g(x) + g(x + 1)) * f(x + S.Half) - (g(x) - g(x - 1)) * f(x - S.Half) assert res3 == ref3 res4 = differentiate_finite(f(x) * g(x).diff(x).diff(x), x) ref4 = -((g(x - Rational(3, 2)) - 2*g(x - S.Half) + g(x + S.Half))*f(x - S.Half)) \ + (g(x - S.Half) - 2*g(x + S.Half) + g(x + Rational(3, 2)))*f(x + S.Half) assert res4 == ref4 res5_expr = f(x).diff(x) * g(x).diff(x) res5 = differentiate_finite(res5_expr, points=[x - h, x, x + h]) ref5 = (-2*f(x)/h + f(-h + x)/(2*h) + 3*f(h + x)/(2*h))*(-2*g(x)/h + g(-h + x)/(2*h) \ + 3*g(h + x)/(2*h))/(2*h) - (2*f(x)/h - 3*f(-h + x)/(2*h) - \ f(h + x)/(2*h))*(2*g(x)/h - 3*g(-h + x)/(2*h) - g(h + x)/(2*h))/(2*h) assert res5 == ref5 res6 = res5.limit(h, 0).doit() ref6 = diff(res5_expr, x) assert res6 == ref6
def test_negative_binomial(): r = 5 p = S.One / 3 x = NegativeBinomial('x', r, p) assert E(x) == p*r / (1-p) # This hangs when run with the cache disabled: assert variance(x) == p*r / (1-p)**2 assert E(x**5 + 2*x + 3) == Rational(9207, 4) with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert isinstance(E(x, evaluate=False), Expectation)
def test_sample_discrete(): X = Geometric('X', S.Half) scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests') with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(X)) in X.pspace.domain.set samps = next(sample(X, size=2)) # This takes long time if ran without scipy for samp in samps: assert samp in X.pspace.domain.set
def test_yule_simon(): from sympy import S rho = S(3) x = YuleSimon('x', rho) assert simplify(E(x)) == rho / (rho - 1) assert simplify(variance(x)) == rho**2 / ((rho - 1)**2 * (rho - 2)) with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert isinstance(E(x, evaluate=False), Expectation) # To test the cdf function assert cdf(x)(x) == Piecewise((-beta(floor(x), 4)*floor(x) + 1, x >= 1), (0, True))
def test_ignore_allows_other_warnings(): with warnings.catch_warnings(record=True) as w: # This is needed when pytest is run as -Werror # the setting is reverted at the end of the catch_Warnings block. warnings.simplefilter("always") with ignore_warnings(UserWarning): warnings.warn('this is the warning message', UserWarning) warnings.warn('this is the other message', RuntimeWarning) assert len(w) == 1 assert isinstance(w[0].message, RuntimeWarning) assert str(w[0].message) == 'this is the other message'
def test_issue_11463(): numpy = import_module('numpy') if not numpy: skip("numpy not installed.") x = Symbol('x') f = lambdify(x, real_root((log(x/(x-2))), 3), 'numpy') # numpy.select evaluates all options before considering conditions, # so it raises a warning about root of negative number which does # not affect the outcome. This warning is suppressed here with ignore_warnings(RuntimeWarning): assert f(numpy.array(-1)) < -1
def test_Poisson(): l = 3 x = Poisson('x', l) assert E(x) == l assert variance(x) == l assert density(x) == PoissonDistribution(l) with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert isinstance(E(x, evaluate=False), Expectation) assert isinstance(E(2*x, evaluate=False), Expectation) # issue 8248 assert x.pspace.compute_expectation(1) == 1
def test_mix_expression(): Y, E = Poisson('Y', 1), Exponential('E', 1) k = Dummy('k') expr1 = Integral(Sum(exp(-1)*Integral(exp(-k)*DiracDelta(k - 2), (k, 0, oo) )/factorial(k), (k, 0, oo)), (k, -oo, 0)) expr2 = Integral(Sum(exp(-1)*Integral(exp(-k)*DiracDelta(k - 2), (k, 0, oo) )/factorial(k), (k, 0, oo)), (k, 0, oo)) assert P(Eq(Y + E, 1)) == 0 assert P(Ne(Y + E, 2)) == 1 with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert P(E + Y < 2, evaluate=False).rewrite(Integral).dummy_eq(expr1) assert P(E + Y > 2, evaluate=False).rewrite(Integral).dummy_eq(expr2)
def test_ignore_many_warnings(): with warnings.catch_warnings(record=True) as w: # This is needed when pytest is run as -Werror # the setting is reverted at the end of the catch_Warnings block. warnings.simplefilter("always") with ignore_warnings(UserWarning): warnings.warn("this is the warning message", UserWarning) warnings.warn("this is the other message", RuntimeWarning) warnings.warn("this is the warning message", UserWarning) warnings.warn("this is the other message", RuntimeWarning) warnings.warn("this is the other message", RuntimeWarning) assert len(w) == 3 for wi in w: assert isinstance(wi.message, RuntimeWarning) assert str(wi.message) == "this is the other message"
def test_sample_pymc3(): distribs_pymc3 = [Bernoulli('B', 0.2), Binomial('N', 5, 0.4)] size = 3 pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 is not installed. Abort tests for _sample_pymc3.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_pymc3: samps = next(sample(X, size=size, library='pymc3')) for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: next(sample(Die("D"), library='pymc3')))
def test_product_spaces(): X1 = Geometric('X1', S.Half) X2 = Geometric('X2', Rational(1, 3)) #assert str(P(X1 + X2 < 3, evaluate=False)) == """Sum(Piecewise((2**(X2 - n - 2)*(2/3)**(X2 - 1)/6, """\ # + """(-X2 + n + 3 >= 1) & (-X2 + n + 3 < oo)), (0, True)), (X2, 1, oo), (n, -oo, -1))""" n = Dummy('n') with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed assert P(X1 + X2 < 3, evaluate=False).rewrite(Sum).dummy_eq(Sum(Piecewise((2**(-n)/4, n + 2 >= 1), (0, True)), (n, -oo, -1))/3) #assert str(P(X1 + X2 > 3)) == """Sum(Piecewise((2**(X2 - n - 2)*(2/3)**(X2 - 1)/6, """ +\ # """(-X2 + n + 3 >= 1) & (-X2 + n + 3 < oo)), (0, True)), (X2, 1, oo), (n, 1, oo))""" assert P(X1 + X2 > 3).dummy_eq(Sum(Piecewise((2**(X2 - n - 2)*(Rational(2, 3))**(X2 - 1)/6, -X2 + n + 3 >= 1), (0, True)), (X2, 1, oo), (n, 1, oo))) # assert str(P(Eq(X1 + X2, 3))) == """Sum(Piecewise((2**(X2 - 2)*(2/3)**(X2 - 1)/6, """ +\ # """X2 <= 2), (0, True)), (X2, 1, oo))""" assert P(Eq(X1 + X2, 3)) == Rational(1, 12)