Skip to main content

COMPASS: A Python package for bayesian model comparison in a simulation based setting

Project description

PyPi version DOI Static Badge Static Badge Static Badge

COMPASS: Comparison Of Models using Probabilistic Assessment in Simulation-based Settings

COMPASS is a Python package designed for Bayesian Model Comparison in simulation-based settings. By comparing the predictive power of various models, it aims to identify the most suitable model for a given dataset.
It is especially suited for fields like astrophysics and computational biology, where simulation is integral to the modeling process.

Features

  • Perform Bayesian model comparison in simulation-based settings with ModelTransfuser
  • Perform Simulation-Based Inference with ScoreBasedInferenceModel

Installation

Install the package using pip:

pip install bayes-compass

Usage

Model Comparison Example

The ModelTransfuser class provides a framework for the model comparison workflow.
It uses the ScoreBasedInferenceModel class to perform the simulation-based inference, predicts the posterior distribution and samples from the Likelihood function with the inferred parameters.
With a Gaussian Kernel Density Estimator, it evaluates the observed data at the Likelihood function and computes the posterior model probabilities.

from compass import ModelTransfuser 

# Initialize the ModelTransfuser
mtf = ModelTransfuser()

# Add data from simulators
mtf.add_data(model_name="Model1", train_data=data_1, val_data=val_data_1)
mtf.add_data(model_name="Model2", train_data=data_2, val_data=val_data_2)

# Initialize ScoreBasedInferenceModels
mtf.init_models()

# Train the models
mtf.train_models()

# Compare Posterior Model Probabilities
observations = load_your_observations
mtf.compare(x=observations, err=observations_err)

stats = mtf.stats

# Plot results
mtf.plots()

mtf.stats is a dictionary containing all inferred parameters and their uncertainties, the posterior model probabilities, and the log-likelihood values for each model.

Simulation-Based Inference Model

The ScoreBasedInferenceModel is a Diffusion Model with a score predicting Transformer network.
It is able to sample from the posterior and likelihood function by utalizing the attention mechanism of the Transformer architecture.
The model is also able to handle observational uncertainties and is designed to run on all available GPUs.

from compass import ScoreBasedInferenceModel as SBIm

sbimodel = SBIm(nodes_size=nodes_size, sigma=sigma, depth=depth, hidden_size=hidden_size, num_heads=num_heads, mlp_ratio=mlp_ratio)

sbimodel.train(train_data, val_data=val_data, path=path, device="cuda")

nodes_size - The number of parameters $\theta$ and the number of data points $x$.
sigma - The noise level of the initial noise distribution of the diffusion model.
depth - The number of layers in the Transformer
hidden_size - The size of the embedding in the Transformer
num_heads - The number of attention heads in the Transformer
mlp_ratio - The ratio of the hidden size to the number of Nodes in the MLPs in the Transformer

To sample from the posterior distribution, you can use the sample() function and provide the observations x and the observational uncertainties err (optional):

# Sample from the posterior distribution
posterior_samples = sbimodel.sample(x=observations, err=observations_err, timesteps=100)

To sample from the Likelihood function, you can again use the sample() function and provide the Maximum-A-Posteriori $\hat\theta$ values in theta and the standard deviation err (optional):

# Samlpe from the likelihood function
likelihood_samples = sbimodel.sample_likelihood(theta=theta_hat, err=std_theta_hat, timesteps=100)

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve this package.


Project details


Download files

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

Source Distribution

bayes_compass-0.1.5.tar.gz (256.3 kB view details)

Uploaded Source

Built Distribution

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

bayes_compass-0.1.5-py3-none-any.whl (39.3 kB view details)

Uploaded Python 3

File details

Details for the file bayes_compass-0.1.5.tar.gz.

File metadata

  • Download URL: bayes_compass-0.1.5.tar.gz
  • Upload date:
  • Size: 256.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for bayes_compass-0.1.5.tar.gz
Algorithm Hash digest
SHA256 928956588c0007d512bf319537dba3bc2e94a5d92bc52321f4fd73384f8768e2
MD5 97622d26fd82526cb6ae270872b1c928
BLAKE2b-256 797ef0c70e85774ff8865498b0687ee22877957de21a28312b19349865ea1ab4

See more details on using hashes here.

File details

Details for the file bayes_compass-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: bayes_compass-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 39.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for bayes_compass-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 dd1662d64716999e037e9f27b7ddb8879841ece22508ad09c659267de1a8e759
MD5 e44c56f0dc2572abeb1ffbc3fd4fe4d4
BLAKE2b-256 315b722d7fdd0a4a31c0a098f0fafe623eb091b7236d87f5add0eda63ccf88e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page