예제 #1
0
def new_player(name, sink=0.5, foul=1e-10): 
    player = {}
    player['sink'] = pm.Beta(name + "_sink", alpha=3, beta=3, value=sink)
    player['foul_end'] = pm.Beta(name + "_foul_end", alpha=3,
                                 beta=3, value=foul)
    vars = player.values()
    player['balance'] = pm.Potential(logp = sum_less_than_one,
                                     name = name + "_balance",
                                     parents = {'vars': vars},
                                     doc = name + "_balance")
        
    return player
예제 #2
0
    def make_model(data):
        switchpoint = pm.DiscreteUniform('switchpoint', 0, len(data))
        early_rate = pm.Beta('early_rate', 0.5, 0.5)
        late_rate = pm.Beta('late_rate', 0.5, 0.5)

        @pm.deterministic(plot=False)
        def rate(s=switchpoint, early=early_rate, late=late_rate):
            out = np.empty(len(data))
            out[:s] = early
            out[s:] = late
            return out

        phredscore = pm.Bernoulli('phredscore', p=rate, value=data,
                                  observed=True)
        return locals()
예제 #3
0
def binomial_model():
    n_samples = 100
    xs = intX(np.random.binomial(n=1, p=0.2, size=n_samples))
    with pm.Model() as model:
        p = pm.Beta("p", alpha=1, beta=1)
        pm.Binomial("xs", n=1, p=p, observed=xs)
    return model
예제 #4
0
def make_bernoulli(name, value=None, N=None, return_coeffs=False, fixed={}):
    """ creates a Bernoulli random variable with a uniform parent

    :param name: name of the variable
    :param value: optional - list of observed values of the variable. May be a masked array - if
        the variable has missing values
    :param N: size of the variable (number of values). Either N or value must be specified
    :param return_coeffs: if true, will return the parent Beta variable as well as the bernoulli
        child. False by defaut.
    :param fixed: optional dictionary of values of coefficients to be fixed.
    :return: Bernoulli pymc random variable, or (if return_coeffs == True) a tuple
        (bernoulli variable, a list with a single element - the Beta parent of the bernoulli)
    """
    if value is None and N is None:
        raise ValueError('either "value" or "N" must be specified')
    if value is not None:
        value = mask_missing(value)

    parent_name = COEFFS_PREFIX + 'p(%s)' % name
    parent = fixed.get(parent_name, pymc.Beta(parent_name, 1, 1))

    if value is None:
        child = pymc.Bernoulli(name, p=parent, value=np.zeros(N))
    else:
        child = pymc.Bernoulli(name, p=parent, observed=True, value=value)

    set_levels_count(child, 2)
    if return_coeffs:
        return child, [parent]
    else:
        return child
예제 #5
0
def beta_binom(name, pi, p, n):
    """ Generate PyMC objects for a beta-binomial model

    :Parameters:
      - `name` : str
      - `pi` : pymc.Node, expected values of rates
      - `p` : array, observed values of rates
      - `n` : array, effective sample sizes of rates

    :Results:
      - Returns dict of PyMC objects, including 'p_obs' and 'p_pred' the observed stochastic likelihood and data predicted stochastic

    """
    assert pl.all(p >= 0), 'observed values must be non-negative'
    assert pl.all(n >= 0), 'effective sample size must non-negative'

    p_n = mc.Uniform('p_n_%s'%name, lower=1.e4, upper=1.e9, value=1.e4)  # convergence requires getting these bounds right
    pi_latent = [mc.Beta('pi_latent_%s_%d'%(name,i), pi[i]*p_n, (1-pi[i])*p_n, value=pi_i) for i, pi_i in enumerate(pi.value)]

    i_nonzero = (n!=0.)
    @mc.observed(name='p_obs_%s'%name)
    def p_obs(value=p, pi=pi_latent, n=n):
        pi_flat = pl.array(pi)
        return mc.binomial_like((value*n)[i_nonzero], n[i_nonzero], pi_flat[i_nonzero])

    # for any observation with n=0, make predictions for n=1.e6, to use for predictive validity
    n_nonzero = pl.array(n.copy(), dtype=int)
    n_nonzero[n==0] = 1.e6
    @mc.deterministic(name='p_pred_%s'%name)
    def p_pred(pi=pi_latent, n=n_nonzero):
        return mc.rbinomial(n, pi) / (1.*n)

    return dict(p_n=p_n, pi_latent=pi_latent, p_obs=p_obs, p_pred=p_pred)
