install.packages('discreteRV')
7.1 Sums of Discrete Random Variables
7.1.11. page 291
Solution:
The number of possible combinations for outcome of 2 to 6 is the integers between 1 and n/2. The number of possible combinations for outcome of 7 to 12 is the integers between n/2 and 6.
c<-list()
for (i in seq(1,12)){
if(i==1){
c[[i]] = 0
}
if (i <= 6 & i>1){
if (ceiling(i/2 - 0.5)<=1){
c[[i]] = 1
}
else{
c[[i]]=ceiling(i/2 - 0.5)
}
}
if (i > 6){
c[[i]]= ceiling(6-i/2+0.5)
}
}
for (i in seq(1,12)){
if (i%%2==0){
c[[i]]=(c[[i]]-1)*2+1
}
if (i%%2==1){
c[[i]]=c[[i]]*2
}
}
p<-list()
for (i in seq(1,12)){
p[[i]]=c[[i]]/36
}
(craps.df <- data.frame(outcomes=seq(2,12),probs=unlist(p)[2:12]))
## outcomes probs
## 1 2 0.02777778
## 2 3 0.05555556
## 3 4 0.08333333
## 4 5 0.11111111
## 5 6 0.13888889
## 6 7 0.16666667
## 7 8 0.13888889
## 8 9 0.11111111
## 9 10 0.08333333
## 10 11 0.05555556
## 11 12 0.02777778
According to the rule of craps, there are two circumstancies that you win. Roll a 7 or 11 on the first roll, the Come-Out Roll. Or, if you didn’t win or lose at the Come-Out Roll but roll a 4, 5, 6, 8, 9, or 10, and then roll it again before a 7 comes up.
(win <- data.frame(winORlose=c('win','win','lose','lose'),round=c('come-out','point','come-out','point'),outcome=c(c('7 or 11'),c('4, 5, 6, 8, 9, or 10'),c('2, 3, or 12'),c('7'))))
## winORlose round outcome
## 1 win come-out 7 or 11
## 2 win point 4, 5, 6, 8, 9, or 10
## 3 lose come-out 2, 3, or 12
## 4 lose point 7
probability of winning in first rounds
(p1 <- craps.df$probs[craps.df$outcomes==7]+craps.df$probs[craps.df$outcomes ==11])
## [1] 0.2222222
probability of winning in two rounds
To win in two rolls we have to get two match rolls before obtain a 7. If the probabilities are as they are, the probability to win in two rounds can be calculated as follows:
# probability throwing a 4 and contitue
#(1-p(4)-p(7))
n <- 1-craps.df$probs[craps.df$outcomes==4]-craps.df$probs[craps.df$outcomes ==7]
# probability of winning in throwing the second 4 at point round
# p(4)+n*p(4)+n^2*p(4)+n^3*p(4)....
# p(4)(1+n+n^2+n^3+...)
# the sum of infinited geometric series (1+n+n^2+n^3+...) equals to 1/(1-n)
p_4<-craps.df$probs[craps.df$outcomes==4]*1/(1-n)
# probability of winning in throwing two 4s
p_4<-craps.df$probs[craps.df$outcomes==4]*p_4
# similarly, probability of winning in throwing two 5,6,8,9,10 could be calckulated as follows:
n5 <- 1-craps.df$probs[craps.df$outcomes==5]-craps.df$probs[craps.df$outcomes ==7]
p_5<-craps.df$probs[craps.df$outcomes==5]*1/(1-n5)
p_5<-craps.df$probs[craps.df$outcomes==5]*p_5
n6 <- 1-craps.df$probs[craps.df$outcomes==6]-craps.df$probs[craps.df$outcomes ==7]
p_6<-craps.df$probs[craps.df$outcomes==6]*1/(1-n6)
p_6<-craps.df$probs[craps.df$outcomes==6]*p_6
n8 <- 1-craps.df$probs[craps.df$outcomes==8]-craps.df$probs[craps.df$outcomes ==7]
p_8<-craps.df$probs[craps.df$outcomes==8]*1/(1-n8)
p_8<-craps.df$probs[craps.df$outcomes==8]*p_8
n9 <- 1-craps.df$probs[craps.df$outcomes==9]-craps.df$probs[craps.df$outcomes ==7]
p_9<-craps.df$probs[craps.df$outcomes==9]*1/(1-n9)
p_9<-craps.df$probs[craps.df$outcomes==9]*p_9
n10 <- 1-craps.df$probs[craps.df$outcomes==10]-craps.df$probs[craps.df$outcomes ==7]
p_10<-craps.df$probs[craps.df$outcomes==10]*1/(1-n10)
p_10<-craps.df$probs[craps.df$outcomes==10]*p_10
# total probability of winning
(p_win <- p_4+p_5+p_6+p_8+p_9+p_10)
## [1] 0.2707071
cat("The probability of winning ",p1+p_win)
## The probability of winning 0.4929293
probability of winning in two rounds – using discreteRV
# discreteRV make things easier
suppressWarnings(suppressMessages(library(discreteRV)))
# the random variable for the sum of two rolls
(craps <- RV(1:6) + RV(1:6))
## Random variable with 11 outcomes
##
## Outcomes 2 3 4 5 6 7 8 9 10 11 12
## Probs 1/36 1/18 1/12 1/9 5/36 1/6 5/36 1/9 1/12 1/18 1/36
# probability that the game ends at come-out round
P(craps %in% c(7, 11, 2, 3, 12))
## [1] 0.3333333
# probabilities of winning given the game ended after the come-out round:
P(craps%in% c(2, 3, 12) | craps %in% c(7, 11, 2, 3, 12))
## [1] 0.3333333
# probability of winning at come-out round
p1 <-P(craps %in% c(7, 11))
cat("The probability of winning at come-out round:",p1)
## The probability of winning at come-out round: 0.2222222
craps.df <- data.frame(outcomes=outcomes(craps),probs=probs(craps))
probability of winning in two rounds – using discreteRV
To win in two rolls we have to get two match rolls before obtain a 7. If the probabilities are as they are, the probability to win in two rounds can be calculated as follows:
#Probability mass function of X^n
pmf_2roll <- iid(craps, 2)
comeout <- marginal(pmf_2roll, 1)
point <- marginal(pmf_2roll, 2)
(p2 <-P(comeout %in% c(4,5,6,8,9,10) %AND% (comeout == point)))
## [1] 0.07716049
cat("The probability of winning in two rounds:",p2)
## The probability of winning in two rounds: 0.07716049
cat("The probability of winning ",p1+p2)
## The probability of winning 0.2993827
I must missed something so the total wining probability obtained by using discreteRV package is not consisted to the manually calculated result.
If P(X=2,3,4,5)=(1/6) + r P(X=6)=(1/6) ??? 2r, with 0 < r < .02
when r= 0, then the probabilities becomes:
craps.df$probs[craps.df$outcomes %in% c(2,3,4,5)] <- 1/6
craps.df$probs[craps.df$outcomes ==6] <- 1/6
#probability of winning in first rounds
(p1 <- craps.df$probs[craps.df$outcomes==7]+craps.df$probs[craps.df$outcomes ==11])
## 7
## 0.2222222
# probability throwing a 4 and contitue
#(1-p(4)-p(7))
n <- 1-craps.df$probs[craps.df$outcomes==4]-craps.df$probs[craps.df$outcomes ==7]
# probability of winning in throwing the second 4 at point round
# p(4)+n*p(4)+n^2*p(4)+n^3*p(4)....
# p(4)(1+n+n^2+n^3+...)
# the sum of infinited geometric series (1+n+n^2+n^3+...) equals to 1/(1-n)
p_4<-craps.df$probs[craps.df$outcomes==4]*1/(1-n)
# probability of winning in throwing two 4s
p_4<-craps.df$probs[craps.df$outcomes==4]*p_4
# similarly, probability of winning in throwing two 5,6,8,9,10 could be calckulated as follows:
n5 <- 1-craps.df$probs[craps.df$outcomes==5]-craps.df$probs[craps.df$outcomes ==7]
p_5<-craps.df$probs[craps.df$outcomes==5]*1/(1-n5)
p_5<-craps.df$probs[craps.df$outcomes==5]*p_5
n6 <- 1-craps.df$probs[craps.df$outcomes==6]-craps.df$probs[craps.df$outcomes ==7]
p_6<-craps.df$probs[craps.df$outcomes==6]*1/(1-n6)
p_6<-craps.df$probs[craps.df$outcomes==6]*p_6
n8 <- 1-craps.df$probs[craps.df$outcomes==8]-craps.df$probs[craps.df$outcomes ==7]
p_8<-craps.df$probs[craps.df$outcomes==8]*1/(1-n8)
p_8<-craps.df$probs[craps.df$outcomes==8]*p_8
n9 <- 1-craps.df$probs[craps.df$outcomes==9]-craps.df$probs[craps.df$outcomes ==7]
p_9<-craps.df$probs[craps.df$outcomes==9]*1/(1-n9)
p_9<-craps.df$probs[craps.df$outcomes==9]*p_9
n10 <- 1-craps.df$probs[craps.df$outcomes==10]-craps.df$probs[craps.df$outcomes ==7]
p_10<-craps.df$probs[craps.df$outcomes==10]*1/(1-n10)
p_10<-craps.df$probs[craps.df$outcomes==10]*p_10
# total probability of winning
p_win <- p_4+p_5+p_6+p_8+p_9+p_10
cat("The probability of winning ",p1+p_win)
## The probability of winning 0.6075758
######## try to use discreteRV
#craps <- RV(c(2:12), probs = c(1/6,1/6,1/6,1/6,1/6,craps.df$probs[craps.df$outcomes ==7],craps.df$probs[craps.df$outcomes ==8],craps.df$probs[craps.df$outcomes ==9],craps.df$probs[craps.df$outcomes ==10],craps.df$probs[craps.df$outcomes ==11],craps.df$probs[craps.df$outcomes ==12]))
# But an error occured: Probabilities sum to over 1
when r= 0.02, then the probabilities becomes:
r <- 0.02
craps.df$probs[craps.df$outcomes %in% c(2,3,4,5)] <- (1/6) + r
craps.df$probs[craps.df$outcomes == 6] <- (1/6) - 2*r
#probability of winning in first rounds
(p1 <- craps.df$probs[craps.df$outcomes==7]+craps.df$probs[craps.df$outcomes ==11])
## 7
## 0.2222222
# probability throwing a 4 and contitue
#(1-p(4)-p(7))
n <- 1-craps.df$probs[craps.df$outcomes==4]-craps.df$probs[craps.df$outcomes ==7]
# probability of winning in throwing the second 4 at point round
# p(4)+n*p(4)+n^2*p(4)+n^3*p(4)....
# p(4)(1+n+n^2+n^3+...)
# the sum of infinited geometric series (1+n+n^2+n^3+...) equals to 1/(1-n)
p_4<-craps.df$probs[craps.df$outcomes==4]*1/(1-n)
# probability of winning in throwing two 4s
p_4<-craps.df$probs[craps.df$outcomes==4]*p_4
# similarly, probability of winning in throwing two 5,6,8,9,10 could be calckulated as follows:
n5 <- 1-craps.df$probs[craps.df$outcomes==5]-craps.df$probs[craps.df$outcomes ==7]
p_5<-craps.df$probs[craps.df$outcomes==5]*1/(1-n5)
p_5<-craps.df$probs[craps.df$outcomes==5]*p_5
n6 <- 1-craps.df$probs[craps.df$outcomes==6]-craps.df$probs[craps.df$outcomes ==7]
p_6<-craps.df$probs[craps.df$outcomes==6]*1/(1-n6)
p_6<-craps.df$probs[craps.df$outcomes==6]*p_6
n8 <- 1-craps.df$probs[craps.df$outcomes==8]-craps.df$probs[craps.df$outcomes ==7]
p_8<-craps.df$probs[craps.df$outcomes==8]*1/(1-n8)
p_8<-craps.df$probs[craps.df$outcomes==8]*p_8
n9 <- 1-craps.df$probs[craps.df$outcomes==9]-craps.df$probs[craps.df$outcomes ==7]
p_9<-craps.df$probs[craps.df$outcomes==9]*1/(1-n9)
p_9<-craps.df$probs[craps.df$outcomes==9]*p_9
n10 <- 1-craps.df$probs[craps.df$outcomes==10]-craps.df$probs[craps.df$outcomes ==7]
p_10<-craps.df$probs[craps.df$outcomes==10]*1/(1-n10)
p_10<-craps.df$probs[craps.df$outcomes==10]*p_10
# total probability of winning
(p_win <- p_4+p_5+p_6+p_8+p_9+p_10)
## 4
## 0.3872832
cat("The probability of winning:",p1+p_win)
## The probability of winning: 0.6095054
r=0.02 makes craps a favorable game for the player with these dice.
Reference: https://vegasclick.com/games/craps https://www.lolcraps.com/craps/odds/