Advanced sampling and rare eventsLesson 62 of 76 · 8 min
Accept or reject a proposal
Construct a rejection sampler using a valid envelope.
For a continuous variable, a probability density is a relative height whose integral over a region gives probability. Its height is not the probability of observing one exact continuous value. A rejection sampler proposes from q and accepts with probability p(x)/(M q(x)) when M q bounds the target density p.
Use a uniform proposal on [0,1] for target density p(x)=2x. The bound makes acceptance probability x. The accepted values should have target mean two thirds, and the acceptance fraction should approach one half.
The envelope bound must hold over the whole contributing support. Choosing M too small makes an acceptance probability above one and invalidates the algorithm. Poorly matched envelopes can also waste most proposals, especially in higher dimensions. Count attempted and accepted proposals separately.
- Target density
- Proposal × envelope multiplier
Will accepted samples remain uniformly distributed when high proposals are accepted more often?
Write a prediction (optional)
Seeded teaching simulation
Select Run Python below with the code unchanged.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
Published example output
This result comes from the original Python. Your runs appear above. Build environment ↗
Proposals / accepted: 30000 14906 Acceptance fraction / target: 0.4969 0.5 Accepted sample mean / target: 0.6655 0.6666666666666666 All accepted values in support: True
Read the first result
The rejected proposals are part of the computational cost. Only accepted draws belong to the target sample in this construction.
Try an edit
Replace accept = rng.random(len(proposal)) < proposal with accept = rng.random(len(proposal)) < proposal**2 and round(accept.mean(), 4), .5 with round(accept.mean(), 4), 1/3 and round(accepted.mean(), 4), 2/3 with round(accepted.mean(), 4), 3/4. Select Run Python again. Compare Acceptance fraction / target and Accepted sample mean / target. With a uniform proposal the new density is 3*x**2, whose maximum is 3, so the acceptance threshold is proposal**2.
Scope. The target is simple and normalised. General rejection samplers need a justified global envelope and may be inefficient.
Try a fresh case
An envelope allows an acceptance probability above one in part of the support. What should you do?
Need a hint?
Hint 1
Check the maximum target-to-proposal density ratio.
Hint 2
Ensure every stated acceptance probability lies in [0,1].