2.3 - 5 Rolling the Dice

# sum of two dice
S = expand.grid(X1=1:6,X2=1:6)
S$sum = S$X1 + S$X2
pr_sum = table(S$sum)/nrow(S)
sample(2:12,size=10, replace=T, prob=c(1:6,5:1)/36)
##  [1]  7 11 11  5 10  7  3 10  4  6
# only six case
Y = (S$X1==6) + (S$X2==6)
pr = table(Y)/36
# now draw the pmf
sample(0:2,size=10, replace=T, prob=pr)
##  [1] 0 1 1 0 1 1 0 0 0 0
x = sample(0:2,size=10000, replace=T, prob=pr)
table(x)
## x
##    0    1    2 
## 6950 2777  273
hist(x,breaks=seq(-0.5,2.5,1),freq=F,ylim=c(0,.7))
lines(0:2,pr,type="p",col="red")
lines(0:2,pr,type="h",col="red")

# what if we couldn't figure out the entire event space,
# but we knew the dice were fair and had six sides.
iters = 10000000
X1_e = sample(1:6,replace=T,size=iters)
X2_e = sample(1:6,replace=T,size=iters)
Y_e = (X1_e==6) + (X2_e==6)
table(Y_e)/iters
## Y_e
##         0         1         2 
## 0.6942057 0.2779863 0.0278080

12 Let’s make a deal

# http://www.bodowinter.com/tutorial/bw_doodling_monty_hall.pdf
doors = c("A","B","C")

xdata = c()

for (i in 1:1000){
  prize = sample(doors)[1]
  pick  = sample(doors)[1]
  open  = sample(doors[which(doors!=pick & doors != prize)])[1]
  switchyes = doors[which(doors!=pick & doors != open)]

  if(pick==prize)         xdata = c(xdata, "noswitchwin")
  if(switchyes == prize)  xdata = c(xdata,"switchwin")
}

table(xdata)
## xdata
## noswitchwin   switchwin 
##         335         665