Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tsferro2/bb331ddaad5e0dba0d6294124b90aa47 to your computer and use it in GitHub Desktop.
Save tsferro2/bb331ddaad5e0dba0d6294124b90aa47 to your computer and use it in GitHub Desktop.

Revisions

  1. tsferro2 created this gist Mar 10, 2022.
    38 changes: 38 additions & 0 deletions Square Root Diffusion time series simulation
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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');