# Approximate Bayesian Computation example bug?

**URL:** <https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501>\
**Category:** Questions\
**Created:** [July 22, 2020, 5:52pm UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501 "2020-07-22T17:52:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgamble](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jgamble](https://discourse.pymc.io/u/jgamble)\
**Post date:** [July 22, 2020, 5:52pm UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/1 "2020-07-22T17:52:14Z")

</div>

Hi all,

I am trying to use the SMC sampler for Approximate Bayesian Computation, so I started with the great example [here](https://docs.pymc.io/notebooks/SMC-ABC_Lotka-Volterra_example.html).

However, in PyMC3 v. 2.9.3 (checked on both Linux and MacOS), I do the following (straight from the example, as far as I can tell):

```auto
import numpy as np
import pymc3 as pm
import matplotlib.pyplot as plt
import arviz as az

data = np.random.normal(loc=0, scale=1, size=1000)
def normal_sim(a, b):
    return np.sort(np.random.normal(a, b, 1000))

with pm.Model() as example:
    a = pm.Normal('a', mu=0, sd=5)
    b = pm.HalfNormal('b', sd=1)
    s = pm.Simulator('s', normal_sim,observed=np.sort(data))
    trace_example = pm.sample_smc(kernel="ABC", epsilon=0.1)

```

And I get the following error message:

```auto
Sample initial stage: ...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-1fbce33887a0> in <module>
     11 b = pm.HalfNormal('b', sd=1)
     12 s = pm.Simulator('s', normal_sim,observed=np.sort(data))
---> 13 trace_example = pm.sample_smc(kernel="ABC", epsilon=0.1)

~/opt/anaconda3/envs/surrogate-modelling/lib/python3.8/site-packages/pymc3/smc/sample_smc.py in sample_smc(draws, kernel, n_steps, parallel, start, cores, tune_steps, p_acc_rate, threshold, epsilon, dist_func, sum_stat, progressbar, model, random_seed)
    150 stage = 0
    151 smc.initialize_population()
--> 152 smc.setup_kernel()
    153 smc.initialize_logp()
    154 

~/opt/anaconda3/envs/surrogate-modelling/lib/python3.8/site-packages/pymc3/smc/smc.py in setup_kernel(self)
    140 simulator.observations,
    141 simulator.distribution.function,
--> 142 [v.name for v in simulator.distribution.params],
    143 self.model,
    144 self.var_info,

TypeError: 'NoneType' object is not iterable

```

Am I doing something incorrectly? My install works fine for standard PyMC sampling using NUTS. Many thanks!

---

<div class="post-metadata">

**Author:** ![jgamble](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jgamble](https://discourse.pymc.io/u/jgamble)\
**Post date:** [July 23, 2020, 12:16am UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/2 "2020-07-23T00:16:07Z")

</div>

I managed to make this work by digging through the tests - the syntax from the documentation example is indeed incorrect or outdated. The following code works as expected:

```auto
import numpy as np
import pymc3 as pm
import matplotlib.pyplot as plt
import arviz as az

data = np.random.normal(loc=0, scale=1, size=1000)

def normal_sim(a, b):
    return np.random.normal(a, b, 1000)

with pm.Model() as model:
    a = pm.Normal("a", mu=0, sd=5)
    b = pm.HalfNormal("b", sd=2)
    s = pm.Simulator("s", normal_sim, params=(a, b), observed=data)
    trace = pm.sample_smc(draws=1000, kernel="ABC", sum_stat="sorted", epsilon=1)

```

It looks like that @aloctavodia has recently updated this on the main branch.

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [July 23, 2020, 2:04am UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/3 "2020-07-23T02:04:47Z")

</div>

Sorry for the inconvenience. You are right, I am working on improving smc-abc. I hope to have a much more robust and flexible version ready for the next release.

---

<div class="post-metadata">

**Author:** ![nathendel](https://avatars.discourse-cdn.com/v4/letter/n/b77776/32.png) [@nathendel](https://discourse.pymc.io/u/nathendel)\
**Post date:** [August 29, 2020, 12:59am UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/4 "2020-08-29T00:59:42Z")

</div>

Hi, when I try to run this code, I’m getting errors on the pm.Simulator line. “TypeError: **init** () got an unexpected keyword argument ‘params’”. I’m using pymc3 version 3.8. I cannot find any documentation on pm.Simulator. Any ideas what’s causing this? Thanks!

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [August 29, 2020, 1:14am UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/5 "2020-08-29T01:14:52Z")

</div>

Hi,

SMC-ABC is still experimental and changing fast. That argument was introduced in 3.9. Update to the last stable release or to masters.

---

<div class="post-metadata">

**Author:** ![nathendel](https://avatars.discourse-cdn.com/v4/letter/n/b77776/32.png) [@nathendel](https://discourse.pymc.io/u/nathendel)\
**Post date:** [August 29, 2020, 1:30am UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/6 "2020-08-29T01:30:00Z")

</div>

Thanks for the quick reply. I’m getting a message on my terminal saying that 3.8 is the latest version. Is that incorrect, and do you know how I can install 3.9? Thanks!

---

<div class="post-metadata">

**Author:** ![jgamble](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jgamble](https://discourse.pymc.io/u/jgamble)\
**Post date:** [August 29, 2020, 2:08am UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/7 "2020-08-29T02:08:12Z")

</div>

@nathendel, how are you installing pymc3? The [latest release on PyPI](https://pypi.org/project/pymc3/) is 3.9.3, so (conflicts aside), I would expect `pip install pymc3` to install 3.9.3.

---

<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:** [September 1, 2020, 1:30pm UTC](https://discourse.pymc.io/t/approximate-bayesian-computation-example-bug/5501/8 "2020-09-01T13:30:28Z")

</div>

@nathendel I’m guessing you’re installing with conda. So, you can do `conda install pymc3 -c conda-forge` to install via conda-forge (the default conda channel still hasn’t picked up 3.9.3)
