How to Simulate a Poisson Process in R

R Guides

The Poisson process is one of the most commonly used counting processes in statistics. It’s often used when we’re counting the occurrences of certain events that seem to happen at a certain rate, but completely at random without a certain structure.

For example, we may know that monsoons occur in a certain region at a rate of 4 per year, but the timing of the monsoon occurrences seem to be completely random.  To model the occurrences of monsoons, we could use a Poisson process.

In order to simulate a Poisson process in R, we need to find the number of successful arrivals Si for i = 1, 2, 3… as cumulative sums of exponential interarrivals. Then, we simply need to plot the points [Si , i] with a step interpolation.

The following code illustrates how to do so in R:

#define rate of occurrence and max amount of time to plot
occurrenceRate <- 4
maxTime <- 50

#find the number 'n' of exponential random variables vs. required by imposing
#that Pr{N(t) <= n} <= 1 - eps for a small eps
n <- qpois(1-1e-10, lambda = occurrenceRate * maxTime)

#simulate 'n' exponential interarrivals using the occurrence rate defined above
X <- rexp(n = n, rate = occurrenceRate)

#find cumulative sum of occurrences starting at time = 0
S <- c(0, cumsum(X))

#plot the cumulative sum of occurrences from time = 0 to 50 with
#a step interpolation (type = "s")
plot(x = S, y = 0:n, type = "s", xlim = c(0, maxTime))

This code produces the following plot that shows the simulation of the Poisson process:

Leave a Reply

Your email address will not be published. Required fields are marked *