knitr::opts_chunk$set(echo = TRUE)
library(tinytex)
Source files: [https://github.com/djlofland/DATA605_S2020/tree/master/]
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:
# -----------Solve by Simulation -----------
p <- 0.4 # Probability of success
q <- 1 - p # Probability of fail
start <- 1 # initial starting currency
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: 20600 losses: 979400"
(w / (w+l))
## [1] 0.0206
# -----------Solve by Math -----------
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
The probabillity from starting at state 1 ($1) and ending in absobption state ($8) is 0.0203013
# -----------Solve by Simulation -----------
p <- 0.4 # Probability of success
q <- 1 - p # Probability of fail
start <- 1 # initial starting currency
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: 63982 losses: 936018"
(w / (w+l))
## [1] 0.063982
In the second scenario, with each bet, Smith doubles his money if he wins and is out if he looses. So, he needs to win 3 times in a row to have $8 ($1 -> $2 -> $4 -> $8) and there is only one path to success. This is just \(0.4^3\) = 0.064.
Based on simulation and math, the BOLD strategy hedges Smithโs bet and improves his chances of making bail.