Approximate Bayesian Computation example bug?

Hi all,

I am trying to use the SMC sampler for Approximate Bayesian Computation, so I started with the great example here.

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):

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:

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!

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:

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.

1 Like

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.

3 Likes

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!

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.

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!

@nathendel, how are you installing pymc3? The latest release on PyPI is 3.9.3, so (conflicts aside), I would expect pip install pymc3 to install 3.9.3.

@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)