Skip to content

Instantly share code, notes, and snippets.

@mick001
Created August 28, 2015 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mick001/46c75182b8bef59b9e5a to your computer and use it in GitHub Desktop.
Save mick001/46c75182b8bef59b9e5a to your computer and use it in GitHub Desktop.
Estimating arrival times of people in a shop using R. Part 4. Full article at: http://www.firsttimeprogrammer.blogspot.com/2015/07/estimating-arrival-times-of-people-in.html
#-------------------------------------------------------------------------------
# Probability distributions
#-------------------------------------------------------------------------------
# Density plot
#Since the rate is given as person/hour we need to set time in hours
#Initial time Final time Step
#0 hour 0.15 of an hour 0.001 of an hour
#0 minutes 9 minutes 0.06 minutes = 3.6 seconds
# Time vector
time <- seq(0,0.15,0.001)
# 1 row, 2 columns
par(mfrow=c(1,2))
# Probability distribution plot
plot(time*60,dexp(time,rate=lambda),type='l',xlab='Minutes',
ylab='Probability density',main='Probability distribution',col='blue',lwd=2)
# Cumulative distribution plot
plot(time*60,pexp(time,rate=lambda),type='l',xlab='Minutes',
ylab='Cumulative probability',main='Cumulative distribution',col='red',lwd=2)
#-------------------------------------------------------------------------------
# Useful functions
#-------------------------------------------------------------------------------
# T = waiting time before an arrival (Random variable)
l = lambda
# Note: since it is more practical, t must be entered in seconds,
# it will be converted into hours later
# Probability of waiting time of the arrival (T) being greater than t
# P(T > t)
p.right <- function(t)
{
t <- t/60/60
p <- exp(-l*t)
return(p)
}
# Probability of waiting time of the arrival (T) being less than t
# P (T < t) cumulative dist
p.left <- function(t)
{
t <- t/60/60
p <- 1 - exp(-l*t)
return(p)
}
print('Probability of waiting time before an arrival being greater than 3 minutes')
p.right(3*60)
print('Probability of waiting time before an arrival being less than 3 minutes')
p.left(3*60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment