def test_sampling(self): with models.multidimensional_model()[1]: full_rank = FullRankADVI() approx = full_rank.fit(20) trace0 = approx.sample(10000) approx = Empirical(trace0) trace1 = approx.sample(100000) np.testing.assert_allclose(trace0['x'].mean(0), trace1['x'].mean(0), atol=0.01) np.testing.assert_allclose(trace0['x'].var(0), trace1['x'].var(0), atol=0.01)
def test_empirical_from_trace(another_simple_model): with another_simple_model: step = pm.Metropolis() trace = pm.sample(100, step=step) emp = Empirical(trace) assert emp.histogram.shape[0].eval() == 100 trace = pm.sample(100, step=step, njobs=4) emp = Empirical(trace) assert emp.histogram.shape[0].eval() == 400
def test_random_with_transformed(self): p = .2 trials = (np.random.uniform(size=10) < p).astype('int8') with pm.Model(): p = pm.Uniform('p') pm.Bernoulli('trials', p, observed=trials) trace = pm.sample(1000, step=pm.Metropolis()) approx = Empirical(trace) approx.randidx(None).eval() approx.randidx(1).eval() approx.random_fn(no_rand=True) approx.random_fn(no_rand=False) approx.histogram_logp.eval()
def test_aevb_empirical(): _, model, _ = models.exponential_beta(n=2) x = model.x mu = theano.shared(x.init_value) rho = theano.shared(np.zeros_like(x.init_value)) with model: inference = ADVI(local_rv={x: (mu, rho)}) approx = inference.approx trace0 = approx.sample(10000) approx = Empirical(trace0, local_rv={x: (mu, rho)}) trace1 = approx.sample(10000) np.testing.assert_allclose(trace0['y'].mean(0), trace1['y'].mean(0), atol=0.02) np.testing.assert_allclose(trace0['y'].var(0), trace1['y'].var(0), atol=0.02) np.testing.assert_allclose(trace0['x'].mean(0), trace1['x'].mean(0), atol=0.02) np.testing.assert_allclose(trace0['x'].var(0), trace1['x'].var(0), atol=0.02)
def test_aevb_empirical(self): _, model, _ = models.exponential_beta(n=2) x = model.x mu = theano.shared(x.init_value) rho = theano.shared(np.zeros_like(x.init_value)) with model: inference = ADVI(local_rv={x: (mu, rho)}) approx = inference.approx trace0 = approx.sample(10000) approx = Empirical(trace0, local_rv={x: (mu, rho)}) trace1 = approx.sample(10000) approx.random(no_rand=True) approx.random_fn(no_rand=True) np.testing.assert_allclose(trace0['y'].mean(0), trace1['y'].mean(0), atol=0.02) np.testing.assert_allclose(trace0['y'].var(0), trace1['y'].var(0), atol=0.02) np.testing.assert_allclose(trace0['x'].mean(0), trace1['x'].mean(0), atol=0.02) np.testing.assert_allclose(trace0['x'].var(0), trace1['x'].var(0), atol=0.02)
def test_from_empirical(another_simple_model): with another_simple_model: emp = Empirical.from_noise(1000) svgd = SVGD.from_empirical(emp) svgd.fit(20)
def test_init_from_noize(self): with models.multidimensional_model()[1]: approx = Empirical.from_noise(100) assert approx.histogram.eval().shape == (100, 6)