#title: Week 7 Assignment: Oil Change
#author: Julie Lapine
#date: 30 June 2021
#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.
mechanicOne = 12 #Mechanic one takes 12 minutes to change oil
mechanicOneChance = 0.2 #chance of mechanic one changing oil
mechanicTwo = 3 #Mechanic two takes 3 minutes to change oil
mechanicTwoChance = 0.8 #chance of mechanic two changing oil
m = (mechanicOne*mechanicOneChance) + (mechanicTwo*mechanicTwoChance)
#calculate the mean
s = (((mechanicOne^2)*mechanicOneChance) + ((mechanicTwo^2)*mechanicTwoChance)) - (m^2)
#calculate the standard deviation
#(1) Use simulation to generate 10000 service times and estimate the
#mean service time for yourself.
x = rnorm(10000,m,s) #mean=4.8, sd=12.96
hist(x, probability = TRUE)#(2) Summarize your data with R code hist(x, probability = TRUE)
mean(x)#and mean(x), where x is an array representing your simulated data.
## [1] 4.819378
abline(v=mean(x), col="green")#Add to the histogram two vertical lines indicating the simulated mean
abline(v=m, col="blue") #and the theoretical mean.
curve(dnorm(x, m, s), add=TRUE, col = "red")

#(3) Add the curve of the probability density function (which is a mixture
#of two exponential pdfs) of the service time. For a mixture of two
#exponential distributions, refer to formula (15) of the paper
options(max.print = 50)
print(x)#d) the first 50 values of the simulated data
## [1] -12.3106897 40.1746397 6.3178812 29.9587099 17.0840165 -1.9264982
## [7] 8.4496902 13.4936401 27.2185736 1.7388705 -9.9482463 -7.1679100
## [13] -18.2017679 20.9751100 1.6401973 9.5311970 -9.7437453 12.4430839
## [19] 8.7676844 12.5439038 7.7493151 6.4188728 -11.9951938 -13.8722098
## [25] -13.0094255 7.1889833 -5.1706388 -0.6318074 -10.0160160 14.1080411
## [31] 12.3884922 -18.7813269 16.9789842 -4.2110787 12.4660868 -1.5809493
## [37] 14.6285923 37.0378121 9.7522881 14.6925031 4.0391103 -7.8668691
## [43] -10.5505613 8.2129481 -15.1265256 -3.5803613 -9.0369037 2.9007458
## [49] 15.1369569 -8.4214983
## [ reached getOption("max.print") -- omitted 9950 entries ]
#e) summary graphs/tables
#f) a discussion of your findings
#From this simulation, we see that the theoretical mean and the simulated mean
#are almost the same. The theoretical mean ended up being 4.8, while the
#simulated mean ended up being 4.627012. In the scheme of 10,000 trials,
#this is a rather close outcome. It also turned out that the average expected
#wait time is 4.8 minutes, when one mechanic takes 12 minutes and you have a 0.2
#chance of getting that mechanic, and the second mechanic takes 3 minutes and
#you have a 0.8 chance of getting the faster mechanic.
#g) code
#See above