def testGradientOfSimulateOfLookup2(): from venture.lite.sp_registry import builtInSPs sp = builtInSPs()["lookup"] args = [vv.VentureArrayUnboxed([0, 0], t.Number), vv.VentureNumber(1)] grad = sp.outputPSP.gradientOfSimulate(MockArgs(args, sp.constructSPAux()), vv.VentureNumber(0), vv.VentureNumber(1)) assert grad[0].lookup(vv.VentureNumber(0)) == vv.VentureNumber(0) assert grad[0].lookup(vv.VentureNumber(1)) == vv.VentureNumber(1) assert grad[1] == 0
def testForeignDeterminismSmoke(): get_ripl() # Build the SP registry (TODO !?) lite_normal_sp = builtInSPs()["normal"] def doit(seed): r = get_ripl(seed=seed) r.bind_foreign_sp("my_normal", lite_normal_sp) r.assume("x", "(my_normal 0 1)") r.infer("(default_markov_chain 3)") return r.sample("x") ans = doit(1) assert isinstance(ans, numbers.Number) eq_(ans, doit(1)) assert ans != doit(2)
def testInvWishartAssess(): psi = 3 # Parameterization from https://en.wikipedia.org/wiki/Inverse-Wishart_distribution dof = 5 n = 3000 low = 0.00001 high = 500 get_ripl() # Make sure the SP registry is built (!) inv_wishart_sp = builtInSPs()["inv_wishart"] scale_matrix = [[psi]] def inv_wishart(x): return logDensity(inv_wishart_sp, no_wrapper=True)([[x]], [scale_matrix, dof]) inv_wisharts = np.vectorize(inv_wishart)(np.linspace(low, high, n)) inv_gamma = scipy.stats.invgamma(dof*0.5, scale=psi*0.5).logpdf inv_gammas = np.vectorize(inv_gamma)(np.linspace(low, high, n)) cum_w = math.exp(logsumexp(inv_wisharts)) * (high - low) / n cum_g = math.exp(logsumexp(inv_gammas)) * (high - low) / n np.testing.assert_allclose([1, 1], [cum_w, cum_g], rtol=1e-2) np.testing.assert_allclose(inv_wisharts, inv_gammas)
def testVectorArrayExample(): from venture.lite.sp_registry import builtInSPs from venture.lite.value import VentureArray propRiplAgreesWithDeterministicSimulate([[VentureArray([])]], "is_vector", builtInSPs()["is_vector"])