def test_inside(): """ Test inside chart gen. """ for h in hypergraphs(): w = utils.random_inside_potentials(h) inside = ph.inside(h, w)
def test_posteriors(): "Check the posteriors by enumeration." for h in hypergraphs(): w = utils.random_inside_potentials(h) marg = ph.compute_marginals(h, w) paths = utils.all_paths(h) m = defaultdict(lambda: 0.0) total_score = 0.0 for path in paths: path_score = w.dot(path) total_score += path_score for edge in path: m[edge.id] += path_score for edge in h.edges: nt.assert_almost_equal( marg[edge] / marg[h.root], m[edge.id] / total_score, places=4) chart = ph.inside(h, w) nt.assert_almost_equal(chart[h.root], total_score, places=4)