예제 #6
0
 def test_multivariate_observations(self):
     coords = {"direction": ["x", "y", "z"], "experiment": np.arange(20)}
     data = np.random.multinomial(20, [0.2, 0.3, 0.5], size=20)
     with pm.Model(coords=coords):
         p = pm.Beta("p", 1, 1, size=(3, ))
         pm.Multinomial("y",
                        20,
                        p,
                        dims=("experiment", "direction"),
                        observed=data)
         idata = pm.sample(draws=50,
                           chains=2,
                           tune=100,
                           return_inferencedata=True)
     test_dict = {
         "posterior": ["p"],
         "sample_stats": ["lp"],
         "log_likelihood": ["y"],
         "observed_data": ["y"],
     }
     fails = check_multiple_attrs(test_dict, idata)
     assert not fails
     assert "direction" not in idata.log_likelihood.dims
     assert "direction" in idata.observed_data.dims
     assert idata.log_likelihood["y"].shape == (2, 50, 20)
예제 #7
0
def getModel():
    nA, nB, nK = 5, 2, 10;
    B = pm.Beta('1-Beta', alpha=nA/nK, beta=nB*(nK-1)/nK); #@UndefinedVariable
#     p_B = pm.Lambda('p_Bern', lambda b=B: np.where(b==0, 0.9, 0.1), doc='Pr[Bern|Beta]');
    C = pm.Categorical('2-Cat', [1-B, B]); #@UndefinedVariable
#     C = pm.Categorical('1-Cat', [0.2, 0.4, 0.1, 0.3], observed=True, value=3); #@UndefinedVariable
    return pm.Model([B,C]);
예제 #8
0
def yield_models(dataframe):

    bin_ids = dataframe.distance_bin.unique()
    for bin_id in bin_ids:

        assert isinstance(bin_id, int)

        # get our distance binned r^2 d in a nice dataframe
        data = dataframe.query(
            "(distance_bin == {bin_id})".format(bin_id=bin_id))

        # generate names for stocastics
        alpha_name = "{bin_id}_alpha".format(bin_id=bin_id)
        beta_name = "{bin_id}_beta".format(bin_id=bin_id)
        r2_dist_name = "{bin_id}_r2_distribution_beta".format(bin_id=bin_id)

        # set priors for parameters
        alpha_of_beta = mc.Uniform(alpha_name, 0.01, 10)
        beta_of_beta = mc.Uniform(beta_name, 0.01, 10)

        # set the d
        r2_distribution_beta = mc.Beta(name=r2_dist_name,
                                       alpha=alpha_of_beta,
                                       beta=beta_of_beta,
                                       value=data.R2_scaled_for_B.dropna(),
                                       observed=True,
                                       verbose=0)

        # create and yield the model object tagged with its bin_id
        model = mc.Model([r2_distribution_beta, alpha_of_beta, beta_of_beta])
        model.bin_id_tag = bin_id
        model.MCMC_run = None  # allow us to know how many times we had to do the experiments rather than MAP
        yield model
예제 #9
0
def simple_2model_continuous():
    mu = -2.1
    tau = 1.3
    with Model() as model:
        x = pm.Normal("x", mu, tau=tau, initval=0.1)
        pm.Deterministic("logx", at.log(x))
        pm.Beta("y", alpha=1, beta=1, size=2)
    return model.compute_initial_point(), model
