Skip to content
LeetQuant
E01 Advanced Probability and Stochastic Calculus

E01 · Advanced Probability and Stochastic Calculus · Lesson 3 of 4

Stochastic Differential Equations

What an Itô integral is and why it is a martingale, the Itô isometry, explicit solutions and moments of geometric Brownian motion and Ornstein–Uhlenbeck, and simulating SDEs with Euler–Maruyama, including strong versus weak convergence.

20 min

What an SDE means

An SDE such as

dXt=a(t,Xt) dt+b(t,Xt) dWt,X0=x0,dX_t = a(t, X_t)\,dt + b(t, X_t)\,dW_t, \qquad X_0 = x_0,

is shorthand for the integral equation

Xt=x0+∫0ta(s,Xs) ds+∫0tb(s,Xs) dWs.X_t = x_0 + \int_0^t a(s, X_s)\,ds + \int_0^t b(s, X_s)\,dW_s .

The first integral is ordinary. The second is an Itô integral, and Brownian paths are too rough for it to be a Riemann–Stieltjes integral. The objective of this lesson: know what the Itô integral is, compute its moments, solve the two SDEs interviewers ask about (GBM and OU) in closed form, and simulate any SDE when no closed form exists. Itô's lemma is assumed.

The Itô integral

For a process HtH_t that is adapted (its value at tt uses only information up to tt), define

∫0THt dWt=lim⁡n→∞∑iHti (Wti+1−Wti),\int_0^T H_t\,dW_t = \lim_{n\to\infty} \sum_i H_{t_i}\,\big(W_{t_{i+1}} - W_{t_i}\big),

the limit taken in mean square as the grid is refined. The integrand is evaluated at the left endpoint. This is the modelling choice that makes the integral a fair game: you choose your position HtiH_{t_i} before seeing the next increment.

Example. Take H=WH = W. Using WtiΔWi=12(Wti+12−Wti2)−12(ΔWi)2W_{t_i}\Delta W_i = \tfrac12\big(W_{t_{i+1}}^2 - W_{t_i}^2\big) - \tfrac12(\Delta W_i)^2 and summing,

∑iWtiΔWi=12WT2−12∑i(ΔWi)2  ⟶  12WT2−12T.\sum_i W_{t_i}\Delta W_i = \tfrac12 W_T^2 - \tfrac12\sum_i (\Delta W_i)^2 \;\longrightarrow\; \tfrac12 W_T^2 - \tfrac12 T .

So ∫0TWt dWt=12(WT2−T)\int_0^T W_t\,dW_t = \tfrac12(W_T^2 - T), not the 12WT2\tfrac12 W_T^2 of ordinary calculus. Evaluating at the midpoint instead (the Stratonovich integral) gives 12WT2\tfrac12 W_T^2, but loses the martingale property.

Three properties you use constantly

For adapted HH with E∫0THt2 dt<∞\mathbb{E}\int_0^T H_t^2\,dt < \infty:

  1. Zero mean: E[∫0THt dWt]=0\mathbb{E}\left[\int_0^T H_t\,dW_t\right] = 0. Each term HtiΔWiH_{t_i}\Delta W_i has mean zero because ΔWi\Delta W_i is independent of HtiH_{t_i}.
  2. Martingale: Mt=∫0tHs dWsM_t = \int_0^t H_s\,dW_s is a martingale.
  3. Itô isometry:
E[(∫0THt dWt)2]=E∫0THt2 dt.\mathbb{E}\left[\left(\int_0^T H_t\,dW_t\right)^2\right] = \mathbb{E}\int_0^T H_t^2\,dt .

The isometry holds because cross terms E[HtiΔWiHtjΔWj]\mathbb{E}[H_{t_i}\Delta W_i H_{t_j}\Delta W_j] with i<ji < j vanish (condition on time tjt_j), and diagonal terms give E[Hti2] Δti\mathbb{E}[H_{t_i}^2]\,\Delta t_i. A useful special case: if Ht=h(t)H_t = h(t) is deterministic, the integral is Gaussian, ∫0Th(t) dWt∼N(0,∫0Th(t)2 dt)\int_0^T h(t)\,dW_t \sim N\big(0, \int_0^T h(t)^2\,dt\big).

