11# sbijax <img src =" https://raw.githubusercontent.com/dirmeier/sbijax/main/docs/_static/sticker.png " align =" right " width =" 160px " />
22
3- [ ![ active] ( https://www.repostatus.org/badges/latest/active.svg )] ( https://www.repostatus.org/#active )
43[ ![ ci] ( https://github.com/dirmeier/sbijax/actions/workflows/ci.yaml/badge.svg )] ( https://github.com/dirmeier/sbijax/actions/workflows/ci.yaml )
54[ ![ codecov] ( https://codecov.io/gh/dirmeier/sbijax/branch/main/graph/badge.svg?token=dn1xNBSalZ )] ( https://codecov.io/gh/dirmeier/sbijax )
65[ ![ documentation] ( https://readthedocs.org/projects/sbijax/badge/?version=latest )] ( https://sbijax.readthedocs.io/en/latest/?badge=latest )
76[ ![ version] ( https://img.shields.io/pypi/v/sbijax.svg?colorB=black&style=flat )] ( https://pypi.org/project/sbijax/ )
87
98> Simulation-based inference in JAX
109
11- ## About
12-
1310`` Sbijax `` is a Python library for neural simulation-based inference and
1411approximate Bayesian computation using [ JAX] ( https://github.com/google/jax ) .
15- It implements recent methods, such as * Simulated-annealing ABC* ,
12+ It implements recent methods, such as * Simulated Annealing ABC* ,
1613* Surjective Neural Likelihood Estimation* , * Neural Approximate Sufficient Statistics*
17- or * Consistency model posterior estimation* , as well as methods to compute model
18- diagnostics and for visualizing posterior distributions.
14+ or * Neural Posterior Score Estimation* .
1915
2016> [ !CAUTION]
2117> ⚠️ As per the LICENSE file, there is no warranty whatsoever for this free software tool. If you discover bugs, please report them.
2218
23- ## Examples
19+ ## Quick start
2420
25- ` Sbijax ` implements a slim object-oriented API with functional elements stemming from
26- JAX. All a user needs to define is a prior model, a simulator function and an inferential algorithm.
27- For example, you can define a neural likelihood estimation method and generate posterior samples like this:
21+ ` Sbijax ` implements a fully functional API in the idiom of [ Haiku] ( https://github.com/google-deepmind/dm-haiku ) :
22+ every method is a factory returning a record of pure functions, with parameters
23+ threaded explicitly. All a user needs to define is a prior, a simulator function
24+ and an inferential algorithm. For example, you can define a neural likelihood
25+ estimation method and generate posterior samples like this:
2826
2927``` python
3028from jax import numpy as jnp, random as jr
31- from sbijax import NLE
32- from sbijax.nn import make_maf
3329from tensorflow_probability.substrates.jax import distributions as tfd
3430
35- def prior_fn ():
36- prior = tfd.JointDistributionNamed(dict (
37- theta = tfd.Normal(jnp.zeros(2 ), jnp.ones(2 ))
38- ), batch_ndims = 0 )
39- return prior
31+ from sbijax import nle, train, sample, simulate
32+ from sbijax.mcmc import make_sampler, nuts
33+ from sbijax.nn import make_maf
34+
35+ prior = tfd.JointDistributionNamed(dict (
36+ theta = tfd.Normal(jnp.zeros(2 ), jnp.ones(2 ))
37+ ), batch_ndims = 0 )
4038
4139def simulator_fn (seed , theta ):
4240 p = tfd.Normal(jnp.zeros_like(theta[" theta" ]), 0.1 )
4341 y = theta[" theta" ] + p.sample(seed = seed)
4442 return y
4543
46-
47- fns = prior_fn, simulator_fn
48- model = NLE(fns, make_maf(2 ))
44+ estimator = nle(make_maf(2 ))
4945
5046y_observed = jnp.array([- 1.0 , 1.0 ])
51- data, _ = model.simulate_data(jr.PRNGKey(1 ))
52- params, _ = model.fit(jr.PRNGKey(2 ), data = data)
53- posterior, _ = model.sample_posterior(jr.PRNGKey(3 ), params, y_observed)
47+ data = simulate(jr.key(1 ), prior, simulator_fn, n = 10_000 )
48+ params, info = train(jr.key(2 ), estimator, data)
49+ samples, _ = sample(
50+ jr.key(3 ), estimator, params, y_observed,
51+ sampler = make_sampler(nuts, prior = prior),
52+ )
5453```
5554
5655More self-contained examples can be found in [ examples] ( https://github.com/dirmeier/sbijax/tree/main/examples ) .
5756
58- ## Documentation
59-
60- Documentation can be found [ here] ( https://sbijax.readthedocs.io/en/latest/ ) .
61-
6257## Installation
6358
6459Make sure to have a working ` JAX ` installation. Depending whether you want to use CPU/GPU/TPU,
@@ -76,36 +71,9 @@ To install the latest GitHub <RELEASE>, use:
7671pip install git+https://github.com/dirmeier/sbijax@< RELEASE>
7772```
7873
79- ## Contributing
80-
81- Contributions in the form of pull requests are more than welcome. A good way to start is to check out issues labelled
82- [ good first issue] ( https://github.com/dirmeier/sbijax/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22 ) .
83-
84- In order to contribute:
85-
86- 1 ) Clone ` sbijax ` and install ` uv ` from [ here] ( https://docs.astral.sh/uv/getting-started/installation/ ) .
87- 2 ) Install all dependencies using ` uv sync --all-groups ` .
88- 3 ) Install ` pre-commit ` and ` gitlint ` via:
89-
90- ``` shell
91- pre-commit install
92- gitlint install-hook
93- ```
94- 4 ) Create a new branch locally ` git checkout -b feature/my-new-feature ` or ` git checkout -b issue/fixes-bug ` .
95- 5 ) Implement your contribution and ideally a test case.
96- 6 ) Test, lint and format your contribution by running:
97-
98- ``` shell
99- uv run pytest # run the test suite
100- uv run ruff check sbijax examples # lint
101- uv run ruff format sbijax examples # format
102- uv run mypy sbijax # type-check
103- ```
74+ ## Documentation
10475
105- The ` pre-commit ` hook installed in step 3 runs ` ruff ` and ` mypy ` on every
106- commit, so these checks also run automatically. To build the docs locally,
107- run ` make html ` from within the ` docs ` directory.
108- 7 ) Submit a PR 🙂.
76+ Documentation can be found [ here] ( https://sbijax.readthedocs.io/en/latest/ ) .
10977
11078## Citing sbijax
11179
@@ -123,4 +91,4 @@ If you find our work relevant to your research, please consider citing:
12391## Acknowledgements
12492
12593> [ !NOTE]
126- > 📝 The API of the package is heavily inspired by the excellent Pytorch-based [ ` sbi ` ] ( https://github.com/sbi-dev/sbi ) package .
94+ > 📝 The API of the package is heavily inspired by [ ` Haiku ` ] ( https://github.com/google-deepmind/dm-haiku ) .
0 commit comments