def makeObserves(ripl, num_points): data = [ v.list([v.real(-2.4121470179012272), v.real(-4.0898807104175177)]), v.list([v.real(-3.5742473969392483), v.real(16.047761595206289)]), v.list([v.num(-0.44139814963230056), v.num(-4.366209321011203)]), v.list([v.num(-0.23258164982263235), v.num(-5.687017265835844)]), v.list([v.num(0.035565573263568961), v.num(-3.337136380872824)]), v.list([v.num(-4.3443008510924654), v.num(17.827515632039944)]), v.list([v.num(-0.19026889153157978), v.num(-3.6065264508558972)]), v.list([v.num(-4.8535236723040498), v.num(17.098870618931819)]), v.list([v.num(-1.1235421108040797), v.num(-5.261098458581845)]), v.list([v.num(1.1794444234381136), v.num(-3.630199721409284)]) ] for i in range(num_points): ripl.observe('(get_datapoint %d)' % i, data[i])
def pablo(nparticles): # Example program from Github issue #571. r = get_ripl(seed=1) r.set_mode("venture_script") r.execute_program(""" // Collapsed Dirichlet Process Mixture model using // a Chinese Restaurant Process // CRP and cluster assume crp = make_crp(10); assume get_cluster = mem(proc(id){ tag(quote(clustering), id, crp()) }); // Components assume cmvn = mem(proc(cluster){ make_niw_normal(array(1, 1),1,2 + 1,matrix(array(array(1,0),array(0,1)))) }); // Sample data assume get_datapoint = mem(proc(id){ cmvn(get_cluster(id))() }); """) r.observe("get_datapoint(1)", vd.list([vd.real(12.4299342152), vd.real(11.0243723622)])) r.observe("get_datapoint(2)", vd.list([vd.real(0.0876588636563), vd.real(0.46296506961)])) r.observe("get_datapoint(3)", vd.list([vd.real(-7.99964564506), vd.real(-8.98731353208)])) r.observe("get_datapoint(4)", vd.list([vd.real(8.23541122165), vd.real(7.79102768844)])) r.observe("get_datapoint(5)", vd.list([vd.real(0.974810779937), vd.real(1.38726814618)])) r.observe("get_datapoint(6)", vd.list([vd.real(9.01950900535), vd.real(9.30739440906)])) r.observe("get_datapoint(7)", vd.list([vd.real(-10.5666520344), vd.real(-9.79815099761)])) r.observe("get_datapoint(8)", vd.list([vd.real(-0.298512623055), vd.real(-0.893928070534)])) r.observe("get_datapoint(9)", vd.list([vd.real(12.0800451026), vd.real(7.67721242372)])) r.observe("get_datapoint(10)", vd.list([vd.real(8.77103714879), vd.real(9.60268348563)])) r.infer("resample(%d)" % (nparticles, )) return r
def testMoreElaborate(seed): # Confirm that HMC still works in the presence of brush. Do not, # however, mess with the possibility that the principal nodes that # HMC operates over may themselves be in the brush. ripl = get_ripl(seed=seed) ripl.assume("x", "(tag (quote param) 0 (uniform_continuous -10 10))") ripl.assume("y", "(tag (quote param) 1 (uniform_continuous -10 10))", label="pid") ripl.assume("xout", """ (if (< x 0) (normal x 1) (normal x 2))""") ripl.assume( "out", "(multivariate_normal (array xout y) (matrix (list (list 1 0.5) (list 0.5 1))))" ) # Note: Can't observe coordinatewise because observe is not flexible # enough. For this to work we would need observations of splits. # ripl.observe("(lookup out 0)", 0) # ripl.observe("(lookup out 1)", 0) # Can't observe through the ripl literally because the string # substitution (!) is not flexible enough. # ripl.observe("out", [0, 0]) ripl.observe("out", val.list([val.real(0), val.real(0)])) preds_mh = collectSamples(ripl, "pid", infer="(resimulation_mh default one 10)") ripl.sivm.core_sivm.engine.reinit_inference_problem() preds_hmc = collectSamples(ripl, "pid", infer="(hmc 'param all 0.1 20 10)") return reportSameContinuous(preds_mh, preds_hmc)
def testMoveMatrix(): ripl = get_ripl() ripl.assume("mu", "(array 0 0)") ripl.assume("scale", "(matrix (list (list 2 1) (list 1 2)))") ripl.assume("sigma", "(wishart scale 4)", label="pid") ripl.assume("out", "(multivariate_normal mu sigma)") ripl.observe("out", val.list([val.real(1), val.real(1)])) preds_mh = collectSamples(ripl, "pid", infer="(resimulation_mh default one 30)") ripl.sivm.core_sivm.engine.reinit_inference_problem() preds_hmc = collectSamples(ripl, "pid", infer="(hmc default all 0.01 5 5)")