To draw a comparison between this Normal generator (that I will consider as von Neumann’s) and the Box-Müller polar generator,
#Box-Müller
bm=function(N){
a=sqrt(-2*log(runif(N/2)))
b=2*pi*runif(N/2)
return(c(a*sin(b),a*cos(b)))
}
#vonNeumann
vn=function(N){
u=-log(runif(2.64*N))
v=-2*log(runif(2.64*N))>(u-1)^2
w=(runif(2.64*N)<.5)-2
return((w*u)[v])
}
here are the relative computing times
> system.time(bm(1e8))
utilisateur système écoulé
7.015 0.649 7.674
> system.time(vn(1e8))
utilisateur système écoulé
42.483 5.713 48.222
September 15, 2022 at 8:16 am
[…] article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]