# DE-Metropolis with blackbox likelihood does not terminate

**URL:** <https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360>\
**Category:** Questions\
**Created:** [July 1, 2020, 8:22am UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360 "2020-07-01T08:22:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![berakrishn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/berakrishn/32/2821_2.png) [@berakrishn](https://discourse.pymc.io/u/berakrishn)\
**Post date:** [July 1, 2020, 8:22am UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/1 "2020-07-01T08:22:47Z")

</div>

I am trying to run DE-MCMC with a Blackbox likelihood function. I have 3 parameters that go into the blackbox likelihood function. The program starts running and gives me the following output. It doesn’t really terminate even when I leave it running for a long time. Any idea why this is taking too long or maybe not terminating at all?

```auto
Population sampling (4 chains) DEMetropolis: [a1, a2, a3] 
Attempting to parallelize chains to all cores. You can turn this off with `pm.sample(cores=1)`. 
...
Population parallelization failed. Falling back to sequential stepping of chains.
...

Sampling 4 chains for 0 tune and 1_050 draw iterations (0 + 4_200 draws total) took 1054 seconds.

```

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [July 1, 2020, 8:44am UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/2 "2020-07-01T08:44:57Z")

</div>

Hi,  
Thanks for your question!  
Are you Windows? Have you tried with `pm.sample(cores=1)`?

---

<div class="post-metadata">

**Author:** ![berakrishn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/berakrishn/32/2821_2.png) [@berakrishn](https://discourse.pymc.io/u/berakrishn)\
**Post date:** [July 1, 2020, 12:07pm UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/3 "2020-07-01T12:07:27Z")

</div>

@AlexAndorra I use Ubuntu. When I use `trace = pymc3.sample(ndraws, step=step, cores=1)`, I get the following error.

```auto
ValueError: DEMetropolis requires at least 4 chains. For this 3-dimensional model you should use ≥4 chains

```

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [July 2, 2020, 12:08pm UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/4 "2020-07-02T12:08:07Z")

</div>

Ah ok. Looks like you’re sampling less than 4 chains and the sampler complains about that. Did you try setting the number of chains to at least 4 with the kwarg `chains=4` to `pm.sample`?  
Also pinging @michaelosthege, as he’s our in-house expert on DEMetropolis 😉

---

<div class="post-metadata">

**Author:** ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)\
**Post date:** [July 2, 2020, 1:07pm UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/5 "2020-07-02T13:07:08Z")

</div>

If evaluating the likelihood is expensive, it could be due to the conversion to inference data that happens after sampling to calculate ess and rhat. There is one proposal to adress the hang up part here [https://github.com/arviz-devs/arviz/issues/1224](https://github.com/arviz-devs/arviz/issues/1224).

If so, using `idata_kwargs=dict(log_likelihood=False)` shouldsolve the problem.

---

<div class="post-metadata">

**Author:** ![berakrishn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/berakrishn/32/2821_2.png) [@berakrishn](https://discourse.pymc.io/u/berakrishn)\
**Post date:** [July 7, 2020, 4:13am UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/6 "2020-07-07T04:13:28Z")

</div>

Thanks for the help @AlexAndorra. I got it running after setting the appropriate number of chains.  
But I have question about rather unrelated topic: Why does it say that **0 tune + 4000 draw** iterations were executed even when I specify that 3000 is the number of draws and number of burn-in samples is 1000. I understand that number of draw iterations 4000 = 3000(#draws) + 1000(#burn-in). But why does it say **0 tune**?

```
Sampling 16 chains for 0 tune and 4_000 draw iterations (0 + 64_000 draws total) took 785 seconds.
```

---

<div class="post-metadata">

**Author:** ![berakrishn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/berakrishn/32/2821_2.png) [@berakrishn](https://discourse.pymc.io/u/berakrishn)\
**Post date:** [July 7, 2020, 4:29am UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/7 "2020-07-07T04:29:49Z")

</div>

Thanks @OriolAbril. I got the DE-MCMC running with Blackbox likelihood. The program now completes execution in some finite time. Let me know if I understand your point correctly, when I pass the argument `idata_kwargs=dict(log_likelihood=False)` in `pymc3.sample(...)` function, it should stop re-evaluating the model? But when I pass this to the function, the execution time is almost unaffected.

---

<div class="post-metadata">

**Author:** ![michaelosthege](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/michaelosthege/32/371_2.png) [@michaelosthege](https://discourse.pymc.io/u/michaelosthege)\
**Post date:** [July 7, 2020, 8:36am UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/8 "2020-07-07T08:36:37Z")

</div>

@berakrishn I am aware of two things that can inflate the runtime of `pm.sample` in non-obvious ways:

- ArviZ re-evaluates the likelihood of all samples by default. If evaluation of your likelihood is expensive pass `idata_kwargs=dict(log_likelihood=False)` to turn it off
- if the chains did not converge, the convergence diagnostics are slower to compute. Passing `compute_convergence_checks=False` disables them.

I recommend the latter until you found the right DE-MCMC(-Z) settings for your model.

---

<div class="post-metadata">

**Author:** ![berakrishn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/berakrishn/32/2821_2.png) [@berakrishn](https://discourse.pymc.io/u/berakrishn)\
**Post date:** [July 7, 2020, 9:14am UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/9 "2020-07-07T09:14:29Z")

</div>

thanks @michaelosthege. This explains the strange behavior of my code.

---

<div class="post-metadata">

**Author:** ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)\
**Post date:** [July 7, 2020, 11:28pm UTC](https://discourse.pymc.io/t/de-metropolis-with-blackbox-likelihood-does-not-terminate/5360/10 "2020-07-07T23:28:23Z")

</div>

During sampling, PyMC3 does not store pointwise log likelihood values, therefore, after sampling finishes, ArviZ iterates over each chain and each draw to compute the pointwise log likelihood for each sample. This requires evaluating the log likelihood _again_ for each sample, 64\_000 times in your case which could take a long time. Using `idata_kwargs=dict(log_likelihood=False)` tells ArviZ to skip pointwise log likelihood computation.

Note that ArviZ is called after sampling finishes to calculate `ess` and `rhat` so skipping the likelihood computation should not affect the sampling time. This is why I suspected the reason behind the non termination could be this, in cases like this, sampling finishes, but both conversion to ArviZ and ess and rhat computation can still take a long time and have no progress bar so it looks like nothing is happening.
