chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:30:25 +08:00
commit f19b2512d7
562 changed files with 38082 additions and 0 deletions
@@ -0,0 +1,34 @@
import random
import numpy as np
def rejection_sampling(func, rv, k, n):
"""
perform rejection sampling n times
Parameters
----------
func : callable
(un)normalized distribution to be sampled from
rv : RandomVariable
distribution to generate sample
k : float
constant to be multiplied with the distribution
n : int
number of samples to draw
Returns
-------
sample : (n, ndim) ndarray
generated sample
"""
assert hasattr(rv, "draw"), "the distribution has no method to draw random samples"
sample = []
while len(sample) < n:
sample_candidate = rv.draw()
accept_proba = func(sample_candidate) / (k * rv.pdf(sample_candidate))
if random.random() < accept_proba:
sample.append(sample_candidate[0])
sample = np.asarray(sample)
assert sample.shape == (n, rv.ndim), sample.shape
return sample