# Does my Python code correctly simulate realizations of a CIR process?

I've written the following function which should simulate realizations of a CIR process:

def cir_simulations(alpha: float, mu: float, sigma: float, delta_t: float, num_steps: int, num_sims: int):

sims_shape = (num_steps, num_sims)
sims = np.empty(sims_shape)
std_norm_rand = np.random.standard_normal(sims_shape)

prev_r = mu

for r_t, rnd in zip(sims, std_norm_rand):
delta_r = alpha * (mu - prev_r) * delta_t + sigma * np.sqrt(prev_r) * np.sqrt(delta_t) * rnd[:]
r_t[:] = prev_r = prev_r + delta_r

return sims


Can anyone confirm that my code will correctly simulate a CIR process?

As a secondary question, are there any improvements you'd make to this code?

• I don't see any obvious errors. – noob2 Oct 25 '20 at 20:31
• Your conditional distribution around zero will be very wrong if the Feller condition doesn't hold, and slightly wrong if it does. The question you need to ask yourself is what do you do if prev_r < 0. – river_rat Oct 26 '20 at 22:42