Question 1

Solving Via simulation

  • Build get_out_jail_timid function
    • takes probability estimates and runs simulation which reaches 0 or 8 and stops
  • Build get_probability
    • Takes above get_out_jail_timid function and simulates it million times and returns how often we get to 8
  • Answer- .02043
set.seed(4)
get_out_jail_timid <- function(){
    bankroll <- 1
    while(!bankroll%in%c(0,8)){
        new_val <- runif(1)
        if (new_val<.4){
            bankroll <- bankroll+1
        }
        else {bankroll <- bankroll-1
        }
    }
    return(bankroll)
}
#2nd function
get_probability <- function(){
    sim <- replicate(100000,get_out_jail_timid())
   my_count <- plyr::count(sim)
    return(my_count[2,2]/100000)
}
get_probability()
## [1] 0.02043

Solving via Markov method

  • Build transition matrix and probability matrix
library(knitr)
library(kableExtra)
library(tidyverse)
## Build markov transistion matrix
a <- matrix( rep( 0, len=81), nrow = 9)
a[1,1] <- 1
a[9,9] <- 1
for (row in 2:8){
    a[row,row+1] <- .4
    a[row,row-1] <- .6
 } 
prob_vector <- matrix(c(0,1,0,0,0,0,0,0,0),nrow=1)
## Not my function 
repeated <- function(.x, .reps = 1, .f, ...) {
  # A single, finite, non-negative number of repetitions
  assertthat::assert_that(
    length(.reps) == 1,
    !is.na(.reps),
    .reps >= 0,
    is.finite(.reps))
  # accept purrr-style formula functions
  .f <- rlang::as_function(.f, ...)
  
  # 0 .reps
  value <- .x

  while (.reps >= 1) {
    value <- .f(value, ...)
    .reps <- .reps - 1
  }
  value
}

get_stationary_matrix <- function(p,u=NULL,n){
n=n-1
new_p <- p %>% 
    repeated(n,~.x%*%.x) %>% 
    round(.,5)
print(new_p)
newer_p <- u%*% new_p
return(newer_p)
}

kable(as_data_frame(round(get_stationary_matrix(u= prob_vector,p=a, n=8),4)))
##          [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]    [,9]
##  [1,] 1.00000    0    0    0    0    0    0    0 0.00000
##  [2,] 0.97970    0    0    0    0    0    0    0 0.02030
##  [3,] 0.94925    0    0    0    0    0    0    0 0.05075
##  [4,] 0.90357    0    0    0    0    0    0    0 0.09643
##  [5,] 0.83505    0    0    0    0    0    0    0 0.16495
##  [6,] 0.73227    0    0    0    0    0    0    0 0.26772
##  [7,] 0.57811    0    0    0    0    0    0    0 0.42189
##  [8,] 0.34687    0    0    0    0    0    0    0 0.65313
##  [9,] 0.00000    0    0    0    0    0    0    0 1.00000
V1 V2 V3 V4 V5 V6 V7 V8 V9
0.9797 0 0 0 0 0 0 0 0.0203
  • Answer .0203

Question 2

Via Simulation

  • same as before with some added conditions

  • .06417

set.seed(2)
get_out_jail_bold <- function(){
    bankroll <- 1
    while(!bankroll%in%c(0,8)){
        if (bankroll<5){
        new_val <- runif(1)
        if (new_val<.4){
            bankroll <- bankroll*2
       #     print(bankroll)
        }
        else {bankroll <- 0
        }
        }
        else {
            new_val <- runif(1)
        if (new_val<.4){
            bankroll <- 8
     #       print(bankroll)
        }
        else{
           bankroll <- bankroll-(8-bankroll) 
    #       print(bankroll)
        }
        }
    }
   # print("sim done")
    return(bankroll)
}

get_probability <- function(){
    sim <- replicate(100000,get_out_jail_bold())
   my_count <- plyr::count(sim)
    return(my_count[2,2]/100000)
}
get_probability()
## [1] 0.06417

Via Markov

  • same prob_vector as before
  • needed to create a new upated transition matrix
  • .064
## Build markov transistion matrix
a <- matrix( rep( 0, len=81), nrow = 9)
a[1,1] <- 1
a[9,9] <- 1
a[2,1] <- .6
a[2,3] <- .4
a[3,5] <- .4
a[3,1] <- .6
a[5,1] <- .6
a[5,9] <- .4


#a[row,row*2] <- .4

prob_vector <- matrix(c(0,1,0,0,0,0,0,0,0),nrow=1)


kable(as_data_frame(round(get_stationary_matrix(u= prob_vector,p=a, n=3),3)))
##        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]  [,9]
##  [1,] 1.000    0    0    0    0    0    0    0 0.000
##  [2,] 0.936    0    0    0    0    0    0    0 0.064
##  [3,] 0.840    0    0    0    0    0    0    0 0.160
##  [4,] 0.000    0    0    0    0    0    0    0 0.000
##  [5,] 0.600    0    0    0    0    0    0    0 0.400
##  [6,] 0.000    0    0    0    0    0    0    0 0.000
##  [7,] 0.000    0    0    0    0    0    0    0 0.000
##  [8,] 0.000    0    0    0    0    0    0    0 0.000
##  [9,] 0.000    0    0    0    0    0    0    0 1.000
V1 V2 V3 V4 V5 V6 V7 V8 V9
0.936 0 0 0 0 0 0 0 0.064
a
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]  1.0    0  0.0    0  0.0    0    0    0  0.0
##  [2,]  0.6    0  0.4    0  0.0    0    0    0  0.0
##  [3,]  0.6    0  0.0    0  0.4    0    0    0  0.0
##  [4,]  0.0    0  0.0    0  0.0    0    0    0  0.0
##  [5,]  0.6    0  0.0    0  0.0    0    0    0  0.4
##  [6,]  0.0    0  0.0    0  0.0    0    0    0  0.0
##  [7,]  0.0    0  0.0    0  0.0    0    0    0  0.0
##  [8,]  0.0    0  0.0    0  0.0    0    0    0  0.0
##  [9,]  0.0    0  0.0    0  0.0    0    0    0  1.0
  1. Clearly the riskier bet is the smarter move, as the probability for success is higher