Skip to main content

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 probability density functions (PDF).

In rejection sampling, optimal M is the smallest constant such that, for all x in the target support:

target_pdf(x) <= M * proposal_pdf(x)

Installation

pip install rejection-sampler

or

uv add rejection-sampler

Usage

Import the main function:

from rejection_sampler import find_optimal_M

The function supports two ways of PDF definition: callable input and symbolic input (with SymPy). In general, symbolic input has a much higher chance of success. When calling the function, 4 arguments must be provided: target_pdf, target_support, proposal_pdf, proposal_support.

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, the argument bounds must be provided (see Note for more info on 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=(-sp.oo, sp.oo),
    # or use (-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 mathematical expressions (eg. exp, log, sqrt, inf), use SymPy or NumPy instead of the built-in math module.
  • When checking complicated PDFs, SymPy input has a much higher rate of success. Always use SymPy if possible.
  • For infinite-support callable inputs, bounds defines the finite interval used for numerical optimization. It should contain the maximum of target_pdf / proposal_pdf. The returned M is optimized only over this interval and does not prove global domination outside it, so selecting appropriate bounds may require domain knowledge or experimentation.
  • Known issue: For certain PDFs, there might be overflowing/underflowing issues. For example, when sampling the standard Gumbel distribution for maxima with Cauchy distribution, the calculation overflows when bounds are too large, and an appropriate bounds is (-10, 10).

Source code

License

This project is licensed under the MIT License.

Metadata

Release files for rejection-sampler 0.1.4

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

Source distribution (sdist)

Source distribution for rejection-sampler 0.1.4
File Size Uploaded Provenance Actions
rejection_sampler-0.1.4.tar.gz 6.7 kB Without provenance Details for rejection_sampler-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rejection-sampler 0.1.4
File Interpreter ABI Platform Actions
rejection_sampler-0.1.4-py3-none-any.whl Python 3 none any Details for rejection_sampler-0.1.4-py3-none-any.whl

Total release size: 15.3 kB

Release files / rejection_sampler-0.1.4.tar.gz

Download URL rejection_sampler-0.1.4.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
40ab01af412d7934b85a643776109f39a008c87d8d02aa21ad3269cd84845ba8
BLAKE2b-256 checksum
How to use checksums
1ea1d90d0c7e9ebd964f0f7666040173058390916053c5a022dace1582cb8c66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Linux Mint","version":"22.3","id":"zena","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / rejection_sampler-0.1.4-py3-none-any.whl

Download URL rejection_sampler-0.1.4-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d47d56ceb70c0bde70e22b612c235f7b8374fe9507204c30696e00e8acc07a7a
BLAKE2b-256 checksum
How to use checksums
a7052ee2a590942015f86d0d1786b9b8b4da379accf7148aaa2aa731cd62187e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Linux Mint","version":"22.3","id":"zena","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for rejection-sampler 0.1.4

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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