margarine: density estimation made easy
Authors: Harry T.J. Bevins
Version: 2.3.0
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.
Metadata
Release files for margarine 2.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| margarine-2.3.0.tar.gz | 24.3 kB | Without provenance | Details for margarine-2.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| margarine-2.3.0-py3-none-any.whl | Python 3 | none | any | Details for margarine-2.3.0-py3-none-any.whl |
Total release size: 52.1 kB
Release files / margarine-2.3.0.tar.gz
| Download URL | margarine-2.3.0.tar.gz |
|---|---|
| Size | 24.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b0825dffb0072cfe74f018b0859fb6552be65dbb9fab56b4b296746d7a1099b
|
|
BLAKE2b-256 checksum How to use checksums |
c2cd6f1d058fae2a2b8ccef7a0b4d48bb01c44d1d6b861eda7e9a8edbc8f6c49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.11
|
Release files / margarine-2.3.0-py3-none-any.whl
| Download URL | margarine-2.3.0-py3-none-any.whl |
|---|---|
| Size | 27.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
52064379469101a0fac9736d06bd4d1c3e4d5cef101a416f0e7ff0742af21ba7
|
|
BLAKE2b-256 checksum How to use checksums |
a9248d53b672e40aade4433ebc13c6e0a873c1ec17bd7bf9b09c6c6d5e628c49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.11
|
Security for margarine 2.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.