|
1 | 1 | # BayesFlow |
2 | 2 |
|
3 | | -Welcome to the beta-version of our BayesFlow library for simulation-based Bayesian workflows. |
| 3 | +Welcome to our BayesFlow library for amortized simulation-based Bayesian inference. |
| 4 | + |
| 5 | +For starters, check out some or our walk-through notebooks: |
4 | 6 |
|
5 | | -For starters, check out the walk-through notebooks: |
6 | 7 | 1. [Basic amortized posterior estimation](docs/source/tutorial_notebooks/Intro_Amortized_Posterior_Estimation.ipynb) |
7 | 8 | 2. [Intermediate posterior estimation](docs/source/tutorial_notebooks/Covid19_Initial_Posterior_Estimation.ipynb) |
8 | 9 | 3. [Posterior estimation for ODEs](docs/source/tutorial_notebooks/Linear%20ODE%20system.ipynb) |
9 | | -4. Coming soon... |
10 | 10 |
|
11 | 11 | ## Project Documentation |
| 12 | + |
12 | 13 | The project documentation is available at <http://bayesflow.readthedocs.io> |
13 | 14 |
|
14 | 15 | ## Conceptual Overview |
15 | 16 |
|
16 | | -A cornerstone idea of amortized Bayesian inference is to employ generative neural networks for parameter estimation, model comparison, and model validation |
17 | | -when working with intractable simulators whose behavior as a whole is too complex to be described analytically. The figure below presents a higher-level overview of neurally bootstrapped Bayesian inference. |
| 17 | +A cornerstone idea of amortized Bayesian inference is to employ generative |
| 18 | +neural networks for parameter estimation, model comparison, and model validation |
| 19 | +when working with intractable simulators whose behavior as a whole is too |
| 20 | +complex to be described analytically. The figure below presents a higher-level |
| 21 | +overview of neurally bootstrapped Bayesian inference. |
18 | 22 |
|
19 | | -<img src="https://github.com/stefanradev93/BayesFlow/blob/Future/img/high_level_framework.png" width=80% height=80%> |
| 23 | +<img src="img/high_level_framework.png" width=80% height=80%> |
20 | 24 |
|
21 | 25 | ## Parameter Estimation |
22 | 26 |
|
23 | | -The BayesFlow approach for amortized parameter estimation is based on our paper: |
| 27 | +The original BayesFlow approach for amortized parameter estimation is based on our paper: |
24 | 28 |
|
25 | | -Radev, S. T., Mertens, U. K., Voss, A., Ardizzone, L., & Köthe, U. (2020). BayesFlow: Learning complex stochastic models with invertible neural networks. <em>IEEE Transactions on Neural Networks and Learning Systems</em>, available for free at: https://arxiv.org/abs/2003.06281. |
| 29 | +Radev, S. T., Mertens, U. K., Voss, A., Ardizzone, L., & Köthe, U. (2020). |
| 30 | +BayesFlow: Learning complex stochastic models with invertible neural networks. |
| 31 | +<em>IEEE Transactions on Neural Networks and Learning Systems</em>, available |
| 32 | +for free at: https://arxiv.org/abs/2003.06281. |
| 33 | + |
| 34 | +However, since then, we have substantially extended the BayesFlow library such that |
| 35 | +it is now much more general and cleaner than what we describe in the above paper. |
26 | 36 |
|
27 | 37 | ### Minimal Example |
28 | 38 |
|
29 | 39 | ```python |
30 | 40 | import numpy as np |
31 | 41 | import bayesflow as bf |
| 42 | +``` |
32 | 43 |
|
33 | | -# First, we define a simple 2D toy model with a Gaussian prior and a Gaussian simulator (likelihood): |
34 | | -def prior(D=2, mu=0., sigma=1.0): |
35 | | - return np.random.default_rng().normal(loc=mu, scale=sigma, size=D) |
| 44 | +To introduce you to the basic workflow of the library, let's consider |
| 45 | +a simple 2D Gaussian model, from which we want to obtain |
| 46 | +posterior inference. We assume a Gaussian simulator (likelihood) |
| 47 | +and a Gaussian prior for the means of the two components, |
| 48 | +which are our only model parameters in this example: |
36 | 49 |
|
| 50 | +```python |
37 | 51 | def simulator(theta, n_obs=50, scale=1.0): |
38 | 52 | return np.random.default_rng().normal(loc=theta, scale=scale, size=(n_obs, theta.shape[0])) |
| 53 | + |
| 54 | +def prior(D=2, mu=0., sigma=1.0): |
| 55 | + return np.random.default_rng().normal(loc=mu, scale=sigma, size=D) |
| 56 | +``` |
| 57 | + |
| 58 | +Then, we connect the `prior` with the `simulator` using a `GenerativeModel` wrapper: |
| 59 | + |
| 60 | +```python |
| 61 | +generative_model = bf.simulation.GenerativeModel(prior, simulator) |
| 62 | +``` |
39 | 63 |
|
40 | | -# Then, we create our BayesFlow setup consisting of a summary and an inference network: |
| 64 | +Next, we create our BayesFlow setup consisting of a summary and an inference network: |
| 65 | + |
| 66 | +```python |
41 | 67 | summary_net = bf.networks.InvariantNetwork() |
42 | 68 | inference_net = bf.networks.InvertibleNetwork(num_params=2) |
43 | 69 | amortizer = bf.amortizers.AmortizedPosterior(inference_net, summary_net) |
| 70 | +``` |
44 | 71 |
|
45 | | -# Next, we connect the `prior` with the `simulator` using a `GenerativeModel` wrapper: |
46 | | -generative_model = bf.simulation.GenerativeModel(prior, simulator) |
| 72 | +Finally, we connect the networks with the generative model via a `Trainer` instance: |
47 | 73 |
|
48 | | -# Finally, we connect the networks with the generative model via a `Trainer` instance: |
| 74 | +```python |
49 | 75 | trainer = bf.trainers.Trainer(amortizer=amortizer, generative_model=generative_model) |
| 76 | +``` |
50 | 77 |
|
51 | | -# We are now ready to train an amortized posterior approximator. For instance, to run online training, we simply call: |
| 78 | +We are now ready to train an amortized posterior approximator. For instance, |
| 79 | +to run online training, we simply call: |
| 80 | + |
| 81 | +```python |
52 | 82 | losses = trainer.train_online(epochs=10, iterations_per_epoch=500, batch_size=32) |
53 | 83 | ``` |
54 | 84 |
|
55 | | -Before inference, we can use simulation-based calibration (SBC, https://arxiv.org/abs/1804.06788) to check the computational faithfulness of the model-amortizer combination: |
| 85 | +Before inference, we can use simulation-based calibration (SBC, |
| 86 | +https://arxiv.org/abs/1804.06788) to check the computational faithfulness of |
| 87 | +the model-amortizer combination: |
56 | 88 |
|
57 | 89 | ```python |
58 | 90 | fig = trainer.diagnose_sbc_histograms() |
59 | 91 | ``` |
60 | 92 |
|
61 | | -<img src="https://github.com/stefanradev93/BayesFlow/blob/Future/img/showcase_sbc.png" width=65% height=65%> |
| 93 | +<img src="img/showcase_sbc.png" width=65% height=65%> |
62 | 94 |
|
63 | | -Amortized inference on new (real or simulated) data is then easy and fast: |
| 95 | +The histograms are roughly uniform and lie within the expected range for |
| 96 | +well-calibrated inference algorithms as indicated by the shaded gray areas. |
| 97 | +Accordingly, our amortizer seems to have converged to the intended target. |
| 98 | + |
| 99 | +Amortized inference on new (real or simulated) data is then easy and fast. |
| 100 | +For example, we can simulate 200 new data sets and generate 500 posterior draws |
| 101 | +per data set: |
64 | 102 |
|
65 | 103 | ```python |
66 | | -# Simulate 200 new data sets and generate 500 posterior draws per data set |
67 | 104 | new_sims = trainer.configurator(generative_model(200)) |
68 | 105 | posterior_draws = amortizer.sample(new_sims, n_samples=500) |
69 | 106 | ``` |
70 | 107 |
|
71 | | -We can then quickly inspect the parameter recoverability of the model: |
| 108 | +We can then quickly inspect the how well the model can recover its parameters |
| 109 | +across the simulated data sets. |
72 | 110 |
|
73 | 111 | ```python |
74 | 112 | fig = bf.diagnostics.plot_recovery(posterior_draws, new_sims['parameters']) |
75 | 113 | ``` |
76 | 114 |
|
77 | | -<img src="https://github.com/stefanradev93/BayesFlow/blob/Future/img/showcase_recovery.png" width=65% height=65%> |
| 115 | +<img src="img/showcase_recovery.png" width=65% height=65%> |
78 | 116 |
|
79 | | -Or we can look at single posteriors in relation to the prior: |
| 117 | +For any individual data set, we can also compare the parameters' posteriors with |
| 118 | +their corresponding priors: |
80 | 119 |
|
81 | 120 | ```python |
82 | 121 | fig = bf.diagnostics.plot_posterior_2d(posterior_draws[0], prior=generative_model.prior) |
83 | 122 | ``` |
84 | 123 |
|
85 | | -<img src="https://github.com/stefanradev93/BayesFlow/blob/Future/img/showcase_posterior.png" width=45% height=45%> |
| 124 | +<img src="img/showcase_posterior.png" width=45% height=45%> |
86 | 125 |
|
87 | | -### Further Reading |
| 126 | +We see clearly how the posterior shrinks relative to the prior for both |
| 127 | +model parameters as a result of conditioning on the data. |
88 | 128 |
|
89 | | -Coming soon... |
| 129 | +### References and Further Reading |
90 | 130 |
|
91 | | -## Model Misspecification |
| 131 | +- Radev, S. T., Mertens, U. K., Voss, A., Ardizzone, L., & Köthe, U. (2020). |
| 132 | +BayesFlow: Learning complex stochastic models with invertible neural networks. |
| 133 | +<em>IEEE Transactions on Neural Networks and Learning Systems</em>, available |
| 134 | +for free at: https://arxiv.org/abs/2003.06281. |
92 | 135 |
|
93 | | -What if we are dealing with misspecified models? That is, how faithful is our amortized inference if the generative model is a poor representation of reality? A modified loss function optimizes the learned summary statistics towards a unit Gaussian and reliably detects model misspecification during inference time. |
94 | 136 |
|
95 | | - |
| 137 | +## Model Misspecification |
96 | 138 |
|
| 139 | +What if we are dealing with misspecified models? That is, how faithful is our |
| 140 | +amortized inference if the generative model is a poor representation of reality? |
| 141 | +A modified loss function optimizes the learned summary statistics towards a unit |
| 142 | +Gaussian and reliably detects model misspecification during inference time. |
97 | 143 |
|
| 144 | + |
98 | 145 |
|
| 146 | +### References and Further Reading |
| 147 | + |
| 148 | +- Schmitt, M., Bürkner P. C., Köthe U., & Radev S. T. (2022). Detecting Model |
| 149 | +Misspecification in Amortized Bayesian Inference with Neural Networks. <em>ArXiv |
| 150 | +preprint</em>. |
99 | 151 |
|
100 | 152 | ## Model Comparison |
101 | 153 |
|
102 | 154 | Coming soon... |
103 | 155 |
|
| 156 | +### References and Further Reading |
| 157 | + |
| 158 | +- Radev S. T., D’Alessandro M., Mertens U. K., Voss A., Köthe U., & Bürkner P. |
| 159 | +C. (2021). Amortized Bayesian Model Comparison with Evidental Deep Learning. |
| 160 | +<em>IEEE Transactions on Neural Networks and Learning Systems</em>. |
| 161 | +doi:10.1109/TNNLS.2021.3124052 available for free at: https://arxiv.org/abs/2004.10629 |
| 162 | + |
104 | 163 | ## Likelihood emulation |
105 | 164 |
|
106 | 165 | Coming soon... |
0 commit comments