# Metropolis taking too long to initialize

**URL:** <https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86>\
**Category:** Questions\
**Tags:** from\_github\
**Created:** [June 16, 2017, 5:49am UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86 "2017-06-16T05:49:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![san7988](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@san7988](https://discourse.pymc.io/u/san7988)\
**Post date:** [June 16, 2017, 5:49am UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86/1 "2017-06-16T05:49:19Z")

</div>

Hi,

I’m trying to use metropolis method for sampling. It takes too long to initialize the metropolis method.

The code :  
`step = pm.Metropolis()`

This single line takes around 3 minutes to execute.  
The sampling itself is pretty fast using metropolis but the initialization is kind of overshadowing all the speed up I gained over slice method.  
Is this a known behavior? And can it be improved? In general how long should it takes ideally for the metropolis method to initialize?

I first posted this in [Github issues](https://github.com/pymc-devs/pymc3/issues/2321) but was suggested to move this here.  
Also, shortly I’ll provide the output of code profiling

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:** [June 16, 2017, 5:53am UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86/2 "2017-06-16T05:53:03Z")

</div>

Also, more information about the data and the model you are using would be helpful too

---

<div class="post-metadata">

**Author:** ![san7988](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@san7988](https://discourse.pymc.io/u/san7988)\
**Post date:** [June 16, 2017, 9:50am UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86/3 "2017-06-16T09:50:55Z")

</div>

PFB the profiler output :

```python
********************************************************
 ncalls tottime percall cumtime percall filename:lineno(function)
        1 0.001 0.001 134.514 134.514 test6.py:1(<module>)
        1 0.000 0.000 118.053 118.053 test6.py:13(run)
       36 0.002 0.000 68.688 1.908 function.py:76(function)
       36 0.003 0.000 68.680 1.908 pfunc.py:283(pfunc)
        1 0.000 0.000 67.645 67.645 arraystep.py:32( __new__ )
       32 0.001 0.000 67.645 2.114 metropolis.py:90( __init__ )
       32 0.001 0.000 67.477 2.109 metropolis.py:474(delta_logp)
       36 0.003 0.000 64.647 1.796 function_module.py:1722(orig_function)
       36 0.006 0.000 61.128 1.698 function_module.py:1391( __init__ )
6524/1261 0.031 0.000 50.851 0.040 opt.py:75(optimize)
       36 0.000 0.000 50.447 1.401 opt.py:92( __call__ )
   144/36 0.013 0.000 50.446 1.401 opt.py:213(apply)
32450/32159 0.245 0.000 26.275 0.001 op.py:583( __call__ )
     2545 0.004 0.000 26.169 0.010 memoize.py:11(memoizer)
    15893 0.091 0.000 24.787 0.002 op.py:892(make_thunk)
        1 0.000 0.000 24.539 24.539 starting.py:20(find_MAP)
********************************************************************************* 

```

I first create a linear model from formulas, and then run a sampler on it :

```python
glm.GLM.from_formula(formula, data, priors=dict1)
start = find_MAP(fmin=opt.fmin_powell)
step = pm.Metropolis()
trace = sample(n, step, is_cov=True, start=start, njobs=1)

```

Also, when I try and set njobs\>1, it starts using all the cores but the iteration speed decreases significantly from ~22 iterations/sec to ~30 seconds/iteration ans keeps deteriorating with increase in njobs.  
Sorry, but passing on data would not be possible I guess 😐

---

<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:** [June 16, 2017, 10:26am UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86/4 "2017-06-16T10:26:57Z")

</div>

The slowness usually from the `find_MAP` in my experience. Could you please try the default (NUTS with ADVI init):

```python
glm.GLM.from_formula(formula, data, priors=dict1)
trace = sample(n, njobs=2) #multiple trace for computing Rhat

```

---

<div class="post-metadata">

**Author:** ![san7988](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@san7988](https://discourse.pymc.io/u/san7988)\
**Post date:** [June 16, 2017, 10:41am UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86/5 "2017-06-16T10:41:59Z")

</div>

I think I had tried NUTS with ADVI init earlier but the iterations were not as fast as metropolis. But still, I’ll try and run it again and update the results.

---

<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:** [June 16, 2017, 10:50am UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86/6 "2017-06-16T10:50:43Z")

</div>

The faster sample in Metropolis is sometimes a lie 😉 it’s the effective sample that counts. You can have much more effective sample using HMC which means you can use a shorter trace.

---

<div class="post-metadata">

**Author:** ![colcarroll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/colcarroll/32/9_2.png) [@colcarroll](https://discourse.pymc.io/u/colcarroll)\
**Post date:** [June 16, 2017, 9:00pm UTC](https://discourse.pymc.io/t/metropolis-taking-too-long-to-initialize/86/7 "2017-06-16T21:00:51Z")

</div>

If I recall, the iterations per second with multiple jobs is only reporting on a single chain (and, you really care about effective samples per second!)
