Rejection Sampling
Overview 1
Rejection sampling is one of the Monte Carlo methods, where a proposal distribution , easy to sample from, is used to obtain samples following a given distribution , especially when it’s difficult to sample from directly.
Build-up
Let’s assume we are given a random variable with a probability density function . We want to sample from the distribution of , but it’s challenging. (For instance, if it’s difficult to find the inverse function of the cumulative distribution function of , implementing sampling is challenging.) And for all , choose another distribution and a constant that satisfies . Here, should be an easily samplable distribution like uniform or normal distribution and resemble the target distribution as much as possible.
Now, suppose we’ve obtained through sampling the random variable and through sampling for , where is a uniform distribution. Upon comparing two values for ,
- If , then is accepted into the sample.
- If , then is rejected from the sample.
Definition
Let be a random variable with the probability density function of . Choose another probability density function and a constant that satisfy the following.
Rejection sampling is the method of extracting sample as follows.
Here, is called the target distribution, and is referred to as the proposal distribution.
Explanation
This method can be used when it is specifically known about , but difficult to sample from it. In essence, rejection sampling means selecting a that is easy to sample from and satisfies , sampling from , and probabilistically rejecting based on the difference (ratio) between and . The figure above shows that the bigger the difference between the target and the proposal , the higher the probability of rejection. In other words, the probability originally meant to represent has been mathematically manipulated to represent the probability of being accepted, . Or it can be understood as giving a penalty to the probability of sampling based on the difference because the distribution was changed from to .
When the proposal distribution and the constant are chosen, the acceptance rate of the sample can be calculated as follows. Therefore, the time it takes to collect a sufficient amount of samples depends on , and for optimization, the smallest possible satisfying should be chosen.
Implementing rejection sampling in Julia and creating scatter plots and histograms yields the following.
Code
using Distributions
using Plots
using LaTeXStrings
N = 20000
target(x) = 0.5*pdf.(Normal(-3, 0.8), x) + pdf.(Normal(3, 3), x)
proposal = Normal(0, 5)
samples = Float64[]ㅌ
accepted = Array{Float64}[]
rejected = Array{Float64}[]
for i ∈ 1:N
x = rand(proposal)
y = rand(Uniform(0,4.2pdf(proposal, x)))
if y < target(x)
push!(samples, x)
push!(accepted, [x, y])
else
push!(rejected, [x, y])
end
end
accepted = hcat(accepted...)
rejected = hcat(rejected...)
scatter(accepted[1,:], accepted[2,:], label="accepted", color=:blue, markerstrokewidth=0, markersize=2, dpi=300, size=(728,300), legend=:outertopright)
scatter!(rejected[1,:], rejected[2,:], label="rejected", color=:red, markerstrokewidth=0, markersize=2)
savefig("RS3.png")
x = range(-15, 15, length=1000)
plot(x, target(x), label="target "*L"p(x)", framestyle=:none)
histogram!(samples, label="accepted samples", color=:blue, alpha=0.5, bins=100, normed=true, dpi=300, size=(728,300), legend=:outertopright)
savefig("RS4.png")
Environment
- OS: Windows11
- Version: Julia 1.8.3, Plots v1.38.6, Distributions v0.25.80, LaTeXStrings v1.3.0
Christoper M. Bishop, Pattern Recognition annd Machine Learning (2006), p528-531 ↩︎