Пример #1
0
def _test_serialize_program(v, label, action):
    engine = v.sivm.core_sivm.engine

    if action == 'serialize':
        trace1 = engine.getDistinguishedTrace()
        serialized = trace1.dump()
        trace2 = engine.model.restore_trace(serialized)
        assert isinstance(serialized, tuple)
        assert len(serialized) == 3
        assert isinstance(serialized[0], list)
        assert all(isinstance(x, dict) for x in serialized[0])
        assert isinstance(serialized[1],
                          dict)  # Mapping directive ids to directives
        for (key, val) in serialized[1].iteritems():
            assert isinstance(key, int)
            assert isinstance(val, list)
        assert isinstance(serialized[2], set)  # Names of bound foreign sps
        for elem in serialized[2]:
            assert isinstance(elem, basestring)
        assert isinstance(trace2, type(trace1))
        assert isinstance(trace2.trace, type(trace1.trace))
    elif action == 'copy':
        trace1 = engine.getDistinguishedTrace()
        trace2 = engine.model.copy_trace(trace1)
        assert isinstance(trace2, type(trace1))
        assert isinstance(trace2.trace, type(trace1.trace))
    elif action == 'convert_puma':
        try:
            from venture.puma import trace
        except ImportError:
            raise SkipTest("Puma backend does not appear to be installed")
        trace1 = engine.getDistinguishedTrace()
        engine.to_puma()
        trace2 = engine.getDistinguishedTrace()
        assert 'venture.puma' in trace2.trace.__module__
    elif action == 'convert_lite':
        trace1 = engine.getDistinguishedTrace()
        engine.to_lite()
        trace2 = engine.getDistinguishedTrace()
        assert 'venture.lite' in trace2.trace.__module__
    else:
        assert False

    infer = "(resimulation_mh default one %s)" % default_num_transitions_per_sample(
    )
    engine.model.create_trace_pool([trace2])
    r2 = collectStateSequence(v, label, infer=infer)

    engine.model.create_trace_pool([trace1])
    r1 = collectStateSequence(v, label, infer=infer)

    return reportSameDiscrete(r1, r2)
def testEnumerateCoupledChoices2(seed):
    # A second illustration of Issue #462 (second manifestation).
    #
    # If enumeration computes the set of candidate values before
    # detaching, as an independent product, it will invent combinations
    # that are actually distinct representations of semantically the
    # same option.  Thus, even though all possibilities will (as in this
    # variant) be considered, some will be overweighted.
    #
    # Specifically, if the initial state is three calls to the same CRP
    # with distinct values (arranged by the force statements),
    # enumeration will invent 4^3 different combinations of tables to
    # try; whereas there are only 5 that differ up to renumbering of the
    # tables: (1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), and (1, 2, 3).
    # They cannot, therefore, be overrepresented evenly, and this leads
    # to the wrong posterior.
    raise SkipTest(
        "Fails due to https://github.com/probcomp/Venturecxx/issues/462")
    r = get_ripl(seed=seed)
    r.assume("crp", "(make_crp 1)")
    r.assume("result1", "(crp)")
    r.assume("result2", "(crp)")
    r.assume("result3", "(crp)")
    r.predict(
        "(and (not (eq result1 result2))"
        "(and (not (eq result2 result3))"
        "(not (eq result1 result3))))",
        label="pid")
    ans = collectSamples(r,
                         "pid",
                         infer="reset_to_prior",
                         num_samples=default_num_samples(4))
    gibbs_from_different = """(do
    (force result1 atom<1>)
    (force result2 atom<2>)
    (force result3 atom<3>)
    (gibbs default all 1))"""
    # One step of Gibbs from any initial condition should move to the
    # posterior (which in this case equals the prior).
    predicts = collectSamples(r,
                              "pid",
                              infer=gibbs_from_different,
                              num_samples=default_num_samples(4))
    return reportSameDiscrete(ans, predicts)
def testEnumerateCoupledChoices1(seed):
    # A test case for the first problem identified in Issue #462.
    #
    # If enumaration collects the candidate value sets all at once at
    # the beginning of the enumeration run, and if the set of options
    # for a later choice depends on the choice taken at an earlier one
    # (e.g., for the made SP of make_crp), trouble will ensue because we
    # need to compute a dependent rather than independent product.
    #
    # This example suffices to bring the problem into relief.  If a CRP
    # has only one extant table at the time enumeration is invoked,
    # (arranged by the force calls), each node will consider that table
    # and one new table as the only options.  Enumeration will therefore
    # consider 8 options, in none of which will all three nodes be
    # assigned to distinct tables.
    raise SkipTest(
        "Fails due to https://github.com/probcomp/Venturecxx/issues/462")
    r = get_ripl(seed=seed)
    r.assume("crp", "(make_crp 1)")
    r.assume("result1", "(crp)")
    r.assume("result2", "(crp)")
    r.assume("result3", "(crp)")
    r.predict(
        "(and (not (eq result1 result2))"
        "(and (not (eq result2 result3))"
        "(not (eq result1 result3))))",
        label="pid")
    ans = collectSamples(r,
                         "pid",
                         infer="reset_to_prior",
                         num_samples=default_num_samples(4))
    gibbs_from_same = """(do
    (force result1 atom<1>)
    (force result2 atom<1>)
    (force result3 atom<1>)
    (gibbs default all 1))"""
    # One step of Gibbs from any initial condition should move to the
    # posterior (which in this case equals the prior).
    predicts = collectSamples(r,
                              "pid",
                              infer=gibbs_from_same,
                              num_samples=default_num_samples(4))
    return reportSameDiscrete(ans, predicts)
def testEnumerateCoupledChoices3(seed):
    # A third illustration of Issue #462 (second manifestation).
    #
    # Enumerating a single choice should not depend on the initial value
    # of that choice, but due to #462 it does.  The setup here is
    # enumerating one of two choices from a CRP.  If they are initially
    # distinct, enumeration will consider three options, the latter two
    # of which will be equivalent: "become the same as the other point",
    # "remain the same as I was", and "become a unique snowflake".  This
    # will cause it to overweight the state where the choices are
    # distinct by 2:1.
    r = get_ripl(seed=seed)
    r.assume("crp", "(make_crp 1)")
    r.assume("result1", "(crp)")
    r.assume("result2", "(crp)")
    r.predict("(eq result1 result2)", label="pid")
    gibbs_from_same = """(do
    (force result1 atom<1>)
    (force result2 atom<1>)
    (gibbs default one 1))"""
    ans = collectSamples(r,
                         "pid",
                         infer=gibbs_from_same,
                         num_samples=default_num_samples(6))
    gibbs_from_different = """(do
    (force result1 atom<1>)
    (force result2 atom<2>)
    (gibbs default one 1))"""
    # In this case, gibbs_from_same happens to compute the exact
    # posterior, which equals the prior, and is 50-50 on whether the
    # atoms are the same.
    predicts = collectSamples(r,
                              "pid",
                              infer=gibbs_from_different,
                              num_samples=default_num_samples(6))
    return reportSameDiscrete(ans, predicts)
def testEnumerativeGibbsBrushRandomness(seed):
    # Test that Gibbs targets the correct stationary distribution,
    # even when there may be random choices downstream of variables
    # being enumerated.
    ripl = get_ripl(seed=seed)
    ripl.assume("z", "(tag 'z 0 (flip))")
    ripl.assume("x", "(if z 0 (normal 0 10))")
    ripl.observe("(normal x 1)", "4")
    ripl.predict("z", label="pid")

    def posterior_inference_action():
        # Work around the fact that Puma doesn't have rejection sampling
        # by asking for a bunch of MH.
        if backend_name() == 'lite':
            return "(rejection default all 1)"
        else:
            return "(resimulation_mh default one %d)" % (
                default_num_transitions_per_sample(), )

    ans = collectSamples(ripl, "pid", infer=posterior_inference_action())
    gibbs_inference_action = "(do %s (gibbs 'z 0 1))" % \
                             (posterior_inference_action(),)
    predictions = collectSamples(ripl, "pid", infer=gibbs_inference_action)
    return reportSameDiscrete(ans, predictions)
Пример #6
0
def checkHPYMem1(baseline, topC, botC, seed):
    data = predictHPY(topC, botC, seed)
    return reportSameDiscrete(baseline, data)
Пример #7
0
def testTwoSampleChi2():
    data1 = range(5) * 5
    data2 = sorted(data1)
    assert reportSameDiscrete(data1, data2).pval == 1