Skip to main content

Batched Emulator Sampling with Tensorflow

Project description

BEST

(Batched Emulator Sampling with TensorFlow)

A TensorFlow-based inference framework for high-performance Markov Chain Monte Carlo (MCMC) sampling, including support for neural likelihood emulators ("client emulators") and adaptive covariance estimation.


Overview

best provides a unified interface for sampling using multiple MCMC algorithms with GPU acceleration via TensorFlow:

Supported samplers

  • Metropolis-Hastings (MH)
  • Affine Invariant Ensemble Sampler (AIES)
  • Hamiltonian Monte Carlo (HMC)
  • No-U-Turn Sampler (NUTS)
  • Metropolis Adjusted Langevin Algorithm (MALA)

Key features

  • TensorFlow / GPU acceleration
  • Automatic covariance matrix adaptation
  • Bounded parameter inference
  • Parallelized multi-chain sampling
  • Pretrained neural likelihood emulators
  • JIT compilation (XLA support)

Installation

From PyPI

pip install best-inference

From source

git clone https://github.com/AndreasNygaard/best-inference.git
cd best-inference
pip install .

Quick start

import best
import tensorflow as tf

def log_prob(x):
    return -0.5 * tf.reduce_sum(x**2, axis=-1)

sampler = best.Sampler(log_prob, bounds=([-5, -5], [5, 5]))

results = sampler.sample(
    method="hmc",
    n_steps=2000,
    n_chains=50,
    initial_distribution="uniform",
    num_burnin_steps=1000
)

print(results.samples.shape)

Sampler API

Initialisation

best.Sampler(
    log_prob_fn,
    bounds=None,
    enforce_boundaries=True,
    covmat=None,
    initial_state=None,
    n_chains=10,
    initial_distribution="repeat"
)

Sampling

results = sampler.sample(
    method="mh" | "aies" | "hmc" | "nuts" | "mala",
    n_steps=1000,
    n_chains=10,
    initial_state=None,
    initial_distribution="repeat" | "uniform" | "gaussian",
    bounds=None,
    covmat=None,
    num_burnin_steps=100,
    num_covmat_updates=None,
    sampler_kwargs={},
    burnin_kwargs={},
    get_individual_chains=True,
    jit_compile=True
)

Output

results.samples
results.acceptance_rate
results.evaluations

results.burnin_samples
results.burnin_acceptance_rates
results.burnin_evaluations
results.covmat_estimate

Client emulators

BEST includes pretrained neural likelihood emulators for cosmology-inspired inference problems.

Available models

  • lcdm
  • sterile_neutrino

Load a model

from best.client_emulators import load_model_and_scalers

log_prob_fn, lower_bounds, upper_bounds = load_model_and_scalers("lcdm")

Example: emulator-based inference

import best
from best.client_emulators import load_model_and_scalers

log_prob_fn, lower, upper = load_model_and_scalers("lcdm")

sampler = best.Sampler(log_prob_fn, bounds=(lower, upper))

results = sampler.sample(
    method="aies",
    n_steps=5000,
    n_chains=100,
    initial_distribution="uniform",
    num_burnin_steps=2000,
    num_covmat_updates=1
)

Supported Algorithms

Metropolis-Hastings (MH)

Random-walk MCMC with optional adaptive covariance scaling.

Affine Invariant Ensemble Sampler (AIES)

Efficient for highly anisotropic or correlated parameter spaces.

Hamiltonian Monte Carlo (HMC)

Gradient-based sampling with leapfrog integration.

No-U-Turn Sampler (NUTS)

Adaptive HMC variant with automatic trajectory length selection.

Metropolis Adjusted Langevin Algorithm (MALA)

Gradient-informed diffusion-based sampler.

Performance Notes

  • TensorFlow enables GPU acceleration where available
  • JIT compilation (XLA) improves performance for large chains
  • Vectorized multi-chain execution is used throughout
  • Covariance estimation is performed during burn-in when enabled

Example: Multi-sampler comparison

sampler.set_initial_state(initial_state=means, covmat=covmat, initial_distribution="gaussian")
res_aies = sampler.sample(method="aies", n_steps=5000, n_chains=100)
res_hmc  = sampler.sample(method="hmc",  n_steps=5000, n_chains=100)
res_nuts = sampler.sample(method="nuts", n_steps=5000, n_chains=100)
res_mh   = sampler.sample(method="mh",   n_steps=5000, n_chains=100)
res_mala = sampler.sample(method="mala", n_steps=5000, n_chains=100)

Requirements

  • Python ≥ 3.10
  • TensorFlow ≥ 2.17
  • TensorFlow Probability ≥ 0.24
  • NumPy
  • tf-keras
  • hypersphere-sampler

Citation

If you use this package, please cite:

Citation will be added once the accompanying paper is available (arXiv preprint in preparation).

Contributing

Contributions are welcome. ###Steps:

  • Fork repository
  • Create feature branch
  • Add tests in tests/
  • Submit pull request

License

MIT License

Copyright (c) 2026 Andreas Nygaard

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

best_inference-0.1.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

best_inference-0.1.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file best_inference-0.1.0.tar.gz.

File metadata

  • Download URL: best_inference-0.1.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for best_inference-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c0cb1c4b96574d43178a59d9091c45145f39dd8d2aff8475e527a45d42e83f77
MD5 04fbcd7f84e1d9fbecbc9244cb730a62
BLAKE2b-256 02cc5402f90a7e1528230f0a8e37a268247b1fe929669357f7465b549cbfc039

See more details on using hashes here.

File details

Details for the file best_inference-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: best_inference-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for best_inference-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5690074a257a79efea6c66dd66d49b0dbb5d06ac32b4d0c1b63ca7aaa5a7e909
MD5 9f084c7fad590821fa0d1f120780d436
BLAKE2b-256 ae5b2880119754d1ca043f5352b51efb19626558fe4650f2c8c0e9d1db0d1007

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