Skip to main content

Simulation Based Calibration

A PyMC and Bambi implementation of the algorithms from:

Sean Talts, Michael Betancourt, Daniel Simpson, Aki Vehtari, Andrew Gelman: “Validating Bayesian Inference Algorithms with Simulation-Based Calibration”, 2018; arXiv:1804.06788

Many thanks to the authors for providing open, reproducible code and implementations in rstan and PyStan (link).

Installation

May be pip installed from github:

pip install git+https://github.com/arviz-devs/simulation_based_calibration

Quickstart

  1. Define a PyMC or Bambi model. For example, the centered eight schools model:

    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() as centered_eight:
        obs = pm.MutableData("obs", data)
        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=obs)
    
  2. Pass the model to the SBC class, 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': 25, 'tune': 50})
    
    sbc.run_simulations()
    
    79%|███████▉  | 79/100 [05:36<01:29,  4.27s/it]
    
  3. Plot the empirical CDF for the difference between prior and posterior. The lines should be close to uniform and within the oval envelope.

    sbc.plot_results()
    

Simulation based calibration plots, ecdf

What is going on here?

The paper on the arXiv is very well written, and explains the algorithm quite well.

Morally, the example below is exactly what this library does, but it generalizes to more complicated models:

with pm.Model() as model:
    x = pm.Normal('x')
    pm.Normal('y', mu=x, observed=y)

Then what this library does is compute

with my_model():
    prior_samples = pm.sample_prior_predictive(num_trials)

simulations = {'x': []}
for idx in range(num_trials):
    y_tilde = prior_samples['y'][idx]
    x_tilde = prior_samples['x'][idx]
    with model(y=y_tilde):
        idata = pm.sample()
    simulations['x'].append((idata.posterior['x'] < x_tilde).sum())

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simuk-0.1.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simuk-0.1.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file simuk-0.1.1.tar.gz.

File metadata

  • Download URL: simuk-0.1.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for simuk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c2bf3108055720d64900c726b3f6498fb02a7e4f698c4c1fb531f4fd339efc45
MD5 24fdedabf2347c55c5ff26cee32cdc9b
BLAKE2b-256 857c8516e1eaf4a637c2cfe73c075d22873b251ba28feae24f0416e83c9e5472

See more details on using hashes here.

Provenance

The following attestation bundles were made for simuk-0.1.1.tar.gz:

Publisher: publish-to-test-pypi.yml on arviz-devs/simuk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simuk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: simuk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for simuk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 04a1c2abb9ac73ea619caad8baffef461bdd9febdb2fc3f04fce766ed35687b8
MD5 7e83196b7b46550e36cdf84bdc5c9ad3
BLAKE2b-256 7357e42f7f5ebb404587527b7ee592907fb2a5a88297c3186e8f99ed522dc27c

See more details on using hashes here.

Provenance

The following attestation bundles were made for simuk-0.1.1-py3-none-any.whl:

Publisher: publish-to-test-pypi.yml on arviz-devs/simuk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.3.0

2 files

0.2.0

2 files

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page