#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 loses A dollars with probability .6. Find the probability that he wins 8 dollars before losing all of his money if

knitr::opts_chunk$set(echo = TRUE)
library(tinytex)
## Warning: package 'tinytex' was built under R version 4.3.3
library(markovchain)
## Warning: package 'markovchain' was built under R version 4.3.3
## Package:  markovchain
## Version:  0.9.5
## Date:     2023-09-24 09:20:02 UTC
## BugReport: https://github.com/spedygiorgio/markovchain/issues
#a.timid strategy

#1.simulation solution:

p <- 0.4   # P of success
q <- 1 - p # P of fail
start <- 1 
sim_len <- 1000000

w <- 0 
l <- 0
bet <- 1

for (s in 1:sim_len) {
  sum <- start
  
  while (sum > 0 && sum < 8) {
    if (runif(1, 0, 1.0) <= p) {
      sum <- sum + bet
    } else {
      sum <- sum - bet
    }
  }
  
  if (sum == 0) {
    l <- l + 1
  } else {
    w <- w + 1
  }
}

print(paste('wins: ', w, 'losses: ', l))
## [1] "wins:  20445 losses:  979555"
(w / (w+l))
## [1] 0.020445
#p=0.020081

#2. Math solution:

Q <- matrix(
  c(0.0, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.6, 0.0, 0.4, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.6, 0.0, 0.4, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.6, 0.0, 0.4, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.6, 0.0, 0.4, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.6, 0.0, 0.4,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.6, 0.0
    ),
  nrow = 7,
  ncol = 7,
  byrow = TRUE
)

R <- matrix(
  c(0.6, 0.0, 
    0.0, 0.0,
    0.0, 0.0,
    0.0, 0.0,
    0.0, 0.0,
    0.0, 0.0,
    0.0, 0.4
    ), 
  nrow = 7, 
  ncol = 2,
  byrow = TRUE
)

I <- diag(7)
(N <- solve(I-Q))
##           [,1]     [,2]      [,3]      [,4]      [,5]      [,6]       [,7]
## [1,] 1.6328311 1.054718 0.6693101 0.4123711 0.2410785 0.1268834 0.05075337
## [2,] 1.5820777 2.636796 1.6732752 1.0309278 0.6026963 0.3172086 0.12688343
## [3,] 1.5059477 2.509913 3.1792228 1.9587629 1.1451229 0.6026963 0.24107851
## [4,] 1.3917526 2.319588 2.9381443 3.3505155 1.9587629 1.0309278 0.41237113
## [5,] 1.2204600 2.034100 2.5765266 2.9381443 3.1792228 1.6732752 0.66931007
## [6,] 0.9635210 1.605868 2.0340999 2.3195876 2.5099128 2.6367962 1.05471848
## [7,] 0.5781126 0.963521 1.2204600 1.3917526 1.5059477 1.5820777 1.63283109
(B <- N %*% R)
##           [,1]       [,2]
## [1,] 0.9796987 0.02030135
## [2,] 0.9492466 0.05075337
## [3,] 0.9035686 0.09643140
## [4,] 0.8350515 0.16494845
## [5,] 0.7322760 0.26772403
## [6,] 0.5781126 0.42188739
## [7,] 0.3468676 0.65313243
#p=0.02030135
#b.bold strategy

#1. simulation solution: 

p <- 0.4   # P of success
q <- 1 - p # P of fail
start <- 1 # 
sim_len <- 1000000

w <- 0 
l <- 0

for (i in 1:sim_len) {
  sum <- start
  
  while (sum > 0 && sum < 8) {
    bet <- min(sum, 8-sum)
      
    if (runif(1, 0, 1.0) <= p) {
      sum <- sum + bet
    } else {
      sum <- sum - bet
    }
  }
  
  if (sum == 0) {
    l <- l + 1
  } else {
    w <- w + 1
  }
}

print(paste('wins: ', w, 'losses: ', l))
## [1] "wins:  64096 losses:  935904"
(w / (w+l))
## [1] 0.064096
#P=0.063479

#2. Math solution 

matx<- matrix(c(1,0,0,0,0,0,0,0,0,
               0.6,0,0.4,0,0,0,0,0,0,
               0.6,0,0,0,0.4,0,0,0,0,
               0.6,0,0,0,0,0,0.4,0,0,
               0.6,0,0,0,0,0,0,0,0.4,
               0,0,0,0.6,0,0,0,0,0.4,
               0,0,0.6,0,0,0,0,0,0.4,
               0,0.6,0,0,0,0,0,0,0.4,
               0,0,0,0,0,0,0,0,1), byrow = T, nrow = 9)

print(matx)
##       [,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
##  [2,]  0.6  0.0  0.4  0.0  0.0    0  0.0    0  0.0
##  [3,]  0.6  0.0  0.0  0.0  0.4    0  0.0    0  0.0
##  [4,]  0.6  0.0  0.0  0.0  0.0    0  0.4    0  0.0
##  [5,]  0.6  0.0  0.0  0.0  0.0    0  0.0    0  0.4
##  [6,]  0.0  0.0  0.0  0.6  0.0    0  0.0    0  0.4
##  [7,]  0.0  0.0  0.6  0.0  0.0    0  0.0    0  0.4
##  [8,]  0.0  0.6  0.0  0.0  0.0    0  0.0    0  0.4
##  [9,]  0.0  0.0  0.0  0.0  0.0    0  0.0    0  1.0
Names <- c("0","1","2","3","4","5","6","7","8")
bold <- new("markovchain", states = Names, byrow=TRUE, transitionMatrix = matx)

bold_p <- absorptionProbabilities(bold)
print(bold_p)
##         0       8
## 1 0.93600 0.06400
## 2 0.84000 0.16000
## 3 0.80160 0.19840
## 4 0.60000 0.40000
## 5 0.48096 0.51904
## 6 0.50400 0.49600
## 7 0.56160 0.43840
#p=0.06400

#c.The BOLD strategy gives Smith the better chance of getting out of jail