Example #1
0
def toy_model(tau=10000, prior='Beta0.5'):
    b_obs = 200
    f_AB = 400
    f_CB = 1000
    f_CA = 600

    A = np.array([0, f_AB, f_CA, 0, f_CB, 0])

    if prior == 'Normal':
        ABp = Normal('ABp', mu=0.5, tau=100, trace=True)
        CBp = Normal('CBp', mu=0.5, tau=100, trace=True)
        CAp = Normal('CAp', mu=0.5, tau=100, trace=True)
    elif prior == 'Uniform':
        ABp = Uniform('ABp', lower=0.0, upper=1.0, trace=True)
        CBp = Uniform('CBp', lower=0.0, upper=1.0, trace=True)
        CAp = Uniform('CAp', lower=0.0, upper=1.0, trace=True)
    elif prior == 'Beta0.25':
        ABp = Beta('ABp', alpha=0.25, beta=0.25, trace=True)
        CBp = Beta('CBp', alpha=0.25, beta=0.25, trace=True)
        CAp = Beta('CAp', alpha=0.25, beta=0.25, trace=True)
    elif prior == 'Beta0.5':
        ABp = Beta('ABp', alpha=0.5, beta=0.5, trace=True)
        CBp = Beta('CBp', alpha=0.5, beta=0.5, trace=True)
        CAp = Beta('CAp', alpha=0.5, beta=0.5, trace=True)
    elif prior == 'Beta2':
        ABp = Beta('ABp', alpha=2, beta=2, trace=True)
        CBp = Beta('CBp', alpha=2, beta=2, trace=True)
        CAp = Beta('CAp', alpha=2, beta=2, trace=True)
    elif prior == 'Gamma':
        ABp = Gamma('ABp', alpha=1, beta=0.5, trace=True)
        CBp = Gamma('CBp', alpha=1, beta=0.5, trace=True)
        CAp = Gamma('CAp', alpha=1, beta=0.5, trace=True)

    AB1 = ABp
    AB3 = 1 - ABp
    CB4 = CBp
    CB5 = 1 - CBp
    CA42 = CAp
    CA52 = 1 - CAp

    b = Normal('b',
               mu=f_AB * AB3 + f_CB * CB4 + f_CA * CA42,
               tau=tau,
               value=b_obs,
               observed=True,
               trace=True)

    # print [x.value for x in [ABp,CBp,CAp]]
    # print b.logp

    return locals()
Example #2
0
    def test_mixture_list_of_poissons(self):
        with Model() as model:
            w = Dirichlet("w",
                          floatX(np.ones_like(self.pois_w)),
                          shape=self.pois_w.shape)
            mu = Gamma("mu", 1.0, 1.0, shape=self.pois_w.size)
            Mixture(
                "x_obs",
                w,
                [Poisson.dist(mu[0]), Poisson.dist(mu[1])],
                observed=self.pois_x)
            step = Metropolis()
            trace = sample(5000,
                           step,
                           random_seed=self.random_seed,
                           progressbar=False,
                           chains=1)

        assert_allclose(np.sort(trace["w"].mean(axis=0)),
                        np.sort(self.pois_w),
                        rtol=0.1,
                        atol=0.1)
        assert_allclose(np.sort(trace["mu"].mean(axis=0)),
                        np.sort(self.pois_mu),
                        rtol=0.1,
                        atol=0.1)
def generate_route_flows_from_incidence_matrix(M, alpha=1):
    """
    Generate blocks of route flows/splits (x) from an incidence matrix
    :param M: incidence matrix
    :return:
    """
    x_blocks = []
    order, block_sizes = [], []
    m, n = M.shape
    assert M.getnnz() == n
    # Construct a Gamma distribution for each row in the incidence matrix
    for i in xrange(m):
        block_ind = M.getrow(i).nonzero()[1]
        order.extend(block_ind)
        size = len(block_ind)
        block_sizes.append(size)

        block = Gamma('x%d' % i, np.array([alpha] * size), 1, shape=size)
        x_blocks.append(block)

    x_blocks_expanded = [[x[xi]/x.sum() for xi in range(i-1)] for (i,x) in \
                         zip(block_sizes,x_blocks)]
    [x.append(1 - sum(x)) for x in x_blocks_expanded]
    x_pri = list(chain(*x_blocks_expanded))
    # reorder
    x_pri = zip(*sorted(zip(x_pri, order), key=lambda x: x[1]))[0]
    return x_pri
Example #4
0
    def test_normal_mixture(self):
        with Model() as model:
            w = Dirichlet("w", floatX(np.ones_like(self.norm_w)), shape=self.norm_w.size)
            mu = Normal("mu", 0.0, 10.0, shape=self.norm_w.size)
            tau = Gamma("tau", 1.0, 1.0, shape=self.norm_w.size)
            NormalMixture("x_obs", w, mu, tau=tau, observed=self.norm_x)
            step = Metropolis()
            trace = sample(5000, step, random_seed=self.random_seed, progressbar=False, chains=1)

        assert_allclose(np.sort(trace["w"].mean(axis=0)), np.sort(self.norm_w), rtol=0.1, atol=0.1)
        assert_allclose(
            np.sort(trace["mu"].mean(axis=0)), np.sort(self.norm_mu), rtol=0.1, atol=0.1
        )
