Example #1
0
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
Example #2
0
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
Example #3
0
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)
Example #4
0
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))
Example #5
0
def test_solver_26_piecewise_one_expr_basic_event():
    event = (Y**2)*(0 <= Y) < 2
    assert event.solve() == Interval.Ropen(0, sympy.sqrt(2))
    event = (0 <= Y)*(Y**2) < 2
    assert event.solve() == Interval.Ropen(0, sympy.sqrt(2))
    event = ((0 <= Y) < 5)*(Y < 1) << {1}
    assert event.solve() == Interval.Ropen(0, 1)
    event = ((0 <= Y) < 5)*(~(Y < 1)) << {1}
    assert event.solve() == Interval.Ropen(1, 5)
    event = 10*(0 <= Y) << {10}
    assert event.solve() == Interval(0, oo)
    event = 10*(0 <= Y) << {0}
    assert event.solve() == Interval.Ropen(-oo, 0)
Example #6
0
def test_condition():
    model = model_no_latents()
    GPA = Id('GPA')
    model_condition = model.condition(GPA << {4} | GPA << {10})
    assert len(model_condition.children) == 2
    assert model_condition.children[0].support == Interval.Ropen(4, 5)
    assert model_condition.children[1].support == Interval.Ropen(10, 11)

    model_condition = model.condition((0 < GPA < 4))
    assert len(model_condition.children) == 2
    assert model_condition.children[0].support \
        == model_condition.children[1].support
    assert allclose(model_condition.children[0].logprob(GPA < 1),
                    model_condition.children[1].logprob(GPA < 1))
Example #7
0
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
Example #8
0
def test_solver_21__ci_():
    # 1 <= log(x**3 - 3*x + 3) < 5
    # Can only be solved by numerical approximation of roots.
    # https://www.wolframalpha.com/input/?i=1+%3C%3D+log%28x**3+-+3x+%2B+3%29+%3C+5
    solution = Union(
        Interval(
            -1.777221448430427630375448631016427343692,
            0.09418455242255462832154474245589911789464),
        Interval.Ropen(
            1.683036896007873002053903888560528225797,
            5.448658707897512189124586716091172798465))

    expr = Log(Y**3 - 3*Y + 3)
    event = ((1 <= expr) & (expr < 5))
    answer = event.solve()
    assert isinstance(answer, Union)
    assert len(answer.args) == 2
    first = answer.args[0] if answer.args[0].a < 0 else answer.args[1]
    second = answer.args[0] if answer.args[0].a > 0 else answer.args[1]
    # Check first interval.
    assert not first.left_open
    assert not first.right_open
    assert allclose(float(first.a), float(solution.args[0].a))
    assert allclose(float(first.b), float(solution.args[0].b))
    # Check second interval.
    assert not second.left_open
    assert second.right_open
    assert allclose(float(second.a), float(solution.args[1].a))
    assert allclose(float(second.b), float(solution.args[1].b))
Example #9
0
def test_parse_21__ci_():
    # 1 <= log(x**3 - 3*x + 3) < 5
    # Can only be solved by numerical approximation of roots.
    # https://www.wolframalpha.com/input/?i=1+%3C%3D+log%28x**3+-+3x+%2B+3%29+%3C+5
    expr = Log(X**3 - 3*X + 3)
    expr_prime = Log(Poly(Y, [3, -3, 0, 1]))
    assert expr == expr_prime
    assert ((1 <= expr) & (expr < 5)) \
        == EventInterval(expr, Interval.Ropen(1, 5))
Example #10
0
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))
Example #11
0
def test_parse_26_piecewise_one_expr_basic_event():
    assert (Y**2)*(0 <= Y) == Piecewise(
        [Poly(Y, [0, 0, 1])],
        [EventInterval(Y, Interval(0, oo))])
    assert (0 <= Y)*(Y**2) == Piecewise(
        [Poly(Y, [0, 0, 1])],
        [EventInterval(Y, Interval(0, oo))])
    assert ((0 <= Y) < 5)*(Y < 1) == Piecewise(
        [EventInterval(Y, Interval.Ropen(-oo, 1))],
        [EventInterval(Y, Interval.Ropen(0, 5))],
    )
    assert ((0 <= Y) < 5)*(~(Y < 1)) == Piecewise(
        [EventInterval(Y, Interval(1, oo))],
        [EventInterval(Y, Interval.Ropen(0, 5))],
    )
    assert 10*(0 <= Y) == Poly(
        EventInterval(Y, Interval(0, oo)),
        [0, 10])
