Reputation: 335
Consider the Poisson distribution x1, x2, ... ~ pois(1)
with lambda=1.
I want to write a function that receives a number as input (consider it a) and gives us as output the smallest n (minimum n) which is true for sum(xi)>=a, i=1:n
.
I think using a while
loop could be fine for this situation (but I am not sure that it's the best way). Perhaps it can be done using other loops like for
loop. I do not know how to handle this situation containing a Poisson distribution in R?
Upvotes: 0
Views: 506
Reputation: 76402
A while
loop is simple enough to code. It accumulates the values of rpois
in s
for sum while s < a
, counting the iterations in n
.
minpois <- function(a){
n <- 0L
s <- 0L
while(s < a) {
n <- n + 1L
s <- s + rpois(1L, lambda = 1)
}
n
}
set.seed(2020)
minpois(10)
#[1] 12
Upvotes: 2