Skip to content

Commit b9405ba

Browse files
committed
fixed examples and added deterministic traces
1 parent ad799f0 commit b9405ba

22 files changed

+238
-200
lines changed

examples/ARM12-6.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@
4646

4747
lr = Normal('lr', floor*floor_m + means[group], sd**-2., observed = lradon)
4848

49-
start = {'groupmean' : obs_means.mean(),
49+
start = {'groupmean' : obs_means.mean(),
5050
'groupsd' : obs_means.std(),
5151
'sd' : data.groupby('group').lradon.std().mean(),
5252
'means' : np.array(obs_means),
5353
'floor_m' : 0.,
5454
}
5555

56-
start = find_MAP(model, start, [groupmean,sd, floor_m])
57-
H = model.d2logpc()
58-
h = np.diag(H(start))
56+
start = find_MAP(start, [groupmean,sd, floor_m])
57+
H = model.d2logpc()
58+
h = np.diag(H(start))
5959

60-
step = HamiltonianMC(model, model.vars, h)
60+
step = HamiltonianMC(model.vars, h)
61+
62+
trace = sample(3000, step, start)
6163

62-
trace, state, t = sample(3000, step, start)
6364

64-
print " took: ", t

examples/ARM12-6uranium.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,18 @@
4848

4949
lr = Normal('lr', floor*floor_m + means[group] + ufull*u_m, sd**-2., observed = lradon)
5050

51-
start = {'groupmean' : obs_means.mean(),
51+
start = {'groupmean' : obs_means.mean(),
5252
'groupsd' : obs_means.std(),
5353
'sd' : data.groupby('group').lradon.std().mean(),
5454
'means' : np.array(obs_means),
5555
'u_m' : np.array([.72]),
5656
'floor_m' : 0.,
5757
}
5858

59-
start = find_MAP(model, start, model.vars[:-1])
60-
H = model.d2logpc()
61-
h = np.diag(H(start))
59+
start = find_MAP(start, model.vars[:-1])
60+
H = model.d2logpc()
61+
h = np.diag(H(start))
6262

63-
step = HamiltonianMC(model, model.vars, h)
63+
step = HamiltonianMC(model.vars, h)
6464

65-
trace, state, t = sample(3000, step, start)
66-
67-
print " took: ", t
65+
trace = sample(3000, step, start)

examples/ARM5-4.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@
2121

2222
Pa = np.array(P)
2323

24-
model = Model()
25-
with model:
24+
with Model() as model:
2625
effects = Normal('effects', mu = 0, tau = 100.**-2, shape = len(P.columns))
2726
p = sigmoid(dot(Pa, effects))
2827

2928
s = Bernoulli('s', p, observed = np.array(data.switch))
3029

3130

32-
#move the chain to the MAP which should be a good starting point
33-
start = find_MAP(model)
34-
H = model.d2logpc() #find a good orientation using the hessian at the MAP
35-
h = H(start)
31+
#move the chain to the MAP which should be a good starting point
32+
start = find_MAP()
33+
H = model.d2logpc() #find a good orientation using the hessian at the MAP
34+
h = H(start)
3635

37-
step_method = HamiltonianMC(model, model.vars, h)
36+
step = HamiltonianMC(model.vars, h)
3837

39-
trace, state, t = sample(3e3, step_method, start)
40-
print "took :", t
38+
trace = sample(3e3, step, start)

examples/banana.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,24 @@
2525
http://arxiv.org/abs/1011.0057
2626
"""
2727
N = 200
28-
model = Model()
29-
with model:
28+
with Model() as model:
3029

3130

3231
x = Normal('x', 0, 1)
3332
y = Normal('y', 0, 1)
3433
N = 200
3534
d = Normal('d', x + y**2, 1., observed = np.zeros(N))
3635

37-
start = model.test_point
38-
h = np.ones(2)*np.diag(approx_hess(model, start))[0]
36+
start = model.test_point
37+
h = np.ones(2)*np.diag(approx_hess(start))[0]
3938

4039

41-
step = HamiltonianMC(model, model.vars, h, path_length = 4.)
40+
step = HamiltonianMC(model.vars, h, path_length = 4.)
4241

43-
history, state, t = sample(3e3, step, start)
42+
trace = sample(3e3, step, start)
4443

45-
print "took :", t
4644
pl.figure()
47-
pl.hexbin(history['x'], history['y'])
45+
pl.hexbin(trace['x'], trace['y'])
4846

4947

5048

examples/dirichlet.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
simplextransform)
1515

1616

17-
H = model.d2logpc()
17+
H = model.d2logpc()
1818

19-
s = find_MAP(model)
19+
s = find_MAP()
2020

21-
step = HamiltonianMC(model, model.vars, H(s))
22-
trace, _,t = sample(1000, step, s)
21+
step = HamiltonianMC(model.vars, H(s))
22+
trace = sample(1000, step, s)
2323

2424

examples/hierarchical.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@
4949

5050

5151

52-
start = find_MAP(model)
53-
h = approx_hess(model, start) #find a good orientation using the hessian at the MAP
52+
start = find_MAP()
53+
h = approx_hess(start) #find a good orientation using the hessian at the MAP
5454

55-
step = HamiltonianMC(model, model.vars, h, is_cov = False)
55+
step = HamiltonianMC(model.vars, h, is_cov = False)
5656

57-
58-
print "took :", sample(3e3, step, start)
57+
trace = sample(3e3, step, start)

examples/logistic.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ def tinvlogit(x):
3030

3131
o = Bernoulli('o', p, observed = outcomes)
3232

33-
#move the chain to the MAP which should be a good starting point
34-
start = find_MAP(model)
35-
h = np.diag(approx_hess(model, start)) #find a good orientation using the hessian at the MAP
33+
3634

37-
step = HamiltonianMC(model, model.vars, h)
35+
#move the chain to the MAP which should be a good starting point
36+
start = find_MAP()
37+
h = np.diag(approx_hess(start)) #find a good orientation using the hessian at the MAP
3838

39-
history, state, t = sample(3e2, step, start)
40-
print "took :", t
39+
step = HamiltonianMC(model, model.vars, h)
40+
41+
trace = psample(3e2, step, start)

examples/normal.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
with model:
1414
x = Normal('x', 0, 1., n)
1515

16-
#start sampling at the MAP
17-
start = find_MAP(model)
18-
h = approx_hess(model, start) #find a good orientation using the hessian at the MAP
16+
#start sampling at the MAP
17+
start = find_MAP()
18+
h = approx_hess(start) #find a good orientation using the hessian at the MAP
1919

20-
#step_method = split_hmc_step(model, model.vars, hess, chain, hmc_cov)
21-
step = HamiltonianMC(model, model.vars, h, is_cov = False)
20+
step = HamiltonianMC(model.vars, h, is_cov = False)
2221

23-
ndraw = 3e3
24-
history, state, t = sample(ndraw, step, start)
22+
ndraw = 3e3
23+
trace = sample(ndraw, step, start)
2524

26-
print "took :", t

examples/simpletest.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@
1818

1919
d = Normal('data', mu = x, tau = .75**-2, observed = data)
2020

21-
start = model.test_point
21+
start = model.test_point
2222

23-
h = approx_hess(model, start)
24-
step = HamiltonianMC(model, model.vars, h)
23+
h = approx_hess(start)
24+
step = HamiltonianMC(model.vars, h)
2525

26-
history,state, t = sample(1e3, step, start)
27-
28-
print "took :", t
26+
trace = sample(1e3, step, start)
2927

3028
from pylab import *
3129
subplot(2,2,1)
32-
plot(history['x'][:,0,0])
30+
plot(trace[x][:,0,0])
3331
subplot(2,2,2)
34-
hist(history['x'][:,0,0])
32+
hist(trace[x][:,0,0])
3533

3634
subplot(2,2,3)
37-
plot(history['z'])
35+
plot(trace[x])
3836
subplot(2,2,4)
39-
hist(history['x'][:,0,0])
37+
hist(trace[x][:,0,0])
4038
show()

examples/speedtest_pymc3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@
4646

4747
model = Model()
4848
with model:
49-
# Estimated occupancy
49+
# Estimated occupancy
5050
p = Beta('b', 1,1)
5151

52-
# Latent variable for occupancy
52+
# Latent variable for occupancy
5353
z = Bernoulli('z', p , shape = y.shape)
5454

55-
# Estimated mean count
55+
# Estimated mean count
5656
theta = Uniform('theta', 0, 100)
5757

58-
# Poisson likelihood
58+
# Poisson likelihood
5959
z = ZeroInflatedPoisson('z', theta, z)
6060

6161

0 commit comments

Comments
 (0)