def test_binary(self): m = bayesnet.BayesNetMap() with pytest.raises(KeyError): m.get_net("a") m.add_net("a", self.net_1) assert m.get_net("a") == self.net_1 with pytest.raises(KeyError): m.get_net("b") with pytest.raises(AssertionError): m.add_net("a", self.net_2) m.add_net("b", self.net_2) assert m.get_net("a") == self.net_1 assert m.get_net("b") == self.net_2 with pytest.raises(KeyError): m.get_net("c") with pytest.raises(AssertionError): m.add_net("c", sprinkler_net.get(self.rng)) m.add_net("c", self.net_1) m.add_net("d", self.net_2) assert m.get_net("a") == self.net_1 assert m.get_net("b") == self.net_2 assert m.get_net("c") == self.net_1 assert m.get_net("d") == self.net_2 assert len(m.items()) == 4 assert sorted(m.items())[0] == ("a", self.net_1) assert sorted(m.keys()) == ["a", "b", "c", "d"]
def setup(self): """Set up random stream and network.""" rng = utils.RandomState(seed=0) self.net = sprinkler_net.get(rng) self.rain_node = self.net.find_node("Rain") self.sprinkler_node = self.net.find_node("Sprinkler") self.grass_node = self.net.find_node("Grass") self.n = 10000
def test_marg_counter(): rng = utils.RandomState(seed=0) net = sprinkler_net.get(rng) counter = marg.MarginalCounter(net) with pytest.raises(AssertionError): counter.marginals() value_lists = [[0, 0, 0], [0, 0, 1], [0, 1, 1]] for values in value_lists: world = random_world.RandomWorld([0, 1, 2], values) counter.observe(world) assert counter.num_observations == 3 marginals = counter.marginals() np.testing.assert_almost_equal(marginals[0][0], 1.0) np.testing.assert_almost_equal(marginals[0][1], 0.0) np.testing.assert_almost_equal(marginals[1][0], 2 / 3) np.testing.assert_almost_equal(marginals[1][1], 1 / 3) np.testing.assert_almost_equal(marginals[2][0], 1 / 3) np.testing.assert_almost_equal(marginals[2][1], 2 / 3)
def test_gibbs_learner(): """Verify that Gibbs learner makes same predictions as enumerator.""" num_samples = 10000 rng = utils.RandomState(seed=0) net = sprinkler_net.get(rng) for node in net.nodes_by_index: learner = learn.GibbsLearner(node, rng) learner.finalize() for markov_blanket_values in utils.lexicographic_combinations( [[0, 1]] * len(node.markov_blanket)): world = random_world.RandomWorld( [n.index for n in node.markov_blanket], markov_blanket_values) enumerator = exact_inference.Enumerator(net, world) probabilities = enumerator.marginalize_node(node) check_scorer( lambda v: learner.log_probability( markov_blanket_values, v), probabilities) check_sampler( lambda: learner.sample( markov_blanket_values), probabilities, num_samples)
def setup(self): self.rng = utils.RandomState(seed=0) self.net = sprinkler_net.get(self.rng)
def setup(self): """Set up random stream and sprinkler network.""" self.rng = utils.RandomState(seed=0) self.net = sprinkler_net.get(self.rng)
def test_sprinkler_net(proposal_size, evidence_index): rng = utils.RandomState(seed=0) net = sprinkler_net.get(rng) evidence = sprinkler_net.evidence(evidence_index) run_test(rng, net, evidence, proposal_size=proposal_size)