Esempio n. 1
0
def test_simple_model_eq():
    command_switch = Sequence(
        Sample(Y, randint(low=0, high=4)),
        Switch(Y, range(0, 4), lambda i: Sample(X, bernoulli(p=1 / (i + 1)))))
    model_switch = command_switch.interpret()

    command_ifelse = Sequence(
        Sample(Y, randint(low=0, high=4)),
        IfElse(
            Y << {0},
            Sample(X, bernoulli(p=1 / (0 + 1))),
            Y << {1},
            Sample(X, bernoulli(p=1 / (1 + 1))),
            Y << {2},
            Sample(X, bernoulli(p=1 / (2 + 1))),
            Y << {3},
            Sample(X, bernoulli(p=1 / (3 + 1))),
        ))
    model_ifelse = command_ifelse.interpret()

    for model in [model_switch, model_ifelse]:
        symbols = model.get_symbols()
        assert symbols == {X, Y}
        assert allclose(model.logprob(X << {1}),
                        logsumexp([-log(4) - log(i + 1) for i in range(4)]))
Esempio n. 2
0
def get_command_beta():
    return Sequence(
        Sample(simAll, beta(a=2, b=3)),
        For(
            0, 5, lambda k: Switch(
                simAll, binspace(0, 1, ns), lambda i: Sequence(
                    Sample(sim[k], bernoulli(p=i.right)),
                    Sample(p1[k], uniform()),
                    IfElse(
                        sim[k] << {1},
                        Sequence(
                            Transform(p2[k], p1[k]),
                            Switch(
                                p1[k], binspace(0, 1, ns), lambda j: Sequence(
                                    Sample(clickA[k], bernoulli(p=i.right)),
                                    Sample(clickB[k], bernoulli(p=i.right))))),
                        True,
                        Sequence(
                            Sample(p2[k], uniform()),
                            Switch(
                                p1[k], binspace(0, 1, ns), lambda j: Sample(
                                    clickA[k], bernoulli(p=j.right))),
                            Switch(
                                p2[k], binspace(0, 1, ns), lambda j: Sample(
                                    clickB[k], bernoulli(p=j.right)))))))))
Esempio n. 3
0
def get_command_randint():
    return Sequence(
        Sample(simAll, randint(low=0, high=ns)),
        For(
            0, 5, lambda k: Switch(
                simAll, range(0, ns), lambda i: Sequence(
                    Sample(sim[k], bernoulli(p=i / nd)),
                    Sample(p1[k], randint(low=0, high=ns)),
                    IfElse(
                        sim[k] << {1},
                        Sequence(
                            Transform(p2[k], p1[k]),
                            Switch(
                                p1[k], range(ns), lambda j: Sequence(
                                    Sample(clickA[k], bernoulli(p=i / nd)),
                                    Sample(clickB[k], bernoulli(p=i / nd))))),
                        True,
                        Sequence(
                            Sample(p2[k], randint(low=0, high=ns)),
                            Switch(
                                p1[k], range(ns), lambda j: Sample(
                                    clickA[k], bernoulli(p=j / nd))),
                            Switch(
                                p2[k], range(ns), lambda j: Sample(
                                    clickB[k], bernoulli(p=j / nd)))))))))
Esempio n. 4
0
def make_model_for(n=2):
    command = Sequence(
        Sample(Y, choice({'0': .2, '1': .2, '2': .2, '3': .2, '4': .2})),
        For(0, n, lambda i: Sequence(
            Sample(Z[i], bernoulli(p=.5)),
            IfElse(
                (Y << {str(i)}) | (Z[i] << {0}), Sample(X[i], bernoulli(p=.1)),
                Otherwise,                       Sample(X[i], bernoulli(p=.5))))))
    return command.interpret()
Esempio n. 5
0
def test_complex_model_reorder():
    command = Sequence(
        Sample(Y, choice({'0': .2, '1': .2, '2': .2, '3': .2, '4': .2})),
        For(0, 3, lambda i:
            Sample(Z[i], bernoulli(p=0.1))),
        For(0, 3, lambda i:
            IfElse(
                Y << {str(i)}, Sample(X[i], bernoulli(p=1/(i+1))),
                Z[i] << {0},   Sample(X[i], bernoulli(p=1/(i+1))),
                Otherwise,     Sample(X[i], bernoulli(p=0.1)))))
    model = command.interpret()
    assert(allclose(model.prob(Y << {'0'}), 0.2))
