GROUP3 108356021 洪御哲、108356035 楊鈞宜、109356003 楊仁瀚
Three dices are to be rolled. Suppose two of the three are fair dies, but one die is unfair with probability of 0.25 to be 2 and equal probabilities for (1, 3, 4, 5, 6). Please write a simulation program (use sample( )) that calculates the expected value of the smallest number of rolling the three dies (三顆骰子中最小的值).
set.seed(9487)
sim.runs = 1:100000
unfair_prob = rep((1-0.25)/5, 6) # unfair die
unfair_prob[2] = 0.25
die_result = matrix(NA, nrow=1, ncol=length(sim.runs))
for (i in sim.runs) {
# rolling the 3 dies every time
die1 = sample(1:6, 1, prob=rep(1/6, 6), replace=T)
die2 = sample(1:6, 1, prob=rep(1/6, 6), replace=T)
die3 = sample(1:6, 1, prob=unfair_prob, replace=T)
# the smallest number of 3 dies
die_result[i] = min(die1, die2, die3)
}
# expected value of smallest number of rolling the 3 dies
mean(die_result)
## [1] 2.00254
You are a manager of SAP, and you just hire a salesperson from Oracle. Suppose the salesperson has to visit 20 potential clients this month. Before he/she makes these visits, you think high skill or low skill is equally likely. If there is high skill, then the probability of making a sale is 2/3 in each of the 20 visits. If there is low skill, then the probability of making a sale is 1/3 in each of the 20 visits. Use the sample( ) and whatever functions needed in R to write a simulation program for the case described above. Simulate the scenario for 1,000 runs and answer the following questions based on simulation results.
# prerequisites
phigh = 1/2
plow = 1/2
phigh.success = 2/3
phigh.fail = 1 - phigh.success
plow.success = 1/3
plow.fail = 1 - plow.success
# n = 20 clients visiting / month
sales.sim = function(n=20) {
sim.modules = rep(NA, n)
# simulate whether is high skill or low skill
# notation 1: high skill; -1: low skill
sim.labels = sample(c(1,-1), 1, replace = T, prob = c(phigh, plow))
# there's one labels is equal to 1 at least
if (sim.labels == 1) {
sim.modules = sample(c("high.success", "high.failure"), n,
prob = c(phigh.success, phigh.fail), replace = T)
}
else if (sim.labels == -1) {
sim.modules = sample(c("low.success", "low.failure"), n,
prob = c(plow.success, plow.fail), replace = T)
}
return(sim.modules)
}
Let’s get simulation!
# simulation times
S = 1000
sim.table = replicate(S, sales.sim())
num.high = c()
num.high.success = c()
num.success = c()
for (i in 1:ncol(sim.table)) {
num.high[i] = sum(any((sim.table[,i] == "high.success")))
num.high.success[i] = sum(sim.table[,i] == "high.success")
num.success[i] = sum(sim.table[,i]=="high.success") + sum(sim.table[,i]=="low.success")
}
sum(num.success >= 9) / S
## [1] 0.597
sum(num.high.success >= 9)/sum(num.high)
## [1] 0.9796334
sum(num.high.success >= 9) / sum(num.success >= 9)
## [1] 0.8056951
all.prob.mat = matrix(NA, nrow = 20, ncol = 3)
rownames(all.prob.mat) = sprintf("n = %s", seq(1:20))
colnames(all.prob.mat) = c("P(promoted)", "P(promoted|high)", "P(high|policy)")
for (i in 1:nrow(all.prob.mat)) {
all.prob.mat[i,1] = sum(num.success >= i) / S
all.prob.mat[i,2] = sum(num.high.success >= i) / sum(num.high)
all.prob.mat[i,3] = sum(num.high.success >= i) / sum(num.success >= i)
}
all.prob.mat
## P(promoted) P(promoted|high) P(high|policy)
## n = 1 1.000 1.00000000 0.4910000
## n = 2 0.999 1.00000000 0.4914915
## n = 3 0.994 1.00000000 0.4939638
## n = 4 0.968 1.00000000 0.5072314
## n = 5 0.932 0.99796334 0.5257511
## n = 6 0.866 0.99796334 0.5658199
## n = 7 0.764 0.99796334 0.6413613
## n = 8 0.675 0.99389002 0.7229630
## n = 9 0.597 0.97963340 0.8056951
## n = 10 0.520 0.95723014 0.9038462
## n = 11 0.481 0.93075356 0.9501040
## n = 12 0.409 0.81466395 0.9779951
## n = 13 0.329 0.65987780 0.9848024
## n = 14 0.234 0.47657841 1.0000000
## n = 15 0.143 0.29124236 1.0000000
## n = 16 0.071 0.14460285 1.0000000
## n = 17 0.031 0.06313646 1.0000000
## n = 18 0.008 0.01629328 1.0000000
## n = 19 0.001 0.00203666 1.0000000
## n = 20 0.000 0.00000000 NaN
Suppose you go to the college of commerce library (商圖) on Monday evening and would like to borrow a book. You are told that the book has been checked out the previous Thursday. Assume no one else is waiting for the book. The library staff tells you that borrowers return books after 4, 5, 6, or 7 days, with probabilities of 0.1, 0.2, 0.3, 0.4. Note that the library is open 7 days a week. As before, 50% of students return their books to a “foreign” library (社圖/總圖), resulting in an extra 2-day delay before the book arrives back to the home library. You decide to check the status of the book every evening. What is the probability you will need to wait until Wednesday evening to get the book? Write a Monte-Carlo simulation program to compute the probability (Hint: the probability should be close to 4/19).
Assumption: 從他借書的天數開始算4,5,6,7天可能會還書 Uncertainty: - 「借書的人會在幾天內還書」 - 「還書是哪個圖書館」
還商圖: 4天還書是Monday、5天Tuesday、6天wednesday、7天Thursday,只要4,5,6天還書的話都可以在Wednesday evening 拿到書。 還foreign library: 4天還書
# 4: 0.1, 5: 0.2, 6:0.3, 7:0.4
pReturn = seq(0.1, 0.4, 0.1)
pForeignLibrary = 0.5
pOrigninLibrary = 0.5
return.sim = function() {
return.modules = rep(NA, 2)
# whether's libary the borrower return; 1: origin, -1: foreign
sim.library = sample(c(1, -1), 1, replace = T,
prob = c(pOrigninLibrary, pForeignLibrary))
return.modules[1] = sim.library
if (sim.library == 1) {
return.modules[2] = sample(c("Mon", "Tue", "Wed", "Thur"), 1, prob = pReturn)
}
else {
return.modules[2] = sample(c("Wed", "Thur", "Fri", "Sat"), 1, prob = pReturn)
}
return(return.modules)
}
S = 10000
sim.library.table = replicate(S, return.sim())
num.got.book = c()
for (i in 1:ncol(sim.library.table)) {
num.got.book[i] = sum((sim.library.table[,i] == "Wed"))
}
sum(num.got.book > 0)/S
## [1] 0.1999
4/19
## [1] 0.2105263
Please store the answers in a pdf file and upload the file onto WM5. Each group submits only ONE copy. Make sure names and IDs of students within each group can be found on the file. For Q3, Q4 & Q5, please show the written simulation program on the document too. NO late submission will be accepted.