Simuk
Simuk is a Python library for simulation-based calibration (SBC) and the generation of synthetic data.
Prior Simulation-Based Calibration (Prior SBC) is a method for validating Bayesian inference by checking whether the posterior distributions align with the expected theoretical results derived from the prior.
Posterior Simulation-Based Calibration (Posterior SBC) is a method for validating Bayesian inference by checking whether the posterior distributions conditioned on the augmented data (original + posterior predictive) align with the expected theoretical results derived from the posterior.
For Prior SBC, Simuk works with PyMC, Bambi and NumPyro models. For Posterior SBC, Simuk only works with PyMC models for now.
Installation
May be pip installed from github:
pip install simuk
Quickstart
Prior SBC
-
Define a PyMC or Bambi model. For example, the centered eight schools model:
import numpy as np import pymc as pm from arviz_plots import plot_ecdf_pit data = np.array([28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0]) sigma = np.array([15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0]) with pm.Model() as centered_eight: mu = pm.Normal('mu', mu=0, sigma=5) tau = pm.HalfCauchy('tau', beta=5) theta = pm.Normal('theta', mu=mu, sigma=tau, shape=8) y_obs = pm.Normal('y', mu=theta, sigma=sigma, observed=data)
-
Pass the model to the
SBCclass, and run the simulations. This will take a while, as it is running the model many times.sbc = SBC(centered_eight, num_simulations=100, # ideally this should be higher, like 1000 sample_kwargs={'draws': 100, 'tune': 100}) sbc.run_simulations()
79%|███████▉ | 79/100 [05:36<01:29, 4.27s/it]
-
Plot the empirical CDF for the difference between prior and posterior. The lines should be close to uniform and within the oval envelope.
plot_ecdf_pit(sbc.simulations, visuals={"xlabel":False}, );
We see that due to the funnel neck in the eight schools model, the inference algorithm is not well-calibrated, as indicated by the red points.
Posterior SBC
Posterior SBC evaluates validity locally, conditional on observed data. It is
currently implemented for PyMC. This requires storing observed data in
pm.Data containers, using dims instead of static shapes, and resizing
covariates and coords in an update_data callback to match the augmented data.
-
Define the model with
pm.Dataanddims:import numpy as np import pymc as pm data = np.array([28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0]) sigma = np.array([15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0]) with pm.Model(coords={"school": np.arange(8)}) as centered_eight: school_idx = pm.Data("school_idx", np.arange(8)) y_data = pm.Data("y_data", data) sigma_data = pm.Data("sigma_data", sigma) mu = pm.Normal("mu", mu=0, sigma=5) tau = pm.HalfCauchy("tau", beta=5) theta = pm.Normal("theta", mu=mu, sigma=tau, dims="school") y_obs = pm.Normal("y", mu=theta[school_idx], sigma=sigma_data, observed=y_data)
-
Sample once to obtain the original trace:
with centered_eight: idata = pm.sample(progressbar=False)
-
Define
update_datato resize covariates and run Posterior SBC:import simuk from arviz_plots import plot_ecdf_pit def update_data(model, augmented_data, simulation_idx): with model: pm.set_data({ "sigma_data": np.concatenate([sigma, sigma]), "school_idx": np.concatenate([np.arange(8), np.arange(8)]) }) post_sbc = simuk.SBC( centered_eight, method="posterior", trace=idata, update_data=update_data, num_simulations=50, sample_kwargs={"draws": 100, "tune": 100}, progress_bar=False ) post_sbc.run_simulations() plot_ecdf_pit(post_sbc.simulations, group="posterior_sbc", visuals={"xlabel": False})
We see that the funnel neck in the eight schools model is avoided and the inference algorithm is well-calibrated locally for the observed data, as indicated by the absence of red points.
References
- Talts, S., Betancourt, M., Simpson, D., Vehtari A., and Gelman A. (2018). Validating Bayesian Inference Algorithms with Simulation-Based Calibration.
- 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.
- Säilynoja, T., Marvin Schmitt, Paul-Christian Bürkner and Aki Vehtari (2025). Posterior SBC: Simulation-Based Calibration Checking Conditional on Data.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simuk-0.3.0.tar.gz.
File metadata
- Download URL: simuk-0.3.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5d80a0dd52c443e04c048805771ad1b81a1ff5a850c66f77a11e0dca14f8c14
|
|
| MD5 |
96c7e85995219726e43ac139207e3c2c
|
|
| BLAKE2b-256 |
c472e217ed7de393d313f02625f1e1cc138f323d058a97d55cfee03d8ec719b7
|
Provenance
The following attestation bundles were made for simuk-0.3.0.tar.gz:
Publisher:
publish-to-test-pypi.yml on arviz-devs/simuk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simuk-0.3.0.tar.gz -
Subject digest:
a5d80a0dd52c443e04c048805771ad1b81a1ff5a850c66f77a11e0dca14f8c14 - Sigstore transparency entry: 2142698180
- Sigstore integration time:
-
Permalink:
arviz-devs/simuk@08ba511bbc7f5c2c40e533bbf94aa1de3507dfd6 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/arviz-devs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-test-pypi.yml@08ba511bbc7f5c2c40e533bbf94aa1de3507dfd6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file simuk-0.3.0-py3-none-any.whl.
File metadata
- Download URL: simuk-0.3.0-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d9289fdf3f441eedf8d08ff3a03ef8b786c28199f326b38be91d8ff606f3df1
|
|
| MD5 |
47e7d9f127ded6b8a266c93118048a9e
|
|
| BLAKE2b-256 |
307c93fa5bc3605d73e9cc42eeb8e26b4118fe716fd6c069503416d43c70f6ac
|
Provenance
The following attestation bundles were made for simuk-0.3.0-py3-none-any.whl:
Publisher:
publish-to-test-pypi.yml on arviz-devs/simuk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simuk-0.3.0-py3-none-any.whl -
Subject digest:
7d9289fdf3f441eedf8d08ff3a03ef8b786c28199f326b38be91d8ff606f3df1 - Sigstore transparency entry: 2142698196
- Sigstore integration time:
-
Permalink:
arviz-devs/simuk@08ba511bbc7f5c2c40e533bbf94aa1de3507dfd6 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/arviz-devs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-test-pypi.yml@08ba511bbc7f5c2c40e533bbf94aa1de3507dfd6 -
Trigger Event:
release
-
Statement type: