# Euler Discretization python code

Write the Euler discretization of the 1-dimensional stochastic equation

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

For this part I would say all right because it is a purely theoretical part that I find on any stochastic calculus book.

The problem arises on the optional part: Set $$b (t, x) = tx$$, $$\sigma (t, x) = \sqrt {t (x + 1)}$$ e $$X_0 = 1$$, numerically approximate the expected value $$max$$ {$$0, 1 - (X_{0.5})^2$$} using the Euler-Monte Carlo method.

The discretized equation will be:

$$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$$ with $$X_{t_0} = 1$$

Could you point me to a book or a site with a python code for writing this method? Thanks!

I find this code:

# 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)
plt.plot(t, X_em_small, label="EM ($$X_t$$): Fine Grid", color=pal, ls='--')
plt.plot(coarse_grid, X_em_big, label="EM ($$X_t$$): Coarse Grid", color=pal, ls='--')
plt.title('E-M Approximation vs. Exact Simulation'); plt.xlabel('t'); plt.legend(loc = 2);


how can i apply it to my text? Thanks very very much.

• Hi, can someone help me with the code? Thanks. May 30 at 9:53