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
(a) he bets 1 dollar each time (timid strategy).

###Answer was provided in class
if (!require("markovchain")) {
  install.packages("markovchain")
  library(markovchain)
}


states_names=c("0","1","2","3","4","5","6","7","8")
my_matrix=matrix(c(1,  0,  0,  0,  0,  0,  0,  0,  0,#0
                 0.6,  0,0.4,  0,  0,  0,  0,  0,  0,#1
                   0,0.6,  0,0.4,  0,  0,  0,  0,  0,#2
                   0,  0,0.6,  0,0.4,  0,  0,  0,  0,#3
                   0,  0,  0,0.6,  0,0.4,  0,  0,  0,#4
                   0,  0,  0,  0,0.6,  0,0.4,  0,  0,#5
                   0,  0,  0,  0,  0,0.6,  0,0.4,  0,#6
                   0,  0,  0,  0,  0,  0,0.6,  0,0.4,#7
                   0,  0,  0,  0,  0,  0,  0,  0,  1), byrow=TRUE,nrow=9, dimnames=list(states_names,states_names)) #transitionmatrx=my_matrix#8

my_matrix
##     0   1   2   3   4   5   6   7   8
## 0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
## 1 0.6 0.0 0.4 0.0 0.0 0.0 0.0 0.0 0.0
## 2 0.0 0.6 0.0 0.4 0.0 0.0 0.0 0.0 0.0
## 3 0.0 0.0 0.6 0.0 0.4 0.0 0.0 0.0 0.0
## 4 0.0 0.0 0.0 0.6 0.0 0.4 0.0 0.0 0.0
## 5 0.0 0.0 0.0 0.0 0.6 0.0 0.4 0.0 0.0
## 6 0.0 0.0 0.0 0.0 0.0 0.6 0.0 0.4 0.0
## 7 0.0 0.0 0.0 0.0 0.0 0.0 0.6 0.0 0.4
## 8 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0
markove <- new("markovchain", states = states_names, transitionMatrix=my_matrix, name = "A markovchain object")


timid<-absorptionProbabilities(markove)

timid<-timid[1,"8"]*100

answertimid <- paste("The probability that Smith wins 8 dollars before losing all of his money if he bets 1 dollar each time is",timid,"%")    
print(answertimid)
## [1] "The probability that Smith wins 8 dollars before losing all of his money if he bets 1 dollar each time is 2.03013481363997 %"

(b) he bets, each time, as much as possible but not more than necessary to bring his fortune up to 8 dollars (bold strategy).

#b
states_names=c("0","1","2","4","8")
my_matrix=matrix(c(1.00,    0.00, 0.00, 0.00,   0.00,
                   0.60,    0.00, 0.40, 0.00,   0.00,
                   0.60,    0.00, 0.00, 0.40,   0.00,
                   0.60,    0.00, 0.00, 0.00,   0.40,
                   0.00,    0.00, 0.00, 0.00,   1.00), byrow=TRUE,nrow=5, dimnames=list(states_names,states_names)) 

my_matrix
##     0 1   2   4   8
## 0 1.0 0 0.0 0.0 0.0
## 1 0.6 0 0.4 0.0 0.0
## 2 0.6 0 0.0 0.4 0.0
## 4 0.6 0 0.0 0.0 0.4
## 8 0.0 0 0.0 0.0 1.0
markove <- new("markovchain", states = states_names, transitionMatrix=my_matrix, name = "A markovchain object")


bold<-absorptionProbabilities(markove)

bold
##       0     8
## 1 0.936 0.064
## 2 0.840 0.160
## 4 0.600 0.400
bold<-bold[1,"8"]*100


answerbold <- paste("The probability that Smith bets, each time, as much as possible but not more than necessary to bring his fortune up to 8 dollars",bold,"%")    
print(answerbold)
## [1] "The probability that Smith bets, each time, as much as possible but not more than necessary to bring his fortune up to 8 dollars 6.4 %"

(c) Which strategy gives Smith the better chance of getting out of jail?
The bold strategy provides Smith with slightly higher probability of getting out of jail. 2% timid versus 6% bold.

print(answertimid)      
## [1] "The probability that Smith wins 8 dollars before losing all of his money if he bets 1 dollar each time is 2.03013481363997 %"
print(answerbold)    
## [1] "The probability that Smith bets, each time, as much as possible but not more than necessary to bring his fortune up to 8 dollars 6.4 %"