Euler–Maruyama method


In Itô calculus, the Euler–Maruyama method is a method for the approximate numerical solution of a stochastic differential equation. It is a simple generalization 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.
Consider the stochastic differential equation
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 . Then the Euler–Maruyama approximation to the true solution X is the Markov chain Y defined as follows:
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.
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 -*-
import numpy as np
import matplotlib.pyplot as plt
num_sims = 5 # Display five runs
t_init = 3
t_end = 7
N = 1000 # Compute 1000 grid points
dt = float / N
y_init = 0
c_theta = 0.7
c_mu = 1.5
c_sigma = 0.06
def mu:
"""Implement the Ornstein–Uhlenbeck mu.""" # = heta
return c_theta *
def sigma:
"""Implement the Ornstein–Uhlenbeck sigma.""" # = \sigma
return c_sigma
def dW:
"""Sample a random number at each call."""
return np.random.normal
ts = np.arange
ys = np.zeros
ys = y_init
for _ in range:
for i in range:
t = * dt
y = ys
ys = y + mu * dt + sigma * dW
plt.plot
plt.show

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

%% Initialization and Utility
close all;
clear all;
numSims = 5; % display five runs
tBounds = ; % The bounds of t
N = 1000; % Compute 1000 grid points
dt = - tBounds) / N ;
y_init = 1; % Initial y condition
pd = makedist; % Initialize the probability distribution for our
% random variable with mean 0 and
% stdev of sqrt
c = ; % the initial Theta, Mu, and Sigma, respectively
ts = linspace, tBounds; % From t0-->t1 with N points
ys = zeros; % 1xN Matrix of zeros
ys = y_init;
%% Computing the Process
for j = 1:numSims
for i = 2:numel
t = .* dt;
y = ys;
mu = c.* ;
sigma = c;
dW = random;

ys = y + mu.* dt + sigma.* dW;
end
figure
hold on;
plot
end