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 M=2M=2 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.

Accept under a valid envelope
00.250.50.75100.571.11.72.3DensityProposal x
00.250.50.75100.571.11.72.3DensityProposal x
P(accept∣x)=2x2⋅1=xP(\mathrm{accept}\mid x)=\frac{2x}{2\cdot1}=x
  • Target density
  • Proposal × envelope multiplier
Reference illustration. Reference target density 2x on [0,1]. A uniform proposal with envelope multiplier 2 is accepted with probability x. The dashed envelope dominates the target; accepted values have mean 2/3.

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.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

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.

Back to the Python editor

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?

Choose an answer
Need a hint?
Hint 1

Check the maximum target-to-proposal density ratio.

Hint 2

Ensure every stated acceptance probability lies in [0,1].

Your notes