PyVBMC

PyVBMC: Efficient Bayesian inference in Python - Published in JOSS (2023)

https://github.com/acerbilab/pyvbmc

Science Score: 98.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 7 DOI reference(s) in README and JOSS metadata
  • Academic publication links
    Links to: arxiv.org, joss.theoj.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
    Published in Journal of Open Source Software

Keywords

bayesian-inference data-analysis gaussian-processes machine-learning python variational-inference

Keywords from Contributors

optimization-algorithms mesh

Scientific Fields

Physics Physical Sciences - 40% confidence
Last synced: 4 months ago · JSON representation ·

Repository

PyVBMC: Variational Bayesian Monte Carlo algorithm for posterior and model inference in Python

Basic Info
Statistics
  • Stars: 120
  • Watchers: 4
  • Forks: 8
  • Open Issues: 2
  • Releases: 7
Topics
bayesian-inference data-analysis gaussian-processes machine-learning python variational-inference
Created almost 5 years ago · Last pushed 11 months ago
Metadata Files
Readme License Citation

README.md

PyVBMC

PyVBMC: Variational Bayesian Monte Carlo in Python

Version Conda PyPI
Discussion tests docs build

What is it?

PyVBMC is a Python implementation of the Variational Bayesian Monte Carlo (VBMC) algorithm for posterior and model inference, previously implemented in MATLAB. VBMC is an approximate inference method designed to fit and evaluate Bayesian models with a limited budget of potentially noisy likelihood evaluations (e.g., for computationally expensive models). Specifically, VBMC simultaneously computes: - an approximate posterior distribution of the model parameters; - an approximation — technically, an approximate lower bound — of the log model evidence (also known as log marginal likelihood or log Bayes factor), a metric used for Bayesian model selection.

Extensive benchmarks on both artificial test problems and a large number of real model-fitting problems from computational and cognitive neuroscience show that VBMC generally — and often vastly — outperforms alternative methods for sample-efficient Bayesian inference [2,3].

Documentation

The full documentation is available at: https://acerbilab.github.io/pyvbmc/

When should I use PyVBMC?

PyVBMC is effective when:

  • the model log-likelihood function is a black-box (e.g., the gradient is unavailable);
  • the likelihood is at least moderately expensive to compute (say, half a second or more per evaluation);
  • the model has up to D = 10 continuous parameters (maybe a few more, but no more than D = 20);
  • the target posterior distribution is continuous and reasonably smooth (see here);
  • optionally, log-likelihood evaluations may be noisy (e.g., estimated via simulation).

Conversely, if your model can be written analytically, you should exploit the powerful machinery of probabilistic programming frameworks such as Stan or PyMC.

Note: If you are interested in point estimates or in finding better starting points for PyVBMC, check out Bayesian Adaptive Direct Search in Python (PyBADS), our companion method for fast Bayesian optimization.

Installation

PyVBMC is available via pip and conda-forge.

  1. Install with: console python -m pip install pyvbmc or: console conda install --channel=conda-forge pyvbmc PyVBMC requires Python version 3.10 or newer.
  2. (Optional): Install Jupyter to view the example Notebooks. You can skip this step if you're working from a Conda environment which already has Jupyter, but be aware that if the wrong jupyter executable is found on your path then import errors may arise. console conda install jupyter If you are running Python 3.11 and get an UnsatisfiableError you may need to install Jupyter from conda-forge: console conda install --channel=conda-forge jupyter The example notebooks can then be accessed by running console python -m pyvbmc

If you wish to install directly from latest source code, please see the instructions for developers and contributors.

Quick start

A typical PyVBMC workflow follows four steps:

  1. Define the model, which defines a target log density (i.e., an unnormalized log posterior density);
  2. Setup the parameters (parameter bounds, starting point);
  3. Initialize and run the inference;
  4. Examine and visualize the results.

PyVBMC is not concerned with how you define your model in step 1, as long as you can provide an (unnormalized) target log density. Running the inference in step 3 only involves a couple of lines of code: ```python from pyvbmc import VBMC

... define your model/target density here

vbmc = VBMC(target, x0, LB, UB, PLB, PUB) vp, results = vbmc.optimize() `` with input arguments: -target: the target (unnormalized) log density — often an unnormalized log posterior.targetis a callable that should take as input a parameter vector and return the log density at the point. The returned log density must return a *finite* real value, i.e. nonNaNor-inf. See the [VBMC FAQ](https://github.com/acerbilab/vbmc/wiki#how-do-i-prevent-vbmc-from-evaluating-certain-inputs-or-regions-of-input-space) for more details; -x0: an array representing the starting point of the inference in parameter space; -LBandUB: arrays of hard lower (resp. upper) bounds constraining the parameters (possibly-/+np.inffor unbounded parameters); -PLBandPUB`: arrays of plausible lower (resp. upper) bounds: that is, a box that ideally brackets a high posterior density region of the target.

The outputs are: - vp: a VariationalPosterior object which approximates the true target density; - results: a dict containing additional information. Important keys are: - "elbo": the estimated lower bound on the log model evidence (log normalization constant); - "elbo_sd": the standard deviation of the estimate of the ELBO (not the error between the ELBO and the true log model evidence, which is generally unknown).

The vp object can be manipulated in various ways. For example, we can draw samples from vp with the vp.sample() method, or evaluate its density at a point with vp.pdf() (or log-density with vp.log_pdf()). See the VariationalPosterior class documentation for details.

PyVBMC with noisy targets

The quick start example above works for deterministic (noiseless) evaluations of the target log-density. Py(VBMC) also supports noisy evaluations of the target. Noisy evaluations often arise from simulation-based models, for which a direct expression of the (log) likelihood is not available.

For information on how to run PyVBMC on a noisy target, see this example notebook and the VBMC FAQ (for MATLAB, but most concepts still apply).

Next steps

Once installed, example Jupyter notebooks can be found in the pyvbmc/examples directory. They can also be viewed statically on the main documentation pages. These examples will walk you through the basic usage of PyVBMC as well as some if its more advanced features.

For practical recommendations, such as how to set LB and UB and the plausible bounds, check out the FAQ on the VBMC wiki. The wiki was written with the MATLAB toolbox in mind, but the general advice applies to the Python version as well.

How does it work?

VBMC/PyVBMC combine two machine learning techniques in a novel way: - variational inference, a method to perform approximate Bayesian inference; - Bayesian quadrature, a technique to estimate the value of expensive integrals.

PyVBMC iteratively builds an approximation of the true, expensive target posterior via a Gaussian process (GP), and it matches a variational distribution — an expressive mixture of Gaussians — to the GP.

This matching process entails optimization of the evidence lower bound (ELBO), that is a lower bound on the log marginal likelihood (LML), also known as log model evidence. Crucially, we estimate the ELBO via Bayesian quadrature, which is fast and does not require further evaluation of the true target posterior.

In each iteration, PyVBMC uses active sampling to select which points to evaluate next in order to explore the posterior landscape and reduce uncertainty in the approximation.

VBMC Demo

In the figure above, we show an example PyVBMC run on a Rosenbrock "banana" function. The bottom-left panel shows PyVBMC at work: in grayscale are samples from the variational posterior (drawn as small points) and the corresponding estimated density (drawn as contours). The solid orange circles are the active sampling points chosen at each iteration, and the hollow blue circles are the previously sampled points. The topmost and rightnmost panels show histograms of the marginal densities along the $x1$ and $x2$ dimensions, respectively. PyVBMC converges to an excellent approximation of the true posterior with a few dozens evaluations of the target density.

See the VBMC papers [1-3] for more details.

Troubleshooting and contact

PyVBMC is under active development. The VBMC algorithm has been extensively tested in several benchmarks and published papers, and the benchmarks have been replicated using PyVBMC. But as with any approximate inference technique, you should double-check your results. See the examples for descriptions of the convergence diagnostics and suggestions on validating PyVBMC's results with multiple runs.

If you have trouble doing something with PyVBMC, spot bugs or strange behavior, or you simply have some questions, please feel free to: - Post in the lab's Discussions forum with questions or comments about PyVBMC, your problems & applications; - Open an issue on GitHub; - Contact the project lead at luigi.acerbi@helsinki.fi, putting 'PyVBMC' in the subject of the email.

References and citation

  1. Huggins, B., Li, C., Tobaben, M., Aarnos, M., & Acerbi, L. (2023). PyVBMC: Efficient Bayesian inference in Python. Journal of Open Source Software 8(86), 5428, https://doi.org/10.21105/joss.05428.
  2. Acerbi, L. (2018). Variational Bayesian Monte Carlo. In Advances in Neural Information Processing Systems 31: 8222-8232. (paper + supplement on arXiv, NeurIPS Proceedings)
  3. Acerbi, L. (2020). Variational Bayesian Monte Carlo with Noisy Likelihoods. In Advances in Neural Information Processing Systems 33: 8211-8222 (paper + supplement on arXiv, NeurIPS Proceedings).

Please cite all three references if you use PyVBMC in your work (the 2018 paper introduced the framework, and the 2020 paper includes a number of major improvements, including but not limited to support for noisy likelihoods). You can cite PyVBMC in your work with something along the lines of

