title: How to vectorize (OR speed up) the following (numpy / pandas) code which needs to fill values conditional on last appended value?
body: <p>I want to append <code>'Y'</code> or <code>'N'</code> to a <code>list / numpy array / pandas series</code> called <code>x</code> with varying probability (<em>e.g. probability of 'Y' again given the last element was 'Y' is 0.8, else 0.5 if the last element was 'N'</em>). This is done using <code>np.random.choice</code>. I want to do this 100,000 times. I have written a <code>for</code> loop for this but I am not sure if this is the fastest / most efficient way.</p>
  <pre><code># choose 'Y' or 'N' at random for first element
  x = [np.random.choice(a = ['Y', 'N'])]
  
  for i in range(10**6):
      # append 'Y' or 'N' with probability dependent on x[-1] being 'Y' or 'N'
      x.append(np.random.choice(a = ['Y', 'N'], p = [0.8, 0.2]) if x[-1] == 'Y' else np.random.choice(a = ['Y', 'N'], p = [0.5, 0.5]))
  </code></pre>
  <p>I guess maybe <code>numpy select / where</code> in conjunction with <code>pandas shift / numpy roll</code> method may help but don't know how (because of dependence on last element / above row complicates the vectorization)</p>
  <p>Any help to speed up the code would be appreciated. Thanks!</p>