Esempio n. 6
0
def test_complex_model():
    # Slow for larger number of repetitions
    # https://github.com/probcomp/sum-product-dsl/issues/43
    command = Sequence(
        Sample(Y, choice({'0': .2, '1': .2, '2': .2, '3': .2, '4': .2})),
        For(0, 3, lambda i: Sequence(
            Sample(Z[i], bernoulli(p=0.1)),
            IfElse(
                Y << {str(i)} | Z[i] << {0}, Sample(X[i], bernoulli(p=1/(i+1))),
                Otherwise,                   Sample(X[i], bernoulli(p=0.1))))))
    model = command.interpret()
    assert allclose(model.prob(Y << {'0'}), 0.2)
Esempio n. 7
0
def test_simple_parse_real():
    assert isinstance(.3 * bernoulli(p=.1), DistributionMix)
    a = .3 * bernoulli(p=.1) | .5 * norm() | .2 * poisson(mu=7)
    spe = a(X)
    assert isinstance(spe, SumSPE)
    assert allclose(spe.weights, [log(.3), log(.5), log(.2)])
    assert isinstance(spe.children[0], DiscreteLeaf)
    assert isinstance(spe.children[1], ContinuousLeaf)
    assert isinstance(spe.children[2], DiscreteLeaf)
    assert spe.children[0].support == Interval(0, 1)
    assert spe.children[1].support == Interval(-oo, oo)
    assert spe.children[2].support == Interval(0, oo)
Esempio n. 8
0
def get_model():
    Y = Id('Y')
    X = Id('X')
    Z = Id('Z')
    command = Sequence(
        Sample(Y, choice({
            '0': .2,
            '1': .2,
            '2': .2,
            '3': .2,
            '4': .2
        })), Sample(Z, bernoulli(p=0.1)),
        IfElse(Y << {str(0)} | Z << {0}, Sample(X, bernoulli(p=1 / (0 + 1))),
               Otherwise, Transform(X, Z**2 + Z)))
    return command.interpret()
Esempio n. 9
0
def test_error_linspace():
    with pytest.raises(AssertionError):
        # Switch cases do not sum to one.
        command = Sequence(
            Sample(Y, beta(a=2, b=3)),
            Switch(Y, linspace(0, .5, 5), lambda i: Sample(X, bernoulli(p=i))))
        command.interpret()
Esempio n. 10
0
def test_error_range():
    with pytest.raises(AssertionError):
        # Switch cases do not sum to one.
        command = Sequence(
            Sample(Y, randint(low=0, high=4)),
            Switch(Y, range(0, 3), lambda i: Sample(X, bernoulli(p=1 /
                                                                 (i + 1)))))
        command.interpret()
Esempio n. 11
0
def test_simple_model_enumerate():
    command_switch = Sequence(
        Sample(Y, randint(low=0, high=4)),
        Switch(Y, enumerate(range(0, 4)),
               lambda i, j: Sample(X, bernoulli(p=1 / (i + j + 1)))))
    model = command_switch.interpret()
    assert allclose(model.prob(Y << {0} & (X << {1})), .25 * 1 / (0 + 0 + 1))
    assert allclose(model.prob(Y << {1} & (X << {1})), .25 * 1 / (1 + 1 + 1))
    assert allclose(model.prob(Y << {2} & (X << {1})), .25 * 1 / (2 + 2 + 1))
    assert allclose(model.prob(Y << {3} & (X << {1})), .25 * 1 / (3 + 3 + 1))
Esempio n. 12
0
def test_simple_parse_nominal():
    assert isinstance(.7 * choice({'a': .1, 'b': .9}), DistributionMix)
    a = .3 * bernoulli(p=.1) | .7 * choice({'a': .1, 'b': .9})
    spe = a(X)
    assert isinstance(spe, SumSPE)
    assert allclose(spe.weights, [log(.3), log(.7)])
    assert isinstance(spe.children[0], DiscreteLeaf)
    assert isinstance(spe.children[1], NominalLeaf)
    assert spe.children[0].support == Interval(0, 1)
    assert spe.children[1].support == FiniteNominal('a', 'b')
