Skip to main content

Simulated Annealing ABC in MLX.

Project description

sabc

ci version

sabc performs likelihood-free Bayesian inference (Approximate Bayesian Computation) via Simulated Annealing. The annealing loop — empirical-CDF distance transform, importance resampling, epsilon schedule, and a Differential-Evolution MCMC kernel — runs in a compiled nanobind extension (sabc._core), while models, priors, and the entry point live in Python and exchange mlx.core.arrays with the C++ core zero-copy via DLPack.

Quickstart

Infer the location of a 2-D model whose summary statistics are observed at [1, -1]:

import mlx.core as mx

import sabc
from sabc import distributions as dist


def simulator(theta):
    return theta + mx.random.normal(theta.shape) * 0.1

observed = mx.array([1.0, -1.0])

prior = dist.JointDistributionNamed(
    dict(theta=dist.Normal(mx.zeros(2), mx.ones(2) * 3.0))
)

posterior = sabc.run(
    simulator,
    prior=prior,
    observed=observed,
    n_particles=2000,
    n_simulation=200_000,
    schedule=sabc.SingleEps(v=1.0),
    proposal=sabc.DiffEvolution(),
    distance="abs",
    key=mx.random.key(0),
)

print(posterior.samples.mean(axis=0))      # ~ [1, -1]

sabc.run returns a Posterior with samples, u, rho, and the epsilon_history / u_history traces.

Priors

Priors are built from MLX-native, TFP-style distributions:

prior = dist.JointDistributionNamed(dict(
    a=dist.Normal(mx.zeros(1), mx.ones(1)),
    b=lambda a: dist.Normal(a, mx.ones(1)),   # p(b | a)
))

Examples

Self-contained examples can be found in examples.

Installation

Requires Python ≥ 3.11 and uv. MLX targets Apple Silicon (Metal); Linux support is experimental.

uv sync --all-extras

Development

The project uses uv, scikit-build-core (CMake/C++23), ruff for Python, and clang-format + cpplint for C++. The Makefile wraps the common tasks:

make check    # cpp checks
make tests    # run the tests
make format   # autoformat
make lints    # lint

Install the git hooks once (the commit-msg type is needed for gitlint):

uv run pre-commit install --hook-type pre-commit --hook-type commit-msg

The hooks then run automatically on changed files at commit time. To run every hook against the whole repository (useful after adding a hook or before a PR):

uv run pre-commit run --all-files

Citing SABC

If you find this package relevant to your research, please consider citing:

@article{albert2025simulated,
  title={Simulated Annealing ABC with multiple summary statistics},
  author={Albert, Carlo and Ulzega, Simone and Dirmeier, Simon and Scheidegger, Andreas and Bassi, Alberto and Mira, Antonietta},
  journal={arXiv preprint arXiv:2505.23261},
  year={2025}
}

Author

Simon Dirmeier simd23 @ pm me

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

sabc-0.2.0.tar.gz (125.6 kB view details)

Uploaded Source

Built Distribution

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

sabc-0.2.0-cp313-cp313-macosx_26_0_arm64.whl (98.5 kB view details)

Uploaded CPython 3.13macOS 26.0+ ARM64

File details

Details for the file sabc-0.2.0.tar.gz.

File metadata

  • Download URL: sabc-0.2.0.tar.gz
  • Upload date:
  • Size: 125.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sabc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3fb91e787fce6ece1468711773ef66c187c42edb600346f6ae9dd9ca2015c37c
MD5 458689331b1c6d1503115440c8edfad6
BLAKE2b-256 87b97c547fece1e01089d2f0b9bbba28025c3a0542343a9f30569caece0e8274

See more details on using hashes here.

File details

Details for the file sabc-0.2.0-cp313-cp313-macosx_26_0_arm64.whl.

File metadata

  • Download URL: sabc-0.2.0-cp313-cp313-macosx_26_0_arm64.whl
  • Upload date:
  • Size: 98.5 kB
  • Tags: CPython 3.13, macOS 26.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sabc-0.2.0-cp313-cp313-macosx_26_0_arm64.whl
Algorithm Hash digest
SHA256 a70446a49df25e44f415f1955e9dd24fc3d38c92f0a8557b592ffc7ce88e14b6
MD5 ab81234b997ec926b32d7a09f0382f40
BLAKE2b-256 9c7372400dea1a8f8670ab601fc652c02e17fb415655b63430cc358603d7f66d

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