Example #12
0
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)
Example #13
0
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
Example #14
0
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))
Example #15
0
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)]
Example #16
0
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))
Example #17
0
def test_solver_23_reciprocal_range():
    solution = Interval.Ropen(-1, -Rat(1, 3))
    event = ((-3 < 1/Y) <= -1)
    assert event.solve() == solution

    solution = Interval.open(0, Rat(1, 3))
    event = ((-3 < 1/(2*Y-1)) < -1)
    assert event.solve() == solution

    solution = Interval.open(-1 / sympy.sqrt(3), 1 / sympy.sqrt(3))
    event = ((-3 < 1/(2*(abs(Y)**2)-1)) <= -1)
    assert event.solve() == solution

    solution = Union(
        Interval.open(-1 / sympy.sqrt(3), 0),
        Interval.open(0, 1 / sympy.sqrt(3)))
    event = ((-3 < 1/(2*(abs(Y)**2)-1)) < -1)
    assert event.solve() == solution
Example #18
0
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
Example #19
0
def test_product_condition_basic():
    X = Id('X')
    Y = Id('Y')
    spe = ProductSPE([X >> norm(loc=0, scale=1), Y >> gamma(a=1)])

    # Condition on (X > 0) and ((X > 0) | (Y < 0))
    # where the second clause reduces to first as Y < 0
    # has probability zero.
    for event in [(X > 0), (X > 0) | (Y < 0)]:
        dX = spe.condition(event)
        assert isinstance(dX, ProductSPE)
        assert dX.children[0].symbol == Id('X')
        assert dX.children[0].conditioned
        assert dX.children[0].support == Interval.open(0, oo)
        assert dX.children[1].symbol == Id('Y')
        assert not dX.children[1].conditioned
        assert dX.children[1].Fl == 0
        assert dX.children[1].Fu == 1

    # Condition on (Y < 0.5)
    dY = spe.condition(Y < 0.5)
    assert isinstance(dY, ProductSPE)
    assert dY.children[0].symbol == Id('X')
    assert not dY.children[0].conditioned
    assert dY.children[1].symbol == Id('Y')
    assert dY.children[1].conditioned
    assert dY.children[1].support == Interval.Ropen(0, 0.5)

    # Condition on (X > 0) & (Y < 0.5)
    dXY_and = spe.condition((X > 0) & (Y < 0.5))
    assert isinstance(dXY_and, ProductSPE)
    assert dXY_and.children[0].symbol == Id('X')
    assert dXY_and.children[0].conditioned
    assert dXY_and.children[0].support == Interval.open(0, oo)
    assert dXY_and.children[1].symbol == Id('Y')
    assert dXY_and.children[1].conditioned
    assert dXY_and.children[1].support == Interval.Ropen(0, 0.5)

    # Condition on (X > 0) | (Y < 0.5)
    event = (X > 0) | (Y < 0.5)
    dXY_or = spe.condition((X > 0) | (Y < 0.5))
    assert isinstance(dXY_or, SumSPE)
    assert all(isinstance(d, ProductSPE) for d in dXY_or.children)
    assert allclose(dXY_or.logprob(X > 0), dXY_or.weights[0])
    samples = dXY_or.sample(100, prng=numpy.random.RandomState(1))
    assert all(event.evaluate(sample) for sample in samples)

    # Condition on a disjoint union with one term in second clause.
    dXY_disjoint_one = spe.condition((X > 0) & (Y < 0.5) | (X <= 0))
    assert isinstance(dXY_disjoint_one, SumSPE)
    component_0 = dXY_disjoint_one.children[0]
    assert component_0.children[0].symbol == Id('X')
    assert component_0.children[0].conditioned
    assert component_0.children[0].support == Interval.open(0, oo)
    assert component_0.children[1].symbol == Id('Y')
    assert component_0.children[1].conditioned
    assert component_0.children[1].support == Interval.Ropen(0, 0.5)
    component_1 = dXY_disjoint_one.children[1]
    assert component_1.children[0].symbol == Id('X')
    assert component_1.children[0].conditioned
    assert component_1.children[0].support == Interval(-oo, 0)
    assert component_1.children[1].symbol == Id('Y')
    assert not component_1.children[1].conditioned

    # Condition on a disjoint union with two terms in each clause
    dXY_disjoint_two = spe.condition((X > 0) & (Y < 0.5)
                                     | ((X <= 0) & ~(Y < 3)))
    assert isinstance(dXY_disjoint_two, SumSPE)
    component_0 = dXY_disjoint_two.children[0]
    assert component_0.children[0].symbol == Id('X')
    assert component_0.children[0].conditioned
    assert component_0.children[0].support == Interval.open(0, oo)
    assert component_0.children[1].symbol == Id('Y')
    assert component_0.children[1].conditioned
    assert component_0.children[1].support == Interval.Ropen(0, 0.5)
    component_1 = dXY_disjoint_two.children[1]
    assert component_1.children[0].symbol == Id('X')
    assert component_1.children[0].conditioned
    assert component_1.children[0].support == Interval(-oo, 0)
    assert component_1.children[1].symbol == Id('Y')
    assert component_1.children[1].conditioned
    assert component_1.children[1].support == Interval(3, oo)

    # Some various conditioning.
    spe.condition((X > 0) & (Y < 0.5) | ((X <= 1) | ~(Y < 3)))
    spe.condition((X > 0) & (Y < 0.5) | ((X <= 1) & (Y < 3)))
