程式

歐拉離散化python程式碼

  • May 28, 2022

寫出一維隨機方程的歐拉離散化

$ dXt = b (t, X_t) \space dt + \sigma (t, X_t) \space dW_t $

對於這一部分,我會說好的,因為它是我在任何隨機微積分書上都能找到的純理論部分。

問題出現在可選部分:設置 $ b (t, x) = tx $ , $ \sigma (t, x) = \sqrt {t (x + 1)} $ 和 $ X_0 = 1 $ , 數值近似期望值 $ max $ { $ 0, 1 - (X_{0.5})^2 $ } 使用歐拉-蒙地卡羅方法。

離散方程為:

$$ dX_{t,j+1} = X_{t,j} tX_{t,j} + t_j X_{t,j} \Delta + \sqrt {t_j (X_{t,j} + 1)^+} \Delta W_j $$和 $ X_{t_0} = 1 $

你能指點我一本書或一個帶有python程式碼的網站來編寫這個方法嗎?謝謝!

我發現這段程式碼:

# Create Brownian Motion
np.random.seed(1)
dB = np.sqrt(dt) * np.random.randn(N)
B  = np.cumsum(dB)

# Exact Solution
Y = X0 * np.exp((mu - 0.5*sigma**2)*t + (sigma * B))

# EM Approximation - small dt
X_em_small, X = [], X0
for j in range(N):  
   X += mu*X*dt + sigma*X*dB[j]
   X_em_small.append(X)

# EM Approximation - big dt
X_em_big, X, R = [], X0, 2
coarse_grid = np.arange(dt,1+dt,R*dt)
for j in range(int(N/R)):
   X += mu*X* (R*dt) + sigma*X*sum(dB[R*(j-1):R*j])
   X_em_big.append(X)    
   
# Plot
plt.plot(t, Y, label="Exact ($Y_t$)", color=pal[0])
plt.plot(t, X_em_small, label="EM ($X_t$): Fine Grid", color=pal[1], ls='--')
plt.plot(coarse_grid, X_em_big, label="EM ($X_t$): Coarse Grid", color=pal[2], ls='--')
plt.title('E-M Approximation vs. Exact Simulation'); plt.xlabel('t'); plt.legend(loc = 2);

我怎樣才能將它應用到我的文本中?非常非常感謝。

我相信這個連結會有你正在尋找的東西。

Python 數值方法——第 22.3 章

引用自:https://quant.stackexchange.com/questions/71024