Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Euler–Maruyama method

In Itô calculus, the Euler–Maruyama method (also called the Euler method) is a method for the
approximate numerical solution of a stochastic differential equation (SDE). It is an extension of the Euler
method for ordinary differential equations to stochastic differential equations. It is named after Leonhard
Euler and Gisiro Maruyama. Unfortunately, the same generalization cannot be done for any arbitrary
deterministic method.[1]

Consider the stochastic differential equation (see Itô calculus)

with initial condition X0  = x0 , where Wt stands for the Wiener process, and suppose that we wish to solve
this SDE on some interval of time [0, T]. Then the Euler–Maruyama approximation to the true solution
X is the Markov chain Y defined as follows:

partition the interval [0, T] into N equal subintervals of width :

set Y0 = x0
recursively define Yn for 0 ≤ n ≤ N-1 by

where

The random variables ΔWn are independent and identically distributed normal random variables with
expected value zero and variance .

Example

Numerical simulation

An area that has benefited significantly from SDE is biology or


more precisely mathematical biology. Here the number of
publications on the use of stochastic model grew, as most of the
models are nonlinear, demanding numerical schemes.

Gene expression modelled as


stochastic process
The graphic depicts a stochastic differential equation being solved using the Euler Scheme. The
deterministic counterpart is shown as well.

Computer implementation

The following Python code implements the Euler–Maruyama method and uses it to solve the Ornstein–
Uhlenbeck process defined by

The random numbers for are generated using the NumPy mathematics package.

1 # -*- coding: utf-8 -*-


2 import numpy as np
3 import matplotlib.pyplot as plt
4
5
6 class Model:
7 """Stochastic model constants."""
8 THETA = 0.7
9 MU = 1.5
10 SIGMA = 0.06
11
12
13 def mu(y: float, _t: float) -> float:
14 """Implement the Ornstein–Uhlenbeck mu."""
15 return Model.THETA * (Model.MU - y)
16
17
18 def sigma(_y: float, _t: float) -> float:
19 """Implement the Ornstein–Uhlenbeck sigma."""
20 return Model.SIGMA
21
22
23 def dW(delta_t: float) -> float:
24 """Sample a random number at each call."""
25 return np.random.normal(loc=0.0, scale=np.sqrt(delta_t))
26
27
28 def run_simulation():
29 """ Return the result of one full simulation."""
30 T_INIT = 3
31 T_END = 7
32 N = 1000 # Compute at 1000 grid points
33 DT = float(T_END - T_INIT) / N
34 TS = np.arange(T_INIT, T_END + DT, DT)
35 assert TS.size == N + 1
36
37 Y_INIT = 0
38
39 ys = np.zeros(TS.size)
40 ys[0] = Y_INIT
41 for i in range(1, TS.size):
42 t = T_INIT + (i - 1) * DT
43 y = ys[i - 1]
44 ys[i] = y + mu(y, t) * DT + sigma(y, t) * dW(DT)
45
46 return TS, ys
47
48
49 def plot_simulations(num_sims: int):
50 """ Plot several simulations in one image."""
51 for _ in range(num_sims):
52 plt.plot(*run_simulation())
53
54 plt.xlabel("time")
55 plt.ylabel("y")
56 plt.show()
57
58
59 if __name__ == "__main__":
60 NUM_SIMS = 5
61 plot_simulations(NUM_SIMS)

The following is simply the translation of the above code into the MATLAB (R2019b) programming
language:

1 %% Initialization and Utility


2 close all;
3 clear all;
4
5 numSims = 5; % display five runs
6 tBounds = [3 7]; % The bounds of t
7 N = 1000; % Compute at 1000 grid points
8 dt = (tBounds(2) - tBounds(1)) / N ;
9 y_init = 0; % Initial y condition
10
11
12 % Initialize the probability distribution for our
13 % random variable with mean 0 and stdev of sqrt(dt)
14 pd = makedist('Normal',0,sqrt(dt));
15
16 c = [0.7, 1.5, 0.06]; % Theta, Mu, and Sigma, respectively
17
18 ts = linspace(tBounds(1), tBounds(2), N); % From t0-->t1 with N points
19 ys = zeros(1,N); % 1xN Matrix of zeros
20
21 ys(1) = y_init;
22 %% Computing the Process
23 for j = 1:numSims
24 for i = 2:numel(ts)
25 t = tBounds(1) + (i-1) .* dt;
26 y = ys(i-1);
27 mu = c(1) .* (c(2) - y);
28 sigma = c(3);
29 dW = random(pd);
30
31 ys(i) = y + mu .* dt + sigma .* dW;
32 end
33 figure;
34 hold on;
35 plot(ts, ys, 'o')
36 end

See also
Milstein method
Runge–Kutta method (SDE)
Leimkuhler–Matthews method

References
1. Kloeden, P.E. & Platen, E. (1992). Numerical Solution of Stochastic Differential Equations.
Springer, Berlin. ISBN 3-540-54062-8.

Retrieved from "https://en.wikipedia.org/w/index.php?title=Euler–Maruyama_method&oldid=1158095817"

You might also like