I am trying to simulate how soon the product of uniform(0,1) random number will be < exp(-3). So I write like below
n = c(10^2, 10^4)
p <- 0:6
df2 <- data.frame(expand.grid(n, p))
names(df2) <- c("n", "p")
find.p <- function(n, p){
v <- c()
for(i in 1:n){
product <- 1
j <- 0
while(product > exp(-3)){
product <- product * runif(1)
j <- j + 1
}
v[i] <- j - 1
}
v.re <- table(v)/n
print(p)
return(v.re[names(v.re)==p])
}
df2$pr <- sapply(df2$n, find.p, p=df2$p)
I record when the product < exp(-3). I want to know what is the probability that the product < exp(-3) at 0:6 and redo 100 and 10000 times, expecting convergence. I try to do it with sapply, but I always got wrong result the distribution should be approximate to Poisson (3).
Any suggestion would help me a lot!
Aucun commentaire:
Enregistrer un commentaire