Beispiel #1
0
def testReportObserveInferActionSmoke():
  vals = get_ripl().execute_program("""\
[assume x (normal 0 1)]
foo : [observe (normal x 1) 0.2]
[infer (mh default one 1)]
(report 'foo)
""")
  eq_(0.2, u.strip_types(vals[3]['value']))
Beispiel #2
0
def testReportObserveListActionSmoke():
  vals = get_ripl().execute_program("""\
[assume m (array 0. 0.)]
[assume s (matrix (array (array 1. 0.) (array 0. 1.)))]
foo : [observe (multivariate_normal m s) (array 0.1 -0.1)]
(report 'foo)
""")
  eq_([0.1, -0.1], u.strip_types(vals[3]['value']))
Beispiel #3
0
def testForceSugar():
  r = get_ripl()
  r.set_mode("venture_script")
  vals = r.execute_program("""\
assume x = normal(0,1);
force x = 5;
report(quote(x));
""")
  eq_(5, u.strip_types([v['value'] for v in vals])[2])
Beispiel #4
0
def hmm_map(inferrer, states):
    sequence = map(int, u.strip_types(states[0]))
    time = hmm_time()
    hmm_pause()
    import sys
    print >> sys.stderr, 'MAP sequence: %s' % (sequence, )
    print >> sys.stderr, 'Score: %s' % inferrer.engine.ripl.infer(
        'global_log_joint')[0]
    print '%s,%s' % (time, metric1(sequence))
    hmm_resume()
Beispiel #5
0
def testEnumerativeSmoke():
    r = get_ripl()
    r.assume("x", "(categorical (simplex 0.1 0.2 0.3 0.4) (list 1 2 3 4))")
    r.infer("(enumerative_diversify default all)")
    assert np.allclose([1, 2, 3, 4],
                       strip_types(
                           r.sivm.core_sivm.engine.sample_all(v.sym("x"))))
    assert np.allclose([0.1, 0.2, 0.3, 0.4],
                       logWeightsToNormalizedDirect(
                           r.sivm.core_sivm.engine.model.log_weights))
Beispiel #6
0
def hmm_state(inferrer, states):
    global hmm_marginals
    assert hmm_marginals is not None
    import sys
    marginals = marginalize(inferrer, u.strip_types(states))
    tvd = total_variation_distance(marginals,
                                   filtered_marginals[len(hmm_marginals)])
    print >> sys.stderr, ('Filtered marginals at %2d (tvd %s): %s' % (
        len(hmm_marginals),
        tvd,
        marginals,
    ))
    hmm_marginals.append(marginals)
Beispiel #7
0
def testEnumerativeStacking():
    r = get_ripl()
    r.assume("x", "(categorical (simplex 0.1 0.2 0.3 0.4) (list 1 2 3 4))")
    r.infer("(enumerative_diversify default all)")
    r.infer("(enumerative_diversify default all)")
    assert np.allclose([1, 2, 3, 4] * 4,
                       strip_types(
                           r.sivm.core_sivm.engine.sample_all(v.sym("x"))))
    assert np.allclose(
        [
            0.01, 0.02, 0.03, 0.04, 0.02, 0.04, 0.06, 0.08, 0.03, 0.06, 0.09,
            0.12, 0.04, 0.08, 0.12, 0.16
        ],  #TODO Are these actually the weights I want here?
        logWeightsToNormalizedDirect(
            r.sivm.core_sivm.engine.model.log_weights))
Beispiel #8
0
def hmm_smoothed(inferrer, states, likelihood_weight=True):
    states = u.strip_types(states)
    n = len(states[0])
    marginals = [
        marginalize(inferrer, [s[i] for s in states], likelihood_weight)
        for i in range(n)
    ]
    time = hmm_time()
    hmm_pause()
    for i in range(n):
        import sys
        tvd = total_variation_distance(marginals[i], smoothed_marginals[i])
        print >> sys.stderr, ('Smoothed marginals at %2d (tvd %s): %s' %
                              (i, tvd, marginals[i]))
    m, v = metric2(marginals)
    print '%s,%s,%s' % (time, m, v)
    hmm_resume()
Beispiel #9
0
def printValue(directive):
  '''Gets the actual value returned by an assume, predict, report, or sample directive.'''
  if directive is None or 'value' not in directive:
    pass
  else:
    print strip_types(directive['value'])
Beispiel #10
0
def testSampleSugar():
  r = get_ripl()
  r.set_mode("venture_script")
  vals = r.execute_program("sample 2 + 2;")
  eq_([4], u.strip_types([v['value'] for v in vals]))
Beispiel #11
0
def testReportObserveActionSmoke():
  vals = get_ripl().execute_program("""\
foo : [observe (normal 0 1) 0.2]
(report 'foo)
""")
  eq_(0.2, u.strip_types(vals[1]['value']))
Beispiel #12
0
def testReportActionSmoke():
  vals = get_ripl().execute_program("""\
foo : [assume x (+ 1 2)]
(report 'foo)
""")
  eq_([3.0, 3.0], u.strip_types([v['value'] for v in vals]))