Search code examples
rplotsimulationexponentialpoisson

Simulating a Poisson Process in R


How would I go about simulating a Poisson process with a rate of lambda = 0.5 arrivals per time unit. The simulation needs to run until there are 8 arrivals, and from that I would like to create a plot that represents this. Could anyone lend a hand? Many thanks in advance.


Solution

  • The inter-arrival times of a Poisson process are independent and exponentially distributed with mean 1/lambda. Here is a reference.

    Therefore, a simple way to simulate the first 8 arrivals of a Poisson process is using the accumulated sum of independent Exponential random variables (results may differ because they're random):

    X <- cumsum(rexp(8, rate = 0.5))
    # [1] 1.640417 1.855639 1.988687 2.936651 6.192125 7.682924 8.159302 8.963526
    

    As for plotting it, depending on what kind if plot you need: A very simple option using the x-axis as time and y-axis as the number of occurrences until that point of time. Using ggplot2:

    library(ggplot2)
    ggplot(data.frame(t = X, count = seq_along(X)), aes(x = t, y = count)) +
      geom_step()
    

    Result:

    enter image description here