gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
python code for simulating Ornstein-Uhlenbeck processes with jumps
- User
python code for simulating Ornstein-Uhlenbeck processes with jumps
- Assistant
To simulate an Ornstein-Uhlenbeck process with jumps in Python, you can use the
numpylibrary to generate the random variables andmatplotliblibrary to plot the results. Here's an example code to get you started:import numpy as np import matplotlib.pyplot as plt def ornstein_uhlenbeck(x0, theta, mu, sigma, dt, N, lambd, jump_mean, jump_std): t = np.linspace(0.0, N*dt, N) dW = np.sqrt(dt)*np.random.normal(size=N) jumps = np.random.poisson(lambd*dt, size=N) X = np.zeros_like(t) X[0] = x0 for i in range(1, N): drift = theta * (mu - X[i-1]) * dt diffusion = sigma * dW[i-1] jumps_diffusion = np.sum(np.random.normal(jump_mean, jump_std, jumps[i])) X[i] = X[i-1] + drift + diffusion + jumps_diffusion return t, X # Parameters x0 = 0 theta = 0.7 mu = 0.5 sigma = 0.25 dt = 0.01 N = 1000 lambd = 0.1 jump_mean = 0.0 jump_std = 0.1 # Simulate Ornstein-Uhlenbeck process with jumps t, X = ornstein_uhlenbeck(x0, theta, mu, sigma, dt, N, lambd, jump_mean, jump_std) # Plot the results plt.plot(t, X) plt.xlabel('Time') plt.ylabel('Value') plt.title('Ornstein-Uhlenbeck Process with Jumps') plt.show()In this code, the
ornstein_uhlenbeck()function simulates an Ornstein-Uhlenbeck process with jumps using the Euler-Maruyama method. The resulting time points and process values are returned, and then plotted usingmatplotlib. You can adjust the parameters as per your requirements.