Example #1
0
def test_normalise_fixed_point():
    root_expr = sigma(v('x'), product([prob([v('z'), v('y')], [v('b'), v('x'), do(v('a'))]),
        prob([v('z'), v('y'), v('x')], [do(v('a'))])]))

    bindings = {'x' : 'xxx', 'z' : 'zzz', 'y' : 'yyy', 'a' : 'aaa'}

    state = ProofState(0, 0, bindings, root_expr)

    normalised_state = state.normalise()

    expected_result = sigma(v(0), product((prob((v(0), v(1), v(2)),(do(v(3)), )),
        prob((v(1), v(2)), (do(v(3)), v(0), v(4))))))

    assert normalised_state.root_expr == expected_result
Example #2
0
def test_gen_matches_deep():
    # sigma_y { p(x|y,do(z)) * p(y|do(z)) }
    root_expr = sigma(v("y"), product([prob([v("x")], [v("y"), do(v("z"))]), prob([v("y")], [do(v("z"))])]))

    matches = list(gen_matches(is_v, root_expr))
    assert len(matches) == 6

    expr, inject = matches[3]
    assert expr == v("z")

    root_expr_prime = inject("walrus")
    assert root_expr_prime == sigma(
        v("y"), product([prob([v("x")], [v("y"), do("walrus")]), prob([v("y")], [do(v("z"))])])
    )
Example #3
0
    def gen_expansions(value, proof_state):
        for (prob_expr, inject) in E.gen_matches(E.is_prob, proof_state.root_expr):
            prob_vars = get_variable_order(prob_expr)
            prob_values = [proof_state.bindings[v] for v in prob_vars]
            if value in prob_values:
                continue
            # prob([x],[w]) -> sigma(y, product([prob([x], [y, w]), p([y], [w])]))
            i = new_variable_name(proof_state.bindings)
            v_i = E.v(i)
            alpha_left = tuple(prob_expr[1])
            alpha_right = (v_i, ) + tuple(prob_expr[2])
            alpha = E.prob(alpha_left, alpha_right)
            beta_left = (v_i, )
            beta_right = tuple(prob_expr[2])
            beta = E.prob(beta_left, beta_right)
            expr_prime = E.sigma(v_i, E.product([alpha, beta]))

            succ_length = proof_state.length + 1
            succ_heuristic = 0
            succ_bindings = dict(proof_state.bindings)
            succ_bindings[i] = value
            succ_root_expr = inject(expr_prime)

            succ_comment = 'conditioned %s on %s' % (
                pleasantly_fmt(proof_state.bindings, prob_expr),
                make_canonical_variable_name(value))

            succ_proof_state = ProofState(succ_length, succ_heuristic, succ_bindings, succ_root_expr,
                parent=proof_state, comment=succ_comment)
            yield succ_proof_state
Example #4
0
def test_gen_matches_deep():
    # sigma_y { p(x|y,do(z)) * p(y|do(z)) }
    root_expr = sigma(
        v('y'),
        product([
            prob([v('x')], [v('y'), do(v('z'))]),
            prob([v('y')], [do(v('z'))])
        ]))

    matches = list(gen_matches(is_v, root_expr))
    assert len(matches) == 6

    expr, inject = matches[3]
    assert expr == v('z')

    root_expr_prime = inject('walrus')
    assert root_expr_prime == sigma(
        v('y'),
        product([
            prob([v('x')], [v('y'), do('walrus')]),
            prob([v('y')], [do(v('z'))])
        ]))
Example #5
0
def test_normalise_single_iter():
    root_expr = sigma(v('x'), product([prob([v('z'), v('y')], [v('b'), v('x'), do(v('a'))]),
        prob([v('z'), v('y'), v('x')], [do(v('a'))])]))

    bindings = {'x' : 'xxx', 'z' : 'zzz', 'y' : 'yyy', 'a' : 'aaa'}

    state = ProofState(0, 0, bindings, root_expr)

    normalised_state = state.normalise(max_iters=1)

    # first up: expression ordering (nb do(v()) comes before v() in sorted lists)
    # sigma(x, product([prob([x y z],[do(a)]), prob([y z], [(do a) b x])]))
    # so, variable order should be:
    # x y z a b
    # so, new variable names should be
    # 0 1 2 3 4
    # so, normalised state should be
    # sigma(0, product([prob([0 1 2],[do(3)]), prob([1 2], [(do 3) 4 0])]))

    expected_result = sigma(v(0), product((prob((v(0), v(1), v(2)),(do(v(3)), )),
        prob((v(1), v(2)), (do(v(3)), v(4), v(0))))))

    assert normalised_state.root_expr == expected_result