示例#1
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
示例#2
0
def run_Bernoulli_Normal():
    #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
    #Bernoulli Nodes
    B1 = pm.Bernoulli('Bern1', 0.8);  # @UndefinedVariable
    B2 = pm.Bernoulli('Bern2', 0.8);  # @UndefinedVariable
    B3 = pm.Bernoulli('Bern3', 0.5);  # @UndefinedVariable
    #Points
    p_N1 = pm.Lambda('p_Norm1', lambda k=B1, c1=Nc1, c2=Nc2: [c2,c1][int(k)]);
    p_N2 = pm.Lambda('p_Norm2', lambda k=B2, c1=Nc1, c2=Nc2: [c1,c2][int(k)]);
    p_N3 = pm.Lambda('p_Norm3', lambda k=B3, c1=Nc1, c2=Nc2: [c1,c2][int(k)]);
    normalObs1 = pm.Normal('NormX1', mu=p_N1, tau=1, observed=True, value=-3);  # @UndefinedVariable
    normalObs2 = pm.Normal('NormX2', mu=p_N2, tau=1, observed=True, value=3);  # @UndefinedVariable
    normalObsZ = pm.Normal('NormZ',  mu=p_N3, tau=1);  # @UndefinedVariable

#     @pm.stochastic(observed=True)
#     def normalObs1(name='NormX1', parentVar=B1, parentVals=[Nc1,Nc2], value=8):
#         return pm.normal_like(x=value, mu=parentVals[int(parentVar)], tau=1);
#     @pm.stochastic(observed=True)
#     def normalObs2(name='NormX2', parentVar=B2, parentVals=[Nc1,Nc2], value=2):
#         return pm.normal_like(x=value, mu=parentVals[int(parentVar)], tau=1);
#     @pm.stochastic(observed=False)
#     def normalObsZ(name='NormZ', parentVar=B3, parentVals=[Nc1,Nc2], value=1):
#         return pm.normal_like(x=value, mu=parentVals[int(parentVar)], tau=1);

#     normalObs1 = pm.Normal('NormX1', mu=[Nc1,Nc2][int(B1)], tau=1, observed=True, value=2);  # @UndefinedVariable
#     normalObs2 = pm.Normal('NormX2', mu=[Nc1,Nc2][int(B2)], tau=1, observed=True, value=8);  # @UndefinedVariable
#     normalObsZ = pm.Normal('NormZ', mu=[Nc1,Nc2][int(B3)], tau=1);  # @UndefinedVariable

    return [Nc1,Nc2,B1,B2,B3,Uh1,Uh2,normalObs1,normalObs2,normalObsZ];
示例#3
0
def run_Bernoulli_Normal():
    #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
    #Bernoulli Nodes
    B1 = pm.Bernoulli('Bern1', 0.8)
    # @UndefinedVariable
    B2 = pm.Bernoulli('Bern2', 0.8)
    # @UndefinedVariable
    B3 = pm.Bernoulli('Bern3', 0.5)
    # @UndefinedVariable
    #Points
    p_N1 = pm.Lambda('p_Norm1', lambda k=B1, c1=Nc1, c2=Nc2: [c2, c1][int(k)])
    p_N2 = pm.Lambda('p_Norm2', lambda k=B2, c1=Nc1, c2=Nc2: [c1, c2][int(k)])
    p_N3 = pm.Lambda('p_Norm3', lambda k=B3, c1=Nc1, c2=Nc2: [c1, c2][int(k)])
    normalObs1 = pm.Normal('NormX1', mu=p_N1, tau=1, observed=True, value=-3)
    # @UndefinedVariable
    normalObs2 = pm.Normal('NormX2', mu=p_N2, tau=1, observed=True, value=3)
    # @UndefinedVariable
    normalObsZ = pm.Normal('NormZ', mu=p_N3, tau=1)
    # @UndefinedVariable
    return [
        Nc1, Nc2, B1, B2, B3, Uh1, Uh2, normalObs1, normalObs2, normalObsZ
    ]