Check on the example: Var⁡(12(WT2−T))=14Var⁡(WT2)=14⋅2T2=12T2\operatorname{Var}\big(\tfrac12(W_T^2 - T)\big) = \tfrac14\operatorname{Var}(W_T^2) = \tfrac14 \cdot 2T^2 = \tfrac12 T^2, and the isometry gives ∫0TE[Wt2] dt=∫0Tt dt=12T2\int_0^T \mathbb{E}[W_t^2]\,dt = \int_0^T t\,dt = \tfrac12 T^2.

Worked example

the integral of Brownian motion

Find the distribution of I=∫0TWt dtI = \int_0^T W_t\,dt and its correlation with WTW_T. (A classic interview question; it is also why a geometric-average Asian option has a closed form, since log prices are linear in WW.)

Show solution

Rewrite as an Itô integral. Itô's product rule on tWttW_t (no cross-variation term, since tt has none) gives d(tWt)=Wt dt+t dWtd(tW_t) = W_t\,dt + t\,dW_t. Integrating from 00 to TT,

I=∫0TWt dt=TWT−∫0Tt dWt=∫0T(T−t) dWt.I = \int_0^T W_t\,dt = T W_T - \int_0^T t\,dW_t = \int_0^T (T - t)\,dW_t .

Distribution. The integrand T−tT - t is deterministic, so II is Gaussian with mean 00 and, by the isometry,

Var⁡(I)=∫0T(T−t)2 dt=T33.\operatorname{Var}(I) = \int_0^T (T - t)^2\,dt = \frac{T^3}{3} .

Correlation with WTW_T. Write WT=∫0T1 dWtW_T = \int_0^T 1\,dW_t. The polarised isometry, E[∫f dW∫g dW]=∫fg dt\mathbb{E}\left[\int f\,dW \int g\,dW\right] = \int f g\,dt, gives

Cov⁡(I,WT)=∫0T(T−t) dt=T22,ρ=T2/2T3/3 T=32≈0.866.\operatorname{Cov}(I, W_T) = \int_0^T (T - t)\,dt = \frac{T^2}{2}, \qquad \rho = \frac{T^2/2}{\sqrt{T^3/3}\,\sqrt T} = \frac{\sqrt 3}{2} \approx 0.866 .

Sanity check by direct computation: Var⁡(I)=∫0T ⁣∫0Tmin⁡(s,t) ds dt=T3/3\operatorname{Var}(I) = \int_0^T\!\int_0^T \min(s, t)\,ds\,dt = T^3/3, and Cov⁡(I,WT)=∫0Tmin⁡(t,T) dt=T2/2\operatorname{Cov}(I, W_T) = \int_0^T \min(t, T)\,dt = T^2/2. The isometry route is faster and generalises to any deterministic integrand.

When does an SDE have a solution?

If aa and bb are Lipschitz in xx and grow at most linearly, the SDE has a unique strong solution (a process built from the given Brownian path). Both examples below satisfy this. Square-root diffusions such as CIR, b(x)=σxb(x) = \sigma\sqrt{x}, are not Lipschitz at 00 and need a separate argument, which is one reason they are harder to simulate.

Geometric Brownian motion: moments

The Itô's lemma lesson solved dSt=μSt dt+σSt dWtdS_t = \mu S_t\,dt + \sigma S_t\,dW_t as ST=S0exp⁡((μ−12σ2)T+σWT)S_T = S_0\exp\big((\mu - \tfrac12\sigma^2)T + \sigma W_T\big). Moments follow from E[ecWT]=ec2T/2\mathbb{E}[e^{cW_T}] = e^{c^2T/2}:

E[ST]=S0eμT,Var⁡(ST)=S02e2μT(eσ2T−1),median(ST)=S0e(μ−σ2/2)T.\mathbb{E}[S_T] = S_0 e^{\mu T}, \qquad \operatorname{Var}(S_T) = S_0^2 e^{2\mu T}\big(e^{\sigma^2 T} - 1\big), \qquad \text{median}(S_T) = S_0 e^{(\mu - \sigma^2/2)T}.

For example, S0=100S_0 = 100, μ=8%\mu = 8\%, σ=20%\sigma = 20\%, T=1T = 1: mean 108.33108.33, standard deviation 21.8821.88, median 106.18106.18, and P(ST<100)=Φ(−0.06/0.2)=Φ(−0.3)≈0.382P(S_T < 100) = \Phi(-0.06/0.2) = \Phi(-0.3) \approx 0.382. You can also get the mean without solving: taking expectations of the SDE kills the dWdW term, so m(t)=E[St]m(t) = \mathbb{E}[S_t] solves m′=μmm' = \mu m.

Ornstein–Uhlenbeck: the explicit solution

dXt=θ(μ−Xt) dt+σ dWt,θ>0.dX_t = \theta(\mu - X_t)\,dt + \sigma\,dW_t, \qquad \theta > 0 .

This is linear with additive noise, so use an integrating factor as for a linear ODE. By the product rule,

d(eθtXt)=eθt(θXt dt+dXt)=eθt(θμ dt+σ dWt).d\big(e^{\theta t}X_t\big) = e^{\theta t}\big(\theta X_t\,dt + dX_t\big) = e^{\theta t}\big(\theta\mu\,dt + \sigma\,dW_t\big).

Integrate from 00 to tt and divide by eθte^{\theta t}:

Xt=μ+(x0−μ)e−θt+σ∫0te−θ(t−s) dWs.X_t = \mu + (x_0 - \mu)e^{-\theta t} + \sigma\int_0^t e^{-\theta(t - s)}\,dW_s .

The stochastic integral has a deterministic integrand, so XtX_t is Gaussian with

E[Xt]=μ+(x0−μ)e−θt,Var⁡(Xt)=σ2∫0te−2θ(t−s) ds=σ22θ(1−e−2θt).\mathbb{E}[X_t] = \mu + (x_0 - \mu)e^{-\theta t}, \qquad \operatorname{Var}(X_t) = \sigma^2\int_0^t e^{-2\theta(t - s)}\,ds = \frac{\sigma^2}{2\theta}\big(1 - e^{-2\theta t}\big).

The same calculation for two times s≤ts \le t gives Cov⁡(Xs,Xt)=σ22θ(e−θ(t−s)−e−θ(t+s))\operatorname{Cov}(X_s, X_t) = \dfrac{\sigma^2}{2\theta}\big(e^{-\theta(t - s)} - e^{-\theta(t + s)}\big), which tends to σ22θe−θ(t−s)\dfrac{\sigma^2}{2\theta}e^{-\theta(t - s)} in the stationary regime. The QR stochastic processes lesson used these formulas; this is where they come from.

Worked example

a Vasicek short rate

The short rate follows drt=0.5 (0.03−rt) dt+0.02 dWtdr_t = 0.5\,(0.03 - r_t)\,dt + 0.02\,dW_t (time in years) and is 8% today. Find the mean and standard deviation of r2r_2, the probability that r2<0r_2 < 0, and the long-run standard deviation.

Show solution

This is OU with θ=0.5\theta = 0.5, μ=0.03\mu = 0.03, σ=0.02\sigma = 0.02, x0=0.08x_0 = 0.08, t=2t = 2, so θt=1\theta t = 1.

Mean. 0.03+0.05 e−1=0.03+0.05×0.3679=0.048390.03 + 0.05\,e^{-1} = 0.03 + 0.05 \times 0.3679 = 0.04839, about 4.84%.

Variance. 0.0222×0.5(1−e−2)=0.0004×0.8647=0.000346\dfrac{0.02^2}{2 \times 0.5}\big(1 - e^{-2}\big) = 0.0004 \times 0.8647 = 0.000346, so the standard deviation is 0.018600.01860, about 1.86%.

Negative rates. r2r_2 is Gaussian, so P(r2<0)=Φ(−0.04839/0.01860)=Φ(−2.60)≈0.0046P(r_2 < 0) = \Phi(-0.04839/0.01860) = \Phi(-2.60) \approx 0.0046.

Long run. σ/2θ=0.02\sigma/\sqrt{2\theta} = 0.02, so 2%. After two years the variance is already 86% of its stationary value, because 1−e−2θt1 - e^{-2\theta t} approaches 1 at twice the rate the mean decays.

The non-zero probability of negative rates is the standard criticism of Vasicek, and the reason CIR uses σr\sigma\sqrt{r} noise.

Euler–Maruyama simulation

Most SDEs have no closed form. The Euler–Maruyama scheme freezes the coefficients over each step of length Δt\Delta t:

