def checkConstrainAVar1b(program):
    ripl = get_ripl()
    ripl.assume("x", "(normal 0.0 1.0)")
    ripl.assume("y", "(normal 0.0 1.0)")
    ripl.predict("x", label="pid")
    ripl.observe("(if (tag 0 0 (flip)) x y)", 3.0)
    # Not collectSamples because we depend on the trace being in a
    # non-reset state at the end
    collectStateSequence(ripl, "pid", infer=program)
    eq_(ripl.report("pid"), 3)
def checkConstrainAVar4a(program):
    # We allow downstream processing with no requests and no randomness.
    ripl = get_ripl()
    ripl.assume("x", "(normal 0.0 1.0)")
    ripl.assume("y", "(normal 0.0 1.0)")
    ripl.assume("f", "(mem (lambda () (tag 0 0 (flip))))")
    ripl.predict("(if (f) (* x 5) (* y 5))", label="pid")
    ripl.observe("(if (f) x y)", 3.0)
    # Not collectSamples because we depend on the trace being in a
    # non-reset state at the end
    collectStateSequence(ripl, "pid", infer=program)
    eq_(ripl.report("pid"), 15.0)
Exemple #3
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)
Exemple #4
0
def test_serialize_forget(seed):
    with tempfile.NamedTemporaryFile(prefix='serialized.ripl') as f:
        v1 = get_ripl(seed=seed)
        v1.assume('is_tricky', '(flip 0.2)')
        v1.assume('theta', '(if is_tricky (beta 1.0 1.0) 0.5)')
        v1.assume('flip_coin', '(lambda () (flip theta))')
        for i in range(10):
            v1.observe('(flip_coin)', 'true', label='y{}'.format(i))

        v1.infer("(incorporate)")
        v1.save(f.name)

        v2 = get_ripl()
        v2.load(f.name)

        for i in range(10):
            v2.forget('y{}'.format(i))

        v2.predict('is_tricky', label='pid')

        infer = "(resimulation_mh default one %s)" % default_num_transitions_per_sample(
        )
        samples = collectStateSequence(v2, 'pid', infer=infer)
        ans = [(False, 0.8), (True, 0.2)]
        return reportKnownDiscrete(ans, samples)
def testHiddenDeterminism2():
    # Makes sure that blocking can avoid proposing impossible things.
    ripl = progHiddenDeterminism()
    # TODO enumerative gibbs triggers the log(0) bug even when blocked.
    predictions = collectStateSequence(ripl, "c2", infer="(mh default all 50)")
    # Block MH should explore the posterior
    ans = [(True, .5), (False, .5)]
    return reportKnownDiscrete(ans, predictions)
def testHiddenDeterminism1():
  # Makes sure that proposals of impossible things don't cause trouble
  ripl = progHiddenDeterminism()
  raise SkipTest("Crashes with a log(0) problem in log density of bernoulli.  Issue: https://app.asana.com/0/9277419963067/10386828313646")
  c1 = ripl.report("c1")
  # TODO Expand collectSamples to accept a list of indices and report all of them
  # TODO Expand collectSamples to accept the inference command as a string
  predictions = collectStateSequence(ripl, "c1", infer="(resimulation_mh default one 20)")
  # Single-site MH can't move on this problem
  for pred in predictions:
    eq_(pred, c1)