Skip to content

Instantly share code, notes, and snippets.

@breakbee
Created August 3, 2014 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breakbee/efb9de34274ec02b68ff to your computer and use it in GitHub Desktop.
Save breakbee/efb9de34274ec02b68ff to your computer and use it in GitHub Desktop.
import pymc as pm
import numpy as np
ndims = 2
nobs = 20
n = 1000
y_sample = np.random.binomial(1, 0.5, size=(n,))
x_sample=np.empty(n)
x_sample[y_sample==0] = np.random.normal(-1, 1, size=(n, ))[y_sample==0]
x_sample[y_sample==1] = np.random.normal(1, 1, size=(n, ))[y_sample==1]
with pm.Model() as model:
p = pm.Beta('p', alpha=1.0, beta=1.0)
y = pm.Bernoulli('y', p=p, observed=y_sample)
mu0 = pm.Normal('mu0', mu=0., sd=1.)
mu1 = pm.Normal('mu1', mu=0., sd=1.)
mu = pm.Deterministic('mu', mu0 * (1-y_sample) + mu1 * y_sample)
x = pm.Normal('x', mu=mu, sd=1., observed=x_sample)
with model:
start = pm.find_MAP()
step = pm.NUTS()
trace = pm.sample(10000, step, start)
pm.traceplot(trace).savefig("result2.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment