Created
March 10, 2022 15:56
-
-
Save tsferro2/bb331ddaad5e0dba0d6294124b90aa47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import numpy as np | |
| import numpy.random as npr | |
| from pylab import plt, mpl | |
| # Square-Root Diffusion (mean-reverting process)------------------------------- | |
| # widely used for short rates & volatility | |
| x0 = 1.00 # starting value | |
| kappa = 3.0 # mean-reversion paramater | |
| theta = 1.00 # long term mean | |
| sigma = 0.1 | |
| I = 10000 | |
| M = 365 | |
| dt = T / M | |
| def srd_euler(): | |
| xh = np.zeros((M + 1, I)) | |
| x = np.zeros_like(xh) | |
| xh[0] = x0 | |
| x[0] = x0 | |
| for t in range(1, M + 1): | |
| xh[t] = (xh[t - 1] + | |
| kappa * (theta - np.maximum(xh[t - 1], 0)) * dt + | |
| sigma * np.sqrt(np.maximum(xh[t - 1], 0)) * | |
| math.sqrt(dt) * npr.standard_normal(I)) | |
| x = np.maximum(xh, 0) | |
| return x | |
| x1 = srd_euler() | |
| plt.figure(figsize=(10, 6)) | |
| plt.hist(x1[-1], bins=50) | |
| plt.xlabel('value') | |
| plt.ylabel('frequency'); | |
| plt.figure(figsize=(10, 6)) | |
| plt.plot(x1[:, :10], lw=1.5) | |
| plt.xlabel('time') | |
| plt.ylabel('index level'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment