def test_domains(): x, y = symbols('x y') X, Y= Die(6, symbol=x), Die(6, symbol=y) # Domains d = Where(X>Y) assert d.condition == (x > y) d = Where(And(X>Y, Y>3)) assert d.as_boolean() == Or(And(Eq(x,5), Eq(y,4)), And(Eq(x,6), Eq(y,5)), And(Eq(x,6), Eq(y,4))) assert len(d.elements) == 3 assert len(pspace(X+Y).domain.elements) == 36 Z = Die(4, symbol=x) raises(ValueError, "P(X>Z)") # Two domains with same internal symbol pspace(X+Y).domain.set == FiniteSet(1,2,3,4,5,6)**2 assert Where(X>3).set == FiniteSet(4,5,6) assert X.pspace.domain.dict == FiniteSet( Dict({X.symbol:i}) for i in range(1,7)) assert Where(X>Y).dict == FiniteSet(Dict({X.symbol:i, Y.symbol:j}) for i in range(1,7) for j in range(1,7) if i>j)
def test_ContinuousDomain(): X = Normal(0,1) assert Where(X**2<=1).set == Interval(-1,1) assert Where(X**2<=1).symbol == X.symbol Where(And(X**2<=1, X>=0)).set == Interval(0,1) raises(ValueError, "Where(sin(X)>1)") Y = Given(X, X>=0) assert Y.pspace.domain.set == Interval(0, oo)
def test_RandomDomain(): from sympy.stats import Normal, Die, Exponential, pspace, Where X = Normal(0, 1, symbol=Symbol('x1')) assert str(Where(X > 0)) == "Domain: 0 < x1" D = Die(6, symbol=Symbol('d1')) assert str(Where(D > 4)) == "Domain: Or(d1 == 5, d1 == 6)" A = Exponential(1, symbol=Symbol('a')) B = Exponential(1, symbol=Symbol('b')) assert str(pspace(Tuple(A, B)).domain) == "Domain: And(0 <= a, 0 <= b)"
def test_latex_RandomDomain(): from sympy.stats import Normal, Die, Exponential, pspace, Where X = Normal(0, 1, symbol=Symbol('x1')) assert latex(Where(X > 0)) == "Domain: 0 < x_{1}" D = Die(6, symbol=Symbol('d1')) assert latex(Where(D > 4)) == r"Domain: d_{1} = 5 \vee d_{1} = 6" A = Exponential(1, symbol=Symbol('a')) B = Exponential(1, symbol=Symbol('b')) assert latex(pspace(Tuple(A, B)).domain) == "Domain: 0 \leq a \wedge 0 \leq b"
def test_domains(): x, y = symbols('x y') X, Y = Die(6, symbol=x), Die(6, symbol=y) # Domains d = Where(X > Y) assert d.condition == (x > y) d = Where(And(X > Y, Y > 3)) assert d.as_boolean() == Or(And(Eq(x, 5), Eq(y, 4)), And(Eq(x, 6), Eq(y, 5)), And(Eq(x, 6), Eq(y, 4))) assert len(d.elements) == 3 assert len(pspace(X + Y).domain.elements) == 36 Z = Die(4, symbol=x) raises(ValueError, "P(X>Z)") # Two domains with same internal symbol pspace(X + Y).domain.set == FiniteSet(1, 2, 3, 4, 5, 6)**2 assert Where(X > 3).set == FiniteSet(4, 5, 6) assert X.pspace.domain.dict == FiniteSet( Dict({X.symbol: i}) for i in range(1, 7)) assert Where(X > Y).dict == FiniteSet( Dict({ X.symbol: i, Y.symbol: j }) for i in range(1, 7) for j in range(1, 7) if i > j)
def test_exponential(): rate = Symbol('lambda', positive=True, real=True, bounded=True) X = Exponential(rate) assert E(X) == 1/rate assert Var(X) == 1/rate**2 assert Skewness(X) == 2 assert P(X>0) == S(1) assert P(X>1) == exp(-rate) assert P(X>10) == exp(-10*rate) assert Where(X<=1).set == Interval(0,1)
def test_dice(): X, Y, Z = Die(6), Die(6), Die(6) a, b = symbols('a b') assert E(X) == 3 + S.Half assert Var(X) == S(35) / 12 assert E(X + Y) == 7 assert E(X + X) == 7 assert E(a * X + b) == a * E(X) + b assert Var(X + Y) == Var(X) + Var(Y) assert Var(X + X) == 4 * Var(X) assert Covar(X, Y) == S.Zero assert Covar(X, X + Y) == Var(X) assert Density(Eq(cos(X * S.Pi), 1))[True] == S.Half assert P(X > 3) == S.Half assert P(2 * X > 6) == S.Half assert P(X > Y) == S(5) / 12 assert P(Eq(X, Y)) == P(Eq(X, 1)) assert E(X, X > 3) == 5 assert E(X, Y > 3) == E(X) assert E(X + Y, Eq(X, Y)) == E(2 * X) assert E(X + Y - Z, 2 * X > Y + 1) == S(49) / 12 assert P(X > 3, X > 3) == S.One assert P(X > Y, Eq(Y, 6)) == S.Zero assert P(Eq(X + Y, 12)) == S.One / 36 assert P(Eq(X + Y, 12), Eq(X, 6)) == S.One / 6 assert Density(X + Y) == Density(Y + Z) != Density(X + X) d = Density(2 * X + Y**Z) assert d[S(22)] == S.One / 108 and d[S(4100)] == S.One / 216 and S( 3130) not in d assert pspace(X).domain.as_boolean() == Or( *[Eq(X.symbol, i) for i in [1, 2, 3, 4, 5, 6]]) assert Where(X > 3).set == FiniteSet(4, 5, 6)
def test_where(): X, Y = Die(), Die() Z = Normal(0, 1) assert Where(Z**2 <= 1).set == Interval(-1, 1) assert Where(Z**2 <= 1).as_boolean() == Interval(-1, 1).as_relational(Z.symbol) assert Where(And(X > Y, Y > 4)).as_boolean() == And(Eq(X.symbol, 6), Eq(Y.symbol, 5)) assert len(Where(X < 3).set) == 2 assert 1 in Where(X < 3).set X, Y = Normal(0, 1), Normal(0, 1) assert Where(And(X**2 <= 1, X >= 0)).set == Interval(0, 1) XX = Given(X, And(X**2 <= 1, X >= 0)) assert XX.pspace.domain.set == Interval(0, 1) assert XX.pspace.domain.as_boolean() == And(0 <= X.symbol, X.symbol**2 <= 1) raises(TypeError, "XX = Given(X, X+3)")
def test_given(): X = Die(6) Density(X, X > 5) == {S(6): S(1)} Where(X > 2, X > 5).as_boolean() == Eq(X.symbol, 6) Sample(X, X > 5) == 6