title: Python code explanation for stationary distribution of a Markov chain
body: <p>I have got this code: </p>
  
  <pre><code>import numpy as np
  from scipy.linalg import eig 
  transition_mat = np.matrix([
      [.95, .05, 0., 0.],\
      [0., 0.9, 0.09, 0.01],\
      [0., 0.05, 0.9, 0.05],\
      [0.8, 0., 0.05, 0.15]])
  
  S, U = eig(transition_mat.T)
  stationary = np.array(U[:, np.where(np.abs(S - 1.) &lt; 1e-8)[0][0]].flat)
  stationary = stationary / np.sum(stationary)
  
  &gt;&gt;&gt; print stationary
  [ 0.34782609  0.32608696  0.30434783  0.02173913]
  </code></pre>
  
  <p>But I can't understand the line: </p>
  
  <pre><code>stationary = np.array(U[:, np.where(np.abs(S - 1.) &lt; 1e-8)[0][0]].flat)
  </code></pre>
  
  <p>Can anyone explain the part: <code>U[:, np.where(np.abs(S - 1.) &lt; 1e-8)[0][0]].flat</code> ?</p>
  
  <p>I know that the routine returns <code>S</code>: eigenvalue, <code>U</code> : eigenvector. I need to find the eigenvector corresponding to the eigenvalue 1. I have wrote the code below: </p>
  
  <pre><code>for i in range(len(S)):
      if S[i] == 1.0:
          j = i
  
   matrix = np.array(U[:, j].flat)
  </code></pre>
  
  <p>I am getting output:</p>
  
  <pre><code>:  [ 0.6144763   0.57607153  0.53766676  0.03840477]
  </code></pre>
  
  <p>but it does not give the same output. why?!</p>