Abstract
Modern Bayesian inference involves a mixture of computational methods for estimating, validating, and drawing conclusions from probabilistic models as part of principled workflows. An overarching motif of many Bayesian methods is that they are relatively slow, which often becomes prohibitive when fitting complex models to large data sets. Amortized Bayesian inference (ABI) offers a path to solving the computational challenges of Bayes. ABI trains neural networks on model simulations, rewarding users with rapid inference of any model-implied quantity, such as point estimates, likelihoods, or full posterior distributions. In this work, we present the Python library BayesFlow, Version 2.0, for general-purpose ABI. Along with direct posterior, likelihood, and ratio estimation, the software includes support for multiple popular deep learning backends, a rich collection of generative networks for sampling and density estimation, complete customization and high-level interfaces, as well as new capabilities for hyperparameter optimization, design optimization, and hierarchical modeling. Using a case study on dynamical system parameter estimation, combined with comparisons to similar software, we show that our streamlined, user-friendly workflow has strong potential to support broad adoption.
Introduction
Traditional Bayesian inference methods, such as Markov Chain Monte Carlo (MCMC), are often computationally expensive. When fitting complex models to large datasets—or when performing rigorous model validation by refitting to hundreds of simulated datasets—the computational cost can become prohibitive. Simulation-based inference (SBI) addresses this by bypassing the need for explicit likelihood evaluation, relying instead on forward simulations from the model.
Amortized Bayesian Inference (ABI) takes this a step further by framing inference as a machine learning problem. By training a neural network on model simulations upfront, the computational cost is "amortized." Once trained, the network can instantly estimate posteriors, likelihoods, or point estimates for any new observed data. BayesFlow 2.0 is a major update to the BayesFlow library, providing a comprehensive, backend-agnostic framework for ABI that supports complex hierarchical models and modern generative architectures.
Methods and Architecture
BayesFlow 2.0 utilizes a modular architecture to convert Bayesian parameter estimation into a supervised learning task. The workflow is divided into several key components:
Simulator and Adapter
Users define their scientific model as a simulator. The Adapter interface then automatically formats, constrains, and routes the simulated data to the neural networks. This allows for both "online" training (simulating data on-the-fly to prevent overfitting) and "offline" training (using pre-computed datasets).
Summary and Inference Networks
To handle diverse data types, BayesFlow employs specialized neural architectures:
- Summary Networks: These networks (e.g., DeepSets, CNNs, or Transformers) compress variable-length observations, such as time series or sets of observations, into fixed-length summary vectors.
- Inference Networks: These are generative models conditioned on the summary vector. BayesFlow 2.0 supports a wide range of architectures, including Normalizing Flows, Diffusion Models, and Flow Matching, to approximate the target posterior or likelihood distributions.
Multi-Backend Support via Keras 3
A core technical innovation in BayesFlow 2.0 is its foundation on Keras 3. This makes the library completely backend-agnostic, allowing users to execute their code using JAX, PyTorch, or TensorFlow. This flexibility is a significant advantage, as it avoids ecosystem lock-in and allows users to leverage specific backend features, such as JAX's Just-In-Time (JIT) compilation for massive speedups during the training phase.
Key Insights and Features
BayesFlow 2.0 introduces several capabilities that enhance the Bayesian workflow:
- Native Hierarchical Modeling: Unlike many competitors, BayesFlow natively supports hierarchical models with arbitrarily nested probabilistic factorizations, which are essential in fields like psychology and epidemiology.
- Efficient Model Verification: Amortization makes model calibration checks, such as Simulation-Based Calibration (SBC), computationally trivial. What used to take days of MCMC sampling can now be done in seconds.
- Parallel Ensemble Training: The library allows for the joint training of multiple network ensembles. This maximizes GPU utilization and provides a way to estimate the epistemic uncertainty of the neural approximation itself.
Results and Case Study
The authors demonstrated the software's efficacy using the Lotka-Volterra (LV) predator-prey system, a classic nonlinear ODE model. BayesFlow successfully recovered the four governing parameters of the system. Diagnostic metrics indicated high posterior contraction (0.92–0.94) and very low calibration error (0.03–0.04), confirming that the network accurately learned the true posterior distribution.
In a comparative analysis with other leading SBI packages like sbi (PyTorch) and NeuralEstimators.jl (Julia), BayesFlow 2.0 stood out as the only package offering multi-backend support, native hierarchical modeling, and parallel ensemble training.
Conclusion
BayesFlow 2.0 represents a significant step forward for amortized Bayesian inference. By providing a flexible, high-performance, and user-friendly library, it empowers researchers to apply sophisticated Bayesian methods to complex, simulation-heavy models. Future developments will focus on improving robustness against model misspecification and integrating with established probabilistic programming languages like Stan and PyMC to further broaden its accessibility.
Summary updated