def testAssumedGibbsWithLatent(): # raise(SkipTest('for quick results.')) "Checks an exact proposal with latent randomness" ripl = get_ripl() ripl.assume("x", "(scope_include (quote X) 0 (normal 0 1.0))", label="pid") ripl.observe("(scope_include (quote Y) 0 (normal (sqrt (* x x)) 1.0))", 10.0) MyBimodalExactPosterior = partial(BimodalExactPosterior, mu1=0, sigma1=1.0, sigma2=1.0) ripl.register_proposal_program_class("MyBimodalExactPosterior", MyBimodalExactPosterior) # Posterior for a is bi-modal with two modes at -5, 5, both with precision 2 # ripl.predict("(normal a 1.0)") proposal_src = """ [declare { "name":"bimodalexact", "class":"MyBimodalExactPosterior", "conditioned":[["Y",0]], "target":[["X",0]], "ready":"yes", "num_samples":0}] """ ripl.execute_program(proposal_src) predictions = collectSamples(ripl,"pid",infer="(custommh bimodalexact assumed_gibbs 1 1)") cdf = (lambda x:(stats.norm(loc=-5, scale=math.sqrt(0.5)).cdf(x)+stats.norm(loc=5, scale=math.sqrt(0.5)).cdf(x))*0.5) return reportKnownContinuous(cdf, predictions, "0.5*N(-5,sqrt(0.5))+0.5*N(5,sqrt(0.5))")
def testExpon1(seed): # Check that exponential distribution is parameterized correctly ripl = get_ripl(seed=seed) ripl.assume("a", "(expon 4.0)", label="pid") observed = collectSamples(ripl, "pid") expon_cdf = lambda x: expon.cdf(x, scale=1. / 4) return reportKnownContinuous(expon_cdf, observed)
def testAuxWithoutLatent(): # raise(SkipTest('for quick results.')) "Checks the posterior distribution on a Gaussian given an unlikely observation" ripl = get_ripl() ripl.assume("a", "(scope_include (quote A) 0 (normal 10.0 1.0))", label="pid") ripl.observe("(scope_include (quote B) 0 (normal a 1.0))", 20.0) # fake mu1=9, so that assumed_gibbs is doomed to fail the statistical test MyNormalBiasedPosterior = partial(NormalExactPosterior, mu1=9, sigma1=1.0, sigma2=1.0) ripl.register_proposal_program_class("MyNormalBiasedPosterior", MyNormalBiasedPosterior) # Posterior for a is normal with mean 15, precision 2 # ripl.predict("(normal a 1.0)") proposal_src = """ [declare { "name":"normalbiased", "class":"MyNormalBiasedPosterior", "conditioned":[["B",0]], "target":[["A",0]], "ready":"yes", "num_samples":0}] """ ripl.execute_program(proposal_src) predictions = collectSamples(ripl,"pid",infer="(custommh normalbiased aux 1 20)") cdf = stats.norm(loc=15, scale=math.sqrt(0.5)).cdf return reportKnownContinuous(cdf, predictions, "N(15,sqrt(0.5))")
def check_loggamma_ks(shape, seed): np_rng = numpy.random.RandomState(seed) nsamples = default_num_samples() log_samples = [simulateLogGamma(shape, np_rng) for _ in xrange(nsamples)] samples = np.exp(np.array(log_samples)) dist = scipy.stats.gamma(shape) return reportKnownContinuous(dist.cdf, samples)
def checkForceBrush2(infer, seed): ripl = get_ripl(seed=seed) ripl.assume("x", "(normal 0 1)") ripl.predict("(if (< x 0) (normal 0 1) (normal 100 1))", label="pid") predictions = collectSamples(ripl, "pid", infer=infer) cdf = lambda x: 0.5 * stats.norm(loc=0, scale=1).cdf(x) + 0.5 * stats.norm( loc=100, scale=1).cdf(x) return reportKnownContinuous(cdf, predictions, "N(0,1)/2 + N(100,1)/2")
def testInvGamma1(seed): # Check that Gamma is parameterized correctly ripl = get_ripl(seed=seed) # samples ripl.assume("a", "(inv_gamma 10.0 10.0)", label="pid") observed = collectSamples(ripl, "pid") # true CDF inv_gamma_cdf = lambda x: invgamma.cdf(x, a=10, scale=10) return reportKnownContinuous(inv_gamma_cdf, observed)
def testLaplace1(seed): # Test that laplace distribution does what it should ripl = get_ripl(seed=seed) # samples ripl.assume("a", "(laplace -3 2)", label="pid") observed = collectSamples(ripl, "pid") # true CDF laplace_cdf = lambda x: laplace.cdf(x, -3, 2) return reportKnownContinuous(laplace_cdf, observed)
def testBasicRejection3(seed): ripl = get_ripl(seed=seed) ripl.assume("p", "(uniform_continuous 0 1)", label="pid") ripl.observe("(flip p)", "true") predictions = collectSamples(ripl, "pid", infer="(rejection default all 1)") cdf = stats.beta(2, 1).cdf return reportKnownContinuous(cdf, predictions, "beta(2,1)")
def testPoisson1(seed): # Check that Poisson simulates and absorbs without crashing. ripl = get_ripl(seed=seed) ripl.assume("lambda", "(gamma 1 1)", label="pid") #ripl.predict("(poisson lambda)") predictions = collectSamples(ripl, "pid") return reportKnownContinuous( scipy.stats.gamma(1, scale=1 / 1.0).cdf, predictions, "(gamma 1 1)")
def check_loggamma_ks_quad(shape, seed): inf = float('inf') np_rng = numpy.random.RandomState(seed) nsamples = default_num_samples() samples = [simulateLogGamma(shape, np_rng) for _ in xrange(nsamples)] def pdf(x): return exp(logDensityLogGamma(x, shape)) def cdf(x): p, _e = scipy.integrate.quad(pdf, -inf, x) return p return reportKnownContinuous(np.vectorize(cdf), samples)
def testBernoulliIfNormal1(seed): # A simple program with bernoulli, if, and normal applications in the brush ripl = get_ripl(seed=seed) ripl.assume("b", "(bernoulli 0.3)") ripl.predict("(if b (normal 0.0 1.0) (normal 10.0 1.0))", label="pid") predictions = collectSamples(ripl, "pid") def cdf(x): return 0.3 * stats.norm.cdf(x, loc=0, scale=1) + \ 0.7 * stats.norm.cdf(x, loc=10, scale=1) return reportKnownContinuous(cdf, predictions, "0.3*N(0,1) + 0.7*N(10,1)")
def testInvWishartPrior2(seed): # Confirm that the diagonal elements of an inverse Wishart are an # inverse Gamma distribution. if inParallel() and backend_name() == "puma": raise SkipTest("The Lite SPs in Puma interface is not thread-safe, and wishart comes from Lite.") ripl = get_ripl(seed=seed) ripl.assume("s", "(matrix (array (array 2 -1) (array -1 3)))") ripl.assume("m", "(inv_wishart s 4.2)") ripl.predict("(lookup m (pair 1 1))", label="prediction") predictions = collectSamples(ripl, "prediction") cdf = scipy.stats.invgamma(a=1.6, scale=1.5).cdf return reportKnownContinuous(cdf, predictions)
def testWishartPrior1(seed): # Confirm that the diagonal elements of a Wishart are a chi-squared # distribution. if inParallel() and backend_name() == "puma": raise SkipTest("The Lite SPs in Puma interface is not thread-safe, and wishart comes from Lite.") ripl = get_ripl(seed=seed) ripl.assume("s", "(matrix (array (array 2 -1) (array -1 3)))") ripl.assume("m", "(wishart s 5)") ripl.predict("(lookup m (pair 0 0))", label="prediction") predictions = collectSamples(ripl, "prediction") cdf = scipy.stats.chi2(df=5, scale=2).cdf return reportKnownContinuous(cdf, predictions)
def testBernoulliIfNormal2(seed): # A simple program with bernoulli, if, and an absorbing application of normal if not collect_iid_samples(): raise SkipTest("This test should not pass without reset.") ripl = get_ripl(seed=seed) ripl.assume("b", "(bernoulli 0.3)") ripl.predict("(normal (if b 0.0 10.0) 1.0)", label="pid") predictions = collectSamples(ripl, "pid") def cdf(x): return 0.3 * stats.norm.cdf(x, loc=0, scale=1) + \ 0.7 * stats.norm.cdf(x, loc=10, scale=1) return reportKnownContinuous(cdf, predictions, "0.3*N(0,1) + 0.7*N(10,1)")
def testModelForkingSmoke(seed): ripl = get_ripl(seed=seed, persistent_inference_trace=True) ripl.execute_program(""" [assume p (beta 1 1)] [define beta_through_model (lambda (a b) (do (m <- (fork_model)) (res <- (in_model m (do (repeat (- a 1) (observe (flip p) true)) (repeat (- b 1) (observe (flip p) false)) (mh default one %s) (sample p)))) (return (first res))))] """ % default_num_transitions_per_sample()) predictions = [ripl.infer("(beta_through_model 3 2)") for _ in range(default_num_samples())] cdf = stats.beta(3,2).cdf return reportKnownContinuous(cdf, predictions)
def testObserveOutputOfIf1(seed): # It is natural to want deterministic conditionals in one's error # models. Some cases Venture can handle gracefully. ripl = get_ripl(seed=seed) ripl.assume("p", "(uniform_continuous 0.0 1.0)", label="pid") ripl.assume( "x", """ (if (bernoulli p) (normal 10.0 1.0) (normal 0.0 1.0)) """) ripl.observe("x", 11.0) predictions = collectSamples(ripl, "pid") cdf = stats.beta( 2, 1).cdf # The observation nearly guarantees the first branch is taken return reportKnownContinuous(cdf, predictions, "approximately beta(2,1)")
def testEval2(seed): ripl = get_ripl(seed=seed) ripl.assume("p", "(uniform_continuous 0.0 1.0)", label="pid") ripl.assume("globalEnv", "(get_current_environment)") ripl.assume( "expr", """ (quote ((biplex (bernoulli p) (lambda () (normal 10.0 1.0)) (lambda () (normal 0.0 1.0))))) """) ripl.assume("x", "(eval expr globalEnv)") ripl.observe("x", 11.0) predictions = collectSamples(ripl, "pid") cdf = stats.beta( 2, 1).cdf # The observation nearly guarantees the first branch is taken return reportKnownContinuous(cdf, predictions, "approximately beta(2,1)")
def testDefaultTrainer(): # raise(SkipTest('for quick results.')) "Train a proposal to inverse a joint Gaussian model, using default (target) trainer" ripl = get_ripl() ripl.assume("a", "(scope_include (quote A) 0 (normal 0.0 1.0))", label="pid") ripl.observe("(scope_include (quote B) 0 (normal (+ (* a 2) 2) 1.0))", 4.5) ripl.register_proposal_program_class("LinearRegressionProposalProgram", LinearRegressionProposalProgram) # Posterior for a is normal with mean 1.0, precision 5 proposal_src = """ [declare { "name":"linreg", "class":"LinearRegressionProposalProgram", "conditioned":[["B",0]], "target":[["A",0]], "num_samples":1000}] """ ripl.execute_program(proposal_src) predictions = collectSamples(ripl,"pid",infer="(custommh linreg aux 1 5)") cdf = stats.norm(loc=1.0, scale=math.sqrt(0.2)).cdf return reportKnownContinuous(cdf, predictions, "N(1.0,sqrt(0.2))")
def testCustommhSaveLoad(): # raise(SkipTest('for quick results.')) "Checks the posterior distribution on a Gaussian given an unlikely observation" ripl = get_ripl() ripl.assume("a", "(scope_include (quote A) 0 (normal 10.0 1.0))", label="pid") ripl.observe("(scope_include (quote B) 0 (normal a 1.0))", 20.0) MyNormalExactPosterior = partial(NormalExactPosterior, mu1=10.0, sigma1=1.0, sigma2=1.0) ripl.register_proposal_program_class("MyNormalExactPosterior", MyNormalExactPosterior) # Posterior for a is normal with mean 15, precision 2 # ripl.predict("(normal a 1.0)") proposal_src_1 = """ [declare { "name":"normalexact1", "class":"MyNormalExactPosterior", "conditioned":[["B",0]], "target":[["A",0]], "ready":"yes", "save_to":"test_custommh_save_load.npy", "num_samples":0}] """ ripl.execute_program(proposal_src_1) proposal_src_2 = """ [declare { "name":"normalexact2", "class":"MyNormalExactPosterior", "conditioned":[["B",0]], "target":[["A",0]], "load_from":"test_custommh_save_load.npy", "num_samples":0}] """ ripl.execute_program(proposal_src_2) predictions = collectSamples(ripl,"pid",infer="(custommh normalexact2 assumed_gibbs 1 1)") cdf = stats.norm(loc=15, scale=math.sqrt(0.5)).cdf return reportKnownContinuous(cdf, predictions, "N(15,sqrt(0.5))")
def testNormalWithObserves2(seed): ripl, post_mean, post_std, cdf = setupNormalWithObserves(100, 3.0, seed) # The sample distribution is an approximation to cdf with the accuracy # controlled by epsilon, the fifth argument. predictions = collectSamples(ripl,"pid",infer="(subsampled_mh default one 2 3 0.01 true %f false 50)" % post_std) return reportKnownContinuous(cdf, predictions, "N(%f,%f^2)" % (post_mean, post_std))
def testNormalWithObserves1(seed): ripl, post_mean, post_std, cdf = setupNormalWithObserves(100, 3.0, seed) predictions = collectSamples(ripl,"pid",infer="(subsampled_mh default one 2 3 0.01 false 0 false 50)") return reportKnownContinuous(cdf, predictions, "N(%f,%f^2)" % (post_mean, post_std))
def checkCDF(expr, cdf, seed): ripl = get_ripl(seed=seed) ripl.predict(expr, label="pid") predictions = collectSamples(ripl, "pid") return reportKnownContinuous(cdf, predictions, expr)