def test_sampling(self): with models.multidimensional_model()[1]: full_rank = FullRankADVI() approx = full_rank.fit(20) trace0 = approx.sample_vp(10000) histogram = Histogram(trace0) trace1 = histogram.sample_vp(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_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()) histogram = Histogram(trace) histogram.randidx(None).eval() histogram.randidx(1).eval() histogram.random_fn(no_rand=True) histogram.random_fn(no_rand=False) histogram.histogram_logp.eval()
def test_aevb_histogram(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_vp(10000) histogram = Histogram(trace0, local_rv={x: (mu, rho)}) trace1 = histogram.sample_vp(10000) histogram.random(no_rand=True) histogram.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_init_from_noize(self): with models.multidimensional_model()[1]: histogram = Histogram.from_noise(100) assert histogram.histogram.eval().shape == (100, 6)