예제 #10
0
 def test_zeroinflatedpoisson(self):
     with pm.Model():
         theta = pm.Beta("theta", alpha=1, beta=1)
         psi = pm.HalfNormal("psi", sd=1)
         pm.ZeroInflatedPoisson("suppliers", psi=psi, theta=theta, size=20)
         gen_data = pm.sample_prior_predictive(samples=5000)
         assert gen_data.prior["theta"].shape == (1, 5000)
         assert gen_data.prior["psi"].shape == (1, 5000)
         assert gen_data.prior["suppliers"].shape == (1, 5000, 20)
예제 #11
0
def getModel():
    nA, nB, nK, nT = 5, 2, 10, 10
    # @UnusedVariable
    nW = np.linspace(1, 9, nT)
    #     B = pm.Beta('1-Beta', alpha=[nA/nK]*nT, beta=[nB*(nK-1)/nK]*nT); #@UndefinedVariable
    B = pm.Beta('1-Beta', alpha=nW, beta=[nB * (nK - 1) / nK] * nT)
    #@UndefinedVariable
    #     Bern = pm.Bernoulli('2-Bern', np.linspace(0.1,0.9,nT)); #@UndefinedVariable
    return pm.Model([B])
예제 #12
0
def indep_samples(x1,n1,x2,n2):
	class experimental_data(object):
		
		def __init__(self,x1,n1,x2,n2):
			self.data1 = np.hstack( (np.ones((x1,)) , np.zeros((n1-x1,))) )
			self.data2 = np.hstack( (np.ones((x2,)) , np.zeros((n2-x2,))) )

	### for testing purposes
	#example = True

	data = experimental_data(x1,n1,x2,n2)

	#if example:
	#	example_data1 = np.hstack( (np.ones((15,)) , np.zeros((20,))) )
	#	example_data2 = np.hstack( (np.ones((16,)) , np.zeros((23,))) )
	#	sim_data_size = 14

	#	data1 = example_data1
	#	data2 = example_data2

	p1_val = np.mean(data.data1)
	p2_val = np.mean(data.data2)
	ind_val = p1_val+p2_val-p1_val*p2_val
	print "P1 = " + str(p1_val)

	print "P2 = " + str(p2_val)

	print "Independence = " + str(ind_val)

	p1 = pymc.Beta('p1',alpha=0.5,beta=0.5)
	p2 = pymc.Beta('p2',alpha=0.5,beta=0.5)

	x1 = pymc.Binomial('x',n=len(data.data1),p=p1,value=np.sum(data.data1),observed=True)
	x2 = pymc.Binomial('x',n=len(data.data2),p=p2,value=np.sum(data.data2),observed=True)

	@pymc.deterministic
	def ind_assump(p1=p1,p2=p2):
		return p1+p2-p1*p2

	return locals()
	#@pymc.deterministic
	#def sim():
	#	sim_data = pymc.Binomial('sim',n=sim_data_size, p=ind_assump)
	#	return sim_data
예제 #13
0
    def test_transformed(self):
        n = 18
        at_bats = 45 * np.ones(n, dtype=int)
        hits = np.random.randint(1, 40, size=n, dtype=int)
        draws = 50

        with pm.Model() as model:
            phi = pm.Beta("phi", alpha=1.0, beta=1.0)

            kappa_log = pm.Exponential("logkappa", lam=5.0)
            kappa = pm.Deterministic("kappa", at.exp(kappa_log))

            thetas = pm.Beta("thetas", alpha=phi * kappa, beta=(1.0 - phi) * kappa, size=n)

            y = pm.Binomial("y", n=at_bats, p=thetas, observed=hits)
            gen = pm.sample_prior_predictive(draws)

        assert gen.prior["phi"].shape == (1, draws)
        assert gen.prior_predictive["y"].shape == (1, draws, n)
        assert "thetas" in gen.prior.data_vars
