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.2.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.2.0.tar.gz (22.6 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.2.0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: margarine-2.2.0.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for margarine-2.2.0.tar.gz
Algorithm Hash digest
SHA256 11834e65e9de1e6a2b1e872dac5eac6728ba3696ab2dcfaeac8e610f64eeed9e
MD5 5fc89d09b59ca6a1ed8307fc5287faee
BLAKE2b-256 e6a6c388ff6086e16b4ad07b0b00d70164f152bbf99ed5d449a64d728e9b4f61

See more details on using hashes here.

File details

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

File metadata

  • Download URL: margarine-2.2.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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f6a3ad31332c54143aec8110d8941b170c011871e36eeab9a516baf2f9818d0
MD5 b6c64452d4d149222a0fe4a9f355f702
BLAKE2b-256 7d5e360b149ea80fe8b9318b17a2a1449fff6072b09b0bd258eaee9a4d916ad1

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