Example #20
0
def test_poisson():
    X = Id('X')
    spe = X >> poisson(mu=5)

    a = spe.logprob((1 <= X) <= 7)
    b = spe.logprob(X << {1, 2, 3, 4, 5, 6, 7})
    c = logsumexp([spe.logprob(X << {i}) for i in range(1, 8)])
    assert allclose(a, b)
    assert allclose(a, c)
    assert allclose(b, c)

    spe_condition = spe.condition(10 <= X)
    assert spe_condition.conditioned
    assert spe_condition.support == Range(10, oo)
    assert spe_condition.logZ == logdiffexp(0, spe.logprob(X <= 9))

    assert allclose(spe_condition.logprob(X <= 10),
                    spe_condition.logprob(X << {10}))
    assert allclose(spe_condition.logprob(X <= 10),
                    spe_condition.logpdf({X: 10}))

    samples = spe_condition.sample(100)
    assert all(10 <= s[X] for s in samples)

    # Unify X = 5 with left interval to make one distribution.
    event = ((1 <= X) < 5) | ((3 * X + 1) << {16})
    spe_condition = spe.condition(event)
    assert isinstance(spe_condition, DiscreteLeaf)
    assert spe_condition.conditioned
    assert spe_condition.xl == 1
    assert spe_condition.xu == 5
    assert spe_condition.support == Range(1, 5)
    samples = spe_condition.sample(100, prng=numpy.random.RandomState(1))
    assert all(event.evaluate(s) for s in samples)

    # Ignore X = 14/3 as a probability zero condition.
    spe_condition = spe.condition(((1 <= X) < 5) | (3 * X + 1) << {15})
    assert isinstance(spe_condition, DiscreteLeaf)
    assert spe_condition.conditioned
    assert spe_condition.xl == 1
    assert spe_condition.xu == 4
    assert spe_condition.support == Interval.Ropen(1, 5)

    # Make a mixture of two components.
    spe_condition = spe.condition(((1 <= X) < 5) | (3 * X + 1) << {22})
    assert isinstance(spe_condition, SumSPE)
    xl = spe_condition.children[0].xl
    idx0 = 0 if xl == 7 else 1
    idx1 = 1 if xl == 7 else 0
    assert spe_condition.children[idx1].conditioned
    assert spe_condition.children[idx1].xl == 1
    assert spe_condition.children[idx1].xu == 4
    assert spe_condition.children[idx0].conditioned
    assert spe_condition.children[idx0].xl == 7
    assert spe_condition.children[idx0].xu == 7
    assert spe_condition.children[idx0].support == Range(7, 7)

    # Condition on probability zero event.
    with pytest.raises(ValueError):
        spe.condition(((-3 <= X) < 0) | (3 * X + 1) << {20})

    # Condition on FiniteReal contiguous.
    spe_condition = spe.condition(X << {1, 2, 3})
    assert spe_condition.xl == 1
    assert spe_condition.xu == 3
    assert allclose(spe_condition.logprob((1 <= X) <= 3), 0)

    # Condition on single point.
    assert allclose(0, spe.condition(X << {2}).logprob(X << {2}))

    # Constrain.
    with pytest.raises(Exception):
        spe.constrain({X: -1})
    with pytest.raises(Exception):
        spe.constrain({X: .5})
    spe_constrain = spe.constrain({X: 10})
    assert allclose(spe_constrain.prob(X << {0, 10}), 1)
Example #21
0
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))
Example #22
0
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)
Example #23
0
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)
Example #24
0
def test_solver_5_ropen():
    # (2*x+10 < 4) & (x + 10 >= 3)
    solution = Interval.Ropen(3-10, (4-10)/2)
    event = ((2*Y + 10) < 4) & (Y + 10 >= 3)
    answer = event.solve()
    assert answer == solution
Example #25
0
def test_solver_16():
    # (x**(1/7))**4 < 9
    solution = Interval.Ropen(0, 27*sympy.sqrt(3))
    event = ((Y**Rat(1,7)))**4 < 9
    answer = event.solve()
    assert answer == solution