Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

minipcn

DOI

A minimal implementation of preconditioned Crank-Nicolson MCMC sampling.

Installation

minipcn can be installed from PyPI using pip:

pip install minipcn

For scan-based sampling (including automatic scan selection under JAX JIT), install the optional extra:

pip install 'minipcn[scan]'

Without this extra, use_scan=None falls back to the Python loop, including under JAX JIT. use_scan=True requires the extra and verbose=False; use_scan=False always uses the Python loop.

Usage

The basic usage is:

from minipcn import Sampler
import numpy as np

log_prob_fn = ...    # Log-probability function - must be vectorized
dims = ...    # The number of dimensions
rng = np.random.default_rng(42)

sampler = Sampler(
    log_prob_fn=log_prob_fn,
    dims=dims,
    step_fn="pcn",  # Or "tpcn"
)

x0 = rng.normal(size=(100, dims))

chain, history = sampler.sample(x0, n_steps=500, rng=rng)

For a complete example, see the examples directory.

Array API support

minipcn also supports different array API backends via array-api-compat and orng for random number generation.

Usage is then similar to when using numpy, except one must use the RNG from orng and specify the backend via xp:

from minipcn import Sampler
from orng import RandomGenerator
import torch

log_prob_fn = ...    # Log-probability function - must be vectorized
dims = ...    # The number of dimensions
rng = RandomGenerator(backend="torch", seed=42)

sampler = Sampler(
    log_prob_fn=log_prob_fn,
    dims=dims,
    step_fn="pcn",    # Or tpcn
    xp=torch,
)

# Generate initial samples
x0 = rng.randn(size=(100, dims))

# Run the sampler
chain, history = sampler.sample(x0, n_steps=500, rng=rng)

Note: the tpCN step falls back to numpy for fitting the Student-t distribution

Functional API

minipcn also supports explicit functional RNG state via Sampler.sample_functional(...). This is the path to use for JAX compilation or any workflow where RNG state must be threaded explicitly.

The functional API does not take an RNG object but a backend and state:

import jax
import jax.numpy as jnp
from minipcn import Sampler
from orng.functional import create_functional_backend

dims = 4
rng_backend = create_functional_backend("jax")
rng_state = rng_backend.init_state(seed=42, generator=None)
x0, rng_state = rng_backend.normal(
    rng_state,
    loc=0.0,
    scale=1.0,
    size=(32, dims),
    dtype=jnp.float32,
)

def log_prob_fn(x):
    return -0.5 * jnp.sum(x**2, axis=-1)

sampler = Sampler(
    log_prob_fn=log_prob_fn,
    dims=dims,
    step_fn="pcn",
    xp=jnp,
)

samples, history, next_rng_state = sampler.sample_functional(
    x0,
    n_steps=8,
    rng_state=rng_state,
    verbose=False,
    return_last_only=True,
)

sample_functional(...) returns (chain, history, next_rng_state).

To use it under jax.jit, thread the state through the compiled function:

@jax.jit
def
run(x, state):
    samples, history, next_state = sampler.sample_functional(
        x,
        n_steps=8,
        rng_state=state,
        verbose=False,
        return_last_only=True,
    )
    return samples, history, next_state

samples, history, rng_state = run(x0, rng_state)

The backend for sample_functional(...) is inferred from xp. For example:

  • xp=np uses the NumPy functional backend
  • xp=jax.numpy uses the JAX functional backend
  • xp=torch uses the PyTorch functional backend

Use sample(...) for stateful RNG objects and sample_functional(...) when you want explicit RNG state.

Citing minipcn

If you use minipcn in your work, please cite our DOI

If using the tpcn kernel, please also cite Grumitt et al

Metadata

Release files for minipcn 0.3.0a1

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

Source distribution (sdist)

Source distribution for minipcn 0.3.0a1
File Size Uploaded
minipcn-0.3.0a1.tar.gz 29.2 kB Details

Built distribution (wheel)

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

Total release size: 48.2 kB

Release files / minipcn-0.3.0a1.tar.gz

Download URL minipcn-0.3.0a1.tar.gz
Size 29.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c1fe1cba250ac48af7dc51d2f2a1efdee89bf884a7feefd0b30adc02ea4d170e
BLAKE2b-256 checksum
How to use checksums
97e9e587706b4756c7157cdb62702546ff9282ffba007764deddf689494c2c5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / minipcn-0.3.0a1-py3-none-any.whl

Download URL minipcn-0.3.0a1-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bc483602761d4ff95b3e1de32a8626d2d5f7ec034bc8a7be9290b76f9a90c3b
BLAKE2b-256 checksum
How to use checksums
1ecf83c3377e7018ee3c455153d9560c6a972b0a407075868f5e58979fc640a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log
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