Ejemplo n.º 1
0
    def test_multiple_simulators(self):
        true_a = 2
        true_b = -2

        data1 = np.random.normal(true_a, 0.1, size=1000)
        data2 = np.random.normal(true_b, 0.1, size=1000)

        with pm.Model() as m:
            a = pm.Normal("a", mu=0, sigma=3)
            b = pm.Normal("b", mu=0, sigma=3)
            sim1 = pm.Simulator(
                "sim1",
                self.normal_sim,
                a,
                0.1,
                distance="gaussian",
                sum_stat="sort",
                observed=data1,
            )
            sim2 = pm.Simulator(
                "sim2",
                self.normal_sim,
                b,
                0.1,
                distance="laplace",
                sum_stat="mean",
                epsilon=0.1,
                observed=data2,
            )

        assert self.count_rvs(m.logpt) == 2

        # Check that the logps use the correct methods
        a_val = m.rvs_to_values[a]
        sim1_val = m.rvs_to_values[sim1]
        logp_sim1 = pm.logp(sim1, sim1_val)
        logp_sim1_fn = aesara.function([sim1_val, a_val], logp_sim1)

        b_val = m.rvs_to_values[b]
        sim2_val = m.rvs_to_values[sim2]
        logp_sim2 = pm.logp(sim2, sim2_val)
        logp_sim2_fn = aesara.function([sim2_val, b_val], logp_sim2)

        assert any(node for node in logp_sim1_fn.maker.fgraph.toposort()
                   if isinstance(node.op, SortOp))

        assert not any(node for node in logp_sim2_fn.maker.fgraph.toposort()
                       if isinstance(node.op, SortOp))

        with m:
            trace = pm.sample_smc(return_inferencedata=False)

        assert abs(true_a - trace["a"].mean()) < 0.05
        assert abs(true_b - trace["b"].mean()) < 0.05
Ejemplo n.º 2
0
    def test_potential(self):
        with pm.Model():
            x = pm.Normal("x", 0.0, 1.0)
            pm.Potential("z", pm.logp(pm.Normal.dist(x, 1.0), np.random.randn(10)))
            inference_data = pm.sample(100, chains=2, return_inferencedata=True)

        assert inference_data
Ejemplo n.º 3
0
 def test_gaussian_random_walk_init_dist_logp(self, init):
     grw = pm.GaussianRandomWalk.dist(init=init, steps=1)
     assert np.isclose(
         pm.logp(grw, [0, 0]).eval(),
         pm.logp(init, 0).eval() + scipy.stats.norm.logpdf(0),
     )
Ejemplo n.º 4
0
 def logp(x):
     out = pm.logp(normal_dist, x)
     return out
Ejemplo n.º 5
0
 def logp(x):
     out = pm.logp(normal_dist, x, transformed=False)
     return out
Ejemplo n.º 6
0
 def logp(x, mu):
     normal_dist = pm.Normal.dist(mu, 1, size=N)
     out = pm.logp(normal_dist, x)
     return out