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
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)
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)
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)
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)
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)
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
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'))
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)
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)
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)
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)
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)
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)
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)
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)