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 compareSampleDicts(dicts_hists,labels,plot=False): '''Input: dicts_hists :: ({exp:values}) | (History) where the first Series in History is used as values. History's are converted to dicts.''' if not isinstance(dicts_hists[0],dict): dicts = [historyNameToValues(h,seriesInd=0) for h in dicts_hists] else: dicts = dicts_hists dicts = map(filterScalar,dicts) # could skip for Analytics stats = (np.mean,np.median,np.std,len) # FIXME stderr stats_dict = {} print 'compareSampleDicts: %s vs. %s \n'%(labels[0],labels[1]) for exp in dicts[0].keys(): stats_dict[exp] = [] for dict_i,label_i in zip(dicts,labels): samples=dict_i[exp] s_stats = tuple([s(samples) for s in stats]) stats_dict[exp].append(s_stats) print '\nDict: %s. Exp: %s'%(label_i,exp) print 'Mean, median, std, N = %.3f %.3f %.3f %i'%s_stats testResult=reportSameContinuous(dicts[0][exp],dicts[1][exp]) print 'KS SameContinuous:', ' '.join(testResult.report.split('\n')[-2:]) stats_dict[exp].append( testResult ) fig = qqPlotAll(dicts,labels) if plot else None return stats_dict,fig
def testForceBrush5(seed): ripl = get_ripl(seed=seed) ripl.assume("x", "(normal 0 1)", label="pid") ripl.assume("y", "(if (< x 0) (normal x 1) (normal (+ x 10) 1))") ripl.observe("y", 8) preds_mh = collectSamples(ripl, "pid", infer="(mh default one 10)") ripl.sivm.core_sivm.engine.reinit_inference_problem() preds_hmc = collectSamples(ripl, "pid", infer="(hmc default one 0.1 20 10)") return reportSameContinuous(preds_mh, preds_hmc)
def testRollingResample(): ripl = get_ripl() ripl.assume("a", "(normal 10.0 1.0)", label="pid") ripl.observe("(normal a 1.0)", 14.0) flat_resample = "(do (resample 5) (likelihood_weight) (resample 1))" predictions1 = collectSamples(ripl, "pid", infer=flat_resample) rolling_resample = "(repeat 5 (do add_particle (resample 1)))" predictions2 = collectSamples(ripl, "pid", infer=rolling_resample) return reportSameContinuous(predictions1, predictions2)
def testAAAHyperInference(seed): def try_at_five(maker): r = get_ripl(seed=seed) r.assume("mu", "(normal 0 1)") r.assume("obs", "(%s mu 1 1 1)" % maker) r.observe("(obs)", 5) infer = "(mh default all %d)" % default_num_transitions_per_sample() return collectSamples(r, "mu", infer=infer, num_samples=default_num_samples(2)) collapsed = try_at_five("make_nig_normal") uncollapsed = try_at_five("make_uc_nig_normal") return reportSameContinuous(collapsed, uncollapsed)
def testTwoSampleKS(seed): rng = np.random.RandomState(seed) data1 = rng.normal(size=100, loc=0., scale=1) data2 = rng.normal(size=100, loc=0., scale=1) return reportSameContinuous(data1, data2)