Description of the Problem

Two mechanics are changing oil filters for the arriving customers. The service time has an Exponential distribution with mean 12 minutes for the first mechanic, and mean 3 minutes for the second mechanic. When you arrive to have your oil filter changed, your probability of being served by the faster mechanic is 0.8.


Problem Solving

The first step was to set the seed to 123 for reproducibility. Next, we set n to 10,000 to generate 10,000 service times. We assigned lambda1 to 1/12 for the first mechanic and lambda2 to 1/3 for the second mechanic. We set p to 0.8, representing the probability of being serviced by the fastest mechanic.


set.seed(123)

n <- 10000

lambda1 <- 1/12 
lambda2 <- 1/3  
p <- 0.8 


The second step is to write an ifelse statement: if a randomly generated number is smaller than the probability p, generate service times using the exponential distribution with lambda2 (second mechanic) otherwise generate service using the exponential distribution with lambda1 (first mechanic).

After the ifelse statement, find the estimated mean of the service times using the mean function. Then, extract the first fifty values of the simulated data.


service_time <- ifelse(runif(n) < p, rexp(n, lambda2), rexp(n, lambda1))

est_mean <- mean(service_time)

fifty_values <- service_time[1:50]


The last step is to create a histogram of the service times. We will add two vertical lines: one for the estimated mean and one for the theoretical mean. The theoretical mean is calculated as the probability times 1/lambda2, plus 1 - p (which is 0.2) times 1/lambda1. Adding the curve function which represents the probability density function (PDF) of a mixture of two exponential distributions. It combines two exponential distributions with different rates, weighted by the probability p for the faster mechanic and 1 minus p for the slower mechanic. Specifically, it calculates the PDF value for a given x by adding p times the PDF of the exponential distribution with rate lambda2 and 1 minus p times the PDF of the exponential distribution with rate lambda1.


hist(service_time, probability = TRUE, main = "Density Histogram of Service Times", 
     xlab = "Service Time (minutes)", col = "lightblue", border = "black")


abline(v = est_mean, col = 2, lty = 2, lwd = 2)


theoretical_mean <- p * (1/lambda2) + (1 - p) * (1/lambda1)


abline(v = theoretical_mean, col = 3, lty = 3, lwd = 2)


curve(p * dexp(x, rate = lambda2) + (1 - p) * dexp(x, rate = lambda1), 
      col = "blue", lwd = 2, add = TRUE)

legend("topright", legend = c("Simulated Mean", "Theoretical Mean"), 
       col = c(2, 3), lty = c(2, 3), lwd = 2, bty = "n")


Discussion of the finding

In the density histogram of service times, we can see that the simulated mean and the theoretical mean are almost identical. Additionally, the probability density function (PDF) closely follows the density of the service times.