Skip to main content

Kernel-based MCMC post-processing algorithms

Kernax is a small package that implements kernel-based post-processing and subsampling algorithms for MCMC output. It currently provides three algorithms:

Documentation

Full documentation is available on Read the Docs.

Quick start

Example usage of Stein thinning on a Gaussian sample:

import jax
import jax.numpy as jnp
from jax.scipy.stats import multivariate_normal
from kernax.utils import median_heuristic
from kernax import SteinThinning

rng_key = jax.random.PRNGKey(0)
x = jax.random.normal(rng_key, (1000,2))

def logprob_fn(x):
    return multivariate_normal.logpdf(x, mean=jnp.zeros(2), cov=jnp.eye(2))
score_fn = jax.grad(logprob_fn)
score_values = jax.vmap(score_fn, 0)(x)

lengthscale = jnp.array([median_heuristic(x)])
stein_fn = SteinThinning(x, score_values, lengthscale)
indices = stein_fn(100)

To use the regularized variant, add a few lines:

from kernax.utils import laplace_log_p_softplus
from kernax import RegularizedSteinThinning

log_p = jax.vmap(score_fn, 0)(x)
laplace_log_p_values = laplace_log_p_softplus(x, score_fn)

reg_stein_fn = RegularizedSteinThinning(x, log_p, score_values, laplace_log_p_values, lengthscale)
indices = reg_stein_fn(100)

Install guide

As a user

A Python wheel is available on PyPi. Install Kernax into your Python environment with:

pip install kernax

As a developper

We recommand using uv. Clone the repository, then run:

uv sync

This creates a virtual environment for developing Kernax. If you’re not familiar with uv, have a look at their Getting started guide.

Paper experiments

This repository implements the regularized Stein thinning algorithm introduced in Kernel Stein Discrepancy thinning: a theoretical perspective of pathologies and a practical fix with regularization.

If you use this library, please consider citing:

@article{benard2023kernel,
  title={Kernel Stein Discrepancy thinning: a theoretical perspective of pathologies and a practical fix with regularization},
  author={B{\'e}nard, Cl{\'e}ment and Staber, Brian and Da Veiga, S{\'e}bastien},
  journal={arXiv preprint arXiv:2301.13528},
  year={2023}
}

All numerical experiments presented in the paper can be reproduced using the scripts in the example/ folder.

In particular:

  • Figures 1–3: example/mog_randn.py
  • Section 4 and Appendix 1:
    • Gaussian mixture: example/mog4_mcmc/ and example/mog4_mcmc_dim/
    • Mixture of banana-shaped distributions: example/mobt2_mcmc/ and example/mobt2_mcmc_dim/
    • Bayesian logistic regression: example/logistic_regression.py
  • Supplementary material:
    • Figure 2: example/mog_weight_weights.py
    • Figure 6: example/mog4_mcmc_lambda

Metadata

Release files for kernax 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kernax 0.3.0
File Size Uploaded
kernax-0.3.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kernax 0.3.0
File Interpreter ABI Platform
kernax-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / kernax-0.3.0.tar.gz

Download URL kernax-0.3.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cef1d80dad79ae71e722959ee4ff8a3913398a2ad7a035df2c777f110cd2ac1d
BLAKE2b-256 checksum
How to use checksums
5791575995807085942b13845b9437771fd6e62fd927a3d28ec2db69ab8f0169
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / kernax-0.3.0-py3-none-any.whl

Download URL kernax-0.3.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9daa2bf05d55c39d98cae55f61fd73c48a5761dcdd38780f73d74d7cde564700
BLAKE2b-256 checksum
How to use checksums
77726ef75fc73f7045e510bd20228746a023eb97633590ec33a4066030f42b05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page