예제 #14
0
def HRBayes(pa, hr, alpha, beta):
    hrpg = np.zeros(pa)
    hrpg[:hr] = 1
    phr = pm.Beta("phr", alpha, beta)
    obshr = pm.Bernoulli("obshr", phr, value=hrpg, observed=True)
    mcmc = pm.MCMC([phr, obshr])
    mcmc.sample(20000, 7000)
    sumstats = mcmc.stats()
    loint = sumstats['phr']['95% HPD interval'][0]
    hiint = sumstats['phr']['95% HPD interval'][1]
    hr_trace = mcmc.trace('phr')[:]
    hr_trace = [x for x in hr_trace if (x > loint) and (x < hiint)]
    return hr_trace
예제 #15
0
def getModel():
    B = pm.Beta('1-Beta', alpha=1.2, beta=5)
    #@UndefinedVariable
    #     p_B = pm.Lambda('p_Bern', lambda b=B: np.where(b==0, 0.9, 0.1), doc='Pr[Bern|Beta]');
    C = pm.Categorical('2-Cat', [1 - B, B])
    #@UndefinedVariable
    #     C = pm.Categorical('1-Cat', [0.2, 0.4, 0.1, 0.3], observed=True, value=3); #@UndefinedVariable
    p_N = pm.Lambda('p_Norm',
                    lambda n=C: np.where(n == 0, 0, 5),
                    doc='Pr[Norm|Cat]')
    N = pm.Normal('3-Norm', mu=p_N, tau=1)
    #@UndefinedVariable
    #     N = pm.Normal('2-Norm', mu=p_N, tau=1, observed=True, value=2.5); #@UndefinedVariable
    return pm.Model([B, C, N])
예제 #16
0
    def test_aesara_switch_broadcast_edge_cases_1(self):
        # Tests against two subtle issues related to a previous bug in Theano
        # where `tt.switch` would not always broadcast tensors with single
        # values https://github.com/pymc-devs/aesara/issues/270

        # Known issue 1: https://github.com/pymc-devs/pymc/issues/4389
        data = pm.floatX(np.zeros(10))
        with pm.Model() as m:
            p = pm.Beta("p", 1, 1)
            obs = pm.Bernoulli("obs", p=p, observed=data)

        npt.assert_allclose(
            logpt_sum(obs).eval({p.tag.value_var: pm.floatX(np.array(0.0))}),
            np.log(0.5) * 10,
        )
예제 #17
0
def getModel():
    nA, nB, nK, nT = 5, 2, 10, 10
    # @UnusedVariable
    nW = np.linspace(1, 9, nT)
    #     B = pm.Beta('Beta', alpha=[nA/nK]*nT, beta=[nB*(nK-1)/nK]*nT); #@UndefinedVariable
    B = pm.Beta('Beta', alpha=nW, beta=[nB * (nK - 1) / nK] * nT)
    #@UndefinedVariable
    BernO = pm.Bernoulli('Bern_O1',
                         B,
                         observed=True,
                         value=[0, 1, 1, 1, 1, 0, 1, 1, 1, 1])
    #@UndefinedVariable @UnusedVariable
    #     BernO2 = pm.Bernoulli('Bern_O2', B, observed=True, value=[1]*nT); #@UndefinedVariable @UnusedVariable
    Bern = pm.Bernoulli('Bern', B)
    #@UndefinedVariable
    return pm.Model([B, Bern])
예제 #18
0
    def test_marginal_likelihood(self):
        data = np.repeat([1, 0], [50, 50])
        marginals = []
        a_prior_0, b_prior_0 = 1.0, 1.0
        a_prior_1, b_prior_1 = 20.0, 20.0

        for alpha, beta in ((a_prior_0, b_prior_0), (a_prior_1, b_prior_1)):
            with pm.Model() as model:
                a = pm.Beta("a", alpha, beta)
                y = pm.Bernoulli("y", a, observed=data)
                trace = pm.sample_smc(2000, return_inferencedata=False)
                marginals.append(trace.report.log_marginal_likelihood)
        # compare to the analytical result
        assert abs(
            np.exp(np.nanmean(marginals[1]) - np.nanmean(marginals[0])) -
            4.0) <= 1
