Exemplo n.º 1
0
def test_empirical_probabilities():
    # In this test case we sample 10000 from randomly generated model and use
    # samples to calculate empirical probabilities for every configuration.
    # Then we explicitly evaluate theoretical probabilities of every state and
    # assert that they are close to empirical probabilities.
    model = tree_potts_model(gr_size=5, al_size=2)
    num_samples = 10000
    samples = model.sample(num_samples=num_samples, algorithm='tree_dp')
    assert samples.shape == (num_samples, model.gr_size)

    # Calculate empirical probabilities of states by counting how many
    # times each state appeared in samples.
    emp_proba = np.zeros(model.al_size**model.gr_size)
    for sample in samples:
        state_id = model.encode_state(sample)
        emp_proba[state_id] += 1
    emp_proba /= num_samples

    # Calculate true probabilities of states by definition.
    true_proba = [
        model.evaluate(np.array(model.decode_state(i)))
        for i in range(model.al_size**model.gr_size)
    ]
    true_proba /= np.sum(true_proba)

    assert np.max(true_proba - emp_proba) < 0.02
Exemplo n.º 2
0
def test_tree():
    model = tree_potts_model(gr_size=50, al_size=3, seed=0)
    gt = model.infer(algorithm='tree_dp')

    result = model.infer(algorithm='mean_field')

    assert_results_close(result, gt, log_pf_tol=10.0, mp_mse_tol=0.03)
Exemplo n.º 3
0
def test_infer_compare_with_pairwise_tree():
    pw_model = tree_potts_model(gr_size=50, al_size=5, seed=0)
    true_pf = np.exp(pw_model.infer(algorithm='tree_dp').log_pf)
    nfg_model = inferlo.NormalFactorGraphModel.from_model(pw_model)

    pf = infer_edge_elimination(nfg_model)

    assert np.allclose(true_pf, pf)
Exemplo n.º 4
0
def test_max_likelihood_potts_tree_1000x5():
    libdai = LibDaiInterop()
    if not libdai.is_libdai_ready():
        return
    model = tree_potts_model(1000, 5)
    true_ml = model.max_likelihood()
    libdai_ml = libdai.max_likelihood(model, "BP")
    assert np.allclose(true_ml, libdai_ml)
Exemplo n.º 5
0
def test_marg_probs_potts_tree_1000x5():
    libdai = LibDaiInterop()
    if not libdai.is_libdai_ready():
        return
    model = tree_potts_model(1000, 5)
    true_result = model.infer()
    libdai_result = libdai.infer(model, "BP")
    assert_results_close(true_result, libdai_result, log_pf_tol=1e-8)
Exemplo n.º 6
0
def test_big_tree():
    gr_size = 1000
    al_size = 5
    j = np.ones((al_size, al_size)) + np.eye(al_size)
    model = tree_potts_model(gr_size=gr_size,
                             al_size=al_size,
                             seed=111,
                             same_j=j,
                             zero_field=True)
    result = model.infer(algorithm='tree_dp')
    assert np.allclose(result.marg_prob, np.ones((gr_size, al_size)) / al_size)
Exemplo n.º 7
0
def test_one_very_likely_state():
    # In this test case we generate model on tree and then set very large
    # fields such that they force values in all nodes to be equal to given
    # value with probability very close to 1. Then we assert that this
    # configuration was sampled at least 99% of times.
    gr_size = 100
    al_size = 5
    model = tree_potts_model(gr_size=gr_size, al_size=al_size)
    expected_configuration = np.random.choice(al_size, size=gr_size)
    field = np.zeros((gr_size, al_size))
    for i in range(gr_size):
        field[i][expected_configuration[i]] = 100
    model.set_field(field)

    samples = model.sample(num_samples=100, algorithm='tree_dp')
    good_count = sum(
        [np.all(sample == expected_configuration) for sample in samples])
    assert good_count >= 99
Exemplo n.º 8
0
def test_tree_exact():
    model = tree_potts_model(gr_size=50, al_size=3, seed=0)
    assert_results_close(model.infer(algorithm='message_passing'),
                         model.infer(algorithm='tree_dp'))
Exemplo n.º 9
0
def test_max_likelihood_tree_100x5():
    model = tree_potts_model(gr_size=100, al_size=5, seed=0)
    true_ml = model.max_likelihood(algorithm='tree_dp')
    ml = max_likelihood_junction_tree(model)
    assert np.allclose(ml, true_ml)
Exemplo n.º 10
0
def test_inference_tree_100x5():
    model = tree_potts_model(gr_size=100, al_size=5, seed=0)
    ground_truth = model.infer(algorithm='tree_dp')
    result = infer_junction_tree(model)
    assert_results_close(result, ground_truth)
Exemplo n.º 11
0
def test_tree_50x2():
    model = tree_potts_model(gr_size=50, al_size=2, seed=0)
    max_lh_gt = model.max_likelihood(algorithm='tree_dp')
    max_lh = max_lh_path_dp(model)
    assert np.allclose(max_lh, max_lh_gt)
Exemplo n.º 12
0
def test_vert15_alph2():
    model = tree_potts_model(gr_size=10, al_size=2, seed=123)
    ground_truth = model.infer(algorithm='bruteforce')
    result = model.infer(algorithm='tree_dp')
    assert_results_close(result, ground_truth)
Exemplo n.º 13
0
def test_tree_12x2():
    for seed in range(5):
        model = tree_potts_model(gr_size=12, al_size=2, seed=seed)
        truth = model.max_likelihood(algorithm='bruteforce')
        result = model.max_likelihood(algorithm='tree_dp')
        assert np.allclose(truth, result)
Exemplo n.º 14
0
def test_tree_5x3():
    model = tree_potts_model(gr_size=5, al_size=3, seed=0)
    truth = model.max_likelihood(algorithm='bruteforce')
    result = model.max_likelihood(algorithm='tree_dp')
    assert np.allclose(truth, result)
Exemplo n.º 15
0
def test_tree():
    model = tree_potts_model(gr_size=100, al_size=3)
    true_result = model.infer(algorithm='tree_dp')
    result_bp = BP.infer(model)
    assert_results_close(true_result, result_bp, log_pf_tol=2e-9)
Exemplo n.º 16
0
def test_tree_100x2():
    model = tree_potts_model(gr_size=100, al_size=2, seed=0)
    true_marg_probs = model.infer(algorithm='tree_dp').marg_prob
    samples = model.sample(num_samples=10000, algorithm='tree_dp')

    check_samples(samples=samples, true_marg_probs=true_marg_probs, tol=1e-4)