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.0a3.tar.gz (14.6 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.0a3-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: minipcn-0.2.0a3.tar.gz
  • Upload date:
  • Size: 14.6 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.0a3.tar.gz
Algorithm Hash digest
SHA256 6e5197f6448ef9f8d99352fa4a94f375734beb3d0d6872e81895f6f9a59bf3e9
MD5 634d25ee21894cb1ad1220dde3551d02
BLAKE2b-256 da550415a541eb22629b587928b47fbdc95f9ab9b9fd2e3f72d1e74cbf6b9838

See more details on using hashes here.

Provenance

The following attestation bundles were made for minipcn-0.2.0a3.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.0a3-py3-none-any.whl.

File metadata

  • Download URL: minipcn-0.2.0a3-py3-none-any.whl
  • Upload date:
  • Size: 10.2 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.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 38aec26d12272a7aee426dcb75a3401c0e891fe8e68175c13ea63ac9c9c5ddc2
MD5 c26aaea6b1d4db7566bba16b51128614
BLAKE2b-256 2e3784603d714b9c8d8d1dc45af1db9665e3c7f6e9af9173702c567d5f0110fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for minipcn-0.2.0a3-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