Skip to main content

A minimal preconditioned Crank-Nicolson MCMC sampler

Project description

minipcn

DOI

A minimal implementation of preconditioned Crank-Nicolson MCMC sampling.

Installation

minipcn can be installed using from PyPI using pip:

pip install minipcn

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
    rng=rng,
)

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

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

For a complete example, see the examples directory.

Support for array-api

minipcn also supports different array API backends via array-api-compat and orng for random number generation. These can be installed by running

pip install minicpn[array-api]

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 ArrayRNG
import torch

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

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

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

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

Note: this still uses numpy when initializing the tpcn kernel.

Citing minipcn

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

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

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

minipcn-0.2.0a2.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

minipcn-0.2.0a2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file minipcn-0.2.0a2.tar.gz.

File metadata

  • Download URL: minipcn-0.2.0a2.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for minipcn-0.2.0a2.tar.gz
Algorithm Hash digest
SHA256 0456f8010e6faea36de4c37008ef4535f5376f1090b7cf6df72f712be8feeef8
MD5 338792c0dac13cf340421b7b901f08aa
BLAKE2b-256 4e5b246a2dfc74741d72fff28eeddfc3f68e95d0e951b145eb41b210633c60fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for minipcn-0.2.0a2.tar.gz:

Publisher: publish.yml on mj-will/minipcn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file minipcn-0.2.0a2-py3-none-any.whl.

File metadata

  • Download URL: minipcn-0.2.0a2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for minipcn-0.2.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d7e4f9a929a7ddeef65255049c3a7711fe18805e7a653f526f2b99462e95d6
MD5 fbdd119ebefd99ad3f57b7bbba41dfba
BLAKE2b-256 8ee35c8b3d70a176086282738e24364a915fee64f41084c30920371b085833fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for minipcn-0.2.0a2-py3-none-any.whl:

Publisher: publish.yml on mj-will/minipcn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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