Ejemplo n.º 1
0
def test_best_path():
    """
    Test viterbi path finding.
    """
    for h in hypergraphs():
        w = utils.random_log_viterbi_potentials(h)
        path = ph.best_path(h, w)
        nt.assert_not_equal(w.dot(path), 0.0)
        valid_path(h, path)
        same = False
        for other_path in utils.all_paths(h):
            assert w.dot(path) >= w.dot(other_path)
            if path == other_path: same = True
        assert same
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def test_all():
    s = simple_hypergraph()
    for path in utils.all_paths(s):
        valid_path(s, path)