margarine: Posterior Sampling and Marginal Bayesian Statistics
Project description
margarine: density estimation made easy
Authors: Harry T.J. Bevins
Version: 2.0.1
Homepage: https://github.com/htjb/margarine
Documentation: https://margarine.readthedocs.io/
margarine provides a suite of density estimation tools including KDEs, normalizing flows like NICE and RealNVP as well as a novel method for improved performance on multimodal distributions.
The code can be used to:
- Emulate posterior distributions from weightened samples (e.g. MCMC, nested sampling)
- Build non-trivial priors from samples
- Perform density estimation tasks in general machine learning applications
- Emulate correctly normalised marginal likelihoods
- Calculate statistics like the KL divergence between different density estimators and marginal model dimensionalities.
Installation
From version 2.0.0 margarine moved to JAX for improved performance. Older versions (1.x.x) using TensorFlow are still available via pip with the last release being 1.4.2.
Install from Git:
git clone https://github.com/htjb/margarine.git # or use SSH
cd margarine
pip install .
Or via pip:
pip install margarine
Note: pip may not always give the latest version.
Getting Started
All of the density estimators in margarine have a common interface and set of methods including train(), sample(), log_prob(), log_like(), save() and load(). The below example shows how to train a RealNVP and generate samples.
import jax
import jax.numpy as jnp
import matplotlib.pyplot as plt
from margarine.estimators.realnvp import RealNVP
nsamples = 5000
key = jax.random.PRNGKey(0)
original_samples = jax.random.multivariate_normal(
key,
mean=jnp.array([0.0, 0.0]),
cov=jnp.array([[1.0, 0.8], [0.8, 1.0]]),
shape=(nsamples,),
)
weights = jnp.ones(len(original_samples))
realnvp_estimator = RealNVP(
original_samples,
weights=weights,
in_size=2,
hidden_size=50,
num_layers=6,
num_coupling_layers=6,
)
key, subkey = jax.random.split(key)
realnvp_estimator.train(
subkey,
learning_rate=1e-3,
epochs=2000,
patience=50,
batch_size=1000,
)
generated_samples = realnvp_estimator.sample(key, num_samples=nsamples)
plt.scatter(
original_samples[:, 0], original_samples[:, 1], alpha=0.5, label="Original Samples"
)
plt.scatter(
generated_samples[:, 0], generated_samples[:, 1], alpha=0.5, label="Generated Samples"
)
plt.legend()
plt.title("RealNVP: Original vs Generated Samples")
plt.xlabel("X1")
plt.ylabel("X2")
plt.show()
for more details see the documentation.
Documentation
Available at: https://margarine.readthedocs.io/. To build locally:
pip install ".[docs]"
mkdocs serve
Licence & Citation
Licensed under MIT.
If used for academic work, please cite:
- Main paper: https://arxiv.org/abs/2205.12841
- MaxEnt22 proceedings: https://arxiv.org/abs/2207.11457
- Piecewise Normalising Flows Paper: https://arxiv.org/abs/2305.02930
Contributing
Contributions and feature suggestions welcome. Open an issue to report bugs or discuss ideas. See CONTRIBUTING.md for details.
The future goals of the project are outlined in ROADMAP.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file margarine-2.0.1.tar.gz.
File metadata
- Download URL: margarine-2.0.1.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91b5afe78826468a988bb90f110984dfd88db4fe5ff9d64d98d384ffc7d52559
|
|
| MD5 |
30153606e9ca4602c3146259c3c88f14
|
|
| BLAKE2b-256 |
b7b080e3ff11d0258ba9ab97ffecce7aecd7bb3cf940ac2cd18cd992aa052dfe
|
File details
Details for the file margarine-2.0.1-py3-none-any.whl.
File metadata
- Download URL: margarine-2.0.1-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04760f299d8f9697d70ca84ad7d90b0e9c00f01abad40ada005265476e86cf89
|
|
| MD5 |
91fd2d454c2c2a0d5ff8cb5201fc7bbe
|
|
| BLAKE2b-256 |
19a694f7f16ed86ebdfd15d55a56ee9ea2ffc0c204c16964197e05b387b80fef
|