We estimated approximate posterior distibutions and approximate lower bounds to the model evidence of our models using Variational Bayesian Monte Carlo (PyVBMC; Acerbi, 2018, 2020) via the PyVBMC software (Huggins et al., 2023). PyVBMC combines variational inference and active-sampling Bayesian quadrature to perform approximate Bayesian inference in a sample-efficient manner.

Besides formal citations, you can demonstrate your appreciation for PyVBMC in the following ways:

  • Star :star: the VBMC repository on GitHub;
  • Subscribe to the lab's newsletter for news and updates (new features, bug fixes, new releases, etc.);
  • Follow Luigi Acerbi on Twitter for updates about VBMC/PyVBMC and other projects;
  • Tell us about your model-fitting problem and your experience with PyVBMC (positive or negative) in the lab's Discussions forum.

You may also want to check out Bayesian Adaptive Direct Search in Python (PyBADS), our companion method for fast Bayesian optimization.

Additional references

  1. Acerbi, L. (2019). An Exploration of Acquisition and Mean Functions in Variational Bayesian Monte Carlo. In Proc. Machine Learning Research 96: 1-10. 1st Symposium on Advances in Approximate Bayesian Inference, Montréal, Canada. (paper in PMLR)

BibTeX

```BibTeX @article{huggins2023pyvbmc, title = {PyVBMC: Efficient Bayesian inference in Python}, author = {Bobby Huggins and Chengkun Li and Marlon Tobaben and Mikko J. Aarnos and Luigi Acerbi}, publisher = {The Open Journal}, journal = {Journal of Open Source Software}, url = {https://doi.org/10.21105/joss.05428}, doi = {10.21105/joss.05428}, year = {2023}, volume = {8}, number = {86}, pages = {5428} }

@article{acerbi2018variational, title={{V}ariational {B}ayesian {M}onte {C}arlo}, author={Acerbi, Luigi}, journal={Advances in Neural Information Processing Systems}, volume={31}, pages={8222--8232}, year={2018} }

@article{acerbi2020variational, title={{V}ariational {B}ayesian {M}onte {C}arlo with noisy likelihoods}, author={Acerbi, Luigi}, journal={Advances in Neural Information Processing Systems}, volume={33}, pages={8211--8222}, year={2020} }

@article{acerbi2019exploration, title={An Exploration of Acquisition and Mean Functions in {V}ariational {B}ayesian {M}onte {C}arlo}, author={Acerbi, Luigi}, journal={PMLR}, volume={96}, pages={1--10}, year={2019} } ```

License

PyVBMC is released under the terms of the BSD 3-Clause License.

Acknowledgments

PyVBMC was developed by members (past and current) of the Machine and Human Intelligence Lab at the University of Helsinki. Work on the PyVBMC package was supported by the Academy of Finland Flagship programme: Finnish Center for Artificial Intelligence FCAI.

Owner

  • Name: acerbilab
  • Login: acerbilab
  • Kind: organization
  • Location: Finland

Machine and Human Intelligence Research Group - University of Helsinki

JOSS Publication

PyVBMC: Efficient Bayesian inference in Python
Published
June 21, 2023
Volume 8, Issue 86, Page 5428
Authors
Bobby Huggins ORCID
University of Helsinki
Chengkun Li ORCID
University of Helsinki
Marlon Tobaben ORCID
University of Helsinki
Mikko J. Aarnos
University of Helsinki
Luigi Acerbi ORCID
University of Helsinki
Editor
Rachel Kurchin ORCID
Tags
Bayesian statistics Bayesian inference Probabilistic programming Model evidence Machine learning Simulator-based inference

Citation (CITATION.cff)

cff-version: "1.2.0"
authors:
- family-names: Huggins
  given-names: Bobby
  orcid: "https://orcid.org/0009-0006-3475-5964"
- family-names: Li
  given-names: Chengkun
  orcid: "https://orcid.org/0000-0001-5848-910X"
- family-names: Tobaben
  given-names: Marlon
  orcid: "https://orcid.org/0000-0002-9778-0853"
- family-names: Aarnos
  given-names: Mikko J.
- family-names: Acerbi
  given-names: Luigi
  orcid: "https://orcid.org/0000-0001-7471-7336"
contact:
- family-names: Huggins
  given-names: Bobby
  orcid: "https://orcid.org/0009-0006-3475-5964"
- family-names: Acerbi
  given-names: Luigi
  orcid: "https://orcid.org/0000-0001-7471-7336"
doi: 10.5281/zenodo.7966315
message: If you use this software, please cite our article in the
  Journal of Open Source Software.
preferred-citation:
  authors:
  - family-names: Huggins
    given-names: Bobby
    orcid: "https://orcid.org/0009-0006-3475-5964"
  - family-names: Li
    given-names: Chengkun
    orcid: "https://orcid.org/0000-0001-5848-910X"
  - family-names: Tobaben
    given-names: Marlon
    orcid: "https://orcid.org/0000-0002-9778-0853"
  - family-names: Aarnos
    given-names: Mikko J.
  - family-names: Acerbi
    given-names: Luigi
    orcid: "https://orcid.org/0000-0001-7471-7336"
  date-published: 2023-06-21
  doi: 10.21105/joss.05428
  issn: 2475-9066
  issue: 86
  journal: Journal of Open Source Software
  publisher:
    name: Open Journals
  start: 5428
  title: "PyVBMC: Efficient Bayesian inference in Python"
  type: article
  url: "https://joss.theoj.org/papers/10.21105/joss.05428"
  volume: 8
title: "PyVBMC: Efficient Bayesian inference in Python"

GitHub Events

Total
  • Watch event: 7
  • Delete event: 4
  • Issue comment event: 2
  • Push event: 4
  • Pull request review event: 3
  • Pull request event: 10
  • Fork event: 2
  • Create event: 5
Last Year
  • Watch event: 7
  • Delete event: 4
  • Issue comment event: 2
  • Push event: 4
  • Pull request review event: 3
  • Pull request event: 10
  • Fork event: 2
  • Create event: 5

Committers

Last synced: 5 months ago

All Time
  • Total Commits: 310
  • Total Committers: 6
  • Avg Commits per committer: 51.667
  • Development Distribution Score (DDS): 0.513
Past Year
  • Commits: 9
  • Committers: 2
  • Avg Commits per committer: 4.5
  • Development Distribution Score (DDS): 0.444
Top Committers
Name Email Commits
Marlon Tobaben 2****s 151
Bobby-Huggins 7****s 69
Luigi Acerbi l****i@g****m 49
pipme 3****e 33
dependabot[bot] 4****] 5
Mikko Aarnos m****s@g****m 3

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 7
  • Total pull requests: 102
  • Average time to close issues: 5 months
  • Average time to close pull requests: 2 days
  • Total issue authors: 2
  • Total pull request authors: 7
  • Average comments per issue: 1.86
  • Average comments per pull request: 0.18
  • Merged pull requests: 98
  • Bot issues: 0
  • Bot pull requests: 7
Past Year
  • Issues: 0
  • Pull requests: 9
  • Average time to close issues: N/A
  • Average time to close pull requests: 13 days
  • Issue authors: 0
  • Pull request authors: 2
  • Average comments per issue: 0
  • Average comments per pull request: 0.33
  • Merged pull requests: 6
  • Bot issues: 0
  • Bot pull requests: 7
Top Authors
Issue Authors
  • pipme (5)
  • Solosneros (2)
Pull Request Authors
  • Solosneros (37)
  • Bobby-Huggins (23)
  • lacerbi (20)
  • pipme (16)
  • dependabot[bot] (11)
  • mAarnos (3)
  • aloctavodia (1)
Top Labels
Issue Labels
bug (3) question (2) enhancement (1)
Pull Request Labels
enhancement (12) dependencies (11) documentation (5) bug (4)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 91 last-month
  • Total dependent packages: 1
  • Total dependent repositories: 0
  • Total versions: 7
  • Total maintainers: 2
pypi.org: pyvbmc

Variational Bayesian Monte Carlo in Python.

  • Documentation: https://pyvbmc.readthedocs.io/
  • License: BSD 3-Clause License Copyright (c) 2021, PyVBMC Developers and their Assignees All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  • Latest release: 1.0.4
    published over 1 year ago
  • Versions: 7
  • Dependent Packages: 1
  • Dependent Repositories: 0
  • Downloads: 91 Last month
Rankings
Dependent packages count: 6.6%
Average: 18.6%
Dependent repos count: 30.6%
Maintainers (2)
Last synced: 4 months ago

Dependencies

.github/workflows/build.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • actions/upload-artifact v3 composite
.github/workflows/docs.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/merge-tests.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/release.yml actions
  • actions/download-artifact v3 composite
  • pypa/gh-action-pypi-publish v1.6.4 composite
.github/workflows/tests.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
pyproject.toml pypi
  • cma >= 3.1.0
  • corner >= 2.2.1
  • dill >= 0.3.5.1
  • gpyreg >= 0.1.0
  • imageio >= 2.13.5
  • matplotlib >= 3.5.1
  • numpy >= 1.22.1
  • plotly >= 5.11.0
  • pytest >= 6.2.5
  • pytest-mock >= 3.6.1
  • pytest-rerunfailures >= 10.2
  • scipy >= 1.7.3
setup.py pypi