This is my task:
Peter goes to the Casino with 1 dollar. With the chance of p, Peter wins 1 dollar and the chance of (1-p) he looses 1 dollar. The process can be seen as a markov chain.
If Peter reaches 0 dollars he goes home bankrupt, if he manages to reach 5 dollars he goes home happy.
Find the probability of Peter going home with 5 dollars when p= 30%, 40%, 50%, 60% & 70%. Construct matrixes for each probability where the first 4 states are the transient class( 1- 4 dollars) and the last two states are the two recurrent states( 0 & 5 dollars).
My plan in solving it
Find when each separate matrix converges(P^n = P^n+1) with when_converged.
Then use that n in mpow to see the probability of going from 1 dollars to 5 dollars, in other words from state 1 to 6.
This is my code:
mpow <- function(P, n) {
if (n == 0) {
return(diag(nrow(P)))
} else if (n == 1) {
return(P)
} else {
return(P %*% mpow(P, n - 1))
}
}
when_converged <- function(P, tol=0.00005) {
n = 1; diff = 1
while (diff > tol) {
A <- mpow(P, n)
B <- mpow(P, n+1)
diff <- max(abs(A - B))
n <- n + 1
}
return(n)
}
P30 <- matrix(c(0, 0.3, 0, 0, 0.7, 0, 0.7, 0, 0.3, 0, 0, 0, 0, 0.7, 0, 0.3, 0, 0, 0, 0, 0.7, 0, 0, 0.3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1), nrow = 6, ncol = 6, byrow = TRUE)
P40 <- matrix(c(0, 0.4, 0, 0, 0.6, 0, 0.6, 0, 0.4, 0, 0, 0, 0, 0.6, 0, 0.4, 0, 0, 0, 0, 0.6, 0, 0, 0.4, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1), nrow = 6, ncol = 6, byrow = TRUE)
P50 <- matrix(c(0, 0.5, 0, 0, 0.5, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0.5, 0, 0, 0.5, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1), nrow = 6, ncol = 6, byrow = TRUE)
P60 <- matrix(c(0, 0.6, 0, 0, 0.4, 0, 0.6, 0, 0.4, 0, 0, 0, 0, 0.6, 0, 0.4, 0, 0, 0, 0, 0.6, 0, 0, 0.4, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1), nrow = 6, ncol = 6, byrow = TRUE)
P70 <- matrix(c(0, 0.7, 0, 0, 0.3, 0, 0.7, 0, 0.3, 0, 0, 0, 0, 0.7, 0, 0.3, 0, 0, 0, 0, 0.7, 0, 0, 0.3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1), nrow = 6, ncol = 6, byrow = TRUE)
when_converged(P30, 0.00005)
From Rstudio I get that P30 is converged at 35.
when_converged(P40, 0.00005)
From Rstudio I get that P40 is converged at 37.
when_converged(P50, 0.00005)
From Rstudio I get that P50 is converged at 47.
when_converged(P60, 0.00005)
From Rstudio I get that P60 is converged at 61.
when_converged(P70, 0.00005)
From Rstudio I get that P70 is converged at 79.
mpow(P30, 35)
mpow(P40, 37)
mpow(P50, 47)
mpow(P60, 61)
mpow(P70, 79)
What I need help with
What I get from Rstudio is that for mpow(P60, 61) & mpow(P70, 79) the probability of going home with 5 dollars becomes less compared to mpow(P50, 47) & mpow(P40, 37). Where the probability of winning 1 dollar is less. Which feels wrong. Is there anything I'm doing wrong? Please try to solve it using my method & not with an entire different code.