Example #1
0
def test_line():
    model = line_potts_model(gr_size=40, al_size=3, seed=0)
    gt = model.infer(algorithm='path_dp')

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

    assert_results_close(result, gt, log_pf_tol=3.0, mp_mse_tol=1e-2)
Example #2
0
def test_long_line_compare_with_tree():
    # Test to ensure Tree DP and Path DP are consistent.
    gr_size = 1000
    al_size = 3
    model = line_potts_model(gr_size=gr_size, al_size=al_size, seed=111)
    result1 = model.infer(algorithm='path_dp')
    result2 = model.infer(algorithm='tree_dp')
    assert_results_close(result1, result2)
Example #3
0
def test_line_potts_4x3_sherali_adams():
    """
    Sherali-Adams is exact on line graph.
    """
    model = line_potts_model(gr_size=4, al_size=3, seed=0)
    max_lh_gt = model.max_likelihood(algorithm='tree_dp')
    sa_res = sherali_adams(model, level=3)
    max_lh_ub = sa_res.upper_bound
    assert np.allclose(max_lh_ub, np.log(model.evaluate(max_lh_gt)), atol=1e-2)
Example #4
0
def test_long_line():
    gr_size = 1000
    al_size = 5
    j = np.ones((al_size, al_size)) + np.eye(al_size)
    model = line_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)
Example #5
0
def test_line_potts_4x2_lasserre():
    """
    Second step of Lasserre hierarchy is exact on line graph.

    This test is skipped for Windows since the performance of SCS
    solver was unstable.
    """
    model = line_potts_model(gr_size=4, al_size=2, seed=0)
    max_lh_gt = model.max_likelihood(algorithm='tree_dp')
    lasserre_res = lasserre(model, level=2)
    max_lh_ub = lasserre_res.upper_bound
    x = np.log(model.evaluate(max_lh_gt))
    assert np.allclose(max_lh_ub, x, atol=1e-2)
Example #6
0
def test_antiferromagnetic_ising_line():
    # We create Ising model on a line with very high interactions forcing
    # samples to be alternate (i.e. 10101010). Then we assert that in at
    # least 99% of cases we got alternating sample.
    model = line_potts_model(gr_size=100,
                             al_size=2,
                             same_j=[[0, 100], [100, 0]],
                             zero_field=True)
    samples = model.sample(num_samples=100)

    def is_alternating(x):
        return np.all(np.roll(x, 1) == 1 - x)

    good_count = sum([is_alternating(state) for state in samples])
    assert good_count >= 99
Example #7
0
def test_line_1000x10():
    model = line_potts_model(gr_size=1000, al_size=10, 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)