Trying to understand the inverse transform method for generating a Poisson random variable

别来无恙 提交于 2019-12-11 18:24:04

问题


The algorithm Wikipedia gives for generating Poisson-distributed random variables using the inverse transform method is:

init:
     Let x ← 0, p ← e^−λ, s ← p.
     Generate uniform random number u in [0,1].
while u > s do:
     x ← x + 1.
     p ← p * λ / x.
     s ← s + p.
return x.

I implemented it in R:

f<-function(lambda)
{
x<-0
p<-exp(-lambda)
s<-p    
u<-runif(1)    
while (u>s )
{
   x<-x+1    
   p<-p*lambda/x    
   s<-s+p
}    
return(x)
}

but I don't understand how this generates values of this random variable, and I also think the code is incomplete because the output is always 1.

Could someone please explain?

来源:https://stackoverflow.com/questions/55426744/trying-to-understand-the-inverse-transform-method-for-generating-a-poisson-rando

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!