Skip to content

Commit f47dddb

Browse files
committed
MAINT Remove Metropolis adaptation so that only BinaryGibbsMetropolis remains.
1 parent 4d8f341 commit f47dddb

File tree

1 file changed

+11
-37
lines changed

1 file changed

+11
-37
lines changed

pymc3/step_methods/metropolis.py

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@ class Metropolis(ArrayStepShared):
7373
default_blocked = False
7474

7575
def __init__(self, vars=None, S=None, proposal_dist=NormalProposal, scaling=1.,
76-
tune=True, tune_interval=100, model=None, gibbs=None, **kwargs):
76+
tune=True, tune_interval=100, model=None, **kwargs):
7777

7878
model = modelcontext(model)
7979

8080
if vars is None:
8181
vars = model.vars
8282
vars = inputvars(vars)
83-
self.dim = sum(v.dsize for v in vars)
8483

8584
if S is None:
8685
S = np.ones(self.dim)
@@ -90,8 +89,6 @@ def __init__(self, vars=None, S=None, proposal_dist=NormalProposal, scaling=1.,
9089
self.tune_interval = tune_interval
9190
self.steps_until_tune = tune_interval
9291
self.accepted = 0
93-
self.gibbs = gibbs
94-
self.index = 0
9592

9693
# Determine type of variables
9794
self.discrete = np.array([v.dtype in discrete_types for v in vars])
@@ -112,16 +109,6 @@ def astep(self, q0):
112109
self.steps_until_tune = self.tune_interval
113110
self.accepted = 0
114111

115-
if self.gibbs == 'sequential':
116-
self.index = (self.index + 1) % self.dim
117-
elif self.gibbs == 'random':
118-
self.index = np.random.randint(0, self.dim)
119-
else:
120-
self.index = slice(None) # select all
121-
122-
mask = np.zeros(self.dim, dtype=np.bool8)
123-
mask[self.index] = True
124-
125112
delta = (self.proposal_dist() * self.scaling)
126113

127114
if self.any_discrete:
@@ -135,8 +122,6 @@ def astep(self, q0):
135122
else:
136123
q = q0 + delta
137124

138-
q = q[mask]
139-
140125
q_new = metrop_select(self.delta_logp(q, q0), q, q0)
141126

142127
if q_new is q:
@@ -215,27 +200,15 @@ def __init__(self, vars, scaling=1., tune=True, tune_interval=100, gibbs='random
215200
super(BinaryMetropolis, self).__init__(vars, [model.fastlogp])
216201

217202
def astep(self, q0, logp):
203+
# Convert adaptive_scale_factor to a jump probability
204+
p_jump = 1. - .5 ** self.scaling
218205

219-
if (self.gibbs == 'sequential') or (self.gibbs == 'random'):
220-
order = list(range(self.dim))
221-
if self.gibbs == 'random':
222-
np.random.shuffle(order)
223-
224-
q = copy(q0)
225-
for idx in order:
226-
q[idx] = True - q[idx]
227-
q = metrop_select(logp(q) - logp(q0), q, q0)
228-
q_new = q
229-
else:
230-
# Convert adaptive_scale_factor to a jump probability
231-
p_jump = 1. - .5 ** self.scaling
232-
233-
rand_array = random(q0.shape)
234-
q = copy(q0)
235-
# Locations where switches occur, according to p_jump
236-
switch_locs = (rand_array < p_jump)
237-
q[switch_locs] = True - q[switch_locs]
238-
q_new = metrop_select(logp(q) - logp(q0), q, q0)
206+
rand_array = random(q0.shape)
207+
q = copy(q0)
208+
# Locations where switches occur, according to p_jump
209+
switch_locs = (rand_array < p_jump)
210+
q[switch_locs] = True - q[switch_locs]
211+
q_new = metrop_select(logp(q) - logp(q0), q, q0)
239212

240213
return q_new
241214

@@ -274,12 +247,13 @@ def astep(self, q0, logp):
274247

275248
q_prop = copy(q0)
276249
q_cur = copy(q0)
250+
277251
for idx in order:
278252
q_prop[idx] = True - q_prop[idx]
279253
q_cur = metrop_select(logp(q_prop) - logp(q_cur), q_prop, q_cur)
280254
q_prop = copy(q_cur)
281255

282-
return q_prop
256+
return q_cur
283257

284258
@staticmethod
285259
def competence(var):

0 commit comments

Comments
 (0)