title: Optimizing a For loop in a Monte Carlo simulation
body: <p>I'm working on a correlated multivariate simulation with the Nataf Transform. I developed a code in Python (Intermediate knowledge) to perform a Monte Carlo simulation but it takes a lot of time on my computer. Could anyone help me to optimize these lines:</p>
  
  <pre><code># Exponential distribution (stats package)
  loc_exp = 15.0
  scale_exp = 139.21617 
  
  # Gamma distribution (l-moments package)
  loc_gam = 0.72698
  scale_gam = 16.18526
  
  for i in range(len(rho_z)):
  
  # Generate standarn normal variates
      n = int(1e6)
      r = np.random.normal(0, 1, [n,2])
      ui = r[:,0]
      uj = r[:,1]
      xi = stats.expon.ppf(stats.norm.cdf(ui), loc_exp, scale_exp)
      xj = lmoments.quagam(stats.norm.cdf(rho_z[i]*ui + np.sqrt(1.0 - rho_z[i]**2)*uj), para = np.array((loc_gam, scale_gam)))
  # evaluate rho_x
  R = np.corrcoef(xi,xj)
  rho_x[i] = R[0,1]
  </code></pre>
  
  <p>I am using stats and lmoments packages. Thanks</p>