I was trying to illustrate that rejection sampling is inefficient when an alternative approach is available that does not throw away samples. (This post obviously is a candidate for migration to SO, but I feel that there might also be a statistical aspect to it.)
My example was going to be simulating $Beta(2,2)$ variates with a rejection algorithm and rbeta(N,2,2)
. My code is as follows:
# densityatmode <- dbeta(.5,2,2)=1.5
rejectionsampling_beta22 <- function(N){
px <- runif(N,min=0,max=1)
py <- runif(N,min=0,max=1.5)
ikeep <- (py < 6*px*(1-px))
return(px[ikeep])
}
samples <- 1500000
The acceptance probability is area under density/area in box
, area under density
of course equal to one, so 1/1.5, or equivalently, we need 1.5 as many candidate samples when using rejection.
library(microbenchmark)
result <- microbenchmark(rejectionsampling_beta22(1.5*samples),rbeta(samples,2,2))
The result, which is surprising to me, as I felt that rejection of 1/3 of the draws should be wasteful:
Warning message:
In microbenchmark(rejectionsampling_beta22(1.5 * samples), rbeta(samples, :
Could not measure a positive execution time for 33 evaluations.
> result
Unit: nanoseconds
expr min lq mean median uq max neval cld
rejectionsampling_beta22(1.5 * samples) 180171443 254891504 248482870.58 257005186 258866373 329533988 100 b
rbeta(samples, 2, 2) 253091893 254717640 261602707.69 257391248 259902160 333055032 100 c
neval 0 0 63.97 1 1 604 100 a