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)
Note
When writing mathemtatical expressions (eg. exp, log, sqrt, inf), use numpy instead of the built-in math module.
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 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: finite search interval for numerical optimization with infinite support
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.0.tar.gz.
File metadata
- Download URL: rejection_sampler-0.1.0.tar.gz
- Upload date:
- Size: 6.1 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1a65dbc115919bd8dcfbcef9b94861ce8538d1218a8f06bb0d1272dc19c6696
|
|
| MD5 |
33e8ed2786db120e4afb9dc5ea66e655
|
|
| BLAKE2b-256 |
37d9926af5bc4554775c97075974b2b1a23921200131d20bad617a144e226844
|
File details
Details for the file rejection_sampler-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rejection_sampler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09812ef3c28a21f1d28a32a6fd1cbda6219a576a3b3544177a014072adf4d819
|
|
| MD5 |
44dfb9e7d3d3bd85dbfa9c1c9dbc5f04
|
|
| BLAKE2b-256 |
98d7bf31be79c15ca6fcc1d34da4280f096cc304622f6d6a2306e4e43cb497d5
|