Generate a Poisson process using R

I want to generate a process where a Poisson random variable is implemented at each step, this implementation must be saved, and then it must implement the next random Poisson variable and add it to the sum of all the implementations earlier. In addition, there should be a chance that at every step this process will stop. Hope that makes sense to you guys ... Any thought appreciated!

+3
source share
2 answers

More compactly, select one geometrically distributed random number for the total number of steps taken before stopping, then use cumsumto summarize that many Poissons are rejected:

stopping.prob <- 0.3  ## for example
lambda <- 3.5         ## for example
n <- rgeom(1,1-stopping.prob)+1  ## constant probability per step of stopping
cumsum(rpois(n,lambda))
+7

, ?

.

lambda <- 5

, .

th <- 0.999

1000.

bin <- numeric(1000)

. , , "" ( 0 1). th, . th ( ), .

for (i in 1:length(bin)) {
    if (runif(1) < th) {
        bin[i] <- rpois(1, lambda = lambda)
    } else {
        stop("didn't meet criterion, exiting")
    }
}

, .

bin <- bin[bin != 0]

cumsum .

cumsum(bin)
+1

All Articles