Esempio n. 13
0
def test_simple_model():
    command = Sequence(
        Sample(Y, bernoulli(p=0.5)),
        For(0, 5, lambda i:
            Sample(X[i], bernoulli(p=1/(i+1)))))
    model = command.interpret()

    symbols = model.get_symbols()
    assert len(symbols) == 6
    assert Y in symbols
    assert X[0] in symbols
    assert X[1] in symbols
    assert X[2] in symbols
    assert X[3] in symbols
    assert X[4] in symbols
    assert model.logprob(X[0] << {1}) == log(1/1)
    assert model.logprob(X[1] << {1}) == log(1/2)
    assert model.logprob(X[2] << {1}) == log(1/3)
    assert model.logprob(X[3] << {1}) == log(1/4)
    assert model.logprob(X[4] << {1}) == log(1/5)
Esempio n. 14
0
def test_simple_model_lte():
    command_switch = Sequence(
        Sample(Y, beta(a=2, b=3)),
        Switch(Y, binspace(0, 1, 5),
               lambda i: Sample(X, bernoulli(p=i.right))))
    model_switch = command_switch.interpret()

    command_ifelse = Sequence(
        Sample(Y, beta(a=2, b=3)),
        IfElse(
            Y <= 0,
            Sample(X, bernoulli(p=0)),
            Y <= 0.25,
            Sample(X, bernoulli(p=.25)),
            Y <= 0.50,
            Sample(X, bernoulli(p=.50)),
            Y <= 0.75,
            Sample(X, bernoulli(p=.75)),
            Y <= 1,
            Sample(X, bernoulli(p=1)),
        ))
    model_ifelse = command_ifelse.interpret()

    grid = [float(x) for x in linspace(0, 1, 5)]
    for model in [model_switch, model_ifelse]:
        symbols = model.get_symbols()
        assert symbols == {X, Y}
        assert allclose(
            model.logprob(X << {1}),
            logsumexp([
                model.logprob((il < Y) <= ih) + log(ih)
                for il, ih in zip(grid[:-1], grid[1:])
            ]))
Esempio n. 15
0
def test_ifelse_zero_conditions():
    command = Sequence(
        Sample(Y, randint(low=0, high=3)),
        IfElse(
            Y << {-1},
            Transform(X, Y**(-1)),
            Y << {0},
            Sample(X, bernoulli(p=1)),
            Y << {1},
            Transform(X, Y),
            Y << {2},
            Transform(X, Y**2),
            Y << {3},
            Transform(X, Y**3),
        ))
    model = command.interpret()
    assert len(model.children) == 3
    assert len(model.weights) == 3
    assert allclose(model.weights[0], model.logprob(Y << {0}))
    assert allclose(model.weights[1], model.logprob(Y << {1}))
    assert allclose(model.weights[2], model.logprob(Y << {2}))
Esempio n. 16
0
def test_error():
    with pytest.raises(TypeError):
        'a' * bernoulli(p=.1)
    a = .1 * bernoulli(p=.1) | .7 * poisson(mu=8)
    with pytest.raises(Exception):
        a(X)
Esempio n. 17
0
def make_model_handcode():
    command = Sequence(
        Sample(Y, choice({'0': .2, '1': .2, '2': .2, '3': .2, '4': .2})),
        Sample(Z[0], bernoulli(p=.5)),
        Sample(Z[1], bernoulli(p=.5)),
        IfElse(
            Y << {str(0)}, Sequence(
                Sample(X[0], bernoulli(p=.1)),
                IfElse(
                    Z[1] << {0},    Sample(X[1], bernoulli(p=.1)),
                    Otherwise,      Sample(X[1], bernoulli(p=.5)))),
            Y << {str(1)}, Sequence(
                Sample(X[1], bernoulli(p=.1)),
                IfElse(
                    Z[0] << {0},    Sample(X[0], bernoulli(p=.1)),
                    Otherwise,      Sample(X[0], bernoulli(p=.5)))),
            Otherwise, Sequence(
                IfElse(
                    Z[0] << {0},    Sample(X[0], bernoulli(p=.1)),
                    Otherwise,      Sample(X[0], bernoulli(p=.5))),
                IfElse(
                    Z[1] << {0},    Sample(X[1], bernoulli(p=.1)),
                    Otherwise,      Sample(X[1], bernoulli(p=.5))))))
    return command.interpret()
def test_cache_complex_sum_of_product():
    # Test case adapted from the SPE generated by
    # test_repeat.make_model_repeat(n=2)
    duplicate_subtrees = [None, None]
    for i in range(2):
        duplicate_subtrees[i] = SumSPE([
            ProductSPE([(X[0] >> bernoulli(p=.1)),
                        SumSPE([(Z[0] >> bernoulli(p=.5))
                                & (Y >> choice({
                                    '0': .1,
                                    '1': .9
                                })), (Z[0] >> bernoulli(p=.1))
                                & (Y >> choice({
                                    '0': .9,
                                    '1': .1
                                }))],
                               weights=[log(.730), log(.270)])]),
            ProductSPE([
                Z[0] >> bernoulli(p=.1),
                Y >> choice({
                    '0': .9,
                    '1': .1
                }),
                X[0] >> bernoulli(p=.5),
            ]),
        ],
                                       weights=[log(.925),
                                                log(.075)])

    assert duplicate_subtrees[0] == duplicate_subtrees[1]
    assert duplicate_subtrees[0] is not duplicate_subtrees[1]

    left_subtree = ProductSPE([
        X[1] >> bernoulli(p=.5),
        SumSPE([
            ProductSPE([
                duplicate_subtrees[0],
                Z[1] >> bernoulli(p=.5),
            ]),
            ProductSPE([
                Z[1] >> bernoulli(p=.7),
                SumSPE([
                    Y >> choice({
                        '0': .3,
                        '1': .7
                    })
                    & X[0] >> bernoulli(p=.1)
                    & Z[0] >> bernoulli(p=.1),
                    Y >> choice({
                        '0': .7,
                        '1': .3
                    })
                    & X[0] >> bernoulli(p=.5)
                    & Z[0] >> bernoulli(p=.5),
                ],
                       weights=[log(.9), log(.1)])
            ])
        ],
               weights=[log(.783), log(.217)])
    ])

    right_subtree = ProductSPE([
        Z[1] >> bernoulli(p=.8), X[1] >> bernoulli(p=.1), duplicate_subtrees[1]
    ])

    spe = .92 * left_subtree | .08 * right_subtree

    spe_cached = spe_cache_duplicate_subtrees(spe, {})
    assert spe_cached.children[0].children[1].children[0].children[
        0] is duplicate_subtrees[0]
    assert spe_cached.children[1].children[2] is duplicate_subtrees[0]
Esempio n. 19
0
from sppl.compilers.ast_to_spe import Id
from sppl.compilers.ast_to_spe import IfElse
from sppl.compilers.ast_to_spe import Otherwise
from sppl.compilers.ast_to_spe import Sample
from sppl.compilers.ast_to_spe import Sequence
from sppl.distributions import bernoulli

Burglary    = Id('Burglary')
Earthquake  = Id('Earthquake')
Alarm       = Id('Alarm')
JohnCalls   = Id('JohnCalls')
MaryCalls   = Id('MaryCalls')

program = Sequence(
    Sample(Burglary,   bernoulli(p=0.001)),
    Sample(Earthquake, bernoulli(p=0.002)),
    IfElse(
        Burglary << {1},
            IfElse(
                Earthquake << {1},  Sample(Alarm, bernoulli(p=0.95)),
                Otherwise,          Sample(Alarm, bernoulli(p=0.94))),
        Otherwise,
            IfElse(
                Earthquake << {1},  Sample(Alarm, bernoulli(p=0.29)),
                Otherwise,          Sample(Alarm, bernoulli(p=0.001)))),
    IfElse(
        Alarm << {1}, Sequence(
            Sample(JohnCalls, bernoulli(p=0.90)),
            Sample(MaryCalls, bernoulli(p=0.70))),
        Otherwise, Sequence(
Esempio n. 20
0
def test_if_else_transform_reverse():
    command = Sequence(
        Sample(X, norm(loc=0, scale=1)), Sample(Y, bernoulli(p=0.5)),
        IfElse(Y << {0}, Transform(Z, X**2), Otherwise, Transform(Z, X)))
    model = command.interpret()
    assert allclose(model.logprob(Z > 0), log(3) - log(4))