code that generates numbers from the Poisson distribution for small lambdas

42 views Asked by At

Why this code only works for small lambda values ? I don't quite understand why it breaks down for large ones

poisson_numbers <- function(lambda, n) {
  x <- numeric(n)
  U <- runif(n, 0, 1)
  print(U)
  for (j in 1:n) {
    i <- 0
    p <- exp(-lambda)
    suma <- p
    while (suma < U[j]) {
      i <- i + 1
      p <- p * lambda / i
      suma <- suma + p
    }
    x[j] <- i
  }
  return(x)
}


0

There are 0 answers