def test_vars_view(self): _, model, _ = models.multidimensional_model() with model: app = self.inference().approx posterior = app.random(10) x_sampled = app.view(posterior, 'x').eval() assert x_sampled.shape == (10, ) + model['x'].dshape
def test_vars_view(self): _, model, _ = models.multidimensional_model() with model: app = self.inference().approx posterior = app.random(10) x_sampled = app.view(posterior, 'x').eval() assert x_sampled.shape == (10,) + model['x'].dshape
def test_approximate(self): with models.multidimensional_model()[1]: meth = ADVI() fit(10, method=meth) with pytest.raises(KeyError): fit(10, method='undefined') with pytest.raises(TypeError): fit(10, method=1)
def test_combined(self): with models.multidimensional_model()[1]: self.assertRaises(ValueError, fit, 10, method='advi->fullrank_advi', frac=1) fit(10, method='advi->fullrank_advi', frac=.5)
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_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_length_of_hist(self): with models.multidimensional_model()[1]: inf = self.inference() assert len(inf.hist) == 0 inf.fit(10) assert len(inf.hist) == 10 assert not np.isnan(inf.hist).any() inf.fit(self.NITER, obj_optimizer=self.optimizer) assert len(inf.hist) == self.NITER + 10 assert not np.isnan(inf.hist).any()
def test_vars_view_dynamic_size(self): _, model, _ = models.multidimensional_model() with model: app = self.inference().approx i = tt.iscalar('i') i.tag.test_value = 1 posterior = app.random(i) x_sampled = app.view(posterior, 'x').eval({i: 10}) assert x_sampled.shape == (10,) + model['x'].dshape x_sampled = app.view(posterior, 'x').eval({i: 1}) assert x_sampled.shape == (1,) + model['x'].dshape
def test_vars_view_dynamic_size(self): _, model, _ = models.multidimensional_model() with model: app = self.inference().approx i = tt.iscalar('i') i.tag.test_value = 1 posterior = app.random(i) x_sampled = app.view(posterior, 'x').eval({i: 10}) assert x_sampled.shape == (10, ) + model['x'].dshape x_sampled = app.view(posterior, 'x').eval({i: 1}) assert x_sampled.shape == (1, ) + model['x'].dshape
def test_vars_view_dynamic_size_numpy(self): _, model, _ = models.multidimensional_model() with model: app = self.inference().approx i = tt.iscalar('i') i.tag.test_value = 1 x_sampled = app.view(app.random_fn(10), 'x') assert x_sampled.shape == (10, ) + model['x'].dshape x_sampled = app.view(app.random_fn(1), 'x') assert x_sampled.shape == (1, ) + model['x'].dshape x_sampled = app.view(app.random_fn(), 'x') assert x_sampled.shape == () + model['x'].dshape
def test_vars_view_dynamic_size_numpy(self): _, model, _ = models.multidimensional_model() with model: app = self.inference().approx i = tt.iscalar('i') i.tag.test_value = 1 x_sampled = app.view(app.random_fn(10), 'x') assert x_sampled.shape == (10,) + model['x'].dshape x_sampled = app.view(app.random_fn(1), 'x') assert x_sampled.shape == (1,) + model['x'].dshape x_sampled = app.view(app.random_fn(), 'x') assert x_sampled.shape == () + model['x'].dshape
def test_combined(self): with models.multidimensional_model()[1]: fit(10, method='advi->fullrank_advi', frac=.5)
def test_combined(self): with models.multidimensional_model()[1]: with pytest.raises(ValueError): fit(10, method='advi->fullrank_advi', frac=1) fit(10, method='advi->fullrank_advi', frac=.5)
def test_pickling(self): with models.multidimensional_model()[1]: inference = self.inference() inference = pickle.loads(pickle.dumps(inference)) inference.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)
def test_from_advi(self): with models.multidimensional_model()[1]: advi = ADVI() full_rank = FullRankADVI.from_advi(advi) full_rank.fit(20)
def test_from_mean_field(self): with models.multidimensional_model()[1]: advi = ADVI() full_rank = FullRankADVI.from_mean_field(advi.approx) full_rank.fit(20)
def test_profile(self): with models.multidimensional_model()[1]: self.inference().run_profiling(10)
def test_approximate(self): with models.multidimensional_model()[1]: fit(10, method='fullrank_advi')
def test_approximate(self): with models.multidimensional_model()[1]: meth = ADVI() fit(10, method=meth) self.assertRaises(KeyError, fit, 10, method='undefined') self.assertRaises(TypeError, fit, 10, method=1)
def test_init_from_noize(self): with models.multidimensional_model()[1]: histogram = Histogram.from_noise(100) assert histogram.histogram.eval().shape == (100, 6)