Skip to main content

margarine: Posterior Sampling and Marginal Bayesian Statistics

Project description

margarine: density estimation made easy

Authors: Harry T.J. Bevins
Version: 2.1.0
Homepage: https://github.com/htjb/margarine
Documentation: https://margarine.readthedocs.io/

Documentation Status arXiv:2205.12841 arXiv:2305.02930 arXiv:2207.11457 PyPI version Licence: MIT

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:


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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

margarine-2.1.0.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

margarine-2.1.0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file margarine-2.1.0.tar.gz.

File metadata

  • Download URL: margarine-2.1.0.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

Hashes for margarine-2.1.0.tar.gz
Algorithm Hash digest
SHA256 eef63288fbc48902a36e7c2afe0df2ed0d0859beba2e576825fedba30b699f04
MD5 2b87f29466c7ce1f0cd06cc87f079727
BLAKE2b-256 b969353827d7d47f6059e80214299862a44fae603c104a8bdfa2056fff0ee466

See more details on using hashes here.

File details

Details for the file margarine-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: margarine-2.1.0-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

Hashes for margarine-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6907f3777b6524613137d4c58e8a97c656ecce4298307f5e787a24e0c5231f9d
MD5 21745ef054988eae4124f69e12de9304
BLAKE2b-256 f9ff11f0bda02985b4ef92712a8af854bc3f3b78f0610cf875f276f74eb84771

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page