0: Beta(2,5) unimodal asymmetric / 1: bimodal mixture hard to sample
Random numbers come from a deterministic LCG with fixed seed 42. "Resample" advances the seed.
Blue solid = target p(x) / green dashed = envelope M*q(x) / green dots = accepted / red crosses = rejected
Blue bars = normalized histogram of accepted samples / white line = target p(x)
Rejection sampling picks a proposal q(x) and a constant M with M >= sup p(x)/q(x), draws X ~ q(x) and U ~ U(0,1), and accepts when U < p(X)/(M*q(X)).
Acceptance probability of any trial:
$$P(\text{accept}) = \int \frac{p(x)}{M\,q(x)}\,q(x)\,dx = \frac{1}{M}\int p(x)\,dx = \frac{1}{M}$$Conditional distribution of accepted samples:
$$f_{X\,|\,\text{accept}}(x) = p(x)$$Expected number of accepted samples:
$$\mathbb{E}[N_{\text{accept}}] = \frac{N}{M}$$A larger M makes the envelope safer to satisfy but lowers the acceptance rate proportionally to 1/M, increasing computational cost.