# Metropolis step method initialization being called repeatedly and slow

**URL:** <https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411>\
**Category:** Questions\
**Created:** [December 18, 2018, 3:55pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411 "2018-12-18T15:55:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jan/32/1129_2.png) [@Jan](https://discourse.pymc.io/u/Jan)\
**Post date:** [December 18, 2018, 3:55pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/1 "2018-12-18T15:55:17Z")

</div>

I have checked this related [post](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86), but the problem has not been solved.  
I added some print statements in `Metropolis` init method, and found that the init method is called repeatedly for many many times, each time for one variable. This does not finish until 6-7h later!  
Strangely, I have passed all vars to `Metropolis` at once, but why does it only get one var and where is it called repeated? I tried search `Metropolis` entry point on GtiHub source repo but couldn’t find any.

```python
s = pm.step_methods.metropolis.Metropolis(vars=self.model.free_RVs)

```

For some vars, it takes really long to initialize. Can I do anything to avoid it? Thanks.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 18, 2018, 8:22pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/2 "2018-12-18T20:22:07Z")

</div>

Where did you add the print statement? And what is your model like? It is a bit difficult to say without more information.

---

<div class="post-metadata">

**Author:** ![Jan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jan/32/1129_2.png) [@Jan](https://discourse.pymc.io/u/Jan)\
**Post date:** [December 18, 2018, 8:27pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/3 "2018-12-18T20:27:15Z")

</div>

Yep I agree. I added quite a few print statements before and after this [line](https://github.com/pymc-devs/pymc3/blob/master/pymc3/step_methods/metropolis.py#L137). Every statement in this `init` method has been executed many times.  
I also `print(vars)` at the beginning of the init method. It gives only one variable, while in my model I pass all variables to it. I think maybe there is some wrapper for this step method but I couldn’t find any.

```auto
s = pm.step_methods.metropolis.Metropolis(vars=self.model.free_RVs)

```

---

<div class="post-metadata">

**Author:** ![Jan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jan/32/1129_2.png) [@Jan](https://discourse.pymc.io/u/Jan)\
**Post date:** [December 18, 2018, 8:50pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/4 "2018-12-18T20:50:00Z")

</div>

For example, a simple model as follows.

```auto
with pm.Model() as model:
    mu = pm.Normal('mu', mu=0, sd=1)
    sd = pm.Normal('sd', mu=0, sd=1)
    obs = pm.Normal('obs', mu=mu, sd=sd, observed=np.random.randn(100))
    
    s = pm.Metropolis(vars=model.free_RVs)
    trace = pm.sample(100, tune=100, step=s, cores=1)

```

The print statements are added as such.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/b/bfb097b7fb8b8b2ebdc6cd438b1f558e1b6d6e82.png)  
The result follows.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/0/00180b3767389a123848ec362e0ae107feac5fa9.jpeg)

My questions are that, where the wrapper for the `Metropolis` is since I don’t see a `for` loop processing one variable at a time, and why the step after `print('DEBUG: 1')` can be extremely slow for some variable, e.g., a hierarchical parameter in my project model.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 18, 2018, 8:53pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/5 "2018-12-18T20:53:55Z")

</div>

Hmm, maybe you can try passing `blocked=True` (although that is the default already)

---

<div class="post-metadata">

**Author:** ![Jan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jan/32/1129_2.png) [@Jan](https://discourse.pymc.io/u/Jan)\
**Post date:** [December 18, 2018, 8:57pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/6 "2018-12-18T20:57:59Z")

</div>

Oh damn! It works! Now all variables gather together!  
Thanks a lot but i have to say, `Metropolis` has a poor document about its interface…Perhaps most people just stick to the default `NUTS` sampler and don’t pay as much attention to `Metropolis`.

---

<div class="post-metadata">

**Author:** ![Jan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jan/32/1129_2.png) [@Jan](https://discourse.pymc.io/u/Jan)\
**Post date:** [December 18, 2018, 9:01pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/7 "2018-12-18T21:01:50Z")

</div>

BTW, does `HMC` or `NUTS` do the same thing, sort of cloning the vars into shared, which takes a really long time? Because i want fast and massive sampling (tho i know the effective number may be small) I turn to Metropolis, but now it turns out `Metropolis` is even slower because of preprocessing vars. Do you think this can be considered as a performance issue?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 18, 2018, 9:06pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/8 "2018-12-18T21:06:51Z")

</div>

`Metropolis` is not a good sampler in high dimensions, so you really **shouldn’t** be using that. And yes all sampler clone the vars to generate a logp function.

---

<div class="post-metadata">

**Author:** ![Jan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jan/32/1129_2.png) [@Jan](https://discourse.pymc.io/u/Jan)\
**Post date:** [December 18, 2018, 9:08pm UTC](https://discourse.pymc.io/t/metropolis-step-method-initialization-being-called-repeatedly-and-slow/2411/9 "2018-12-18T21:08:18Z")

</div>

Yes I agree. I just want to at least give it a try. The result is as bad as you say :(. Thanks for the clarification.
