Search This Blog

Monday, March 10, 2014

Generating Poisson Distributed time events with R

In this example, I would like to generate a set of values that is based on a set of discrete arrivals. Poisson distribution is ideal for generating these random values. Lets say I want to generate a set of time events for cars arriving into an on-street parking next to a strip mall at a rate of average 2 to 3 per minute. Maximum arrivals are at 9:00 am. Total number of parking available is 10. If parking is available, a car parks for a mean of 45 minutes. If not, drivers look for additional parking around.

To start with, we can start arrivals at 6 AM till 10:00 PM. That is 16 hours. Also, let us assume an average of 3 cars per minute. To make sure, we can generate sub-minute arrivals, we need to convert everything to seconds. In this first attempt, we will ignore that arrival rate is time dependent and assume a homogeneous arrival rate through out the day. We will tackle inhomogeneous time-dependent arrival rates in a future post.

To generate these numbers, we can use the following formula

rate <- 3/60
period <- 16*60*60
start <- 6*60*60
cars <- runif(rpois(1,period*rate), min=start, max=start+period)



To see, how the data is distributed over time, we can see the individual arrivals per hour as a histogram with the following command.

> hist(cars, breaks=16)




To see arrivals per minute, we can use the following command.

> hist(cars, breaks=16*4)



To see the actual data generated, we can just type cars on the prompt to see the generated vector.


We will carry on from here...





No comments: