Ejemplo n.º 1
0
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 __init__(self, n, Param, basis=None):
     self._n = n
     self._Param = Param
     if basis is not None:
         self._basis = basis
     else:
         self._basis = edge_basis(n)
 def __init__(self, n, Param, alpha=.5, tree_prior=None, basis=None):
     # alpha: probability of inclusion
     self._n = n
     self._Param = Param
     self._alpha = alpha
     if tree_prior:
         self._tree_prior = tree_prior
     elif basis is not None:
         self._basis = basis
         self._tree_prior = None
     else:
         self._basis = edge_basis(n)
         self._tree_prior = None
 def __init__(self, n, Param, prob_c, tree_prior=None, basis=None):
     # prob_c: distribution of the number of bases i.e. "count"
     self._n = n
     self._Param = Param
     self._prob_c = prob_c  # for 0, ... number of basis
     if tree_prior:
         self._tree_prior = tree_prior
     elif basis is not None:
         self._basis = basis
         self._tree_prior = None
     else:
         self._basis = edge_basis(n)
         self._tree_prior = None
 def __init__(self, n, dol=None, tree=None, basis=None):
     super().__init__(n, dol)
     self._tree = tree
     # basis is n_edges x n_basis array
     if tree:
         cbc = cycle_basis_complete(tree)
         self._basis = cbc[:, :(n - 1) * (n - 2) // 2]
         self._basis_active = np.linalg.solve(cbc, GF2(
             self.GetBinaryL()))[:(n - 1) * (n - 2) // 2]
     elif basis is not None:
         assert (basis.shape == (n * (n - 1) // 2, n * (n - 1) // 2))
         self._basis = basis
         self._basis_active = np.linalg.solve(self._basis,
                                              GF2(self.GetBinaryL()))
     else:
         self._basis = edge_basis(n)
         self._basis_active = np.linalg.solve(self._basis,
                                              GF2(self.GetBinaryL()))
Ejemplo n.º 6
0
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 cbmcmc(data,
           it=1000,
           basis='cycle',
           treeprior='all',
           r=1,
           p=.5,
           cob_freq=100,
           outfile=None,
           seed=123,
           init=None):
    data = np.loadtxt(data, delimiter=",")
    _, n = data.shape

    # Bases and Tree Prior
    if basis == 'edge':
        basis = edge_basis(n)
        tree_prior = None
        cob_freq = None
    elif basis == 'cycle':
        basis = None
        if treeprior == 'all':
            tree_prior = dists.TreePriors.Uniform(n)
        elif treeprior == 'hub':
            tree_prior = dists.TreePriors.Hubs(n)
        elif treeprior == 'path':
            tree_prior = dists.TreePriors.Paths(n)
        else:
            raise ValueError("Invalid tree prior")
    else:
        raise ValueError("Invalid basis")

    # Count Prior
    if r is None:
        ct_prior = dists.CountPriors.TruncatedNB(data.shape[1], p)
    else:
        ct_prior = dists.CountPriors.TruncatedNB(r, p)

    # Prior, Likelihood, and Proposal
    prior = dists.Priors.BasisCount(n, GraphAndBasis, ct_prior, tree_prior,
                                    basis)
    lik = dists.Likelihoods.GW_LA(data, 3, np.eye(n), GraphAndBasis)
    prop = dists.Proposals.BasisWalk(n, GraphAndBasis, tree_prior, cob_freq)

    # Run MCMC
    print("Starting MCMC...")
    sampler = MCMC_Sampler(prior, prop, lik, data, outfile=outfile)
    if seed:
        np.random.seed(seed)

    if init is None:
        sampler.run(it)
    else:
        import pickle
        with open(init, 'rb') as handle:
            g = pickle.load(handle)
        if g._tree is None and tree_prior is not None:
            g._tree = tree_prior.Sample()
        sampler.run(it, fixed_init=g)

    # Saving Results
    if outfile is not None:
        sampler.save_object()
    return sampler