Skip to content

Commit c56bf3b

Browse files
author
Junpeng Lao
committed
More flexible kernel
that allows antithetical sampling. Setting transit_p=1 we recover the old behavior of the sampler.
1 parent 2865760 commit c56bf3b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pymc3/step_methods/metropolis.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,13 @@ class BinaryGibbsMetropolis(ArrayStep):
300300
"""A Metropolis-within-Gibbs step method optimized for binary variables"""
301301
name = 'binary_gibbs_metropolis'
302302

303-
def __init__(self, vars, order='random', model=None):
303+
def __init__(self, vars, order='random', transit_p=.8, model=None):
304304

305305
model = pm.modelcontext(model)
306306

307+
# transition probabilities
308+
self.transit_p = transit_p
309+
307310
self.dim = sum(v.dsize for v in vars)
308311

309312
if order == 'random':
@@ -330,7 +333,8 @@ def astep(self, q0, logp):
330333
logp_curr = logp(q)
331334

332335
for idx in order:
333-
curr_val, q[idx] = q[idx], nr.randint(0, 2)
336+
state = [q[idx], True-q[idx]]
337+
curr_val, q[idx] = q[idx], state[int(nr.rand() < self.transit_p)]
334338
logp_prop = logp(q)
335339
q[idx], accepted = metrop_select(logp_prop - logp_curr, q[idx], curr_val)
336340
if accepted:

0 commit comments

Comments
 (0)