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, 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

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 mathematical expressions (eg. exp, log, sqrt, inf), use numpy instead of the built-in math module.
  • For infinite-support callable inputs, choose sufficiently large bounds. bounds that are too small may miss the global optimum.

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.2.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.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rejection_sampler-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 ae91f46cafc35c03edb9b8ee642f9f3baa0395f9003ec04b0e741b0fcc087c11
MD5 2d83140bef88615a29aa830710182cfc
BLAKE2b-256 b5d9701ce38d2175424b3626c59a72dba3fd97512ad4612609e3b6881a08b241

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rejection_sampler-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 415ce44bbbd3ee2d902af9b8c6017d1fce5c4142d4e265a8b8b5840dc88df54f
MD5 b768386d1f340453d39fd6fab1bc3b3f
BLAKE2b-256 cb3bb6068349a6542aef6c28e9b08e26e87e784b15eb071558faa4ac7f9fc6ed

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