def yield_models(dataframe):

    bin_ids = dataframe.distance_bin.unique()
    with click.progressbar(bin_ids) as bin_ids:
        for bin_id in bin_ids:

            assert isinstance(bin_id, int)

            # get our distance binned r^2 d in a nice dataframe
            # then drop any rows with R2 == NANs
            data = dataframe.query(
                "(distance_bin == {bin_id})".format(bin_id=bin_id))

            na_mask = data.R2.apply(lambda r2: not np.isnan(r2))
            data = data[na_mask]

            # generate names for stocastics
            alpha_name = "{bin_id}_alpha".format(bin_id=bin_id)
            beta_name = "{bin_id}_beta".format(bin_id=bin_id)
            r2_dist_name = "{bin_id}_r2_distribution_beta".format(
                bin_id=bin_id)

            # set priors for parameters
            alpha_of_beta = mc.Uniform(alpha_name, 0.01, 10)
            beta_of_beta = mc.Uniform(beta_name, 0.01, 10)

            # set the d
            try:
                r2_distribution_beta = mc.Beta(
                    name=r2_dist_name,
                    alpha=alpha_of_beta,
                    beta=beta_of_beta,
                    value=data.R2_scaled_for_B.dropna(),
                    observed=True,
                    verbose=0)

                # create and yield the model object tagged with its bin_id
                model = mc.Model(
                    [r2_distribution_beta, alpha_of_beta, beta_of_beta])
                model.bin_id_tag = bin_id
                model.MCMC_run = None  # allow us to know how many times we had to do the experiments rather than MAP

            except ValueError as exc:
                if "but got (0,)" in exc.message:
                    yield munch.Munch(bin_id_tag=bin_id)

            yield model
예제 #20
0
def _fit_beta_distribution(data, n_iter):
    alpha_var = pm.Exponential('alpha', .5)
    beta_var = pm.Exponential('beta', .5)

    observations = pm.Beta('observations',
                           alpha_var,
                           beta_var,
                           value=data,
                           observed=True)

    model = pm.Model([alpha_var, beta_var, observations])
    mcmc = pm.MCMC(model)
    mcmc.sample(n_iter)

    alphas = mcmc.trace('alpha')[:]
    betas = mcmc.trace('beta')[:]
    return alphas, betas
예제 #21
0
def get_Models():
    #Full Model (Beta & Bernoulli)
    nA, nB = 2, 1
    aD = [0, 1, 1]
    Beta = pm.Beta('Beta', alpha=nA, beta=nB)
    # @UndefinedVariable
    BernD = [
        pm.Bernoulli('BernD_' + str(i), p=Beta, observed=True, value=aD[i])
        for i in range(len(aD))
    ]
    # @UndefinedVariable @UnusedVariable
    BernQ = pm.Bernoulli('BernQ', p=Beta)
    # @UndefinedVariable
    #Collapsed Model (Bernoulli)
    nA2 = nA + sum(aD)
    nB2 = nB + len(aD) - sum(aD)
    nP = nA2 / (nA2 + nB2)
    BernQ2 = pm.Bernoulli('BernQ_2', p=nP)
    # @UndefinedVariable
    return np.concatenate([[Beta, BernQ, BernQ2], BernD])
예제 #22
0
    def test_shared(self):
        n1 = 10
        obs = shared(np.random.rand(n1) < 0.5)
        draws = 50

        with pm.Model() as m:
            p = pm.Beta("p", 1.0, 1.0)
            y = pm.Bernoulli("y", p, observed=obs)
            o = pm.Deterministic("o", obs)
            gen1 = pm.sample_prior_predictive(draws)

        assert gen1.prior["y"].shape == (1, draws, n1)
        assert gen1.prior["o"].shape == (1, draws, n1)

        n2 = 20
        obs.set_value(np.random.rand(n2) < 0.5)
        with m:
            gen2 = pm.sample_prior_predictive(draws)

        assert gen2.prior["y"].shape == (1, draws, n2)
        assert gen2.prior["o"].shape == (1, draws, n2)
예제 #23
0
파일: test_smc.py 프로젝트: t-triobox/pymc3
    def test_marginal_likelihood(self):
        """
        Verifies that the log marginal likelihood function
        can be correctly computed for a Beta-Bernoulli model.
        """
        data = np.repeat([1, 0], [50, 50])
        marginals = []
        a_prior_0, b_prior_0 = 1.0, 1.0
        a_prior_1, b_prior_1 = 20.0, 20.0

        for alpha, beta in ((a_prior_0, b_prior_0), (a_prior_1, b_prior_1)):
            with pm.Model() as model:
                a = pm.Beta("a", alpha, beta)
                y = pm.Bernoulli("y", a, observed=data)
                trace = pm.sample_smc(2000, chains=2, return_inferencedata=False)
            # log_marignal_likelihood is found in the last value of each chain
            lml = np.mean([chain[-1] for chain in trace.report.log_marginal_likelihood])
            marginals.append(lml)

        # compare to the analytical result
        assert abs(np.exp(marginals[1] - marginals[0]) - 4.0) <= 1
예제 #24
0
def get_Models():
    #Full Model (Beta & Binomial)
    nN, nA, nB = 3, 5, 1
    aD = [0, 3, 1]
    Beta = pm.Beta('Beta', alpha=nA, beta=nB)
    # @UndefinedVariable
    BinomD = [
        pm.Binomial('BinomD_' + str(i),
                    n=nN,
                    p=Beta,
                    observed=True,
                    value=aD[i]) for i in range(len(aD))
    ]
    # @UndefinedVariable @UnusedVariable
    BinomQ = pm.Binomial('BinomQ', n=nN, p=Beta)
    # @UndefinedVariable
    #Collapsed Model (Binomial)
    nA2 = nA + sum(aD)
    nB2 = nB + nN * len(aD) - sum(aD)
    BetaBinQ = pm.Betabin('BetaBinQ', n=nN, alpha=nA2, beta=nB2)
    # @UndefinedVariable
    return np.concatenate([[Beta, BinomQ, BetaBinQ], BinomD])
예제 #25
0
def run_Bernoulli_Normal():
    aD = [0, 1, 2, 8, 9]
    nPts = len(aD) + 1
    #Cluster 1
    Uh1 = pm.Uniform('UnifH1', lower=-50, upper=50)
    # @UndefinedVariable
    Nc1 = pm.Normal('NormC1', mu=Uh1,
                    tau=1)  #, observed=True, value=10);  # @UndefinedVariable
    #Cluster 2
    Uh2 = pm.Uniform('UnifH2', lower=-50, upper=50)
    # @UndefinedVariable
    Nc2 = pm.Normal('NormC2', mu=Uh2,
                    tau=1)  #, observed=True, value=10);  # @UndefinedVariable
    #Beta & Bernoulli Nodes
    Bet = pm.Beta('Beta', alpha=1, beta=1)
    # @UndefinedVariable
    aB = [pm.Bernoulli('Bern' + str(i), Bet) for i in range(nPts)]
    # @UndefinedVariable
    aL = [
        pm.Lambda('p_Norm1' + str(i),
                  lambda k=aB[i], c1=Nc1, c2=Nc2: [c1, c2][int(k)])
        for i in range(nPts)
    ]
    # @UndefinedVariable
    #Points
    aN = [
        pm.Normal('NormX' + str(i),
                  mu=aL[i],
                  tau=1,
                  observed=True,
                  value=aD[i]) for i in range(nPts - 1)
    ]
    # @UndefinedVariable
    Nz = pm.Normal('NormZ', mu=aL[-1], tau=1)
    # @UndefinedVariable
    return np.concatenate([[Nz, Nc1, Nc2, Uh1, Uh2, Bet], aB, aN])
예제 #26
0
ax.hist(thetas[G1:], bins=50, normed=True)

