In [1]:
import numpy as np
import pymc
import math
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
def model():
s1 = pymc.Normal('s1', 0, 1.0)
s2 = pymc.Normal('s2', 0, 1.0)
s3 = pymc.Normal('s3', 0, 1.0)
def logit(s1, s2):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout12(s1= s1, s2 = s2):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout23(s1= s2, s2 = s3):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout13(s1= s1, s2 = s3):
return 1.0 / (1 + math.exp(s2 - s1))
toutcome12 = [1, 0, 0, 1, 1, 1, 1, 1, 1]
toutcome23 = [1, 1, 0, 1, 0, 0, 1, 1, 1]
outcome12 = pymc.Bernoulli('outcome12', sout12, value=toutcome12, observed=True)
outcome23 = pymc.Bernoulli('outcome23', sout23, value=toutcome23, observed=True)
outcome13 = pymc.Bernoulli('outcome13', sout13)
return locals()
mcmc = pymc.MCMC(model())
mcmc.sample(iter=10000, burn=500, thin=3)
pymc.Matplot.plot(mcmc)
In [3]:
for i in range(1,4):
print ("s%i: " %i, np.mean(mcmc.trace("s%i" %i)[:]))
In [6]:
import pymc
import math
import matplotlib.pyplot as plt
%matplotlib inline
def model():
s1 = pymc.Normal('s1', 0, 1.0)
s2 = pymc.Normal('s2', 0, 1.0)
s3 = pymc.Normal('s3', 0, 1.0)
s4 = pymc.Normal('s4', 0, 1.0)
def logit(s1, s2):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout12(s1= s1, s2 = s2):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout23(ss1= s2, ss2 = s3):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout13(s1= s1, s2 = s3):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout14(s1= s1, s2 = s4):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout34(s1= s3, s2 = s4):
return 1.0 / (1 + math.exp(s2 - s1))
@pymc.deterministic
def sout42(s1= s4, s2 = s2):
return 1.0 / (1 + math.exp(s2 - s1))
toutcome12 = [1, 0, 0, 1, 1, 1, 1, 1, 1]
toutcome23 = [1, 1, 0, 1, 0, 0, 1, 1, 1]
toutcome14 = [1,1, 1, 1 , 1, 0, 0]
toutcome34 = [1, 1, 0, 0]
outcome12 = pymc.Bernoulli('outcome12', sout12, value=toutcome12, observed=True)
outcome23 = pymc.Bernoulli('outcome23', sout23, value=toutcome23, observed=True)
outcome13 = pymc.Bernoulli('outcome13', sout13)
outcome14 = pymc.Bernoulli('outcome14', sout14, value=toutcome14, observed=True)
outcome34 = pymc.Bernoulli('outcome34', sout34, value=toutcome34, observed=True)
outcome42 = pymc.Bernoulli('outcome42', sout42)
return locals()
mcmc = pymc.MCMC(model())
mcmc.sample(iter=10000, burn=500, thin=3)
pymc.Matplot.plot(mcmc)
In [7]:
for i in range(1,5):
print ("s%i: " %i, np.mean(mcmc.trace("s%i" %i)[:]))
In [ ]: