# Does PyMC work for ABC SMC?

**URL:** <https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798>\
**Category:** Questions\
**Tags:** smc\_abc, smc\
**Created:** [February 14, 2022, 4:22am UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798 "2022-02-14T04:22:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fsosa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/fsosa/32/4801_2.png) [@fsosa](https://discourse.pymc.io/u/fsosa)\
**Post date:** [February 14, 2022, 4:22am UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/1 "2022-02-14T04:22:49Z")

</div>

Hi everyone, I’m new here and new to Bayesian modeling. I’m working on something that involves approximate bayesian inference with a physics engine (the physics engine is the simulator). Is it possible to use PyMC to perform ABC SMC with an external (i.e. not written in PyMC) simulator?

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:** [February 14, 2022, 7:23am UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/2 "2022-02-14T07:23:06Z")

</div>

Yes, you can take a look at [Sequential Monte Carlo - Approximate Bayesian Computation — PyMC3 3.11.4 documentation](https://docs.pymc.io/en/v3/pymc-examples/examples/samplers/SMC-ABC_Lotka-Volterra_example.html) which the simulator is written in `numpy` (any Python Callable would work similarly)

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [February 14, 2022, 2:52pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/3 "2022-02-14T14:52:27Z")

</div>

And [here](https://docs.pymc.io/en/latest/api/distributions/generated/pymc.Simulator.html#pymc.Simulator) is the `pm.Simulator()` documentation that I can never seem to find.

---

<div class="post-metadata">

**Author:** ![fsosa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/fsosa/32/4801_2.png) [@fsosa](https://discourse.pymc.io/u/fsosa)\
**Post date:** [February 14, 2022, 4:26pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/4 "2022-02-14T16:26:29Z")

</div>

Thank you @junpenglao. I saw this, originally could not get it to work, and just updated to v3.11.4 and it now works. I’m able to get my model running in the ABC SMC framework as described in that article. Unfortunately, it seems to hang after the first stage:

```auto
Initializing SMC sampler...
Sampling 2 chains in 1 job
/Users/lollipop/miniconda3/envs/pymc311/lib/python3.9/site-packages/theano/tensor/elemwise.py:826: RuntimeWarning: divide by zero encountered in log1p
  variables = ufunc(*ufunc_args, **ufunc_kwargs)
Stage: 0 Beta: 0.006
INFO:pymc3:Stage: 0 Beta: 0.006

```

After this first stage, which takes around 2-5 minutes on my MacBook Pro M1 Max, it hasn’t nudged after \>20 minutes. I’m new to PyMC and there is very little documentation on ABC with PyMC (as @cluhmann alluded to with the `pm.Simulator()`, so excuse my initial lack of detail.

Is there a way to get a verbose output from either `pm.Simulator` or `pm.sample_smc`? Or, is there a check or a routine I can perform to see what’s going on?

Thanks again!

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [February 14, 2022, 5:08pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/5 "2022-02-14T17:08:29Z")

</div>

I think the Simulator is just struggling a lot. You can try to use a more forgiving epsilon / distance metric perhaps?

---

<div class="post-metadata">

**Author:** ![fsosa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/fsosa/32/4801_2.png) [@fsosa](https://discourse.pymc.io/u/fsosa)\
**Post date:** [February 14, 2022, 6:13pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/6 "2022-02-14T18:13:15Z")

</div>

More forgiving epsilon would be, in this case, setting `epsilon=<1`? So we would be, in effect, dampening the divergence?

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [February 14, 2022, 6:26pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/7 "2022-02-14T18:26:38Z")

</div>

The other way. `epsilon>1` makes the distance metric less peaked

---

<div class="post-metadata">

**Author:** ![fsosa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/fsosa/32/4801_2.png) [@fsosa](https://discourse.pymc.io/u/fsosa)\
**Post date:** [February 14, 2022, 6:40pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/8 "2022-02-14T18:40:22Z")

</div>

Thanks. I’ll report back with results. In the meantime, can I ask if I can use `pm.Simulator()` with other sampling methods (e.g. `pm.sample`)? I’m having a hard time understanding the control flow of these model builds and exactly what pieces are composable and what pieces aren’t (e.g. if I can only use `pm.Simulator` with `pm.sample_smc`). If I need to make a new post asking this specific question, let me know! 🙂

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [February 14, 2022, 7:23pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/9 "2022-02-14T19:23:18Z")

</div>

You can use Simulator with Metropolis + `pm.sample` in the next release v4 (we have a beta out already).

However, Simulators/ABC have very little empirical support outside of SMC, so I wouldn’t endorse it without good reasons 🙂

---

<div class="post-metadata">

**Author:** ![amanda.azevedo](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@amanda.azevedo](https://discourse.pymc.io/u/amanda.azevedo)\
**Post date:** [May 5, 2022, 5:37pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/10 "2022-05-05T17:37:13Z")

</div>

Hello everyone,

I am new here and also new to Bayesian modeling. I am working on a project that needs to use approximate Bayesian computation. I have seen many examples in the documentation and elsewhere using SMC-ABC in PyMC3, but none for the latest release.

I tried to implement a simple example using SMC with pm.sample\_mc running in PyMC 4.0.0b6, but without success. From the new release, I could not understand whether this is my lack of knowledge of this framework or if it’s not an implemented feature.

Is SMC-ABC implemented in PyMC 4.0.0?

I thought my question would be relevant to this thread, but if necessary I can make a new post asking specifically about SMC in v4.

Thanks!

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [May 5, 2022, 7:23pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/11 "2022-05-05T19:23:49Z")

</div>

> [@amanda.azevedo](#):
>
> Is SMC-ABC implemented in PyMC 4.0.0?

Yup. If I recall correctly, there was quite a bit of work on it last summer in preparation for v4. However, the relevant example notebooks (e.g., [this one](https://docs.pymc.io/projects/examples/en/latest/samplers/SMC-ABC_Lotka-Volterra_example.html)) have not yet been updated. But doing so is on the to do list. I might suggest opening a new topic and maybe we can grab one of the relevant developers to provide some guidance.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [May 6, 2022, 7:58am UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/12 "2022-05-06T07:58:55Z")

</div>

@amanda.azevedo ABC is implemented in PyMC 4 whenever you use a `Simulator` in your Model. You can just call `sample_smc` without any special arguments and it will work out of the box. Note that the way we define Simulators changed slightly: [pymc.Simulator — PyMC dev documentation](https://docs.pymc.io/en/latest/api/distributions/generated/pymc.Simulator.html)

---

<div class="post-metadata">

**Author:** ![amanda.azevedo](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@amanda.azevedo](https://discourse.pymc.io/u/amanda.azevedo)\
**Post date:** [May 6, 2022, 9:57am UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/13 "2022-05-06T09:57:52Z")

</div>

Thanks @cluhmann, I will open a new topic on this. 🙂

@ricardoV94 thanks for the link. However, I am not able to run even the example on this page. I will then describe the error in the new topic.

---

<div class="post-metadata">

**Author:** ![karthur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/karthur/32/4470_2.png) [@karthur](https://discourse.pymc.io/u/karthur)\
**Post date:** [May 6, 2022, 2:27pm UTC](https://discourse.pymc.io/t/does-pymc-work-for-abc-smc/8798/14 "2022-05-06T14:27:15Z")

</div>

I have an example of ABC in PyMC(3) here:

> **[Approximate Bayesian computation in Python](http://karthur.org/2021/abc-in-python.html)**
>
> The PyMC library offers a solid foundation for probabilistic programming and Bayesian inference in Python, but it has some drawbacks. Although the API is robust, it has changed frequently along with the shifting momentum of the entire PyMC project...

The `Simulator` class is not used; instead, a custom Theano operator for calculating the likelihood from the black-box model is defined.
