Q3

(ignore issue descriptions)

set.seed(123)
unfair.dice.probs = rep((1-0.25)/5, 6)
unfair.dice.probs[2] = 0.25
sample(c(1,2,3,4,5,6), 2, prob=unfair.dice.probs, replace=TRUE)
## [1] 3 6
sim.times = 1:10000
result = matrix(NA, 
                ncol=1, nrow=length(sim.times))
dim(result)
## [1] 10000     1
rownames(result) = sim.times

for (i in sim.times) {
  dice.fair = sample(c(1,2,3,4,5,6), 2, replace=TRUE)
  dice.unfair = sample(c(1,2,3,4,5,6), 1, prob=unfair.dice.probs, replace=TRUE)
  result[i] = min(dice.fair[1], dice.fair[2], dice.unfair)
}

cat("Expected value: ", mean(result), "\nAns:", round(mean(result)))
## Expected value:  2.0065 
## Ans: 2

Q4

(ignore issue descriptions)

set.seed(486)
highSkill = 0.5
lowSkill = 1 - highSkill
getSales.highSkill = 2/3
notGetSales.highSkill = 1 - getSales.highSkill
getSales.lowSkill = 1/3
notGetSales.lowSkill = 1 - getSales.lowSkill

getsales.sim=function(n=20){
  simulate.list=rep(NA, n)
  highOrLow=sample(c(1, -1),
                   1, 
                   prob=c(highSkill, lowSkill), 
                   replace=TRUE)
  
  if(highOrLow==1) {
    simulate.list=
      sample(c("getSales.highSkill", "notGetSales.highSkill"),
             n,
             prob=c(getSales.highSkill, notGetSales.highSkill),
             replace=TRUE)
  }
  
  else if (highOrLow==-1) {
    simulate.list=
      sample(c("getSales.lowSkill", "notGetSales.lowSkill"),
             n,
             prob=c(getSales.lowSkill, notGetSales.lowSkill),
             replace=TRUE)
  }
  simulate.list
}

getsales.sim()
##  [1] "notGetSales.lowSkill" "notGetSales.lowSkill" "notGetSales.lowSkill"
##  [4] "notGetSales.lowSkill" "notGetSales.lowSkill" "notGetSales.lowSkill"
##  [7] "notGetSales.lowSkill" "notGetSales.lowSkill" "notGetSales.lowSkill"
## [10] "notGetSales.lowSkill" "getSales.lowSkill"    "getSales.lowSkill"   
## [13] "getSales.lowSkill"    "getSales.lowSkill"    "notGetSales.lowSkill"
## [16] "notGetSales.lowSkill" "getSales.lowSkill"    "notGetSales.lowSkill"
## [19] "notGetSales.lowSkill" "getSales.lowSkill"
S = 1000
getsales.sim.table=replicate(S, getsales.sim())
dim(getsales.sim.table)
## [1]   20 1000
num.highSkill=c()
num.getSales.highSkill=c()
num.notGetSales.highSkill=c()
num.lowSkill=c()
num.getSales.lowSkill=c()
num.notGetSales.lowSkill=c()
num.getSales=c()
num.notGetSales=c()

for(i in 1:ncol(getsales.sim.table)) {
  num.getSales.highSkill[i]=sum(getsales.sim.table[,i]=="getSales.highSkill")
  num.notGetSales.highSkill[i]=sum(getsales.sim.table[,i]=="notGetSales.highSkill")
  num.getSales.lowSkill[i]=sum(getsales.sim.table[,i]=="getSales.lowSkill")
  num.notGetSales.lowSkill[i]=sum(getsales.sim.table[,i]=="notGetSales.lowSkill")
  num.highSkill[i]=sum(getsales.sim.table[,i]=="getSales.highSkill")+
    sum(getsales.sim.table[,i]=="notGetSales.highSkill")
  num.highSkill[which(num.highSkill>0)]=1
  num.lowSkill[i]=sum(getsales.sim.table[,i]=="getSales.lowSkill")+
    sum(getsales.sim.table[,i]=="notGetSales.lowSkill")
  num.lowSkill[which(num.lowSkill>0)]=1
  num.getSales[i]=sum(getsales.sim.table[,i]=="getSales.highSkill")+
    sum(getsales.sim.table[,i]=="getSales.lowSkill")
  num.notGetSales[i]=sum(getsales.sim.table[,i]=="notGetSales.highSkill")+
    sum(getsales.sim.table[,i]=="notGetSales.lowSkill")
}