Example #5
0
def test_find_MAP_issue_4488():
    # Test for https://github.com/pymc-devs/pymc/issues/4488
    with Model() as m:
        x = Gamma("x", alpha=3, beta=10, observed=np.array([1, np.nan]))
        y = Deterministic("y", x + 1)
        map_estimate = find_MAP()

    assert not set.difference({"x_missing", "x_missing_log__", "y"},
                              set(map_estimate.keys()))
    np.testing.assert_allclose(map_estimate["x_missing"],
                               0.2,
                               rtol=1e-4,
                               atol=1e-4)
    np.testing.assert_allclose(map_estimate["y"],
                               [2.0, map_estimate["x_missing"][0] + 1])

pathways, features, path_dict, reverse_path_dict, evidence, metfrag_evidence = data
for c,v in evidence.iteritems():
    print c,v 
for c,v in metfrag_evidence.iteritems():
    print c,v 
print "num_pathways:", len(pathways)
print "num_features:", len(features)
print "num_evidence:", len(evidence)
print "num_metfrag: ", len(metfrag_evidence)
rate_prior = 0.5

#eps = Beta('eps', 0.005, 1)
eps = 0.0001
ap =  {p : Gamma('p_' + p, rate_prior, 1) for p in pathways}
bmp = {p : {feat : Gamma('b_{' + p + ',' + feat + '}', ap[p],1) for feat in path_dict[p]} for p in pathways}
y_bmp = {}
g = {}

def logp_f(f, b, eps):
    if f in evidence:
        return math.log(1 - math.e ** (-1 * b) + epsilon)
    if f in metfrag_evidence:
        a_p = (1.0 / (1 - metfrag_evidence[f])) - 1
        return a_p * math.log(1 - math.e ** (-1 * b) + epsilon) - b
    return math.log(eps) - b
psi = {}
for feat, pathways in reverse_path_dict.iteritems():
    y_bmp[feat] = sum([bmp[pname][feat] for pname in pathways])
    g[feat] = Bernoulli('g_' + feat, 1 - math.e ** (-y_bmp[feat]))
features = read.get_metabolites(path_dict)
evidence = read.metlin(observation_file)
evidence |= read.hmdb(observation_file)
evidence -= cofactors
features -= cofactors
evidence &= features
reverse_path_dict = read.reverse_dict(path_dict)
metfrag = read.metfrag(observation_file)
metfrag_evidence = read.dict_of_set(
    read.metfrag_with_scores(observation_file, keep_zero_scores=False),
    metfrag & features - cofactors - evidence)
evidence = {e: 1 for e in evidence}

rate_prior = 0.5

ap = {p: Gamma('p_' + p, rate_prior, 1) for p in pathways}
bmp = {
    p: {
        feat: Gamma('b_{' + p + ',' + feat + '}', ap[p], 1)
        for feat in path_dict[p]
    }
    for p in pathways
}
y_bmp = {}
virtual = {}

