title: Terminal probabilities of a probability matrix Numpy
body: <p>I have a matrix m that represents the probabilities transitioning from states to states.</p>
  <p>E.g. for the sample below I will always get stuck in states 1,3,4, and state 2 I will randomly transition to one of the 4 states.</p>
  <pre><code>import numpy as np
  m = np.eye(4)
  m[1] = 0.25
  print(m)
  </code></pre>
  <pre><code>[[1.   0.   0.   0.  ]
  
   [0.25 0.25 0.25 0.25]
  
   [0.   0.   1.   0.  ]
  
   [0.   0.   0.   1.  ]]
  </code></pre>
  <p>How do I find a matrix representing the eventual end state following infinite transitions?</p>
  <p>E.g. if I do this, I get intuitive result of states 1,3,4 --&gt; 100% sticking in 1,3,4 but state 2 --&gt; 1/3 chance ending up in all the others. Since all cases from state 2 eventually allocated evenly between 1,3,4 through multiple transitions.</p>
  <pre><code>t = m
  for _ in range(100_000):
      t = t @ t
  print(t)
  </code></pre>
  <pre><code>[[1.         0.         0.         0.        ]
  
   [0.33333333 0.         0.33333333 0.33333333]
  
   [0.         0.         1.         0.        ]
  
   [0.         0.         0.         1.        ]]
  </code></pre>
  <p>How can I calculate this without using repeated multiplications? I thought it corresponds to the eigenvector/eigenvalues of the matrix, but I get something very different when I calculate this.</p>
  <pre><code>np.linalg.eig(m)
  </code></pre>
  <pre><code>[[0.        , 0.9486833 , 0.        , 0.        ],
  
  [1.        , 0.31622777, 0.31622777, 0.31622777],
  
  [0.        , 0.        , 0.9486833 , 0.        ],
  
  [0.        , 0.        , 0.        , 0.9486833 ]]
  </code></pre>
  <p>Is there a methodology to calculate this using numpy? I need it to work for an arbitrary matrix, but there will be a known list of terminal states and positive probability of reaching these from all other states.</p>
  <p>At the moment I am thinking of using the repeated multiplication method but it feels suboptimal and something there should be a function/trick that can calculate without looping.</p>
  <p>I was reading this but didn't fully understand what the methodology is and how to implement it.</p>
  <p><a href="https://math.dartmouth.edu/archive/m20x06/public_html/Lecture14.pdf" rel="nofollow noreferrer">https://math.dartmouth.edu/archive/m20x06/public_html/Lecture14.pdf</a></p>
  <p>I also looked in this question. People seemed to give some tips for hand-solving but not a general algorithm:</p>
  <p><a href="https://math.stackexchange.com/questions/2003258/calculating-the-probability-of-reaching-each-absorbing-state-in-markov-chain">https://math.stackexchange.com/questions/2003258/calculating-the-probability-of-reaching-each-absorbing-state-in-markov-chain</a></p>