Xn+1=Xn+a(tn,Xn) Δt+b(tn,Xn) ΔWn,ΔWn=Δt Zn, Zn∼N(0,1).X_{n+1} = X_n + a(t_n, X_n)\,\Delta t + b(t_n, X_n)\,\Delta W_n, \qquad \Delta W_n = \sqrt{\Delta t}\,Z_n,\ Z_n \sim N(0, 1).

It is the left-endpoint Itô sum turned into an algorithm. Below it simulates the Vasicek example, where the exact answer is known.

import math
import random


def euler_maruyama(drift, diffusion, x0, T, n_steps, rng):
    """One Euler–Maruyama path; returns X_T."""
    dt = T / n_steps
    x = x0
    for i in range(n_steps):
        t = i * dt
        dW = rng.gauss(0.0, math.sqrt(dt))
        x += drift(t, x) * dt + diffusion(t, x) * dW
    return x


# Vasicek / OU short rate: dr = 0.5 (0.03 - r) dt + 0.02 dW, r0 = 8%
theta, mu, sigma, r0, T = 0.5, 0.03, 0.02, 0.08, 2.0
rng = random.Random(7)
samples = [
    euler_maruyama(lambda t, x: theta * (mu - x), lambda t, x: sigma, r0, T, 50, rng)
    for _ in range(20_000)
]
m = sum(samples) / len(samples)
v = sum((s - m) ** 2 for s in samples) / (len(samples) - 1)
exact_m = mu + (r0 - mu) * math.exp(-theta * T)
exact_v = sigma**2 / (2 * theta) * (1 - math.exp(-2 * theta * T))
print(f"mean {m:.5f} (exact {exact_m:.5f})")
print(f"sd   {math.sqrt(v):.5f} (exact {math.sqrt(exact_v):.5f})")
print(f"P(r_T < 0) {sum(s < 0 for s in samples) / len(samples):.4f}")

Output:

mean 0.04835 (exact 0.04839)
sd   0.01869 (exact 0.01860)
P(r_T < 0) 0.0048

Two errors are mixed here. The Monte Carlo error on the mean is about 0.0187/20000≈0.000130.0187/\sqrt{20000} \approx 0.00013. The discretisation bias comes from the scheme itself: with Δt=0.04\Delta t = 0.04 the Euler mean is 0.03+0.05 (1−0.5×0.04)50=0.048210.03 + 0.05\,(1 - 0.5 \times 0.04)^{50} = 0.04821 instead of 0.048390.04839. For OU you can avoid the bias entirely by sampling the exact Gaussian transition, Xn+1=μ+(Xn−μ)e−θΔt+σ(1−e−2θΔt)/(2θ) ZnX_{n+1} = \mu + (X_n - \mu)e^{-\theta\Delta t} + \sigma\sqrt{(1 - e^{-2\theta\Delta t})/(2\theta)}\,Z_n; for GBM, simulate ln⁡S\ln S exactly instead. Euler is for the models where no such trick exists.

Strong versus weak convergence

There are two ways to ask whether the scheme is accurate as Δt→0\Delta t \to 0:

MeasuresEuler–Maruyama orderMatters for
StrongE∣XTΔt−XT∣\mathbb{E}\lvert X_T^{\Delta t} - X_T\rvert, path by path, same Brownian path12\tfrac12 in general; 11 when bb does not depend on xx (additive noise, e.g. OU)Hedging simulations, path-dependent payoffs, multilevel Monte Carlo
Weak∣E[f(XTΔt)]−E[f(XT)]∣\lvert\mathbb{E}[f(X_T^{\Delta t})] - \mathbb{E}[f(X_T)]\rvert, distributions only11Pricing European payoffs

Halving Δt\Delta t shrinks the strong error by about 2\sqrt 2 and the weak error by about 22. The Milstein scheme adds the term 12b ∂xb ((ΔW)2−Δt)\tfrac12 b\,\partial_x b\,\big((\Delta W)^2 - \Delta t\big), which comes from the next term in the Itô–Taylor expansion, and raises the strong order to 11.

The strong order is easy to see for GBM, where the exact solution on the same path is known:

import math
import random

S0, mu, sigma, T = 100.0, 0.08, 0.2, 1.0
rng = random.Random(1)
n_paths, fine = 4000, 256
# Brownian increments on the finest grid; coarser grids sum them, so every
# step size sees the same underlying paths.
paths = [[rng.gauss(0.0, math.sqrt(T / fine)) for _ in range(fine)] for _ in range(n_paths)]

for n in (16, 32, 64, 128, 256):
    k, dt = fine // n, T / n
    err = 0.0
    for dws in paths:
        s = S0
        for j in range(n):
            s += mu * s * dt + sigma * s * sum(dws[j * k : (j + 1) * k])
        exact = S0 * math.exp((mu - sigma**2 / 2) * T + sigma * sum(dws))
        err += abs(s - exact)
    print(f"n={n:4d}  strong error {err / n_paths:.4f}")

Output:

n=  16  strong error 0.6088
n=  32  strong error 0.4353
n=  64  strong error 0.3063
n= 128  strong error 0.2173
n= 256  strong error 0.1520

Each halving divides the error by about 1.40≈21.40 \approx \sqrt 2: strong order 12\tfrac12. The weak error for f(x)=xf(x) = x can be done by hand: each Euler step multiplies the mean by 1+μΔt1 + \mu\Delta t, so E[STΔt]=S0(1+μT/n)n\mathbb{E}[S_T^{\Delta t}] = S_0(1 + \mu T/n)^n. For n=10,20,40n = 10, 20, 40 that is 108.2942108.2942, 108.3114108.3114, 108.3201108.3201 against the exact 108.3287108.3287: errors 0.03450.0345, 0.01730.0173, 0.00870.0087, halving each time.

Knowledge check

What is Var⁡(∫02Wt dWt)\operatorname{Var}\left(\int_0^2 W_t\,dW_t\right)?

Knowledge check

An OU process has dXt=2(μ−Xt) dt+0.4 dWtdX_t = 2(\mu - X_t)\,dt + 0.4\,dW_t. What is its stationary standard deviation?

Knowledge check

You price a European option by Monte Carlo, simulating the underlying SDE with Euler–Maruyama. What happens to the discretisation bias in the price when you halve the time step?

In interviews

  • "Compute Var⁡∫0TWt dt\operatorname{Var}\int_0^T W_t\,dt": rewrite as ∫0T(T−t) dWt\int_0^T (T - t)\,dW_t and apply the isometry. Say the integration by parts out loud; that is the step being tested.
  • "Solve the OU SDE": state the integrating factor eθte^{\theta t}, write the solution, then read off mean and variance. Finish with the limits t→∞t \to \infty (stationary law) and the half-life ln⁡2/θ\ln 2/\theta.
  • "Is ∫0tWs2 dWs\int_0^t W_s^2\,dW_s a martingale?" Yes: it is an Itô integral with a square-integrable adapted integrand. By contrast ∫0tWs2 ds\int_0^t W_s^2\,ds is not.
  • "How would you simulate this?" Give Euler–Maruyama, then say whether an exact scheme exists (GBM in logs, OU Gaussian transitions), and distinguish bias (step size) from noise (path count).

Key takeaways

  • An Itô integral is a limit of left-endpoint sums; it has mean zero, is a martingale, and satisfies E[(∫H dW)2]=E∫H2 dt\mathbb{E}\big[(\int H\,dW)^2\big] = \mathbb{E}\int H^2\,dt.
  • With a deterministic integrand the Itô integral is Gaussian with variance ∫h2 dt\int h^2\,dt; this solves most "distribution of" questions.
  • ∫0TW dW=12(WT2−T)\int_0^T W\,dW = \tfrac12(W_T^2 - T); the −12T-\tfrac12 T is quadratic variation.
  • GBM: E[ST]=S0eμT\mathbb{E}[S_T] = S_0e^{\mu T}, Var⁡(ST)=S02e2μT(eσ2T−1)\operatorname{Var}(S_T) = S_0^2e^{2\mu T}(e^{\sigma^2T} - 1). OU: solve with eθte^{\theta t}; Gaussian with mean μ+(x0−μ)e−θt\mu + (x_0 - \mu)e^{-\theta t} and variance σ22θ(1−e−2θt)\frac{\sigma^2}{2\theta}(1 - e^{-2\theta t}).
  • Euler–Maruyama is strong order 12\tfrac12 and weak order 11; use exact transitions when they exist.

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).