se_count = 0
for feat, pathways in reverse_path_dict.iteritems():
    #g_bmp[feat] = Poisson('g_' + feat, sum([bmp[pname][feat] for pname in pathways]))
    y_bmp[feat] = Bernoulli(
        'y_' + feat,
Example #8
0
    def test_normal_mixture_nd(self, nd, ncomp):
        nd = to_tuple(nd)
        ncomp = int(ncomp)
        comp_shape = nd + (ncomp,)
        test_mus = np.random.randn(*comp_shape)
        test_taus = np.random.gamma(1, 1, size=comp_shape)
        observed = generate_normal_mixture_data(
            w=np.ones(ncomp) / ncomp, mu=test_mus, sd=1 / np.sqrt(test_taus), size=10
        )

        with Model() as model0:
            mus = Normal("mus", shape=comp_shape)
            taus = Gamma("taus", alpha=1, beta=1, shape=comp_shape)
            ws = Dirichlet("ws", np.ones(ncomp), shape=(ncomp,))
            mixture0 = NormalMixture("m", w=ws, mu=mus, tau=taus, shape=nd, comp_shape=comp_shape)
            obs0 = NormalMixture(
                "obs", w=ws, mu=mus, tau=taus, shape=nd, comp_shape=comp_shape, observed=observed
            )

        with Model() as model1:
            mus = Normal("mus", shape=comp_shape)
            taus = Gamma("taus", alpha=1, beta=1, shape=comp_shape)
            ws = Dirichlet("ws", np.ones(ncomp), shape=(ncomp,))
            comp_dist = [
                Normal.dist(mu=mus[..., i], tau=taus[..., i], shape=nd) for i in range(ncomp)
            ]
            mixture1 = Mixture("m", w=ws, comp_dists=comp_dist, shape=nd)
            obs1 = Mixture("obs", w=ws, comp_dists=comp_dist, shape=nd, observed=observed)

        with Model() as model2:
            # Expected to fail if comp_shape is not provided,
            # nd is multidim and it does not broadcast with ncomp. If by chance
            # it does broadcast, an error is raised if the mixture is given
            # observed data.
            # Furthermore, the Mixture will also raise errors when the observed
            # data is multidimensional but it does not broadcast well with
            # comp_dists.
            mus = Normal("mus", shape=comp_shape)
            taus = Gamma("taus", alpha=1, beta=1, shape=comp_shape)
            ws = Dirichlet("ws", np.ones(ncomp), shape=(ncomp,))
            if len(nd) > 1:
                if nd[-1] != ncomp:
                    with pytest.raises(ValueError):
                        NormalMixture("m", w=ws, mu=mus, tau=taus, shape=nd)
                    mixture2 = None
                else:
                    mixture2 = NormalMixture("m", w=ws, mu=mus, tau=taus, shape=nd)
            else:
                mixture2 = NormalMixture("m", w=ws, mu=mus, tau=taus, shape=nd)
            observed_fails = False
            if len(nd) >= 1 and nd != (1,):
                try:
                    np.broadcast(np.empty(comp_shape), observed)
                except Exception:
                    observed_fails = True
            if observed_fails:
                with pytest.raises(ValueError):
                    NormalMixture("obs", w=ws, mu=mus, tau=taus, shape=nd, observed=observed)
                obs2 = None
            else:
                obs2 = NormalMixture("obs", w=ws, mu=mus, tau=taus, shape=nd, observed=observed)

        testpoint = model0.recompute_initial_point()
        testpoint["mus"] = test_mus
        testpoint["taus"] = test_taus
        assert_allclose(model0.logp(testpoint), model1.logp(testpoint))
        assert_allclose(mixture0.logp(testpoint), mixture1.logp(testpoint))
        assert_allclose(obs0.logp(testpoint), obs1.logp(testpoint))
        if mixture2 is not None and obs2 is not None:
            assert_allclose(model0.logp(testpoint), model2.logp(testpoint))
        if mixture2 is not None:
            assert_allclose(mixture0.logp(testpoint), mixture2.logp(testpoint))
        if obs2 is not None:
            assert_allclose(obs0.logp(testpoint), obs2.logp(testpoint))
Example #9
0
from pymc import Gamma, Poisson, InverseGamma
import pymc
import numpy as np
import gen

pathways = gen.pathways()
features = gen.features_dict()
detected = gen.detected_features()
evidence = gen.evidence()
ap = {p.name: Gamma('p_' + p.name, p.rate, 1) for p in pathways}
#bmp = [Gamma('b_{' + str(i) + '}', 1, ap[i]) for i in range(3)]
bmp = {
    p.name: {
        feat: Gamma('b_{' + p.name + ',' + str(feat) + '}', ap[p.name], 1)
        for feat in p.mets
    }
    for p in pathways
}
print bmp
#g_bmp = {feat : Poisson('g_' + str(feat), sum([bmp[pname][feat] for pname in pathways])) for feat, pathways in features.iteritems()}
g_bmp = {}
for feat, pathways in features.iteritems():
    if detected(feat):
        print feat, "was detected"
        g_bmp[feat] = Poisson('g_' + str(feat),
                              sum([bmp[pname][feat] for pname in pathways]),
                              value=1,
                              observed=True)
    else:
        print feat, "was not detected"
        g_bmp[feat] = Poisson('g_' + str(feat),
prior = 'Gamma'

if prior == 'Normal':
    ABp = Normal('ABp', mu=0.5, tau=100)
    CBp = Normal('CBp', mu=0.5, tau=100)
    CAp = Normal('CAp', mu=0.5, tau=100)
elif prior == 'Uniform':
    ABp = Uniform('ABp', lower=0.0, upper=1.0)
    CBp = Uniform('CBp', lower=0.0, upper=1.0)
    CAp = Uniform('CAp', lower=0.0, upper=1.0)
elif prior == 'Beta':
    ABp = Beta('ABp', alpha=0.5, beta=0.5)
    CBp = Beta('CBp', alpha=0.5, beta=0.5)
    CAp = Beta('CAp', alpha=0.5, beta=0.5)
elif prior == 'Gamma':
    ABp = Gamma('ABp', alpha=1, beta=0.5)
    CBp = Gamma('CBp', alpha=1, beta=0.5)
    CAp = Gamma('CAp', alpha=1, beta=0.5)

AB1 = ABp
AB3 = 1 - ABp
CB4 = CBp
CB5 = 1 - CBp
CA42 = CAp
CA52 = 1 - CAp

b = Normal('b',
           mu=400 * AB3 + 1000 * CB4 + 600 * CA42,
           tau=10000,
           value=200,
           observed=True)