def test_proposal_COB_complete(): n, n_obs = (5, 500) tree_prior = dists.TreePriors.Hubs(n) ct_prior = dists.CountPriors.TruncatedNB(1, .5) cob_freq = 10 prior = dists.Priors.BasisCount(n, GraphAndBasis, ct_prior, tree_prior) temp = Graph(5) temp.SetComplete() g = GraphAndBasis(5, temp._dol, tree=tree_prior.Sample(), basis=np.ones((n - 1) * (n - 2) // 2, dtype=bool)) data = generate_data(n, n_obs, g, seed=123) lik = dists.Likelihoods.Delta(g.copy()) prop = dists.Proposals.BasisWalk(n, GraphAndBasis, tree_prior, skip=cob_freq) np.random.seed(123) sampler = MCMC_Sampler(prior, prop, lik, data) sampler.run(5000, g) assert (tree_changes_only_every_k_iter(sampler, cob_freq)) assert (if_both_empty_or_complete_cob_always_happen(sampler, cob_freq)) assert (only_propose_one_basis_difference(sampler, cob_freq)) assert (lookup_contains_all_params(sampler)) assert (sampler_types_are_correct(n, sampler))
def test_MCMC_edge_1(): n, n_obs = (5, 1000) basis = edge_basis(n) tree_prior = None ct_prior = dists.CountPriors.Uniform() np.random.seed(123) for i in range(3): g = GraphAndBasis(n) g.SetRandom() sampler = run_MCMC(g, n, n_obs, tree_prior, ct_prior, basis=basis, seed=123, fixed_init=True) print(sampler.res['LIK_'][-1]) median_graph = str_list_to_median_graph(n, sampler.res['SAMPLES'][500:], .5) assert (np.sum(median_graph != g.GetAdjM()) < 3) assert (only_propose_one_edge_difference(sampler)) assert (lookup_contains_all_params(sampler))
def test_MCMC_edge_0(): n, n_obs = (5, 1000) basis = edge_basis(n) tree_prior = None ct_prior = dists.CountPriors.Uniform() # Empty g = GraphAndBasis(n) sampler = run_MCMC(g, n, n_obs, tree_prior, ct_prior, basis=basis, seed=123) print(sampler.res['LIK_'][-1]) median_graph = str_list_to_median_graph(n, sampler.res['SAMPLES'][500:]) assert ((median_graph == g.GetAdjM()).all()) assert (only_propose_one_edge_difference(sampler)) assert (lookup_contains_all_params(sampler)) # Circle g = GraphAndBasis(n, circle_graph(n)) sampler = run_MCMC(g, n, n_obs, tree_prior, ct_prior, basis=basis, seed=123) print(sampler.res['LIK_'][-1]) median_graph = str_list_to_median_graph(n, sampler.res['SAMPLES'][500:], .75) assert ((median_graph == g.GetAdjM()).all()) assert (only_propose_one_edge_difference(sampler)) assert (lookup_contains_all_params(sampler))
def test_MCMC_cb_0(): n, n_obs = (5, 1000) tree_prior = dists.TreePriors.Uniform(n) ct_prior = dists.CountPriors.TruncatedNB(n, .75) cob_freq = 2 # Empty g = GraphAndBasis(n) sampler = run_MCMC(g, n, n_obs, tree_prior, ct_prior, skip=cob_freq, seed=123) print(sampler.res['LIK_'][-1]) median_graph = str_list_to_median_graph(n, sampler.res['SAMPLES'][500:]) assert ((median_graph == g.GetAdjM()).all()) assert (only_propose_one_basis_difference(sampler, cob_freq)) assert (lookup_contains_all_params(sampler)) # Circle g = GraphAndBasis(n, circle_graph(n)) sampler = run_MCMC(g, n, n_obs, tree_prior, ct_prior, skip=cob_freq, seed=123) print(sampler.res['LIK_'][-1]) median_graph = str_list_to_median_graph(n, sampler.res['SAMPLES'][500:]) assert ((median_graph == g.GetAdjM()).all()) assert (only_propose_one_basis_difference(sampler, cob_freq)) assert (lookup_contains_all_params(sampler))
from tests.test_utils.generate_data import generate_data_and_save from utils.generate_basis import circle_graph import numpy as np import networkx as nx # Number of nodes and observations n, n_obs = 20, 2 # Graphs to test on graphs = [] # Empty Graph graphs.append(Param(n)) # Circle Graph g = Param(n, circle_graph(n)) graphs.append(g.copy()) # Random Graph Cycle Space np.random.seed(123) from dists.TreePriors import Hubs as TreePrior from dists.Priors import BasisInclusion as Prior tree_prior = TreePrior(n) prior = Prior(n, Param, alpha=.5, tree_prior=tree_prior) g = Param(n) n_edges = n * (n - 1) // 2 while g.EdgeCount() <= int(.1 * n_edges) or g.EdgeCount() > int(.4 * n_edges): g = prior.Sample() graphs.append(g.copy()) g = Param(n)
from dists.Params import GraphAndBasis from dists.CountPriors import TruncatedNB from dists.Priors import BasisCount, BasisInclusion from dists.TreePriors import Uniform from utils.generate_basis import circle_graph from tests.test_utils.generate_data import generate_data_and_save import numpy as np import networkx as nx import pickle # Number of nodes and observations n, n_obs = 100, 1000 # Empty Graph empty = GraphAndBasis(n) # Circle Graph circle = GraphAndBasis(n, circle_graph(n)) # Random Graphs from the Cycle space print("Generating Graphs from the Cycle space...") ct_prior = TruncatedNB(1, .5) prior = BasisCount(n, GraphAndBasis, ct_prior, Uniform(n)) np.random.seed(123) random0 = prior.Sample() random1 = prior.Sample() # Random Graphs from the Cycle space complement print("Generating Graphs from the Cycle space complement...") random2 = GraphAndBasis(
def get_log_posteriors(config): n = config['n'] n_obs = config['n_obs'] name = config['true_graph'] with open( f"data/graph_{config['true_graph']}_{config['n']}_{config['n_obs']}.pkl", 'rb') as handle: g = pickle.load(handle) g = Param(n, g._dol) if n <= 5: g_list = Graph.get_all_graphs(n) delta, D = 3, np.eye(n) rv = nbinom(1, .5) idx = g.GetID() data = np.loadtxt(f"data/{name}_{n}_{n_obs}.dat", delimiter=',') U = data.transpose() @ data assert (U.shape == D.shape) liks = list( map( lambda x: laplace_approx(x._dol, delta + n_obs, D + U) - laplace_approx(x._dol, delta, D), g_list)) diffs = list( map( lambda x: len( set(g.GetEdgeL()).symmetric_difference(x.GetEdgeL())), g_list)) priors = list(map(lambda x: np.log(rv.pmf(x)), diffs)) posteriors = np.array(liks) + np.array(priors) return np.array(g._basis_active, dtype=int), [(np.array(list(g_list[i].GetID()), dtype=int), posteriors[i]) for i in range(len(g_list))] else: with open(config_to_path(config), 'rb') as handle: sampler = pickle.load(handle) if config['cob_freq'] is not None: warnings.warn( "Bases change throughout the simulation, all graphs is represented w.r.t. the last basis" ) tree = sampler.last_params._tree g = Param(len(g), g._dol, tree) gba = np.array(g._basis_active, dtype=int) log_posts = [] ids = [] for i in range(sampler.iter): if sampler.res['PARAMS'][i] not in ids: g_ba = np.array(list( sampler.res['PARAMS_PROPS'][i]['BASIS_ID']), dtype=int) log_posts.append( (g_ba, sampler.res['LIK_'][i] + sampler.res['PRIOR'][i])) ids.append(sampler.res['PARAMS'][i]) return gba, log_posts