# Plot analytic posterior
X = np.linspace(0,1, 1000)
ax.plot(X, stats.beta(a1_hat, a2_hat).pdf(X), "r")

# Plot prior
ax.plot(X, stats.beta(a1, a2).pdf(X), "g")

# Cleanup
ax.set(title="Metropolis-Hastings via Cython (1,000,000 Draws; 100,000 Burned)", ylim=(0,12))
ax.legend(["Posterior (Analytic)", "Prior", "Posterior Draws (MH)"])

# Metropolis-Hastings using PyMC
print("iming: 1 loops, best of 3: 590 ms per loop")
pymc_theta = pymc.Beta('pymc_theta', a1, a2, value=0.5)
pymc_Y = pymc.Bernoulli('pymc_Y', p=pymc_theta, value=Y, observed=True)
model = pymc.MCMC([pymc_theta, pymc_Y])
model.sample(iter=G+G1, burn=G1, progress_bar=False)
model.summary()
thetas = model.trace("pymc_theta")[:]

# Posterior Mean
# (use all of `thetas` b/c PyMC already removed the burn-in runs here)
print"Posterior Mean (MH):", np.mean(thetas))

# Plot the posterior
fig = plt.figure(figsize=(10,4))
ax = fig.add_subplot(111)

# Plot MH draws
means = np.array(zip(*means)).clip(0.001)
variances = np.array(zip(*variances))  #.clip(0.001)
transitions = np.array(zip(*transitions))
totals = np.sum(transitions, axis=2)[:, :, np.newaxis]
transitions = (transitions / totals)[:, :, :n_states - 1]

mean_params = [
    pymc.Gamma('mean_param{}'.format(i), alpha=1, beta=.1)
    for i in range(n_states * 2)
]
var_params = [
    pymc.Gamma('var_param{}'.format(i), alpha=1, beta=.1)
    for i in range(n_states * 2)
]
trans_params = [
    pymc.Beta('trans_params{}'.format(i), alpha=1, beta=1)
    for i in range(n_states**2)
]

mean_obs = []
mean_pred = []
var_obs = []
var_pred = []
trans_obs = []
trans_pred = []

