Suppose a fraction 17% of the microchips produced by a leading manufacturer is defective. For the manufacturer, the number of microchips delivered to its client depends on how many microchips are inspected and accepted by 50 inspectors in the factory. Historically, given that a microchip is defective, an inspector wrongly accepts the chip 10% of the time, thinking it has no defect. If a microchip is not defective, an inspector, however, wrongly rejects the chip 5% of the time. In general, each of the 50 inspectors can inspect 20 chips per hour in a working day of 8 hours (lunch break NOT included in the 8 hours). Use the rbinom( ) and whatever functions needed in R to write a simulation program for the case above. In a working day, the microchips delivered to the client contain some good ones (correctly accepted) and some bad ones. In your simulation program, calculate the ratio of good ones to the sum of good and bad ones. The simulation program should be a function called dailychips. The function must return 1) the daily number of delivered chips (that is random) and 2) the daily ratio of good ones mentioned above. Simulate the daily operations for 1,000 runs and answer the following questions based on simulation results.
Summary:
| Inspect | Goodone | Badone |
|---|---|---|
| Goodone | TP | FP |
| Badone | FN | TN |
prob.defect = 0.17
prob.normal = 1 - prob.defect
prob.defet.accept = 0.1
prob.goodone.reject = 0.05
total.microchips = 20*8*50
set.seed(9487)
dailychips = function (nday=1) {
# first sample inspected goodones & badones, goodone denotes 1 ; badone denotes 0
microchips = sample(c(1,0), total.microchips * nday,
prob=c(prob.normal, prob.defect), replace=TRUE)
# defect but wrongly accept
defect.accept = rbinom(1, sum(microchips==0), prob=prob.defet.accept)
# goodone and turely accept
goodone.accept = rbinom(1, sum(microchips), prob=1-prob.goodone.reject)
# real goodones we can deliver
real.good = goodone.accept + defect.accept
ratio = goodone.accept / real.good
return(c(real.good, ratio))
}
S = 1000
sim.table = replicate(S, dailychips())
sum(sim.table[2,]>=0.98)/S
## [1] 0.269
not truth
sum(sim.table[1,] >= 6400)/S
## [1] 0.908
Use the dailychips function in Q1 to simulate montlychips and quarterlychips. The monthlychips is the sum of delivered chips over 30 days, whereas the quarterlychips is the sum of delivered chips over 90 days respectively. Ignore good ratios in the two cases and focus on the number of delivered chips over 30 and 90 days. Generate 1,000 random samples for the two numbers (montly and quarterly) of delivered chips.
monthly.sim.table = replicate(S, dailychips(nday=30))
quartly.sim.table = replicate(S, dailychips(nday=90))
Generate the histograms (use hist( ) in R and set breaks=20) of the simulated montlychips and quarterlychips. Do they look like normal distributions?
hist(monthly.sim.table[1,], breaks=20, main="Monthly chips", xlab="Delivered chips")
hist(quartly.sim.table[1,], breaks=20, main="Quartly chips", xlab="Delivered chips")
Use the shaprio.test ( ) in R to perform normality tests for montlychips and quarterlychips (if p-value<=0.05, reject H0: Normality holds). Do you find any evidence for normality? If yes, can you provide theoretical explanations for that (hint: check chapter 3 for a theorem)?
shapiro.test(monthly.sim.table[1,])
##
## Shapiro-Wilk normality test
##
## data: monthly.sim.table[1, ]
## W = 0.99722, p-value = 0.08224
shapiro.test(quartly.sim.table[1,])
##
## Shapiro-Wilk normality test
##
## data: quartly.sim.table[1, ]
## W = 0.99725, p-value = 0.08592
No, both of them are not normal distribution, because their p-value all larger than 0.05.
Your job in this homework is to read the case and apply simulation modeling to answer the following three questions.
| Scenario | Fixed Meal Price | Probability |
|---|---|---|
| Very healthy market(VH) | $20.00 | 25% |
| Healthy market(H) | $18.50 | 35% |
| Not so healthy market(NH) | $16.50 | 30% |
| Unhealthy market(UH) | $15.00 | 10% |
| Position | Wage($/hour) | Minimum Staff | Maximum Staff |
|---|---|---|---|
| Chef | $16.00 | 1 | 1 |
| Wait Staff | $3.00 | 2 | 4 |
| Kitchen Staff | $7.00 | 2 | 3 |
| Total Labor Cost | $5040 | $6860 |
fixed.cost = 3995
meal.cost = 11
prob.VH = 0.25; price.VH = 20.0
prob.H = 0.35; price.H = 18.5
prob.NH = 0.30; price.NH = 16.5
prob.UH = 0.10; price.UH = 15.0
revenue = num of meals sold * (fixed meal price - cost of meal served) - (fixed costs + labor costs)
idx = 0
labor_cost_outcome = c()
for (i in 2:4) {
labor_cost_outcome[idx] = 7*5*4*(16 + i*3 + 2*7)
idx = idx + 1
labor_cost_outcome[idx] = 7*5*4*(16 + i*3 + 3*7)
idx = idx + 1
}
labor_cost_outcome = sort(labor_cost_outcome)
labor_cost_outcome
## [1] 5460 5880 6020 6440 6860
set.seed(9487)
sim.restaurant = function() {
meal.sold = rnorm(1, mean = 3000, sd = 1000)
meal.price = sample(c(price.VH, price.H, price.NH, price.UH), 1,
prob=c(prob.VH, prob.H, prob.NH, prob.UH))
labor.costs = sample(labor_cost_outcome, 1,
prob=rep(1/length(labor_cost_outcome),length(labor_cost_outcome)))
revenue = meal.sold * (meal.price-meal.cost) - fixed.cost - labor.costs
return(revenue)
}
S = 10000
sim.retaurant.revenue = replicate(S, sim.restaurant())
sim.retaurant.revenue[1:5]
## [1] 10458.395 -4983.497 1429.269 10463.269 5144.817
mean(sim.retaurant.revenue)
## [1] 10725.77
Ans:
Sanjay’s expected monthly salary at Gentle Lentil is $10891.65, compared to his monthly salary at the consulting firm ($80,000/12=6666.67) is better.
mothly salary of at least $3500. If earnings were between $3500 and $9000, all such moneys would go to Sanjay. His aunt will receive 90% of all monthly earnings in excess of $9000
partnership.revenue = rep(NA, S)
for (i in 1:length(sim.retaurant.revenue)) {
if (sim.retaurant.revenue[i] < 3500) {
partnership.revenue[i] = 3500
}
else if (sim.retaurant.revenue[i] >= 3500 & sim.retaurant.revenue[i] <= 9000) {
partnership.revenue[i] = sim.retaurant.revenue[i]
}
else {
partnership.revenue[i] = 0.1 * sim.retaurant.revenue[i]
}
}
mean(partnership.revenue)
## [1] 3176.063
If Sunjay accepted the partership, Sanjay’s expected monthly income at Gentle Lentil would plunge to $3163.922 which is lower than consulting firm and the salary without the partnership at Gentle Lentil.
summary(sim.retaurant.revenue)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -14012 4203 9920 10726 16402 53448
summary(partnership.revenue)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 900.3 1511.7 2718.3 3176.1 3500.0 8999.4
We can use the probability of beat the salary of consulting firm. If startup the restaurant Gentle Lentil, P(X > 6667)
sum(sim.retaurant.revenue > 6667)/S
## [1] 0.6407
大概6成的把握會贏 consulting firm’s salary