def test_solver_22(): # 2 < abs(X) < 5 event = (2 < abs(Y)) < 5 solution = Interval.open(2, 5) | Interval.open(-5, -2) assert event.solve() == solution # 2 <= abs(X) < 5 event = (2 <= abs(Y)) < 5 solution = Interval.Ropen(2, 5) | Interval.Lopen(-5, -2) assert event.solve() == solution # 2 < abs(X) <= 5 event = (2 < abs(Y)) <= 5 solution = Interval.Lopen(2, 5) | Interval.Ropen(-5, -2) assert event.solve() == solution # 2 <= abs(X) <= 5 event = (2 <= abs(Y)) <= 5 solution = Interval(2, 5) | Interval(-5, -2) assert event.solve() == solution # -2 < abs(X) < 5 event = (-2 < abs(Y)) < 5 solution = Interval.open(-5, 5) assert event.solve() == solution # # -2 <= abs(X) < 5 event = (-2 <= abs(Y)) < 5 solution = Interval.open(-5, 5) assert event.solve() == solution # -2 < abs(X) <= 5 event = (-2 < abs(Y)) <= 5 solution = Interval(-5, 5) assert event.solve() == solution # 2 <= abs(X) <= 5 event = (-2 <= abs(Y)) <= 5 solution = Interval(-5, 5) assert event.solve() == solution
def test_FiniteReal_or(): assert FR(1) | FR(2) == FR(1, 2) assert FR(1, 2) | FR(2) == FR(1, 2) assert FR(1, 2) | FN('2') == Union(FR(1, 2), FN('2')) assert FR(1, 2) | Interval(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(1, 2) | Interval.Ropen(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(0, 1, 2) | Interval.open(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(0, 2) | Interval.Lopen(0, 1) == Union(Interval(0, 1), FR(2)) assert FR(0, 2) | Interval.Lopen(2.5, 10) == Union(Interval.Lopen(2.5, 10), FR(0, 2)) assert FR(-1, 1) | Interval(-10, 10) == Interval(-10, 10) assert FR(-1, 11) | Interval(-10, 10) == Union(Interval(-10, 10), FR(11))
def test_Interval_in(): with pytest.raises(Exception): Interval(3, 1) assert 1 in Interval(0, 1) assert 1 not in Interval.Ropen(0, 1) assert 1 in Interval.Lopen(0, 1) assert 0 in Interval(0, 1) assert 0 not in Interval.Lopen(0, 1) assert 0 in Interval.Ropen(0, 1) assert inf not in Interval(-inf, inf) assert -inf not in Interval(-inf, 0) assert 10 in Interval(-inf, inf)
def test_solver_24_negative_power_Rat(): # Case 1. event = Y**Rat(-1, 3) < 6 assert event.solve() == Interval.Lopen(Rat(1, 216), oo) # Case 2. event = (-1 < Y**Rat(-1, 3)) < 6 assert event.solve() == Interval.Lopen(Rat(1, 216), oo) # Case 3. event = 5 <= Y**Rat(-1, 3) assert event.solve() == Interval.Lopen(0, Rat(1, 125)) # Case 4. event = (5 <= Y**Rat(-1, 3)) < 6 assert event.solve() == Interval.Lopen(Rat(1, 216), Rat(1, 125))
def test_product_condition_or_probabilithy_zero(): X = Id('X') Y = Id('Y') spe = ProductSPE([X >> norm(loc=0, scale=1), Y >> gamma(a=1)]) # Condition on event which has probability zero. event = (X > 2) & (X < 2) with pytest.raises(ValueError): spe.condition(event) assert spe.logprob(event) == -float('inf') # Condition on event which has probability zero. event = (Y < 0) | (Y < -1) with pytest.raises(ValueError): spe.condition(event) assert spe.logprob(event) == -float('inf') # Condition on an event where one clause has probability # zero, yielding a single product. spe_condition = spe.condition((Y < 0) | ((Log(X) >= 0) & (1 <= Y))) assert isinstance(spe_condition, ProductSPE) assert spe_condition.children[0].symbol == X assert spe_condition.children[0].conditioned assert spe_condition.children[0].support == Interval(1, oo) assert spe_condition.children[1].symbol == Y assert spe_condition.children[1].conditioned assert spe_condition.children[0].support == Interval(1, oo) # We have (X < 2) & ~(1 < exp(|3X**2|) is empty. # Thus Y remains unconditioned, # and X is partitioned into (-oo, 0) U (0, oo) with equal weight. event = (Exp(abs(3 * X**2)) > 1) | ((Log(Y) < 0.5) & (X < 2)) spe_condition = spe.condition(event) # # The most concise representation of spe_condition is: # (Product (Sum [.5 .5] X|X<0 X|X>0) Y) assert isinstance(spe_condition, ProductSPE) assert isinstance(spe_condition.children[0], SumSPE) assert spe_condition.children[0].weights == (-log(2), -log(2)) assert spe_condition.children[0].children[0].conditioned assert spe_condition.children[0].children[1].conditioned assert spe_condition.children[0].children[0].support \ in [Interval.Ropen(-oo, 0), Interval.Lopen(0, oo)] assert spe_condition.children[0].children[1].support \ in [Interval.Ropen(-oo, 0), Interval.Lopen(0, oo)] assert spe_condition.children[0].children[0].support \ != spe_condition.children[0].children[1].support assert spe_condition.children[1].symbol == Y assert not spe_condition.children[1].conditioned
def test_solver_23_reciprocal_lte(): for c in [1, 3]: # Positive # 1 / X < 10 solution = Interval.Ropen(-oo, 0) | Interval.Lopen(Rat(c, 10), oo) event = (c / Y) < 10 assert event.solve() == solution # 1 / X <= 10 solution = Interval.Ropen(-oo, 0) | Interval(Rat(c, 10), oo) event = (c / Y) <= 10 assert event.solve() == solution # 1 / X <= sqrt(2) solution = Interval.Ropen(-oo, 0) | Interval(c / sympy.sqrt(2), oo) event = (c / Y) <= sympy.sqrt(2) assert event.solve() == solution # Negative. # 1 / X < -10 solution = Interval.open(-Rat(c, 10), 0) event = (c / Y) < -10 assert event.solve() == solution # 1 / X <= -10 solution = Interval.Ropen(-Rat(c, 10), 0) event = (c / Y) <= -10 assert event.solve() == solution # 1 / X <= -sqrt(2) solution = Interval.Ropen(-c / sympy.sqrt(2), 0) event = (c / Y) <= -sympy.sqrt(2) assert event.solve() == solution
def test_solver_24_negative_power_integer(): # Case 1. event = Y**(-3) < 6 assert event.solve() == Union( Interval.open(-oo, 0), Interval.open(6**Rat(-1, 3), oo)) # Case 2. event = (-1 < Y**(-3)) < 6 assert event.solve() == Union( Interval.open(-oo, -1), Interval.open(6**Rat(-1, 3), oo)) # Case 3. event = 5 <= Y**(-3) assert event.solve() == Interval.Lopen(0, 5**Rat(-1, 3)) # Case 4. event = (5 <= Y**(-3)) < 6 assert event.solve() == Interval.Lopen(6**Rat(-1, 3), 5**Rat(-1, 3))
def test_union_intervals_finite(): assert union_intervals_finite([ Interval.open(0,1), Interval(2,3), Interval.Lopen(1,2) ], FR(1)) \ == [Interval.Lopen(0, 3)] assert union_intervals_finite([ Interval.open(0,1), Interval.open(2, 3), Interval.open(1,2) ], FR(1, 3)) \ == [Interval.open(0, 2), Interval.Lopen(2, 3)] assert union_intervals_finite([ Interval.open(0,1), Interval.open(1, 3), Interval.open(11,15) ], FR(1, -11, -19, 3)) \ == [Interval.Lopen(0, 3), Interval.open(11,15), FR(-11, -19)]
def test_FiniteReal_and(): assert FR(1) & FR(2) is EmptySet assert FR(1, 2) & FR(2) == FR(2) assert FR(1, 2) & FN('2') is EmptySet assert FR(1, 2) & FN('2', b=True) is EmptySet assert FR(1, 2) & Interval(0, 1) == FR(1) assert FR(1, 2) & Interval.Ropen(0, 1) is EmptySet assert FR(0, 2) & Interval(0, 1) == FR(0) assert FR(0, 2) & Interval.Lopen(0, 1) is EmptySet assert FR(-1, 1) & Interval(-10, 10) == FR(-1, 1) assert FR(-1, 11) & Interval(-10, 10) == FR(-1)
def test_solver_23_reciprocal_gte(): for c in [1, 3]: # Positive # 10 < 1 / X solution = Interval.open(0, Rat(c, 10)) event = 10 < (c / Y) assert event.solve() == solution # 10 <= 1 / X solution = Interval.Lopen(0, Rat(c, 10)) event = 10 <= (c / Y) assert event.solve() == solution # Negative # -10 < 1 / X solution = Interval.Lopen(0, oo) | Interval.open(-oo, -Rat(c, 10)) event = -10 < (c / Y) assert event.solve() == solution # -10 <= 1 / X solution = Interval.Lopen(0, oo) | Interval.Lopen(-oo, -Rat(c, 10)) event = -10 <= (c / Y) assert event.solve() == solution
def test_event_inequality_parse(): assert (5 < (X < 10)) \ == ((5 < X) < 10) \ == EventInterval(X, Interval.open(5, 10)) assert (5 <= (X < 10)) \ == ((5 <= X) < 10) \ == EventInterval(X, Interval.Ropen(5, 10)) assert (5 < (X <= 10)) \ == ((5 < X) <= 10) \ == EventInterval(X, Interval.Lopen(5, 10)) assert (5 <= (X <= 10)) \ == ((5 <= X) <= 10) \ == EventInterval(X, Interval(5, 10)) # GOTCHA: This expression is syntactically equivalent to # (5 < X) and (X < 10) # Since and short circuits and 5 < X is not False, # return value of expression is X < 10 assert (5 < X < 10) == (X < 10) # Yields a finite set . assert ((5 < X) < 5) == EventFiniteReal(X, EmptySet) assert ((5 < X) <= 5) == EventFiniteReal(X, EmptySet) assert ((5 <= X) < 5) == EventFiniteReal(X, EmptySet) assert ((5 <= X) <= 5) == EventFiniteReal(X, FiniteReal(5)) # Negated single interval. assert ~(5 < X) == (X <= 5) assert ~(5 <= X) == (X < 5) assert ~(X < 5) == (5 <= X) assert ~(X <= 5) == (5 < X) # Negated union of two intervals. assert ~(5 < (X < 10)) \ == ~((5 < X) < 10) \ == (X <= 5) | (10 <= X) assert ~(5 <= (X < 10)) \ == ~((5 <= X) < 10) \ == (X < 5) | (10 <= X) assert ~(5 < (X <= 10)) \ == ~((5 < X) <= 10) \ == (X <= 5) | (10 < X) assert ~(5 <= (X <= 10)) \ == ~((5 <= X) <= 10) \ == (X < 5) | (10 < X) assert ~((10 < X) < 5) \ == ~(10 < (X < 5)) \ == EventInterval(X, Interval(-oo, oo)) # A complicated negated union. assert ((~(X < 5)) < 10) \ == ((5 <= X) < 10) \ == EventInterval(X, Interval.Ropen(5, 10))
def test_union_intervals(): assert union_intervals([Interval(0, 1), Interval(2, 3), Interval(1, 2)]) == [Interval(0, 3)] assert union_intervals( [Interval.open(0, 1), Interval(2, 3), Interval(1, 2)]) == [Interval.Lopen(0, 3)] assert union_intervals( [Interval.open(0, 1), Interval(2, 3), Interval.Lopen(1, 2)]) == [Interval.open(0, 1), Interval.Lopen(1, 3)] assert union_intervals( [Interval.open(0, 1), Interval.Ropen(0, 3), Interval.Lopen(1, 2)]) == [Interval.Ropen(0, 3)] assert union_intervals( [Interval.open(-2, -1), Interval.Ropen(0, 3), Interval.Lopen(1, 2)]) == [Interval.open(-2, -1), Interval.Ropen(0, 3)]
def test_event_containment_real(): assert (X << Interval(0, 10)) == EventInterval(X, Interval(0, 10)) for values in [FiniteReal(0, 10), [0, 10], {0, 10}]: assert (X << values) == EventFiniteReal(X, FiniteReal(0, 10)) # with pytest.raises(ValueError): # X << {1, None} assert X << {1, 2} == EventFiniteReal(X, {1, 2}) assert ~(X << {1, 2}) == EventOr([ EventInterval(X, Interval.Ropen(-oo, 1)), EventInterval(X, Interval.open(1, 2)), EventInterval(X, Interval.Lopen(2, oo)), ]) # https://github.com/probcomp/sum-product-dsl/issues/22 # and of EventBasic does not yet perform simplifications. assert ~(~(X << {1, 2})) == \ ((1 <= X) & ((X <= 1) | (2 <= X)) & (X <= 2))
def test_solver_19(): # 3*(x**(1/7))**4 - 3*(x**(1/7))**2 <= 9 # or || 3*(x**(1/7))**4 - 3*(x**(1/7))**2 > 11 solution = Union( Interval(0, (Rat(1, 2) + sympy.sqrt(13)/2)**(Rat(7, 2))), Interval.open((Rat(1,2) + sympy.sqrt(141)/6)**(Rat(7, 2)), oo)) Z = Y**(Rat(1, 7)) expr = 3*Z**4 - 3*Z**2 event = (expr <= 9) | (expr > 11) answer = event.solve() assert answer == solution interval = (~event).solve() assert interval == Interval.Lopen( solution.args[0].right, solution.args[1].left)
def test_Union_and(): x = (Interval(0, 1) | FR(1)) & (FN('a')) assert x is EmptySet x = (FN('x', b=True) | Interval(0, 1) | FR(1)) & (FN('a')) assert x == FN('a') x = (FN('x') | Interval(0, 1) | FR(1)) & (FN('a')) assert x is EmptySet x = (FN('x') | Interval.open(0, 1) | FR(7)) & ( (FN('x')) | FR(.5) | Interval(.75, 1.2)) assert x == Union(FR(.5), FN('x'), Interval.Ropen(.75, 1)) x = (FN('x') | Interval.open(0, 1) | FR(7)) & (FR(3)) assert x is EmptySet x = (Interval.Lopen(-5, inf)) & (Interval(0, inf) | FR(inf)) assert x == Interval(0, inf) x = (FR(1, 2) | Interval.Ropen(-inf, 0)) & Interval(0, inf) assert x == FR(1, 2) x = (FR(1, 12) | Interval(0, 5) | Interval(7, 10)) & Interval(4, 12) assert x == Union(Interval(4, 5), Interval(7, 10), FR(12))
def test_Interval_invert(): assert ~(Interval(0, 1)) == Union(Interval.Ropen(-inf, 0), Interval.Lopen(1, inf)) assert ~(Interval.open(0, 1)) == Union(Interval(-inf, 0), Interval(1, inf)) assert ~(Interval.Lopen(0, 1)) == Union(Interval(-inf, 0), Interval.Lopen(1, inf)) assert ~(Interval.Ropen(0, 1)) == Union(Interval.Ropen(-inf, 0), Interval(1, inf)) assert ~(Interval(-inf, inf)) is EmptySet assert ~(Interval(3, inf)) == Interval.Ropen(-inf, 3) assert ~(Interval.open(3, inf)) == Interval(-inf, 3) assert ~(Interval.Lopen(3, inf)) == Interval(-inf, 3) assert ~(Interval.Ropen(3, inf)) == Interval.Ropen(-inf, 3) assert ~(Interval(-inf, 3)) == Interval.Lopen(3, inf) assert ~(Interval.open(-inf, 3)) == Interval(3, inf) assert ~(Interval.Lopen(-inf, 3)) == Interval.Lopen(3, inf) assert ~(Interval.Ropen(-inf, 3)) == Interval(3, inf) assert ~(Interval.open(-inf, inf)) is EmptySet
def test_Union_or(): x = Interval(0, 1) | Interval(5, 6) | Interval(10, 11) assert x == Union(Interval(0, 1), Interval(5, 6), Interval(10, 11)) x = Interval.Ropen(0, 1) | Interval.Lopen(1, 2) | Interval(10, 11) assert x == Union(Interval.Ropen(0, 1), Interval.Lopen(1, 2), Interval(10, 11)) x = Interval.Ropen(0, 1) | Interval.Lopen(1, 2) | Interval(10, 11) | FR(1) assert x == Union(Interval(0, 2), Interval(10, 11)) x = (Interval.Ropen(0, 1) | Interval.Lopen(1, 2)) | (Interval(10, 11) | FR(1)) assert x == Union(Interval(0, 2), Interval(10, 11)) x = FR(1) | ((Interval.Ropen(0,1) | Interval.Lopen(1,2) | FR(10,13)) \ | (Interval.Lopen(10,11) | FR(7))) assert x == Union(Interval(0, 2), Interval(10, 11), FR(13, 7)) assert 2 in x assert 13 in x x = FN('f') | (FR(1) | FN('g', b=True)) assert x == Union(FR(1), FN('g', b=True)) assert 'w' in x assert 'g' not in x
def test_FiniteReal_invert(): assert ~FR(1) == Union(Interval.Ropen(-inf, 1), Interval.Lopen(1, inf)) assert ~(FR(0, -1)) == Union(Interval.Ropen(-inf, -1), Interval.open(-1, 0), Interval.Lopen(0, inf))
def test_Interval_or(): assert Interval(0, 1) | Interval(-1, 1) == Interval(-1, 1) assert Interval(0, 2) | Interval.open(0, 1) == Interval(0, 2) assert Interval.Lopen(0, 1) | Interval(-1, 1) == Interval(-1, 1) assert Interval.Ropen(0, 1) | Interval(-1, 1) == Interval(-1, 1) assert Interval(0, 1) | Interval(1, 2) == Interval(0, 2) assert Interval.open(0, 1) | Interval(0, 1) == Interval(0, 1) assert Interval(0, 1) | Interval(0, .5) == Interval(0, 1) assert Interval.Lopen(0, 1) | Interval(1, 2) == Interval.Lopen(0, 2) assert Interval.Ropen(-1, 0) | Interval.Ropen(0, 1) == Interval.Ropen( -1, 1) assert Interval.Ropen(0, 1) | Interval.Ropen(-1, 0) == Interval.Ropen( -1, 1) assert Interval.Lopen(0, 1) | Interval.Lopen(1, 2) == Interval.Lopen(0, 2) assert Interval.Lopen(0, 1) | Interval.Ropen(1, 2) == Interval.open(0, 2) assert Interval.open(0, 2) | Interval(0, 1) == Interval.Ropen(0, 2) assert Interval.open(0, 1) | Interval.Ropen(-1, 0) == Union( Interval.open(0, 1), Interval.Ropen(-1, 0)) assert Interval(1, 2) | Interval.Ropen(0, 1) == Interval(0, 2) assert Interval.Ropen(0, 1) | Interval(1, 2) == Interval(0, 2) assert Interval.open(0, 1) | Interval(1, 2) == Interval.Lopen(0, 2) assert Interval.Ropen(0, 1) | Interval.Ropen(1, 2) == Interval.Ropen(0, 2) assert Interval(1, 2) | Interval.open(0, 1) == Interval.Lopen(0, 2) assert Interval(1, 2) | Interval.Lopen(0, 1) == Interval.Lopen(0, 2) assert Interval(1, 2) | Interval.Ropen(0, 1) == Interval(0, 2) assert Interval.open(0, 1) | Interval(1, 2) == Interval.Lopen(0, 2) assert Interval.Lopen(0, 1) | Interval(1, 2) == Interval.Lopen(0, 2) assert Interval.Ropen(0, 1) | Interval(1, 2) == Interval(0, 2) assert Interval(0, 2) | Interval.open(0.5, 2.5) == Interval.Ropen(0, 2.5) assert Interval.open(0, 2) | Interval.open(0, 2.5) == Interval.open(0, 2.5) assert Interval.open(0, 2.5) | Interval.open(0, 2) == Interval.open(0, 2.5) assert Interval.open(0, 1) | Interval.open(1, 2) == Union( Interval.open(0, 1), Interval.open(1, 2)) assert Interval.Ropen(0, 2) | Interval.Lopen(0.5, 2.5) == Interval(0, 2.5) assert Interval.open(0, 2) | Interval(0.5, 2.5) == Interval.Lopen(0, 2.5) assert Interval.Lopen(0, 2) | Interval.Ropen(0, 2) == Interval(0, 2) assert Interval(0, 1) | Interval(2, 3) == Union(Interval(0, 1), Interval(2, 3)) assert Interval(2, 3) | Interval.Ropen(0, 1) == Union( Interval(2, 3), Interval.Ropen(0, 1)) assert Interval.Lopen(0, 1) | Interval(1, 2) == Interval.Lopen(0, 2) assert Interval(-10, 10) | FR(-1, 1) == Interval(-10, 10) assert Interval(-10, 10) | FR(-1, 11) == Union(Interval(-10, 10), FR(11)) assert Interval(-inf, -3, right_open=True) | Interval( -inf, inf) == Interval(-inf, inf)
def test_Interval_and(): assert Interval(0, 1) & Interval(-1, 1) == Interval(0, 1) assert Interval(0, 2) & Interval.open(0, 1) == Interval.open(0, 1) assert Interval.Lopen(0, 1) & Interval(-1, 1) == Interval.Lopen(0, 1) assert Interval.Ropen(0, 1) & Interval(-1, 1) == Interval.Ropen(0, 1) assert Interval(0, 1) & Interval(1, 2) == FR(1) assert Interval.Lopen(0, 1) & Interval(1, 2) == FR(1) assert Interval.Ropen(0, 1) & Interval(1, 2) is EmptySet assert Interval.Lopen(0, 1) & Interval.Lopen(1, 2) is EmptySet assert Interval(1, 2) & Interval.Lopen(0, 1) == FR(1) assert Interval(1, 2) & Interval.open(0, 1) is EmptySet assert Interval(0, 2) & Interval.Lopen(0.5, 2.5) == Interval.Lopen(0.5, 2) assert Interval.Ropen(0, 2) & Interval.Lopen(0.5, 2.5) == Interval.open( 0.5, 2) assert Interval.open(0, 2) & Interval(0.5, 2.5) == Interval.Ropen(0.5, 2) assert Interval.Lopen(0, 2) & Interval.Ropen(0, 2) == Interval.open(0, 2) assert Interval(0, 1) & Interval(2, 3) is EmptySet assert Interval(2, 3) & Interval(0, 1) is EmptySet assert Interval.open(0, 1) & Interval.open(0, 1) == Interval.open(0, 1) assert Interval.Ropen(-inf, -3) & Interval(-inf, inf) == Interval.Ropen( -inf, -3) assert Interval(-inf, inf) & Interval.Ropen(-inf, -3) == Interval.Ropen( -inf, -3) assert Interval(0, inf) & (Interval.Lopen(-5, inf)) == Interval(0, inf) assert Interval.Lopen(0, 1) & Interval.Ropen(0, 1) == Interval.open(0, 1) assert Interval.Ropen(0, 1) & Interval.Lopen(0, 1) == Interval.open(0, 1) assert Interval.Ropen(0, 5) & Interval.Ropen(-inf, 5) == Interval.Ropen( 0, 5)
def test_solver_5_lopen(): # (2*x + 10 < 4) & (x + 10 >= 3) solution =Interval.Lopen(3-10, (4-10)/2) event = ((2*Y + 10) <= 4) & (Y + 10 > 3) answer = event.solve() assert answer == solution