title: Casino game with matrix different probabilities
body: <p><strong>This is my task:</strong></p>
  
  <p>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. </p>
  
  <p>If Peter reaches 0 dollars he goes home bankrupt, if he manages to reach 5 dollars he goes home happy. </p>
  
  <p>Find the probability of Peter going home with 5 dollars when p= 30%, 40%, 50%, 60% &amp; 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 &amp; 5 dollars). </p>
  
  <p><strong>My plan in solving it</strong></p>
  
  <p>Find when each separate matrix converges(P^n = P^n+1) with when_converged. </p>
  
  <p>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.</p>
  
  <p><strong>This is my code:</strong></p>
  
  <pre><code>mpow &lt;- 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 &lt;- function(P, tol=0.00005) { 
      n = 1; diff = 1 
      while (diff &gt; tol) { 
          A &lt;- mpow(P, n) 
          B &lt;- mpow(P, n+1) 
          diff &lt;- max(abs(A - B)) 
          n &lt;- n + 1 
      } 
      return(n) 
  }
  
  
  P30 &lt;- 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 &lt;- 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 &lt;- 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 &lt;- 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 &lt;- 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)
  </code></pre>
  
  <p>From Rstudio I get that P30 is converged at 35.</p>
  
  <pre><code>when_converged(P40, 0.00005)
  </code></pre>
  
  <p>From Rstudio I get that P40 is converged at 37.</p>
  
  <pre><code>when_converged(P50, 0.00005)
  </code></pre>
  
  <p>From Rstudio I get that P50 is converged at 47.</p>
  
  <pre><code>when_converged(P60, 0.00005)
  </code></pre>
  
  <p>From Rstudio I get that P60 is converged at 61.</p>
  
  <pre><code>when_converged(P70, 0.00005)
  </code></pre>
  
  <p>From Rstudio I get that P70 is converged at 79.</p>
  
  <pre><code>mpow(P30, 35)
  
  mpow(P40, 37)
  
  mpow(P50, 47)
  
  mpow(P60, 61)
  
  mpow(P70, 79)
  </code></pre>
  
  <p><strong>What I need help with</strong></p>
  
  <p>What I get from Rstudio is that for mpow(P60, 61) &amp; mpow(P70, 79) the probability of going home with 5 dollars becomes less compared to mpow(P50, 47) &amp; 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 &amp; not with an entire different code. </p>