Esempio n. 1
0
def test_infer_isolated():
    gr_size = 1000
    model = PairWiseFiniteModel(gr_size, 2)
    model.set_field(np.array([[0, 1]] * gr_size))
    res = model.infer(algorithm='bruteforce')
    assert np.allclose(res.log_pf, gr_size * np.log(1 + np.exp(1)))
    assert np.allclose(res.marg_prob - softmax([0, 1]), 0)
Esempio n. 2
0
def tree_potts_model(gr_size=5,
                     al_size=2,
                     seed=111,
                     same_j=None,
                     zero_field=False) -> PairWiseFiniteModel:
    """Generates random PairWiseFinteModel on a random tree.

    :param gr_size: Size of the graph (number of variables).
    :param al_size: Alphabet size.
    :param seed: Random set.
    :param same_j: If set, interaction matrix for all edges.
    :param zero_field: Whether model should be zero-field.
    :return: Generated Potts Model.
    """
    np.random.seed(seed)
    tree = networkx.generators.trees.random_tree(gr_size, seed=seed)
    model = PairWiseFiniteModel(gr_size, al_size)
    if not zero_field:
        model.set_field(-3.0 + 6.0 * np.random.random((gr_size, al_size)))
    for v1, v2 in tree.edges:
        J = np.random.random((al_size, al_size)) * 5.0
        if same_j is not None:
            J = same_j
        model.add_interaction(v1, v2, J)
    return model
Esempio n. 3
0
def test_max_likelihood():
    model = PairWiseFiniteModel(3, 2)
    model.set_field(np.array([[0.4, 0.6], [0.4, 0.6], [0.4, 0.6]]))
    model.add_interaction(0, 1, np.array([[0, 10], [10, 0]]))
    model.add_interaction(1, 2, np.array([[0, 10], [10, 0]]))

    state = model.max_likelihood(algorithm='bruteforce')

    assert np.allclose(state, np.array([1, 0, 1]))
Esempio n. 4
0
def test_infer_1_variable():
    al_size = 10
    probs = _stochastic_vector(al_size)
    model = PairWiseFiniteModel(1, al_size)
    model.set_field(np.log(probs).reshape(1, al_size))

    result = infer_bruteforce(model)

    assert np.allclose(result.log_pf, 0)
    assert np.allclose(result.marg_prob, probs)
Esempio n. 5
0
def clique_potts_model(gr_size=5, al_size=2, seed=0) -> PairWiseFiniteModel:
    """Generates random PairWiseFinteModel on a clique."""
    np.random.seed(seed)
    model = PairWiseFiniteModel(gr_size, al_size)
    model.set_field(-3.0 + 6.0 * np.random.random((gr_size, al_size)))
    for i in range(gr_size):
        for j in range(i + 1, gr_size):
            inter = np.random.random((al_size, al_size)) * 5.0
            model.add_interaction(i, j, inter)
    return model
Esempio n. 6
0
def test_isolated_exact():
    np.random.seed(0)
    gr_size = 1000
    al_size = 5
    model = PairWiseFiniteModel(gr_size, al_size)
    model.set_field(np.random.random((gr_size, al_size)))
    gt = model.infer(algorithm='bruteforce')

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

    assert_results_close(result, gt)
Esempio n. 7
0
def test_sample_bruteforce():
    gr_size, num_samples = 3, 50
    model = PairWiseFiniteModel(gr_size, 2)
    model.set_field(np.array([[0, 20]] * gr_size))
    samples = sample_bruteforce(model, num_samples=num_samples)
    assert np.allclose(samples, np.ones((num_samples, gr_size)))
Esempio n. 8
0
def test_max_likelihood_isolated():
    gr_size = 1000
    model = PairWiseFiniteModel(gr_size, 2)
    model.set_field(np.array([[0, 1]] * gr_size))
    result = model.max_likelihood(algorithm='bruteforce')
    assert np.allclose(result, np.ones(gr_size))