Skip to content

Instantly share code, notes, and snippets.

@stephaniehicks
Last active August 29, 2015 14:08
Show Gist options
  • Save stephaniehicks/c1bb694bba08458bee7f to your computer and use it in GitHub Desktop.
Save stephaniehicks/c1bb694bba08458bee7f to your computer and use it in GitHub Desktop.
Simulating trick-or-treaters as a Poisson Process
# Load library
library(ggplot2)
# ToT = Trick-or-Treators
tEnd = 3*60*60 # 3 hours (in seconds)
lambdaMin = 1 # Define the rate as 1 ToT per minute
lambdaSec = lambdaMin/60 # converted rate in ToT per second
# Randomly inter-arrival times between ToT using an exponential distribution
t = 0; k = 0
times = NULL
while(t < tEnd){
u = runif(1, 0, 1)
t = t - log(u)/lambda
k = k + 1
times = c(times, t)
}
times = round(times)
# Histogram of interarrival times and their frequency
start = ISOdate(2014, 10, 31, 5, 30, 0, tz = "")
qplot(start + times, binwidth = 60*2, xlab = "Halloween Night",
ylab = "Number of Trick-or-Treaters",
main = "Trick-or-Treaters as a Poisson Process")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment