Skip to content

Commit 0190a52

Browse files
committed
docs: redesign docs and examples for the functional API
1 parent 64d3e10 commit 0190a52

42 files changed

Lines changed: 1362 additions & 1979 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
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
1411
approximate 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
3028
from jax import numpy as jnp, random as jr
31-
from sbijax import NLE
32-
from sbijax.nn import make_maf
3329
from 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

4139
def 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

5046
y_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

5655
More 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

6459
Make 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:
7671
pip 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).

docs/_static/theme.css

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
html[data-theme="light"] {
2-
--pst-color-primary: rgb(121, 40, 161);
2+
/* --pst-color-primary: rgb(121, 40, 161); */
3+
/* --pst-color-primary:#b26679;
34
--pst-color-primary-bg: #ffe9dd;
45
--pst-color-secondary: #b26679;
5-
--pst-color-inline-code-links: #b26679;
6+
--pst-color-inline-code-links: #b26679; */
67
}
78

89
pre > span {
910
line-height: 20px;
1011
}
1112

12-
span.kn {
13-
color: rgb(0, 120, 161) !important;
14-
}
15-
16-
span.ml, span.mi, span.nb {
17-
color: lightcoral !important;
18-
}
19-
20-
span.k, span.nn {
21-
color: rgb(168, 70, 185) !important;
22-
}
23-
2413
h1 > code > span {
2514
font-weight: 300 !important;
2615
}
@@ -34,9 +23,6 @@ pre {
3423
h1 {
3524
margin-bottom: 50px;
3625
}
37-
h3, h2, h1 {
38-
39-
}
4026

4127
nav > li > a > code.literal {
4228
padding-top: 0;
@@ -49,18 +35,18 @@ nav.bd-links p.caption {
4935
text-transform: uppercase;
5036
}
5137

52-
code.literal {
38+
/* code.literal {
5339
background-color: white;
5440
border: 0;
5541
border-radius: 0;
56-
}
42+
} */
5743

5844
a > code {
5945
font-weight: 575;
6046
}
6147

6248
a:hover {
63-
text-decoration-thickness: 1px !important;
49+
text-decoration-thickness: 1px !important;
6450
}
6551

6652
ul.bd-breadcrumbs li.breadcrumb-item a:hover {

docs/api/index.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
API Reference
2+
=============
3+
4+
.. toctree::
5+
:maxdepth: 4
6+
7+
sbijax
8+
sbijax.experimental
9+
sbijax.mcmc
10+
sbijax.nn
11+
sbijax.simulators
12+
sbijax.util

docs/api/sbijax.experimental.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
sbijax.experimental
2+
===================
3+
4+
.. currentmodule:: sbijax.experimental
5+
6+
``sbijax.experimental`` contains experimental code that might get ported to the
7+
main code base or possibly deleted again.
8+
9+
``cmpe`` (consistency-model posterior estimation) and ``aio`` are functional
10+
factories; ``aio`` delegates to the ``fmpe`` core, and
11+
``make_truncated_proposal`` builds the truncated-prior proposal used with
12+
:func:`sbijax.run_sequential`. The score networks below are consumed by
13+
:func:`sbijax.npse`, which now lives in the main package.
14+
15+
.. autosummary::
16+
cmpe
17+
aio
18+
make_truncated_proposal
19+
20+
.. autofunction:: cmpe
21+
22+
.. autofunction:: aio
23+
24+
.. autofunction:: make_truncated_proposal
25+
26+
.. currentmodule:: sbijax.experimental.nn
27+
28+
.. autosummary::
29+
make_score_model
30+
make_simformer_based_score_model
31+
ScoreModel
32+
33+
.. autofunction:: make_simformer_based_score_model
34+
35+
.. autofunction:: make_score_model
36+
37+
.. autoclass:: ScoreModel
38+
:members: __call__

docs/api/sbijax.mcmc.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
sbijax.mcmc
2+
===========
3+
4+
.. currentmodule:: sbijax.mcmc
5+
6+
``sbijax.mcmc`` builds the posterior samplers
7+
and exposes the low-level MCMC routines they are built on.
8+
9+
:func:`make_sampler` bundles a :class:`Kernel` -- a handle identifying a
10+
BlackJAX MCMC algorithm -- with the prior and ``N(0, I)`` chain initialisation
11+
into a sampler that is passed to :func:`sbijax.sample`. The available algorithms
12+
are ``nuts``, ``mala``, ``rmh`` and ``imh``::
13+
14+
from sbijax.mcmc import make_sampler, nuts
15+
16+
sampler = make_sampler(nuts, prior=prior)
17+
samples, info = sample(key, estimator, params, y_obs, sampler=sampler)
18+
19+
.. autosummary::
20+
make_sampler
21+
imh
22+
mala
23+
nuts
24+
rmh
25+
sample_with_imh
26+
sample_with_mala
27+
sample_with_nuts
28+
sample_with_rmh
29+
sample_with_slice
30+
31+
.. autofunction:: make_sampler
32+
33+
.. autofunction:: sample_with_imh
34+
35+
.. autofunction:: sample_with_mala
36+
37+
.. autofunction:: sample_with_nuts
38+
39+
.. autofunction:: sample_with_rmh
40+
41+
.. autofunction:: sample_with_slice
42+
43+
.. autodata:: imh
44+
:no-value:
45+
46+
.. autodata:: mala
47+
:no-value:
48+
49+
.. autodata:: nuts
50+
:no-value:
51+
52+
.. autodata:: rmh
53+
:no-value:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
``sbijax.nn``
2-
=============
1+
sbijax.nn
2+
=========
33

44
.. currentmodule:: sbijax.nn
55

0 commit comments

Comments
 (0)