Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
nsim = 10^4 lambda = 6 max.factorial = function(x,u){ k = x parf=1 while (parf*u<1){ k = k + 1 parf = parf * k } k = k - (parf*u>1) return (k) } x = rep(floor(lambda), nsim) for (t in 2:nsim){ v1 = ceiling((log(runif(1))/log(lambda))+x[t-1]) ranj=max(0,v1):max.factorial(x[t-1],runif(1)) x[t]=sample(ranj,size=1) } barplot(as.vector(rbind( table(x)/length(x),dpois(min(x):max(x), lambda))),col=c("sienna","gold"))
As you can easily check by running the code, it does not work. My student actually majored my MCMC class and he spent quite a while pondering why the code was not working. I did ponder as well for a part of a morning in Warwick, removing causes for exponential or factorial overflows (hence the shape of the code), but not eliciting the issue… (This now sounds like lethal fugu sashimi! ) Before reading any further, can you spot the problem?!
x = rep(lambda, nsim) for (t in 2:nsim){ v1=ceiling((log(runif(1))/log(lambda))+x[t-1]) ranj=max(0,v1):max.factorial(x[t-1],runif(1)) if (length(ranj)>1){ x[t] = sample(ranj, size = 1) }else{ x[t]=ranj} }
The culprit is thus the R function sample which simply does not understand Dirac masses and the basics of probability! When running
> sample(150:150,1) [1] 23
you can clearly see where the problem stands…! Well-documented issue with sample that already caused me woes… Another interesting thing about this slice sampler is that it is awfully slow in exploring the tails. And to converge to the centre from the tails. This is not very pronounced in the above graph with a mean of 6. Moving to 50 makes it more apparent:
Filed under: Books, Kids, pictures, R, Running, Statistics, University life Tagged: convergence assessment, ENSAE, Gibbs sampling, Monte Carlo Statistical Methods, Poisson distribution, R, sample, sampling from an atomic population, slice sampling, slow convergence
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.