You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: myst_nbs/samplers/SMC-ABC_Lotka-Volterra_example.myst.md
+36-16
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,25 @@ jupytext:
6
6
format_version: 0.13
7
7
jupytext_version: 1.13.7
8
8
kernelspec:
9
-
display_name: Python (PyMC3 Dev)
9
+
display_name: Python 3.9.7 ('base')
10
10
language: python
11
-
name: pymc3-dev
11
+
name: python3
12
12
---
13
13
14
+
(ABC_introduction)=
15
+
# Approximate Bayesian Computation
16
+
:::{post} May 31, 2022
17
+
:tags: SMC, ABC
18
+
:category: beginner, explanation
19
+
:::
20
+
14
21
```{code-cell} ipython3
15
22
import arviz as az
16
23
import matplotlib.pyplot as plt
17
24
import numpy as np
18
-
import pymc3 as pm
25
+
import pymc as pm
26
+
27
+
print(f"Running on PyMC v{pm.__version__}")
19
28
```
20
29
21
30
```{code-cell} ipython3
@@ -69,8 +78,8 @@ data = np.random.normal(loc=0, scale=1, size=1000)
69
78
Clearly under normal circumstances using a Gaussian likelihood will do the job very well. But that would defeat the purpose of this example, the notebook would end here and everything would be very boring. So, instead of that we are going to define a simulator. A very straightforward simulator for normal data is a pseudo random number generator, in real life our simulator will be most likely something fancier.
70
79
71
80
```{code-cell} ipython3
72
-
def normal_sim(a, b):
73
-
return np.random.normal(a, b, 1000)
81
+
def normal_sim(rng, a, b, size=1000):
82
+
return rng.normal(a, b, size=size)
74
83
```
75
84
76
85
Defining an ABC model in PyMC3 is in general, very similar to defining other PyMC3 models. The two important differences are: we need to define a `Simulator`_distribution_ and we need to use `sample_smc` with `kernel="ABC"`. The `Simulator` works as a generic interface to pass the synthetic data generating function (_normal_sim_ in this example), its parameters, the observed data and optionally a distance function and a summary statistics. In the following code we are using the default distance, `gaussian_kernel`, and the `sort` summary_statistic. As the name suggests `sort` sorts the data before computing the distance.
@@ -83,8 +92,8 @@ with pm.Model() as example:
83
92
b = pm.HalfNormal("b", sigma=1)
84
93
s = pm.Simulator("s", normal_sim, params=(a, b), sum_stat="sort", epsilon=1, observed=data)
Judging by `plot_trace` the sampler did its job very well, which is not surprising given this is a very simple model. Anyway, it is always reassuring to look at a flat rank plot :-)
The posterior predictive check shows that we have an overall good fit, but the synthetic data has heavier tails than the observed one. You may want to decrease the value of epsilon, and see if you can get a tighter fit.
0 commit comments