示例#4
0
 def construct(self):
     first = pymc.Bernoulli('F', .6, value=pl.ones(self.obs))
     p_first = pymc.Lambda('p_F',
                           lambda R=R: pl.where(R, .005, .8),
                           doc='Pr[S|R]')
     second = pymc.Bernoulli('S', p_first, value=pl.ones(self.obs))
     p_G = mc.Lambda('p_G',
                     lambda S=S, R=R: pl.where(S, pl.where(R, .99, .9),
                                               pl.where(R, .8, 0.)),
                     doc='Pr[G|S,R]')
     G = mc.Bernoulli('G', p_G, value=G_obs, observed=True)
示例#5
0
def coef_estimate(matrix, params, iters):
    # using specified set of priors, create coefficients
    coefs, term_list = create_coefs(
        params=params,
        priors={coef: pymc.Normal(coef, 0, 0.001)
                for coef in params})

    @pymc.deterministic
    def probs(term_list=term_list):
        probs = 1 / (1 + np.exp(-1 * sum(term_list)))  # The logistic function
        probs[np.diag_indices_from(probs)] = 0
        # Manually cut off the top triangle:
        probs[np.triu_indices_from(probs)] = 0
        return probs

    # Fitting
    matrix[np.triu_indices_from(matrix)] = 0
    max_attempts = 10
    attempts = 0
    while attempts < max_attempts:
        try:
            outcome = pymc.Bernoulli("outcome",
                                     probs,
                                     value=matrix,
                                     observed=True)
            break
        except:
            print("Encountered zero probability error number", attempts,
                  ", trying again...")
            if attempts >= max_attempts:
                raise
            attempts += 1

    sim_outcome = pymc.Bernoulli("sim_outcome", probs)
    args = [outcome, sim_outcome, probs]
    # density_coef, density_term,
    # block_coef, block_term]
    for coef, info in coefs.items():
        # Add both coefficient and term for each coefficient
        for item in info:
            args.append(item)
    model = pymc.Model(args)
    mcmc = pymc.MCMC(model)
    mcmc.sample(iters, 1000, 50)  # approx. 30 seconds

    traces = diagnostics(coefs=coefs, mcmc=mcmc)

    goodness_of_fit(mcmc=mcmc)

    return {coef: np.mean(trace) for coef, trace in traces.items()}
示例#6
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])
示例#7
0
    def test_model_shared_variable(self):
        rng = np.random.RandomState(9832)

        x = rng.randn(100)
        y = x > 0
        x_shared = aesara.shared(x)
        y_shared = aesara.shared(y)
        with pm.Model(rng_seeder=rng) as model:
            coeff = pm.Normal("x", mu=0, sd=1)
            logistic = pm.Deterministic("p", pm.math.sigmoid(coeff * x_shared))

            obs = pm.Bernoulli("obs", p=logistic, observed=y_shared)
            trace = pm.sample(100, return_inferencedata=False, compute_convergence_checks=False)

        x_shared.set_value([-1, 0, 1.0])
        y_shared.set_value([0, 0, 0])

        samples = 100
        with model:
            post_pred = pm.sample_posterior_predictive(
                trace, return_inferencedata=False, samples=samples, var_names=["p", "obs"]
            )

        expected_p = np.array([logistic.eval({coeff: val}) for val in trace["x"][:samples]])
        assert post_pred["obs"].shape == (samples, 3)
        npt.assert_allclose(post_pred["p"], expected_p)
示例#8
0
def run_Bernoulli_Normal():
    B = pm.Bernoulli('Bern', 0.8)
    # @UndefinedVariable
    p_N1 = pm.Lambda('p_Norm', lambda k=B: [-5, 5][int(k)])
    N = pm.Normal('Norm', mu=p_N1, tau=1)
    # @UndefinedVariable
    return [B, N]
def mcmcSimulations(temperature, failures):
    '''Perform the MCMC-simulations'''

    # Define the prior distributions for alpha and beta
    # 'value' sets the start parameter for the simulation
    # The second parameter for the normal distributions is the "precision",
    # i.e. the inverse of the standard deviation
    np.random.seed(1234)
    beta = pm.Normal("beta", 0, 0.001, value=0)
    alpha = pm.Normal("alpha", 0, 0.001, value=0)

    # Define the model-function for the temperature
    @pm.deterministic
    def p(t=temperature, alpha=alpha, beta=beta):
        return 1.0 / (1. + np.exp(beta * t + alpha))

    # connect the probabilities in `p` with our observations through a
    # Bernoulli random variable.
    observed = pm.Bernoulli("bernoulli_obs", p, value=failures, observed=True)

    # Combine the values to a model
    model = pm.Model([observed, beta, alpha])

    # Perform the simulations
    map_ = pm.MAP(model)
    map_.fit()
    mcmc = pm.MCMC(model)
    mcmc.sample(120000, 100000, 2)

    # --- Show the resulting posterior distributions ---
    alpha_samples = mcmc.trace('alpha')[:, None]  # best to make them 1d
    beta_samples = mcmc.trace('beta')[:, None]

    return (alpha_samples, beta_samples)
示例#10
0
def main():
    # The parameters are the bounds of the Uniform.
    p = pm.Uniform('p', lower=0, upper=1)

    # set constants
    p_true = 0.05  # remember, this is unknown.
    N = 1500

    # sample N Bernoulli random variables from Ber(0.05).
    # each random variable has a 0.05 chance of being a 1.
    # this is the data-generation step
    occurrences = pm.rbernoulli(p_true, N)

    print occurrences
    print occurrences.sum()

    # Occurrences.mean is equal to n/N.
    print "What is the observed frequency in Group A? %.4f" % occurrences.mean(
    )
    print "Does this equal the true frequency? %s" % (occurrences.mean()
                                                      == p_true)

    # include the observations, which are Bernoulli
    obs = pm.Bernoulli("obs", p, value=occurrences, observed=True)

    # To be explained in chapter 3
    mcmc = pm.MCMC([p, obs])
    mcmc.sample(18000, 1000)

    plt.title(
        "Posterior distribution of $p_A$, the true effectiveness of site A")
    plt.vlines(p_true, 0, 90, linestyle="--", label="true $p_A$ (unknown)")
    plt.hist(mcmc.trace("p")[:], bins=25, histtype="stepfilled", normed=True)
    plt.legend()
    plt.show()
示例#11
0
def create_pymc_model(x_mat, y_vec, prior_sigma=5.0):
    tau_mc = 1. / (prior_sigma**2)
    (n, d) = np.shape(x_mat)
    w_mc = list([])
    x_mc = list([])
    for i in np.arange(0, d):
        w_mc[len(w_mc):] = [pymc.Normal('w' + str(i + 1) + '_mc', 0.0, tau_mc)]
        x_mc[len(x_mc):] = [
            pymc.Normal('x' + str(i + 1) + '_mc',
                        0.0,
                        1.0,
                        value=x_mat[:, i],
                        observed=True)
        ]
    w_mc = np.array(w_mc, dtype=object)
    x_mc = np.array(x_mc, dtype=object)

    @pymc.deterministic
    def pred_mu(w_mc=w_mc, x_mc=x_mc):
        return sigmoid(np.dot(x_mc, np.transpose(w_mc)))

    y_mc = pymc.Bernoulli('y_mc',
                          p=pred_mu,
                          value=np.array(
                              map(lambda val: 0 if val == -1 else +1, y_vec)),
                          observed=True)
    return pymc.Model(
        [pred_mu, pymc.Container(w_mc),
         pymc.Container(x_mc), y_mc])
def Main():

    # Plot Raw Data
    plot_raw(challenger_data)

    # Calculate observed values
    observed = pm.Bernoulli("bernoulli_obs", p, value=D, observed=True)

    # Create and sample from model
    model = pm.Model([observed, beta, alpha])

    # Once again, this code will be explored next chapter
    map_ = pm.MAP(model)
    map_.fit()
    mcmc = pm.MCMC(model)
    mcmc.sample(120000, 100000, 2)

    alpha_samples = mcmc.trace('alpha')[:, None]  # best to make them 1d
    beta_samples = mcmc.trace('beta')[:, None]

    # Do some plotting
    plot_posterior(alpha_samples, beta_samples)

    t = np.linspace(temperature.min() - 5, temperature.max() + 5, 50)[:, None]
    p_t = logistic(t.T, beta_samples, alpha_samples)

    mean_prob_t = p_t.mean(axis=0)

    # vectorized bottom and top 2.5% quantiles for credible interval
    qs = mquantiles(p_t, [0.025, 0.975], axis=0)

    plot_against_data(t, mean_prob_t, qs)

    plot_t_31(alpha_samples, beta_samples)
示例#13
0
文件: fit_a_line.py 项目: wiai/agpy
def pymc_linear_fit_perpointoutlier(data1, data2, data1err=None, data2err=None):
    """
    Model 3 from http://astroml.github.com/book_figures/chapter8/fig_outlier_rejection.html

    *IGNORES X ERRORS*
    """
    if data1err is not None:
        raise NotImplementedError("Currently this form of outlier rejection ignores X errors")

    # Third model: marginalizes over the probability that each point is an outlier.
    # define priors on beta = (slope, intercept)
    @pymc.stochastic
    def beta_M2(value=np.array([2., 100.])):
        """Slope and intercept parameters for a straight line.
        The likelihood corresponds to the prior probability of the parameters."""
        slope, intercept = value
        prob_intercept = 1 + 0 * intercept
        # uniform prior on theta = arctan(slope)
        # d[arctan(x)]/dx = 1 / (1 + x^2)
        prob_slope = np.log(1. / (1. + slope ** 2))
        return prob_intercept + prob_slope


    @pymc.deterministic
    def model_M2(xi=data1, beta=beta_M2):
        slope, intercept = beta
        return slope * xi + intercept

    # qui is bernoulli distributed
    qi = pymc.Bernoulli('qi', p=1 - Pb, value=np.ones(len(data1)))


    def outlier_likelihood(yi, mu, dyi, qi, Yb, sigmab):
        """likelihood for full outlier posterior"""
        Vi = dyi ** 2
        Vb = sigmab ** 2

        #root2pi = np.sqrt(2 * np.pi)

        logL_in = -0.5 * np.sum(qi * (np.log(2 * np.pi * Vi)
                                      + (yi - mu) ** 2 / Vi))

        logL_out = -0.5 * np.sum((1 - qi) * (np.log(2 * np.pi * (Vi + Vb))
                                             + (yi - Yb) ** 2 / (Vi + Vb)))

        return logL_out + logL_in

    OutlierNormal = pymc.stochastic_from_dist('outliernormal',
                                              logp=outlier_likelihood,
                                              dtype=np.float,
                                              mv=True)

    y_outlier = OutlierNormal('y_outlier', mu=model_M2, dyi=data2,
                              Yb=Yb, sigmab=sigmab, qi=qi,
                              observed=True, value=yi)

    M2 = dict(y_outlier=y_outlier, beta_M2=beta_M2, model_M2=model_M2,
              qi=qi, Pb=Pb, Yb=Yb, log_sigmab=log_sigmab, sigmab=sigmab)
示例#14
0
def simple_2model():
    mu = -2.1
    tau = 1.3
    p = 0.4
    with Model() as model:
        x = pm.Normal("x", mu, tau=tau, initval=0.1)
        pm.Deterministic("logx", at.log(x))
        pm.Bernoulli("y", p)
    return model.compute_initial_point(), model
示例#15
0
def simple_2model():
    mu = -2.1
    tau = 1.3
    p = .4
    with Model() as model:
        x = pm.Normal('x', mu, tau, testval=.1)
        y = pm.Bernoulli('y', p)

    return model.test_point, model
示例#16
0
def performInference(graph, responses):
    #this is a really hacky solution for now until I can spend more time figuring out how to do this more programatically

    pStr = "p%s"

    def _build_probabilities(cid):
        #return memoized bernoulli variable
        if "_bp" in graph[cid]: return graph[cid]["_bp"]

        #hack since pymc seems to require ascii (and not unicode)
        cida = str(cid).encode('ascii')

        if graph[cid]["dependencies"]:
            # process dependencies first
            deps = map(_build_probabilities, graph[cid]["dependencies"])
            cp = calculateProbability(pStr % cida, deps)
            _bp = mc.Bernoulli(cida, cp, value=1)
        else:
            # roots get special treatment
            _bp = mc.Bernoulli(cida, .5, value=1)

        #memoize bernoulli variable
        graph[cid]["_bp"] = _bp
        return _bp

    concepts = map(_build_probabilities, graph)

    #variables = mc.Bernoulli('variables', .5, value=1)
    #concepts.append(variables);

    #pConditionals = calculateProbability('pConditionals', [variables])
    #conditionals = mc.Bernoulli('conditionals', pConditionals, value=1)
    #concepts.append(conditionals);

    otherQuestions = []
    for example in responses:
        # more pymc ascii hacks
        cida = str(example[0]).encode('ascii')

        tmp = graph[example[0]]["_bp"]
        prob = mc.Lambda(pStr % cida,
                         lambda tmp=tmp: pl.where(tmp, 1 - pS, pG))
        otherQuestions.append(
            mc.Bernoulli(cida, prob, value=example[1], observed=True))

    ##################some simple tests##########

    model = mc.Model(concepts + otherQuestions)

    samples = mc.MCMC(model)
    knownNodes = []
    samples.sample(1000)

    for concept in concepts:
        if concept.trace().mean() > 0.75:
            knownNodes.append(concept.__name__)
    return knownNodes
示例#17
0
def trace(matrix,params,iters,burn,mu=0):
    # using specified set of priors, create coefficients
    coefs, term_list = create_coefs(params=params, priors={coef: pymc.Normal(coef, mu, 0.01) for coef in params})

    @pymc.deterministic
    def probs(term_list=term_list):
        probs = 1 / (1 + np.exp(-1 * sum(term_list)))
        probs = np.array([[prob if prob > 0 else 0 for prob in row] for row in probs])
        probs[np.diag_indices_from(probs)] = 0
        # Manually cut off the top triangle:
        probs[np.triu_indices_from(probs)] = 0
        return probs

    # Fitting
    matrix[np.triu_indices_from(matrix)] = 0
    max_attempts = 50
    attempts = 0
    while attempts < max_attempts:
        try:
            outcome = pymc.Bernoulli("outcome", probs, value=matrix, observed=True)
            break
        except:
            print("Encountered zero probability error number",attempts,", trying again...")
            if attempts >= max_attempts:
                raise ValueError("Something went wrong with the stochastic probabilities")
            attempts += 1

    sim_outcome = pymc.Bernoulli("sim_outcome", probs)
    args = [outcome, sim_outcome, probs]
    # density_coef, density_term,
    # block_coef, block_term]
    for coef, info in coefs.items():
        # Add both coefficient and term for each coefficient
        for item in info:
            args.append(item)
    model = pymc.Model(args)
    mcmc = pymc.MCMC(model)
    mcmc.sample(iter=iters, burn=burn, thin=50)  # approx. 30 seconds

    traces = diagnostics(coefs=coefs, mcmc=mcmc)

    goodness_of_fit(mcmc=mcmc)

    return traces
示例#18
0
def cartesian_bernoulli_child(name,
                              parents,
                              value=None,
                              N=None,
                              return_coeffs=False,
                              fixed={}):
    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)

    ranges = [range(get_levels_count(p)) for p in parents]
    parents2index = {}
    coeffs = []
    for i, parent_vals in enumerate(product(*ranges)):
        parents2index[parent_vals] = i
        parents_repr = ' '.join('%s=%s' % (parent, v)
                                for parent, v in zip(parents, parent_vals))
        coeff_name = COEFFS_PREFIX + 'p(%s | %s)' % (name, parents_repr)
        coeff = fixed.get(coeff_name, pymc.Uniform(coeff_name, 0, 1))
        coeffs.append(coeff)

    intify = lambda x: tuple(map(int, x))

    @pymc.deterministic
    def child_prob(parents=parents, coeffs=coeffs):
        return np.array([
            coeffs[parents2index[intify(parent_vals)]]
            for parent_vals in zip(*parents)
        ])

    child_prob.__name__ = 'p(%s)' % name

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

    if return_coeffs:
        return child, coeffs + [child_prob]
    else:
        return child
示例#19
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
    Bern = pm.Bernoulli('Bern', B)
    #@UndefinedVariable
    return pm.Model([B, Bern])
示例#20
0
    def _build_probabilities(cid):
        #return memoized bernoulli variable
        if "_bp" in graph[cid]: return graph[cid]["_bp"]

        #hack since pymc seems to require ascii (and not unicode)
        cida = str(cid).encode('ascii')

        if graph[cid]["dependencies"]:
            # process dependencies first
            deps = map(_build_probabilities, graph[cid]["dependencies"])
            cp = calculateProbability(pStr % cida, deps)
            _bp = mc.Bernoulli(cida, cp, value=1)
        else:
            # roots get special treatment
            _bp = mc.Bernoulli(cida, .5, value=1)

        #memoize bernoulli variable
        graph[cid]["_bp"] = _bp
        return _bp
示例#21
0
def main():
    N = 100
    p = pm.Uniform("freq_cheating", 0, 1)
    true_answers = pm.Bernoulli("truths", p, size=N)
    first_coin_flips = pm.Bernoulli("first_flips", 0.5, size=N)
    second_coin_flips = pm.Bernoulli("second_flips", 0.5, size=N)

    @pm.deterministic
    def observed_proportion(t_a=true_answers,
                            fc=first_coin_flips,
                            sc=second_coin_flips):
        result = t_a & fc | ~fc & sc
        return float(sum(result)) / len(result)

    X = 35
    observations = pm.Binomial("obs",
                               N,
                               observed_proportion,
                               value=X,
                               observed=True)

    model = pm.Model([
        p, true_answers, first_coin_flips, second_coin_flips,
        observed_proportion, observations
    ])

    # To be explained in Chapter 3!
    mcmc = pm.MCMC(model)
    mcmc.sample(40000, 15000)

    figsize(12.5, 3)
    p_trace = mcmc.trace("freq_cheating")[:]
    plt.hist(p_trace,
             histtype="stepfilled",
             normed=True,
             alpha=0.85,
             bins=30,
             label="posterior distribution",
             color="#348ABD")
    plt.vlines([.05, .35], [0, 0], [5, 5], alpha=0.3)
    plt.xlim(0, 1)
    plt.legend()
    plt.show()
示例#22
0
def simple_2model():
    mu = -2.1
    tau = 1.3
    p = .4
    with Model() as model:
        x = pm.Normal('x', mu, tau, testval=.1)
        logx = pm.Deterministic('logx', log(x))
        y = pm.Bernoulli('y', p)

    return model.test_point, model
示例#23
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])
示例#24
0
class BinaryTestModel:
    series = np.array([0,0,1,1,0,0,0,1,1,1])

    fair = pymc.Bernoulli('fair', p=.5, value=1)

    @pymc.deterministic
    def coin(fair=fair):
        if fair:
            return .5
        else:
            return .1
    # @stoch
    # def coin(value=.5, fair=fair):
    #     """Return the probability of a tail on flipping a coin."""
    #     if fair is True:
    #         return pymc.beta_like(value, 1e6, 1e6)
    #     else:
    #         return pymc.uniform_like(value, .3, .7)

    tail = pymc.Bernoulli('tail', p=coin, value=series, observed=True)
