Skip to content

Commit 658c689

Browse files
authored
Merge pull request #43 from arviz-devs/arviz
Remove `plot_results` from docs, update arviz
2 parents 26d769f + 4a5d2ba commit 658c689

File tree

7 files changed

+26
-47
lines changed

7 files changed

+26
-47
lines changed

README.md

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# Simulation Based Calibration
1+
# Simuk
22

3-
A [PyMC](http://docs.pymc.io) and [Bambi](https://bambinos.github.io/bambi/) implementation of the algorithms from:
4-
5-
Sean Talts, Michael Betancourt, Daniel Simpson, Aki Vehtari, Andrew Gelman: “Validating Bayesian Inference Algorithms with Simulation-Based Calibration”, 2018; [arXiv:1804.06788](http://arxiv.org/abs/1804.06788)
6-
7-
Many thanks to the authors for providing open, reproducible code and implementations in `rstan` and `PyStan` ([link](https://github.com/seantalts/simulation-based-calibration)).
3+
Simuk is a Python library for simulation-based calibration (SBC) and the generation of synthetic data.
4+
Simulation-Based Calibration is a method for validating Bayesian inference by checking whether the posterior distributions align with the expected theoretical results derived from the prior.
85

6+
Simuk works with [PyMC](http://docs.pymc.io), [Bambi](https://bambinos.github.io/bambi/) and [NumPyro](https://num.pyro.ai/en/latest/index.html) models.
97

108
## Installation
119

@@ -22,6 +20,7 @@ pip install simuk
2220
```python
2321
import numpy as np
2422
import pymc as pm
23+
from arviz_plots import plot_ecdf_pit
2524

2625
data = np.array([28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0])
2726
sigma = np.array([15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0])
@@ -48,35 +47,15 @@ pip install simuk
4847
should be close to uniform and within the oval envelope.
4948

5049
```python
51-
sbc.plot_results()
50+
plot_ecdf_pit(sbc.simulations,
51+
visuals={"xlabel":False},
52+
);
5253
```
5354

5455
![Simulation based calibration plots, ecdf](ecdf.png)
5556

57+
## References
5658

57-
## What is going on here?
58-
59-
The [paper on the arXiv](http://arxiv.org/abs/1804.06788) is very well written, and explains the algorithm quite well.
60-
61-
Morally, the example below is exactly what this library does, but it generalizes to more complicated models:
62-
63-
```python
64-
with pm.Model() as model:
65-
x = pm.Normal('x')
66-
pm.Normal('y', mu=x, observed=y)
67-
```
68-
69-
Then what this library does is compute
70-
71-
```python
72-
with my_model():
73-
prior_samples = pm.sample_prior_predictive(num_trials)
74-
75-
simulations = {'x': []}
76-
for idx in range(num_trials):
77-
y_tilde = prior_samples['y'][idx]
78-
x_tilde = prior_samples['x'][idx]
79-
with model(y=y_tilde):
80-
idata = pm.sample()
81-
simulations['x'].append((idata.posterior['x'] < x_tilde).sum())
82-
```
59+
- Talts, S., Betancourt, M., Simpson, D., Vehtari A., and Gelman A. (2018). “Validating Bayesian Inference Algorithms with Simulation-Based Calibration.” `arXiv:1804.06788 <https://doi.org/10.48550/arXiv.1804.06788>`_.
60+
- Modrák, M., Moon, A, Kim, S., Bürkner, P., Huurre, N., Faltejsková, K., Gelman A and Vehtari, A.(2023). "Simulation-based calibration checking for Bayesian computation: The choice of test quantities shapes sensitivity. Bayesian Analysis, advance publication, DOI: `10.1214/23-BA1404 <https://projecteuclid.org/journals/bayesian-analysis/volume--1/issue--1/Simulation-Based-Calibration-Checking-for-Bayesian-Computation--The-Choice/10.1214/23-BA1404.full>`_
61+
- Säilynoja, T., Marvin Schmitt, Paul-Christian Bürkner and Aki Vehtari (2025). "Posterior SBC: Simulation-Based Calibration Checking Conditional on Data" `arXiv:2502.03279 <https://doi.org/10.48550/arXiv.2502.03279>`_.

docs/examples/gallery/sbc.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ We expect a uniform distribution, the gray envelope corresponds to the 94% credi
6262
```{jupyter-execute}
6363
6464
plot_ecdf_pit(sbc.simulations,
65-
pc_kwargs={'col_wrap':4},
66-
plot_kwargs={"xlabel":False},
67-
)
65+
visuals={"xlabel":False},
66+
);
6867
```
6968

7069
:::::
@@ -131,7 +130,7 @@ def eight_schools_cauchy_prior(J, sigma, y=None):
131130
nuts_kernel = NUTS(eight_schools_cauchy_prior)
132131
```
133132

134-
Pass the model to the `SBC` class, set the number of simulations to 100, and run the simulations. For numpyro model,
133+
Pass the model to the `SBC` class, set the number of simulations to 100, and run the simulations. For numpyro model,
135134
we pass in the ``data_dir`` parameter.
136135

137136
```{jupyter-execute}
@@ -147,8 +146,7 @@ To compare the prior and posterior distributions, we will plot the results.
147146
We expect a uniform distribution, the gray envelope corresponds to the 94% credible interval.
148147

149148
```{jupyter-execute}
150-
plot_ecdf_pit(sbc.simulations,
151-
pc_kwargs={'col_wrap':4},
152-
plot_kwargs={"xlabel":False}
153-
)
149+
plot_ecdf_pit(sbc.simulations,
150+
visuals={"xlabel":False},
151+
);
154152
```

docs/index.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ In our case, we will take a PyMC model and pass it into our ``SBC`` class.
1818

1919
.. code-block:: python
2020
21+
from arviz_plots import plot_ecdf_pit
2122
import numpy as np
2223
import pymc as pm
2324
@@ -44,7 +45,9 @@ Plot the empirical CDF to compare the differences between the prior and posterio
4445

4546
.. code-block:: python
4647
47-
sbc.plot_results()
48+
plot_ecdf_pit(sbc.simulations,
49+
visuals={"xlabel":False},
50+
);
4851
4952
The lines should be nearly uniform and fall within the oval envelope. It suggests that the prior and posterior distributions
5053
are properly aligned and that there are no significant biases or issues with the model.
@@ -82,5 +85,6 @@ are properly aligned and that there are no significant biases or issues with the
8285
References
8386
----------
8487

85-
- Talts, Sean, Michael Betancourt, Daniel Simpson, Aki Vehtari, and Andrew Gelman. 2018. “Validating Bayesian Inference Algorithms with Simulation-Based Calibration.” `arXiv:1804.06788 <https://doi.org/10.48550/arXiv.1804.06788>`_.
86-
- Modrák, M., Moon, A. H., Kim, S., Bürkner, P., Huurre, N., Faltejsková, K., … & Vehtari, A. (2023). Simulation-based calibration checking for Bayesian computation: The choice of test quantities shapes sensitivity. Bayesian Analysis, advance publication, DOI: `10.1214/23-BA1404 <https://projecteuclid.org/journals/bayesian-analysis/volume--1/issue--1/Simulation-Based-Calibration-Checking-for-Bayesian-Computation--The-Choice/10.1214/23-BA1404.full>`_
88+
- Talts, S., Betancourt, M., Simpson, D., Vehtari A., and Gelman A. (2018). “Validating Bayesian Inference Algorithms with Simulation-Based Calibration.” `arXiv:1804.06788 <https://doi.org/10.48550/arXiv.1804.06788>`_.
89+
- Modrák, M., Moon, A, Kim, S., Bürkner, P., Huurre, N., Faltejsková, K., Gelman A and Vehtari, A.(2023). "Simulation-based calibration checking for Bayesian computation: The choice of test quantities shapes sensitivity. Bayesian Analysis, advance publication, DOI: `10.1214/23-BA1404 <https://projecteuclid.org/journals/bayesian-analysis/volume--1/issue--1/Simulation-Based-Calibration-Checking-for-Bayesian-Computation--The-Choice/10.1214/23-BA1404.full>`_
90+
- Säilynoja, T., Marvin Schmitt, Paul-Christian Bürkner and Aki Vehtari (2025). "Posterior SBC: Simulation-Based Calibration Checking Conditional on Data" `arXiv:2502.03279 <https://doi.org/10.48550/arXiv.2502.03279>`_.

ecdf.png

-28.1 KB
Loading

hist.png

-91.2 KB
Binary file not shown.

requirements-docs.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pydata-sphinx-theme>=0.6.3
22
myst-nb
33
pymc>=5.20.1
44
bambi>=0.15.0
5-
arviz_plots @ git+https://github.com/arviz-devs/arviz-plots@main
5+
arviz_plots>=0.6.0
66
sphinx>=4
77
sphinx-copybutton
88
sphinx_tabs

simuk/sbc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ class SBC:
7171
7272
sbc = SBC(model)
7373
sbc.run_simulations()
74-
sbc.plot_results()
75-
7674
"""
7775

7876
def __init__(self, model, num_simulations=1000, sample_kwargs=None, seed=None, data_dir=None):

0 commit comments

Comments
 (0)