code: def ar1_gen(rho, mu, sigma, size=1):
      
      mu = np.asarray(mu, float)
      mu = np.resize(mu, size)
      r = mu.copy()
      r += np.random.randn(size) * sigma
      r[0] = np.random.randn(1) * sigma / np.sqrt(1 - rho ** 2)
      i = 0
      while True:
          yield r[i]
          i += 1
          if i == size:
              break
          r[i] += rho * (r[i - 1] - mu[i - 1])
docs: Create an autoregressive series of order one AR(1) generator.
  
      .. math::
          X_t = \mu_t + \rho (X_{t-1}-\mu_{t-1} + \epsilon_t
  
      If mu is a sequence and size > len(mu), the algorithm loops through
      mu.
  
      :Stochastics:
          rho : scalar in [0,1]
          mu : scalar or sequence
          sigma : scalar > 0
          size : integer