k=9
ans.1 = sum(num.getSales>=k)/S
ans.2 = sum(num.getSales.highSkill>=k)/sum(num.highSkill)
ans.3 = sum(num.getSales.highSkill>=k)/sum(num.getSales>=k)
cat("A1: ", ans.1)
## A1:  0.583
# P(get promoted | high skill)
cat("A2: ", ans.2)
## A2:  0.9902153
cat("A3: ", ans.3)
## A3:  0.8679245
prob.table = matrix(NA, 
                    ncol = 3, nrow = 20)
rownames(prob.table) = seq(1, 20, 1)
colnames(prob.table) = c("P(promoted)", "P(promoted | high skill)", "P(promoted with high skill | promoted)")

for (threshold in 1:20) {
  prob.table[threshold, 1] = sum(num.getSales>=threshold)/S
  prob.table[threshold, 2] = sum(num.getSales.highSkill>=threshold)/sum(num.highSkill)
  prob.table[threshold, 3] = sum(num.getSales.highSkill>=threshold)/sum(num.getSales>=threshold)
}; prob.table
##    P(promoted) P(promoted | high skill) P(promoted with high skill | promoted)
## 1        1.000              1.000000000                              0.5110000
## 2        0.998              1.000000000                              0.5120240
## 3        0.990              1.000000000                              0.5161616
## 4        0.974              1.000000000                              0.5246407
## 5        0.930              1.000000000                              0.5494624
## 6        0.862              1.000000000                              0.5928074
## 7        0.765              0.998043053                              0.6666667
## 8        0.663              0.998043053                              0.7692308
## 9        0.583              0.990215264                              0.8679245
## 10       0.535              0.968688845                              0.9252336
## 11       0.491              0.935420744                              0.9735234
## 12       0.438              0.845401174                              0.9863014
## 13       0.349              0.681017613                              0.9971347
## 14       0.253              0.495107632                              1.0000000
## 15       0.157              0.307240705                              1.0000000
## 16       0.081              0.158512720                              1.0000000
## 17       0.038              0.074363992                              1.0000000
## 18       0.016              0.031311155                              1.0000000
## 19       0.003              0.005870841                              1.0000000
## 20       0.000              0.000000000                                    NaN

Q5

set.seed(89898)
p.return.dayofweek = seq(0.1, 0.4, 0.1)
p.return.origin.lib = 0.5
p.return.foreign.lib = 1 - p.return.origin.lib

sim.returnBook = function() {
  return.lib = sample(c("origin.lib", "foreign.lib"), 1, replace=T)
  if (return.lib == "origin.lib") {
    return.dayofweek = sample(c("Mon.", "Tue.", "Wed.", "Thu."),
                              1,
                              prob=p.return.dayofweek,
                              replace=T)
  } 
  else {
    return.dayofweek = sample(c("Wed.", "Thu.", "Fri.", "Sat."),
                              1,
                              prob=p.return.dayofweek,
                              replace=T)
  }
  return(return.dayofweek)
}

sim.returnBook()
## [1] "Thu."
sim.times = 1000
sim.recordTable = replicate(sim.times, sim.returnBook())

#return.Wed = c()
#for (i in 1:sim.times) {
#  return.Wed[i] = sum(sim.recordTable[i]=="Wed.")
#}
#sum(return.Wed==1)/sim.times

num.return.Wed = length(sim.recordTable[which(sim.recordTable=="Wed.")])
cat(num.return.Wed/sim.times)
## 0.214

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.