Smith is in jail and has 1 dollar; he can get out on bail if he has 8 dollars. A guard agrees to make a series of bets with him. If Smith bets A dollars, he wins A dollars with probability .4 and A dollars with probability .6.

Find the probabiltiy that he wins 8 dollars before losting all of his money if a) he bets 1 dollar each time b) he bets, each time as much as possible but not more than necessary to bring his fortune up to 8 dollars c) which strategy gives him the better chance of getting out of jail

Below is scenario A - $1 bet

library(matrixcalc)
#  Possible states: 0,1,2,3,4,5,6,7,8
#Absorbing states:  0 - broke, 8 - out of jail

P<-matrix(c(1,0,0,0,0,0,0,0,0,
            .6,0,.4,0,0,0,0,0,0,
            0,.6,0.4,0,0,0,0,0,
            0,0,.6,0,.4,0,0,0,0,
            0,0,0,.6,0,.4,0,0,0,
            0,0,0,0,.6,0,.4,0,0,
            0,0,0,0,0,0,.6,0,.4,0,
            0,0,0,0,0,0,.6,0,.4,
            0,0,0,0,0,0,0,0,1), byrow=TRUE,nrow=9,ncol=9)
P
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
##  [2,]  0.6  0.0  0.4  0.0  0.0  0.0  0.0  0.0  0.0
##  [3,]  0.0  0.6  0.4  0.0  0.0  0.0  0.0  0.0  0.0
##  [4,]  0.0  0.6  0.0  0.4  0.0  0.0  0.0  0.0  0.0
##  [5,]  0.0  0.0  0.6  0.0  0.4  0.0  0.0  0.0  0.0
##  [6,]  0.0  0.0  0.0  0.6  0.0  0.4  0.0  0.0  0.0
##  [7,]  0.0  0.0  0.0  0.0  0.0  0.6  0.0  0.4  0.0
##  [8,]  0.0  0.0  0.0  0.0  0.0  0.0  0.6  0.0  0.4
##  [9,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0
initial<-matrix(c(0,1,0,0,0,0,0,0,0), byrow=TRUE,nrow=1, ncol=9)

initial
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]    0    1    0    0    0    0    0    0    0
P2<-initial%*% P
P2
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]  0.6    0  0.4    0    0    0    0    0    0
P3<-P2%*% P
P3
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]  0.6 0.24 0.16    0    0    0    0    0    0
P4<-P3%*%P
P4
##       [,1]  [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 0.744 0.096 0.16    0    0    0    0    0    0
P5<-P4 %*% P
P5
##        [,1]  [,2]   [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 0.8016 0.096 0.1024    0    0    0    0    0    0
P6<-P5 %*% P
P6
##        [,1]    [,2]    [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 0.8592 0.06144 0.07936    0    0    0    0    0    0
Trans<-matrix.power(P,100)
Result<-initial %*% Trans
Result
##      [,1]         [,2]         [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]    1 5.949614e-15 7.230272e-15    0    0    0    0    0    0

This tells me, over time the gambler will eventually be ruined.

However, what is the probability her will win before he loses?

Scenario A) Below is scenario A - $1 bet What is the probability he wins before losing?

# gamble(k, n, p)
  #   k: Gambler's initial state
  #   n: Gambler plays until either $n or Ruin
  #   p: Probability of winning $1 at each play
  #   Function returns 1 if gambler is eventually ruined
  #                    returns 0 if gambler eventually wins $n
  
gamble <- function(k,n,p) {
        stake <- k
        while (stake > 0 & stake < n) {
                bet <- sample(c(-1,1),1,prob=c(1-p,p))
                stake <- stake + bet
        }
        if (stake == 0) return(1) else return(0)
        }   

k <- 1 
n <-  8  
p <- .4  
trials <- 100000
simlist <- replicate(trials, gamble(k, n, p))
mean(simlist) 
## [1] 0.97993
#What is probability he will win
1-mean(simlist)
## [1] 0.02007

Below is scenario B - Max bet possible # Possible states: 0,1,2,4,8 #Absorbing states: 0 - broke, 8 - out of jail

Q<-matrix(c(1,0,0,0,0,
            .6,0,.4,0,0,
            0,.6,0.4,0,0,
            0,0,.6,0,.4,
            0,0,0,0,1), byrow=TRUE,nrow=5,ncol=5)
Q
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1.0  0.0  0.0    0  0.0
## [2,]  0.6  0.0  0.4    0  0.0
## [3,]  0.0  0.6  0.4    0  0.0
## [4,]  0.0  0.0  0.6    0  0.4
## [5,]  0.0  0.0  0.0    0  1.0
initial2<-matrix(c(0,1,0,0,0), byrow=TRUE,nrow=1, ncol=5)

initial2
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    1    0    0    0
Q2<-initial2%*% Q
Q2
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  0.6    0  0.4    0    0
Q3<-Q2%*% Q
Q3
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  0.6 0.24 0.16    0    0
Q4<-Q3%*%Q
Q4
##       [,1]  [,2] [,3] [,4] [,5]
## [1,] 0.744 0.096 0.16    0    0
Q5<-Q4 %*% Q
Q5
##        [,1]  [,2]   [,3] [,4] [,5]
## [1,] 0.8016 0.096 0.1024    0    0
Q6<-Q5 %*% Q
Q6
##        [,1]    [,2]    [,3] [,4] [,5]
## [1,] 0.8592 0.06144 0.07936    0    0
Trans2<-matrix.power(Q,100)
Result2<-initial2 %*% Trans2
Result2
##      [,1]         [,2]         [,3] [,4] [,5]
## [1,]    1 5.949614e-15 7.230272e-15    0    0

This tells me, over time the gambler will eventually be ruined.

However, what is the probability her will win before he loses?

Scenario b) Below is scenario B - Max bet possible possible states: 0,1,2,4,8 Absorbing states: 0 - broke, 8 - out of jail

What is the probability of winning?

# gamble(k, n, p)
  #   k: Gambler's initial state
  #   n: Gambler plays until either $n or Ruin
  #   p: Probability of winning $1 at each play
  #   Function returns 1 if gambler is eventually ruined
  #                    returns 0 if gambler eventually wins $n
  
gamble2 <- function(k2,n2,p2) {
        stake2 <- k2
        while (stake2 > 0 & stake2 < n2) {
                bet2 <- sample(c(-stake2,stake2),1,prob=c(1-p2,p2))
                stake2 <- stake2 + bet2
        }
        if (stake2 == 0) return(1) else return(0)
        }   

k2 <- 1 
n2 <-  8  
p2 <- .4  
trials2 <- 100000
simlist2 <- replicate(trials2, gamble2(k2, n2, p2))
mean(simlist2)
## [1] 0.93585
#What is the probability he will win?
1-mean(simlist2)
## [1] 0.06415
#So Scenario A)
1-mean(simlist)
## [1] 0.02007
#Scenario b)
1-mean(simlist2)
## [1] 0.06415
#Both will end in ruin, however, scenario b provides a  better strategy of getting out of jail