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 expressiontarget_support: support of the target PDFproposal_pdf: proposal probability density function, either callable or SymPy expressionproposal_support: support of the proposal PDFerror: numerical tolerance for validationbounds: search interval for numerical optimization for pdfs with infinite support
Note
- When writing mathematical expressions (eg.
exp,log,sqrt,inf), useSymPyorNumPyinstead of the built-inmathmodule. - When checking complicated PDFs,
SymPyinput has a much higher rate of success. Always useSymPyif possible. - For infinite-support callable inputs,
boundsdefines the finite interval used for numerical optimization. It should contain the maximum oftarget_pdf / proposal_pdf. The returnedMis 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
boundsare too large, and an appropriateboundsis (-10, 10).
Source code
- GitHub: https://github.com/HankTaiwan869/rejection-sampler
- Issues are welcome.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rejection_sampler-0.1.4.tar.gz.
File metadata
- Download URL: rejection_sampler-0.1.4.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40ab01af412d7934b85a643776109f39a008c87d8d02aa21ad3269cd84845ba8
|
|
| MD5 |
b38f93888fbcb8bc0051c7e8dc5ddcc6
|
|
| BLAKE2b-256 |
1ea1d90d0c7e9ebd964f0f7666040173058390916053c5a022dace1582cb8c66
|
File details
Details for the file rejection_sampler-0.1.4-py3-none-any.whl.
File metadata
- Download URL: rejection_sampler-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d47d56ceb70c0bde70e22b612c235f7b8374fe9507204c30696e00e8acc07a7a
|
|
| MD5 |
ccc305cb53cb0e894c9a1660a074b915
|
|
| BLAKE2b-256 |
a7052ee2a590942015f86d0d1786b9b8b4da379accf7148aaa2aa731cd62187e
|