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 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 inputs, choose appropriately large bounds. bounds should contain where the maximum of target_pdf / proposal_pdf is. Choose a larger bounds if possible. For certain cases, the appropriate bounds might require some trial and errors to figure out.
  • 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.

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.3.tar.gz (6.5 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.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rejection_sampler-0.1.3.tar.gz
  • Upload date:
  • Size: 6.5 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.3.tar.gz
Algorithm Hash digest
SHA256 db8cd72553fd8cc03a8fadcd78ebe7f33e7afa42d3cf0c9eb16b7039f7691ce6
MD5 0a8b6fe17a9d965f3322ef2435bdcab0
BLAKE2b-256 91c3e013b59baa85be006e24dc109f62e9dac584ee22eaa2dccaf911cb973bda

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rejection_sampler-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.7 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 474f239a0a2702d9970f0e30633910441d1bf0172858349f34b8224c5393559a
MD5 90032074eb492f3740b6b0799ab53169
BLAKE2b-256 5227fee2fa9837f96fd8837369605068171d1058d2e3c8557ee4fbc183ee2170

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