Skip to main content

A statistical computing toolkit for validating proposal distributions and computing optimal rejection-sampling constants.

Project description

rejection-sampler

A small Python package for validating rejection sampling setups and computing the optimal rejection constant (M). The package supports both callable Python functions and symbolic SymPy expressions for target and proposal PDFs.

Installation

pip install rejection-sampler
# or 
uv add rejection-sampler

Usage

Import the main function:

from rejection_sampler import find_optimal_M

Example 1: Callable input

def target_pdf(x):
    return 2 * x if 0 <= x <= 1 else 0.0

def proposal_pdf(x):
    return 1.0 if 0 <= x <= 1 else 0.0

M = find_optimal_M(
    target_pdf=target_pdf,
    target_support=(0.0, 1.0),
    proposal_pdf=proposal_pdf,
    proposal_support=(0.0, 1.0),
)

print(M)

Example 2: SymPy input

import sympy as sp

x = sp.Symbol("x", real=True)

target_pdf = 2 * x
proposal_pdf = sp.Integer(1)

M = find_optimal_M(
    target_pdf=target_pdf,
    target_support=(0, 1),
    proposal_pdf=proposal_pdf,
    proposal_support=(0, 1),
)

print(M)

Infinite support

For numerical inputs with infinite support, provide finite optimization bounds:

import numpy as np
from rejection_sampler import find_optimal_M

def target_pdf(x):
    return np.exp(-0.5 * x * x) / np.sqrt(2 * np.pi)

def proposal_pdf(x):
    return 1.0 / (np.pi * (1 + x * x))

M = find_optimal_M(
    target_pdf=target_pdf,
    target_support=(-np.inf, np.inf),
    # or use (-float("inf"), float("inf")) for infinite support
    proposal_pdf=proposal_pdf,
    proposal_support=(-np.inf, np.inf),
    bounds=(-10.0, 10.0),
)

print(M)

Parameters

  • target_pdf: target probability density function, either callable or SymPy expression
  • target_support: support of the target PDF
  • proposal_pdf: proposal probability density function, either callable or SymPy expression
  • proposal_support: support of the proposal PDF
  • error: numerical tolerance for validation
  • bounds: search interval for numerical optimization for pdfs with infinite support

Note

  • When writing mathemtatical expressions (eg. exp, log, sqrt, inf), use numpy instead of the built-in math module.
  • Provide a sufficiently large bounds to ensure correctness of optimization and prevent finding local optima.

License

This project is licensed under the MIT License.

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

rejection_sampler-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

rejection_sampler-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file rejection_sampler-0.1.1.tar.gz.

File metadata

  • Download URL: rejection_sampler-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rejection_sampler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d5e3c54d65b872d51591508320e25e8b8399403d59c2aec0db30dbb90cfdd5f3
MD5 c815fc5085161c0a2551909eec952129
BLAKE2b-256 a24ddcb9c3f53346f0f558b51ec78c6007444e399c863bd8007f8f7d6e5ad213

See more details on using hashes here.

File details

Details for the file rejection_sampler-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rejection_sampler-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rejection_sampler-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 04f9646938b585993bfa8ded3af1f3c5c4867f1bab634e9ffedef4919f43794a
MD5 360fc5d42a189292d9eedb4be39c5682
BLAKE2b-256 16785ce1dff4611849b5f2c88fbe79452e972477193dd369e746df5d3c95a46a

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