for i in xrange(n_states):
    alpha = mean_params[i * 2]
    beta = mean_params[i * 2 + 1]
    mean_obs.append(
        pymc.Gamma("mean_obs{}".format(i),
예제 #28
0
def get_Bayes(measurements=[], chunksize=5, Ndp=5, iter=50000, burn=5000):

    sc = pymc.Uniform('sc', 0.1, 2.0, value=0.24)
    tau = pymc.Uniform('tau', 0.0, 1.0, value=0.5)

    concinit = 1.0
    conclo = 0.1
    conchi = 10.0
    concentration = pymc.Uniform('concentration',
                                 lower=conclo,
                                 upper=conchi,
                                 value=concinit)

    # The stick-breaking construction: requires Ndp beta draws dependent on the
    # concentration, before the probability mass function is actually constructed.
    #betas = pymc.Beta('betas', alpha=1, beta=concentration, size=Ndp)
    betas = pymc.Beta('betas', alpha=1, beta=1, size=Ndp - 1)

    @pymc.deterministic
    def pmf(betas=betas):
        "Construct a probability mass function for the truncated Dirichlet process"
        # prod = lambda x: np.exp(np.sum(np.log(x))) # Slow but more accurate(?)
        prod = np.prod
        value = map(lambda i, u: u * prod(1.0 - betas[:i]), enumerate(betas))
        value.append(1.0 - sum(value[:]))  # force value to sum to 1
        return value

    # The cluster assignments: each data point's estimated cluster ID.
    # Remove idinit to allow clusterid to be randomly initialized:
    Ndata = len(measurements)
    idinit = np.zeros(Ndata, dtype=np.int64)
    clusterid = pymc.Categorical('clusterid', p=pmf, size=Ndata, value=idinit)

    @pymc.deterministic(name='clustermean')
    def clustermean(clusterid=clusterid, sc=sc, Ndp=Ndp):
        return sc * np.arange(1, Ndp + 1)[clusterid]

    @pymc.deterministic(name='clusterprec')
    def clusterprec(clusterid=clusterid, sc=sc, tau=tau, Ndp=Ndp):
        return 1.0 / (sc * sc * tau * tau * (np.arange(1, Ndp + 1)[clusterid]))

    y = pymc.Normal('y',
                    mu=clustermean,
                    tau=clusterprec,
                    observed=True,
                    value=measurements)

    ## for predictive poeterior simulation
    @pymc.deterministic(name='y_sim')
    def y_sim(value=[0], sc=sc, tau=tau, clusterid=clusterid, Ndp=Ndp):
        n = np.arange(1, Ndp + 1)[np.random.choice(clusterid)]
        return np.random.normal(loc=sc * n, scale=sc * tau * n)

    m = pymc.Model({
        "scale": sc,
        "tau": tau,
        "betas": betas,
        "clusterid": clusterid,
        "normal": y,
        "pred": y_sim
    })

    sc_samples = []
    modes = []
    simulations = []

    for i in range(0, chunksize):
        mc = pymc.MCMC(m)
        mc.sample(iter=50000, burn=10000)
        plot(mc)

        sc_sample = mc.trace('sc')[:]
        sc_samples.append(sc_sample)

        simulation = mc.trace('y_sim')[:]
        simulations.append(simulation)

        plt.hist(measurements,
                 50,
                 fc='gray',
                 histtype='stepfilled',
                 alpha=0.3,
                 normed=False)
        plt.hist(simulation,
                 30,
                 fc='blue',
                 histtype='stepfilled',
                 alpha=0.3,
                 normed=True)
        hist, edges = np.histogram(
            measurements,
            bins=100,
            range=[np.min(measurements) - 0.25,
                   np.max(measurements) + 0.25])

        argm = hist.argmax()
        (edges[argm] + edges[argm + 1]) / 2
        modes.append((edges[argm] + edges[argm + 1]) / 2)

    if chunksize <= 1:
        gr = np.nan
    else:
        pymc.gelman_rubin(sc_samples)

    dic = {
        'gelman_rubin': gr,
        'modes': modes,
        'simulations': simulations,
        'sc_samples': sc_samples
    }
    return dic
예제 #29
0
import pymc as pm
import numpy as np

# True parameter values
mu_true = 5
psi_true = 0.75
n = 100

# Simulate some data
data = np.array(
    [pm.rpoisson(mu_true) * (np.random.random() < psi_true) for i in range(n)])

# Uniorm prior on Poisson mean
mu = pm.Uniform('mu', 0, 20)
# Beta prior on psi
psi = pm.Beta('psi', alpha=1, beta=1)


@pm.observed(dtype=int, plot=False)
def zip(value=data, mu=mu, psi=psi):
    """ ZIP likelihood """

    # Initialise likeihood
    like = 0.0

    # Loop over data
    for x in value:

        if not x:
            # Zero values
예제 #30
0
# model, and present a few possible work arounds.

import pymc as mc

# We define a simple model of a survey with one data point. We use a $Beta$
# distribution for the $p$ parameter in a binomial. We would like to know both
# the posterior distribution for p, as well as the predictive posterior
# distribution over the survey parameter.

alpha = 4
beta = 4
n = 20
yes = 15

with mc.Model() as model:
    p = mc.Beta('p', alpha, beta)
    surv_sim = mc.Binomial('surv_sim', n=n, p=p)
    surv = mc.Binomial('surv', n=n, p=p, observed=yes)

# First let's try and use `find_MAP`.

with model:
    print(mc.find_MAP())

# `find_map` defaults to find the MAP for only the continuous variables we have
# to specify if we would like to use the discrete variables.

with model:
    print(mc.find_MAP(vars=model.vars, disp=True))

# We set the `disp` variable to display a warning that we are using a