def test_init(self): beta = Beta(1, 1) self.assertEqual(beta.n_ones, 1) self.assertEqual(beta.n_zeros, 1) beta = Beta(np.zeros(2), np.ones(2)) self.assertTrue(np.allclose(beta.n_ones, np.zeros(2))) self.assertTrue(np.allclose(beta.n_zeros, np.ones(2)))
def test_draw(self): beta = Beta(np.array([3., 1.]), np.array([1., 3.])) self.assertEqual(beta.draw().shape, (1, 2)) sample = beta.draw(1000) self.assertTrue( np.allclose(np.mean(sample, axis=0), [0.75, 0.25], 1e-1, 1e-1) )
def test_bayes(self): mu = Beta(n_ones=np.ones(1), n_zeros=np.ones(1)) model = Bernoulli(prob=mu) self.assertEqual(repr(model), "Bernoulli(prob=Beta(n_ones=[ 1.], n_zeros=[ 1.]))") model.bayes(np.array([1., 1., 0.])[:, None]) self.assertEqual(repr(model), "Bernoulli(prob=Beta(n_ones=[ 3.], n_zeros=[ 2.]))")
def test_pdf(self): beta = Beta(np.ones(2), np.ones(2)) self.assertTrue( np.allclose(beta.pdf(np.random.uniform(size=(5, 2))), 1.) )
def test_var(self): beta = Beta(np.ones(2) * 3, np.ones(2)) self.assertTrue(np.allclose(beta.var, np.eye(2) * 3 / 80))
def test_mean(self): beta = Beta(np.ones(2) * 3, np.ones(2)) self.assertTrue(np.allclose(beta.mean, 0.75))
def test_repr(self): beta = Beta() self.assertEqual(repr(beta), "Beta(n_ones=[ 1.], n_zeros=[ 1.])")
def test_map(self): mu = Beta(n_ones=np.ones(1), n_zeros=np.ones(1)) model = Bernoulli(prob=mu) model.map(np.array([1., 1., 0.])[:, None]) self.assertTrue((model.prob == 2 / 3))
def test_repr(self): beta = Beta(n_ones=np.ones(1), n_zeros=np.ones(1)) self.assertEqual(repr(beta), "Beta(\nn_ones=[ 1.],\nn_zeros=[ 1.])")