def test_foreign_latents_infer(seed): # Same as test.inference_quality.micro.test_latents.testHMMSP1 builtins = builtin.builtInSPs() ripl = get_ripl(seed=seed) ripl.bind_foreign_sp("test_lazy_hmm", builtins["make_lazy_hmm"]) ripl.assume( "f", """ (test_lazy_hmm (simplex 0.5 0.5) (matrix (array (array 0.7 0.3) (array 0.3 0.7))) (matrix (array (array 0.9 0.2) (array 0.1 0.8)))) """) ripl.observe("(f 1)", "integer<0>") ripl.observe("(f 2)", "integer<0>") ripl.observe("(f 3)", "integer<1>") ripl.observe("(f 4)", "integer<0>") ripl.observe("(f 5)", "integer<0>") ripl.predict("(f 6)", label="pid") ripl.predict("(f 7)") ripl.predict("(f 8)") predictions = collectSamples(ripl, "pid") ans = [(0, 0.6528), (1, 0.3472)] return reportKnownDiscrete(ans, predictions)
def test_foreign_aaa_uc(): builtins = builtin.builtInSPs() ripl = get_ripl() ripl.bind_foreign_sp("test_sym_dir_cat", builtins["make_uc_sym_dir_cat"]) ripl.assume("f", "(test_sym_dir_cat 1 1)") assert ripl.sample("f")["counts"] == [0] ripl.observe("(f)", "integer<0>") assert ripl.sample("f")["counts"] == [1] ripl.infer(defaultInfer()) assert ripl.sample("f")["counts"] == [1]
def test_foreign_latents(): builtins = builtin.builtInSPs() ripl = get_ripl() ripl.bind_foreign_sp("test_lazy_hmm", builtins["make_lazy_hmm"]) ripl.assume("f", "(test_lazy_hmm (simplex 1) (id_matrix 1) (id_matrix 1))") assert ripl.sample("f")[0] == [] ripl.observe("(f 1)", "integer<0>") assert ripl.sample("f")[0] == [np.matrix([[1]]), np.matrix([[1]])] ripl.infer(defaultInfer()) assert ripl.sample("f")[0] == [np.matrix([[1]]), np.matrix([[1]])]
def __init__(self, seed): self.results = {} self.env = env.VentureEnvironment() for name, val in builtin.builtInValues().iteritems(): self.bindPrimitiveName(name, val) for name, sp in builtin.builtInSPs().iteritems(): self.bindPrimitiveSP(name, sp) self.sealEnvironment() # New frame so users can shadow globals assert seed is not None rng = random.Random(seed) self.np_rng = npr.RandomState(rng.randint(1, 2**31 - 1)) self.py_rng = random.Random(rng.randint(1, 2**31 - 1))
def test_foreign_aaa_infer(seed): # Same as test.inference_quality.micro.test_misc_aaa.testMakeBetaBernoulli1 builtins = builtin.builtInSPs() ripl = get_ripl(seed=seed) ripl.bind_foreign_sp("test_beta_bernoulli", builtins["make_uc_beta_bernoulli"]) ripl.assume("a", "(normal 10.0 1.0)") ripl.assume("f", "(test_beta_bernoulli a a)") ripl.predict("(f)", label="pid") for _ in range(20): ripl.observe("(f)", "true") predictions = collectSamples(ripl, "pid") ans = [(False, .25), (True, .75)] return reportKnownDiscrete(ans, predictions)
def __init__(self, seed, trace=None): assert trace is None or isinstance(trace, puma.Trace) self.py_rng = random.Random(None) self.np_rng = npr.RandomState(None) if trace is None: self.trace = puma.Trace() self.set_seed(seed) for name, sp in builtInSPs().iteritems(): if self.trace.boundInGlobalEnv(name): # Already there pass else: # Use the Python SP as a fallback to not having a fast one self.bindPrimitiveSP(name, WarningSP(name, sp)) else: assert isinstance(trace, puma.Trace) self.trace = trace py_state, np_state = seed self.py_rng.setstate(py_state) self.np_rng.set_state(np_state)
def check_foreign_sp(mode): v = get_ripl() builtins = builtin.builtInSPs() resample = '[INFER (resample{0} 1)]'.format(mode) v.execute_instruction(resample) v.bind_foreign_sp('test_binomial', builtins['binomial']) v.bind_foreign_sp('test_sym_dir_cat', builtins['make_sym_dir_cat']) test_binomial_result = 1 test_sym_dir_result = { 'alpha': 1.0, 'counts': [0], 'n': 1, 'type': 'sym_dir_cat' } eq_(v.sample('(test_binomial 1 1)'), test_binomial_result) eq_(v.sample('(test_sym_dir_cat 1 1)'), test_sym_dir_result) engine = v.sivm.core_sivm.engine dumped = engine.model.retrieve_dump(0) restored = engine.model.restore_trace(dumped) engine.model.create_trace_pool([restored]) # Make sure that the restored trace still has the foreign SP's eq_(v.sample('(test_binomial 1 1)'), test_binomial_result) eq_(v.sample('(test_sym_dir_cat 1 1)'), test_sym_dir_result)
def testForeignInfSPs(): r = get_ripl(persistent_inference_trace=True) r.bind_foreign_inference_sp("my_mul", builtin.builtInSPs()["mul"]) r.infer("(resimulation_mh default one (+ (my_mul 2 2) 1))")