示例#25
0
def outcomes(match_vars):
    outcome_vars = []
    
    for i in range(0,len(match_vars)):
        outcome_vars.append(pm.Bernoulli('outcome_%i' % i, 
                                         match_vars[i], 
                                         value=[True], 
                                         observed=True, 
                                         plot=False))

    return outcome_vars
示例#26
0
    def __init__(self, G=nx.balanced_tree(2, 3, create_using=nx.DiGraph())):
        self.G = G
        self.n = len(G)
        roots = [n for n, d in G.in_degree().items() if d == 0]
        # root random variables
        self.roots = [pm.Bernoulli(str(v), 0.5, value=0) for v in roots]

        self.node_top_sort = nx.topological_sort(G)
        node_variables = dict(zip(roots, self.roots))
        self.others = []

        for node in self.node_top_sort:
            if node not in roots:
                Pa_v = [
                    node_variables[key] for key in node_variables.keys()
                    if key in G.predecessors(node)
                ]
                weight = [
                    G[key][node]['weight'] for key in G.predecessors(node)
                ]
                bias = G.node[node]['bias']

                # energy =  w*Pa(v) + bias
                energy = pm.Lambda(
                    'energy_%d' % (node),
                    lambda Pa_v=Pa_v, weight=weight, bias=bias: pm.
                    LinearCombination('weighted_sum', x=Pa_v, y=weight) + bias)

                # Pr(v=1) = sigmoid(energy)
                # use pymc.Lambda deterministic function
                sigmoid_cond_prob = pm.Lambda('sigmoid_%d_cond' % (node),
                                              lambda energy=energy: 1 /
                                              (1 + exp(-energy)))
                node_variables[node] = pm.Bernoulli(str(node),
                                                    sigmoid_cond_prob,
                                                    value=0)

                self.others.append(node_variables[node])

        self.node_variables = node_variables
        pm.MCMC.__init__(self, [self.roots, self.others])
示例#27
0
def dice(data=None):

    if data is None:
        x = [pymc.rbernoulli(1.0 / 6.0) for i in range(0, 100)]
    else:
        x = data

    prob = pymc.Uniform('prob', lower=0, upper=1)

    d = pymc.Bernoulli('d', p=prob, value=x, observed=True)

    return locals()
示例#28
0
def logistic_bernoulli_child(name,
                             parents,
                             value=None,
                             N=None,
                             return_coeffs=False,
                             fixed={}):
    if value is not None:
        value = mask_missing(value)
    N = N or len(value)
    theta, all_coeffs = _linearised_many(parents, name, True, fixed)

    child_prob = pymc.InvLogit('p(%s)' % name, theta)
    if value is None:
        child = pymc.Bernoulli(name, p=child_prob, value=np.zeros(N))
    else:
        child = pymc.Bernoulli(name, p=child_prob, value=value, observed=True)
    set_levels_count(child, 2)

    if return_coeffs:
        return child, all_coeffs
    else:
        return child
示例#29
0
def BIPBayes(pa, bip, mu, tau):
    bippg = np.zeros(pa)
    bippg[:bip] = 1
    pbip = pm.Normal("pbip", mu, tau)
    obsbip = pm.Bernoulli("obsbip", pbip, value=bippg, observed=True)
    mcmc = pm.MCMC([pbip, obsbip])
    mcmc.sample(20000, 7000)
    sumstats = mcmc.stats()
    loint = sumstats['pbip']['95% HPD interval'][0]
    hiint = sumstats['pbip']['95% HPD interval'][1]
    bip_trace = mcmc.trace('pbip')[:]
    bip_trace = [x for x in bip_trace if (x > loint) and (x < hiint)]
    return bip_trace
示例#30
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