26.4 Stochastic Differential Equations

Every trajectory in the previous sections was fixed by its start, and a flow never forgets where it came from: two starts always reach two different ends. The diffusion models of Chapter 17 are built on a process that does forget. Their forward process mixes new noise into its state, a little at a time, until nothing of the start remains, and generation, which runs it backward, has a known place to begin. Such a process solves a differential equation too, but not one that ordinary calculus can handle.

Let us begin with a case small enough to follow by hand. Take one number, x0=2x_0 = 2. At each step keep 99%99\% of it and add an independent Gaussian draw of standard deviation 0.02≈0.14\sqrt{0.02} \approx 0.14:

xk+1=0.99 xk+0.02  ξk,ξk∼N(0,1).x_{k+1} = 0.99\, x_k + \sqrt{0.02}\; \xi_k, \qquad \xi_k \sim \mathcal{N}(0, 1).

After 400400 steps the mean is 2⋅0.99400≈0.0362 \cdot 0.99^{400} \approx 0.036, and the variance is close to 11, where the 2%2\% that a step removes balances the 0.020.02 that it adds. Any other start ends in the same place, a draw from nearly N(0,1)\mathcal{N}(0, 1). One feature of the recurrence looks odd. If it describes a process in continuous time with time step h=0.01h = 0.01, then the decay 0.99=1−h0.99 = 1 - h changes xx by an amount of order hh, but the noise has size h\sqrt{h}, not hh. This choice is forced. Noise of size hh would vanish as h→0h \to 0, while noise of size h\sqrt{h} survives, and its square is of order hh, the same order as the drift. Ordinary calculus discards squares of small changes, so this one fact requires a new calculus.

The limit of such noise is Brownian motion, named after the botanist Robert Brown, who in 1827 watched particles from pollen grains jitter in water. Norbert Wiener made it a mathematical object in 1923, and Kiyoshi Itô built its calculus in the 1940s (Itô, 1944) on one rule: the squares of short Brownian increments add up to the elapsed time, so the chain rule gains a second-derivative term.

We build Brownian motion from coin flips and the calculus from that rule. Then we solve the process behind the recurrence, the forward process of Section 17.2. Given the start, its distribution at every time is a Gaussian, so a diffusion model can noise its data to any level in one draw. Simulation, and the measurement of its error, can wait for a second reading. Only the rule for squares is hard to believe, and its proof takes six lines.

26.4.1 Brownian Motion#

26.4.1.1 Noise as a Forward Process#

Before building any machinery, let us check what a forward process must do. Starting from data distributed as p0=pdatap_0 = p_{\textrm{data}}, it must end at one known distribution, whatever the data looked like,

p0=pdata  ⟶  pT≈N(0,I),p_0 = p_{\textrm{data}} \;\longrightarrow\; p_T \approx \mathcal{N}(\mathbf{0}, \mathbf{I}),

so that generation always knows where to start. No fixed flow does this for two datasets at once. A flow is an invertible map Φ\Phi (Section 26.1), so if it carried two distributions pp and qq to the same one, applying Φ−1\Phi^{-1} would show p=qp = q.

Let us try three processes on two "datasets" that are as simple as datasets can be: point masses at x0=2x_0 = 2 and at x0=−2x_0 = -2.

  • Drift alone, x˙=−x\dot{x} = -x. The solution x(t)=x0e−tx(t) = x_0 e^{-t} puts the two starts at ±2e−3=±0.0996\pm 2e^{-3} = \pm 0.0996 at t=3t = 3. They approach the point 00, which is not a Gaussian, and at every finite time they are still two distinct points.
  • Noise alone, dX=2 dWdX = \sqrt{2}\, dW, which means Xt=x0+2 WtX_t = x_0 + \sqrt{2}\, W_t for a noise WW whose variance grows linearly in time (the next subsection constructs it). The distribution of XtX_t, its law, is N(x0,2t)\mathcal{N}(x_0, 2t): each cloud keeps its center at ±2\pm 2 while its standard deviation 2t\sqrt{2t} grows without bound (2.452.45 at t=3t = 3). The start is forgotten only relative to this growing width, and there is no limiting law.
  • Both together, dX=−X dt+2 dWdX = -X\,dt + \sqrt{2}\, dW. From either start the law at t=3t = 3 is N(±0.0996, 0.9975)\mathcal{N}(\pm 0.0996,\, 0.9975), as Section 26.4.3 proves. The two laws are nearly indistinguishable from each other and from N(0,1)\mathcal{N}(0, 1).
Figure 26.4.1:

One failure, one usable process and the one we use, from the two starts x0=±2x_0=\pm2. Drift alone sends every start to the point 00. Noise alone keeps each start as the center of a cloud whose two-standard-deviation envelope x0±22tx_0\pm2\sqrt{2t} (dashed) grows without bound. Both together carry every start into the same limiting law N(0,1)\mathcal N(0,1), drawn sideways at the right. Green lines are the means.

Figure 26.4.1 draws the three. Only the first fails outright. The second is usable, and is in use. Once its variance 2t2t is far larger than that of the data, the start hardly matters. Processes of this kind are called variance-exploding, because without a limiting law they must make their final variance huge. The third is the running example of this section, and the recurrence of the opening simulates it. Its drift pulls each start toward zero while the noise refills the variance that the drift removes, and the two balance at variance 11.

In general, a forward process of this kind is a deterministic vector field plus a noise term. For data in Rd\mathbb{R}^d we write

dX  =  f(X,t) dt⏟drift  +  g(t) dW⏟diffusion,(26.4.1)d\mathbf{X} \;=\; \underbrace{\mathbf{f}(\mathbf{X}, t)\,dt}_{\textrm{drift}} \;+\; \underbrace{g(t)\,d\mathbf{W}}_{\textrm{diffusion}}, \tag{26.4.1}

which reads: over a short time dtdt, the state moves by the velocity f\mathbf{f} times dtdt and receives a random kick whose size is set by the noise amplitude gg. The drift steers the mean, and the diffusion injects variance. So far the kick dWd\mathbf{W} is only a symbol. We give it a meaning in one coordinate, and return to Rd\mathbb{R}^d only at the end of the Itô calculus.

26.4.1.2 From Random Walks to the Wiener Process#

We put an object behind the symbol by taking the limit of the simplest random process there is, a walk driven by coin flips. The limit explains the square root in our recurrence.

Fix a step duration Δt\Delta t and let a walker take independent steps of size ±Δt\pm\sqrt{\Delta t}, each sign chosen by a fair coin. After kk steps, at time t=k Δtt = k\,\Delta t, the position is a sum of kk independent mean-zero steps of variance Δt\Delta t apiece. Variances of independent terms add (Section 24.2.2.1), so

E ⁣[Wt(Δt)]=0,Var ⁣(Wt(Δt))=k Δt=t,\mathbb{E}\!\left[W^{(\Delta t)}_t\right] = 0, \qquad \textrm{Var}\!\left(W^{(\Delta t)}_t\right) = k\,\Delta t = t,

whatever the step size. With Δt=0.01\Delta t = 0.01, for instance, 100100 steps of ±0.1\pm 0.1 give variance 100⋅0.01=1100 \cdot 0.01 = 1 at time 11. Steps of ±0.01\pm 0.01 on the same clock would give variance 0.010.01, and steps of ±1\pm 1 would give 100100. In general, steps of size c Δtac\,\Delta t^{a} give variance c2 t Δt2a−1c^2\, t\, \Delta t^{2a - 1} at time tt, which tends to 00 for a>12a > \tfrac12 and to ∞\infty for a<12a \lt \tfrac12. Only the square root leaves a finite nonzero variance in the limit, which is why it returns in the noise term of every numerical scheme below.

As Δt→0\Delta t \to 0 the number of steps before time tt grows without bound, and the central limit theorem (Section 24.3.2.3) makes the position Gaussian: a sum of many independent mean-zero terms with total variance tt is approximately N(0,t)\mathcal{N}(0, t).

The limit process is called Brownian motion or the Wiener process (Wiener, 1923). It is defined by four properties:

  1. W0=0W_0 = 0;
  2. independent increments: for s<ts \lt t, the increment Wt−WsW_t - W_s is independent of the entire path up to time ss;
  3. Gaussian increments: Wt−Ws∼N(0, t−s)W_t - W_s \sim \mathcal{N}(0,\, t - s);
  4. continuous sample paths.

That a process with these properties exists is Wiener's theorem, and that the rescaled walks converge to it is Donsker's theorem; we take both as given. The process spreads with standard deviation t\sqrt{t} forever (Figure 26.4.2), and its increments have the form

ΔW  =  Wt+Δt−Wt  =  Δt  ξ,ξ∼N(0,1),(26.4.2)\Delta W \;=\; W_{t + \Delta t} - W_t \;=\; \sqrt{\Delta t}\;\xi, \qquad \xi \sim \mathcal{N}(0, 1), \tag{26.4.2}

a standard normal draw scaled by Δt\sqrt{\Delta t}.

Figure 26.4.2:

One coin-flip walk of 10001000 steps ±0.001\pm\sqrt{0.001}, viewed on meshes Δt=0.1\Delta t=0.1, 0.010.01 and 0.0010.001 (faint gray: the full path). Each view is itself a random walk with step variance Δt\Delta t; refining the mesh fills in detail without changing the large-scale path. At each fixed time the spread follows the ±t\pm\sqrt t and ±2t\pm2\sqrt t envelopes.

Independent increments also fix how the values of WW at two times are related. For s≤ts \le t write Wt=Ws+(Wt−Ws)W_t = W_s + (W_t - W_s). The increment is independent of WsW_s and has mean zero, so Cov(Ws,Wt)=E[Ws2]=s=min⁡(s,t)\textrm{Cov}(W_s, W_t) = \mathbb{E}[W_s^2] = s = \min(s, t): the walk carries its position at time ss forward and adds noise that is uncorrelated with it. The formula is worth more than its one-line proof suggests. Jointly Gaussian values are determined by their means and covariances, so a process with jointly Gaussian values at any finite set of times, mean zero, covariance min⁡(s,t)\min(s, t) and continuous paths is a Brownian motion.

The same increments make the path rough. Brownian paths are continuous but nowhere differentiable, a theorem of Paley, Wiener, and Zygmund (Paley et al., 1933). The increment scaling (26.4.2) gives the heuristic reason:

∣ΔWΔt∣=∣ξ∣Δt  ⟶  ∞in probability as Δt→0,\left|\frac{\Delta W}{\Delta t}\right| = \frac{|\xi|}{\sqrt{\Delta t}} \;\longrightarrow\; \infty \quad \textrm{in probability as } \Delta t \to 0,

so a difference quotient grows without bound as the step shrinks. This is not a proof (it rules out a derivative at each fixed time, while "nowhere" concerns all times at once), but it shows the obstruction.

Rescaling time by a factor cc and space by c\sqrt{c} returns a Brownian motion (Exercise 2), so zooming in on a path shows the same roughness at every scale, where a differentiable curve would flatten into its tangent. Hence dW/dtdW/dt does not exist, and dWdW has to be given a meaning inside integrals. The Itô calculus supplies that meaning.

26.4.1.3 Simulation of Brownian Paths#

The increment form (26.4.2) is also a simulation recipe: take cumulative sums of Δt ξ\sqrt{\Delta t}\,\xi steps. We generate 20,00020{,}000 paths on [0,1][0, 1] in 500500 steps and put a question to them. At any one time Wt∼N(0,t)W_t \sim \mathcal{N}(0, t), so 95.4%95.4\% of the paths lie inside ±2t\pm 2\sqrt{t}, the outer envelope of Figure 26.4.2. What fraction stays inside the envelope at all times? It is worth a guess before running the cell.

rng = np.random.default_rng()
T_bm, n_bm, n_paths = 1.0, 500, 20000
dt_bm = T_bm / n_bm
dW_bm = np.sqrt(dt_bm) * rng.standard_normal((n_paths, n_bm))
W_bm = np.cumsum(dW_bm, axis=1)              # W at t = dt, 2 dt, ..., 1
band = 2 * np.sqrt(dt_bm * np.arange(1, n_bm + 1))  # 2 sqrt(t) on the grid
inside = np.abs(W_bm) < band
print(f'inside +-2 sqrt(t) at t = 1:   {inside[:, -1].mean():.3f}'
      '   (Gaussian: 0.954)')
for stride in (10, 1):                       # same paths, coarse then fine grid
    print(f'inside at all {n_bm // stride} grid times: '
          f'{inside[:, stride - 1::stride].all(axis=1).mean():.3f}')
inside +-2 sqrt(t) at t = 1:   0.954   (Gaussian: 0.954)
inside at all 50 grid times: 0.697
inside at all 500 grid times: 0.535

At the single time t=1t = 1 the fraction is about 95%95\%, as it must be. But only about seven paths in ten are inside at each of 5050 equally spaced times, and only a little over half at all 500500. A band that holds at each time does not hold for a path: every look is another chance to find the path outside. Watched at every instant, no path stays inside, since near t=0t = 0 a Brownian path crosses ±2t\pm 2\sqrt{t} again and again (the law of the iterated logarithm, which we do not prove). A statement about the law at one time is much weaker than a statement about whole paths, and the two return as the weak and the strong error of a simulation (Section 26.4.4.2).

26.4.2 Itô Calculus#

26.4.2.1 Quadratic Variation#

Ordinary calculus stops its Taylor expansions at first order because the square of a small change is negligible. For Brownian motion this fails, and the result of this subsection says by how much. It is the one on which the rest of the section rests.

Take a partition 0=t0<t1<⋯<tn=t0 = t_0 \lt t_1 \lt \cdots \lt t_n = t with increments Δti=ti+1−ti\Delta t_i = t_{i+1} - t_i and mesh hmax⁡=max⁡iΔtih_{\max} = \max_i \Delta t_i, and add up the squared increments of a path along it. For a differentiable path xx with ∣x′∣≤M|x'| \le M, the mean value theorem gives ∣Δxi∣≤M Δti|\Delta x_i| \le M\, \Delta t_i, so

∑i(Δxi)2  ≤  M2∑iΔti2  ≤  M2 hmax⁡ t  ⟶  0\sum_{i} (\Delta x_i)^2 \;\le\; M^2 \sum_i \Delta t_i^2 \;\le\; M^2\, h_{\max}\, t \;\longrightarrow\; 0

as the mesh shrinks: a smooth increment squared is of order Δt2\Delta t^2. A Brownian increment squared is of order Δt\Delta t, since E[(ΔWi)2]=Δti\mathbb{E}[(\Delta W_i)^2] = \Delta t_i, and these add up to tt however fine the mesh (Figure 26.4.3). The sum is random, but its fluctuations vanish in the limit.

Proposition (quadratic variation of Brownian motion). As the mesh hmax⁡→0h_{\max} \to 0,

Qn  =  ∑i=0n−1(Wti+1−Wti)2  ⟶  tin mean square:E ⁣[(Qn−t)2]→0.(26.4.3)Q_n \;=\; \sum_{i=0}^{n-1} \left(W_{t_{i+1}} - W_{t_i}\right)^2 \;\longrightarrow\; t \quad \textrm{in mean square:} \quad \mathbb{E}\!\left[(Q_n - t)^2\right] \to 0. \tag{26.4.3}

Proof. Each increment is N(0,Δti)\mathcal{N}(0, \Delta t_i), so E[(ΔWi)2]=Δti\mathbb{E}[(\Delta W_i)^2] = \Delta t_i and E[Qn]=∑iΔti=t\mathbb{E}[Q_n] = \sum_i \Delta t_i = t for every partition. For the spread, a Gaussian has fourth moment 3 Δti23\,\Delta t_i^2, so Var ⁣((ΔWi)2)=3Δti2−Δti2=2Δti2\textrm{Var}\!\left((\Delta W_i)^2\right) = 3\Delta t_i^2 - \Delta t_i^2 = 2\Delta t_i^2; the increments are independent, so their variances add:

E ⁣[(Qn−t)2]=Var(Qn)=∑i2 Δti2  ≤  2 hmax⁡∑iΔti=2 hmax⁡ t  ⟶  0,\mathbb{E}\!\left[(Q_n - t)^2\right] = \textrm{Var}(Q_n) = \sum_i 2\,\Delta t_i^2 \;\le\; 2\,h_{\max} \sum_i \Delta t_i = 2\,h_{\max}\, t \;\longrightarrow\; 0,

and mean-square convergence follows. ■\blacksquare

The statement deserves a second look: every increment is random, and in the limit the sum of their squares is not. On a uniform grid with nn intervals of [0,1][0, 1] the proof gives the sum a standard deviation of 2/n\sqrt{2/n}, which is 0.350.35 for n=16n = 16 and 0.0030.003 for n=218n = 2^{18}.

Figure 26.4.3:

Running sums of squared increments along one Brownian path for meshes of n=16n = 16, 256256, and 40964096 intervals (darker shades are finer meshes). The sums approach y=ty = t, whereas the sums for the smooth path sin⁡2πt\sin 2\pi t (gray) shrink like 2π2/n2\pi^2/n.

The proposition is usually written as the Itô multiplication table, the working rules for products of differentials:

(dW)2=dt,dW dt=0,(dt)2=0.(26.4.4)(dW)^2 = dt, \qquad dW\,dt = 0, \qquad (dt)^2 = 0. \tag{26.4.4}

The first rule is (26.4.3) in shorthand, and it is a statement about sums. A single squared increment is Δt ξ2\Delta t\,\xi^2, which is rarely close to Δt\Delta t: about one time in four it is below a tenth of Δt\Delta t, and about one time in six it is above twice Δt\Delta t. Only the sum over many steps settles, as the proof showed. (For the coin-flip walk every squared step equals Δt\Delta t, and nothing needs to settle.) The other two rules record that dW dtdW\,dt, of size Δt3/2\Delta t^{3/2}, and (dt)2(dt)^2 vanish faster than Δt\Delta t and contribute nothing in the limit.

At fine meshes the fluctuations are so small that a single path shows the limit. We sample a path on 2182^{18} intervals of [0,1][0, 1], view it on coarser subgrids, and print its sum of squares next to the predicted standard deviation. For comparison we print the same sum for the smooth path x(t)=sin⁡2πtx(t) = \sin 2\pi t next to 2π2/n2\pi^2/n, the mesh 1/n1/n times ∫01x′(t)2 dt=2π2\int_0^1 x'(t)^2\,dt = 2\pi^2.

n_fine = 2**18
dW_qv = np.sqrt(1.0 / n_fine) * rng.standard_normal(n_fine)
W_qv = np.concatenate([[0.0], np.cumsum(dW_qv)])
print(f'{"n":>7} {"QV of W":>8} {"sqrt(2/n)":>10} {"QV of sin":>10}'
      f' {"2 pi^2/n":>9}')
for k in range(4, 19, 2):
    n = 2**k
    W_n = W_qv[::n_fine // n]                 # the same path on a coarser grid
    s_n = np.sin(2 * np.pi * np.linspace(0, 1, n + 1))
    print(f'{n:7d} {np.sum(np.diff(W_n)**2):8.4f} {np.sqrt(2 / n):10.4f} '
          f'{np.sum(np.diff(s_n)**2):10.6f} {2 * np.pi**2 / n:9.6f}')
      n  QV of W  sqrt(2/n)  QV of sin  2 pi^2/n
     16   1.1551     0.3536   1.217927  1.233701
     64   0.9103     0.1768   0.308177  0.308425
    256   0.8957     0.0884   0.077102  0.077106
   1024   0.9560     0.0442   0.019277  0.019277
   4096   1.0044     0.0221   0.004819  0.004819
  16384   0.9893     0.0110   0.001205  0.001205
  65536   1.0007     0.0055   0.000301  0.000301
 262144   1.0017     0.0028   0.000075  0.000075

The Brownian column scatters around 11 with the standard deviation 2/n\sqrt{2/n} that the proof predicts. On 1616 intervals it typically lands between about 0.50.5 and 1.61.6, and at n=218n = 2^{18} it is within about 0.010.01 of 11. The smooth column is still 1.221.22 at n=16n = 16, but it shrinks like 2π2/n2\pi^2/n and agrees with that prediction to three digits from n=256n = 256 on.

26.4.2.2 The Itô Integral#

The rough path forces us to define integrals against dWdW directly. We start with the simplest one, ∫0tW dW\int_0^t W\,dW, and find that the usual Riemann-sum definition does not single out an answer.

For a smooth integrator, Riemann sums with the integrand evaluated at the left endpoint, the right endpoint, or the midpoint of each interval all converge to the same limit. Here they do not. With WW itself as the integrand, the right-endpoint sum exceeds the left-endpoint sum by

∑iWti+1 ΔWi−∑iWti ΔWi=∑i(Wti+1−Wti)ΔWi=∑i(ΔWi)2  ⟶  t,\sum_i W_{t_{i+1}}\,\Delta W_i - \sum_i W_{t_i}\,\Delta W_i = \sum_i \left(W_{t_{i+1}} - W_{t_i}\right)\Delta W_i = \sum_i (\Delta W_i)^2 \;\longrightarrow\; t,

by the proposition just proved. The two discretizations of "the same" integral disagree by a finite, deterministic amount. Let us look at one path with 10001000 steps.

n_lr = 1000
dt_lr = 1.0 / n_lr
t_lr = np.linspace(0, 1, n_lr + 1)
dW_lr = np.sqrt(dt_lr) * rng.standard_normal(n_lr)
W_lr = np.concatenate([[0.0], np.cumsum(dW_lr)])
left = np.sum(W_lr[:-1] * dW_lr)              # integrand at the left endpoint
right = np.sum(W_lr[1:] * dW_lr)              # integrand at the right endpoint
print(f'left sum {left:.4f}   right sum {right:.4f}')
print(f'right - left {right - left:.4f}   sum of (dW)^2 {np.sum(dW_lr**2):.4f}')
left sum -0.3614   right sum 0.6124
right - left 0.9738   sum of (dW)^2 0.9738

The left sum is a random number that changes from run to run. The right sum exceeds it by the sum of squared increments in every printed digit, as the identity above says it must. What the run adds is the size of the gap. It usually lies within about 0.10.1 of 11 (its standard deviation is 2/1000≈0.045\sqrt{2/1000} \approx 0.045), and a finer mesh would not close it.

Both sums have limits, and we can find them with what we have. Their total telescopes, because (Wti+1+Wti)ΔWi=Wti+12−Wti2\left(W_{t_{i+1}} + W_{t_i}\right)\Delta W_i = W_{t_{i+1}}^2 - W_{t_i}^2 adds up to Wt2W_t^2 on every partition, and their difference tends to tt. Hence the left sum tends to 12(Wt2−t)\tfrac12\left(W_t^2 - t\right) and the right sum to 12(Wt2+t)\tfrac12\left(W_t^2 + t\right), in mean square. Ordinary calculus would answer 12Wt2\tfrac12 W_t^2, which is neither limit but their average. We must choose an evaluation point, and the choice changes the answer. Which of the two sums should be the integral?

Itô's choice is the left endpoint, so that the integrand never sees the coming increment. A fair game explains why. Think of ΔWi\Delta W_i as the outcome of the ii-th fair bet and of HiH_i as the stake placed on it. A gambler must choose the stake from what is known at time tit_i, before the bet is settled, and the total winnings are ∑iHi ΔWi\sum_i H_i\,\Delta W_i. Because the increment ΔWi\Delta W_i is independent of everything known at tit_i, including HiH_i, each bet has expected winnings E[Hi ΔWi]=E[Hi] E[ΔWi]=0\mathbb{E}[H_i\,\Delta W_i] = \mathbb{E}[H_i]\,\mathbb{E}[\Delta W_i] = 0, so no betting strategy has positive expected winnings. A stake that could peek at the outcome would make money on average. The right-endpoint sum places such a stake, and its mean is indeed tt, not zero.

The definition writes this rule down. An integrand is predictable if its value on each interval is decided before the increment over that interval is drawn. A simple predictable process HH is constant on each interval, Hs=HiH_s = H_i for s∈(ti,ti+1]s \in (t_i, t_{i+1}], where HiH_i is determined by the path up to time tit_i and E[Hi2]<∞\mathbb{E}[H_i^2] \lt \infty. Its Itô integral is the sum of the winnings,

∫0tHs dWs  =  ∑iHi(Wti+1−Wti).(26.4.5)\int_0^t H_s \, dW_s \;=\; \sum_i H_i\left(W_{t_{i+1}}-W_{t_i}\right). \tag{26.4.5}

The fair-game argument shows that it has mean zero, E ⁣[∫0tH dW]=0\mathbb{E}\!\left[\int_0^t H\,dW\right] = 0. Its variance is computable as well.

Proposition (Itô isometry). For a simple predictable HH,

E ⁣[(∫0tHs dWs) ⁣2 ]  =  ∫0tE ⁣[Hs2]ds.(26.4.6)\mathbb{E}\!\left[\left(\int_0^t H_s\,dW_s\right)^{\!2}\,\right] \;=\; \int_0^t \mathbb{E}\!\left[H_s^2\right] ds. \tag{26.4.6}

Proof. Expand the square of the sum in (26.4.5). The step that needs care is the cross terms. A cross term with i<ji \lt j contains the factor ΔWj\Delta W_j, which is independent of everything known at time tjt_j, including the product Hi ΔWi HjH_i\,\Delta W_i\,H_j. So the expectation factorizes: E ⁣[Hi ΔWi Hj ΔWj]=E ⁣[Hi ΔWi Hj]E[ΔWj]=0\mathbb{E}\!\left[H_i\,\Delta W_i\, H_j\, \Delta W_j\right] = \mathbb{E}\!\left[H_i\,\Delta W_i\, H_j\right]\mathbb{E}[\Delta W_j] = 0. A diagonal term factorizes the same way into E ⁣[Hi2]E ⁣[(ΔWi)2]=E ⁣[Hi2]Δti\mathbb{E}\!\left[H_i^2\right]\mathbb{E}\!\left[(\Delta W_i)^2\right] = \mathbb{E}\!\left[H_i^2\right]\Delta t_i. Summing the diagonal gives ∑iE[Hi2] Δti=∫0tE[Hs2] ds\sum_i \mathbb{E}[H_i^2]\,\Delta t_i = \int_0^t \mathbb{E}[H_s^2]\,ds. ■\blacksquare

The isometry also carries the definition beyond step processes, to integrands such as H=WH = W itself. It says that H↦∫H dWH \mapsto \int H\,dW preserves the norm ∥H∥2=∫0tE[Hs2] ds\|H\|^2 = \int_0^t \mathbb{E}[H_s^2]\,ds. So if simple predictable processes converge to HH in this norm, their integrals converge in mean square, and the limit is taken as the definition of ∫0tH dW\int_0^t H\,dW. The zero mean and the isometry survive the limit and hold for every predictable HH of finite norm, and the left-endpoint sums of a continuous integrand of this kind converge to its integral. We do not prove these statements; see Øksendal (2003), chapter 3.

The integral we started from now has a value, the limit of its left-endpoint sums:

∫0tWs dWs=12(Wt2−t).(26.4.7)\int_0^t W_s\,dW_s = \tfrac12\left(W_t^2 - t\right). \tag{26.4.7}

This is the answer of ordinary calculus minus t/2t/2. The −t/2-t/2 gives it the mean zero that a fair game must have, since E[Wt2]=t\mathbb{E}[W_t^2] = t. The isometry can be checked on it too. It turns the variance of a noise integral into an ordinary integral of a second moment, here ∫01E[Ws2] ds=∫01s ds=12\int_0^1 \mathbb{E}[W_s^2]\,ds = \int_0^1 s\,ds = \tfrac12 at t=1t = 1, and the closed form agrees: W12W_1^2 is a squared standard normal, with variance 22, so Var(12(W12−1))=14⋅2=12\textrm{Var}\big(\tfrac12(W_1^2 - 1)\big) = \tfrac14 \cdot 2 = \tfrac12.

The other evaluation points define other integrals. The midpoint choice, or the average of the left and right sums, defines the Stratonovich integral (Stratonovich, 1966). It obeys the ordinary chain rule, as the average 12Wt2\tfrac12 W_t^2 above suggests, but gives up the zero mean, because its integrand sees half of the coming increment. This book uses Itô's convention, for two reasons: the zero mean makes expectations easy, and the simplest simulation step, which evaluates the coefficients at the start of the step, is an Itô sum (Exercise 6).

26.4.2.3 Drift, Diffusion, and Solutions#

With the integral in hand, the forward-process template (26.4.1) acquires a meaning. A stochastic differential equation is written

dX=f(X,t) dt+g(X,t) dW,(26.4.8)dX = f(X, t)\,dt + g(X, t)\,dW, \tag{26.4.8}

and the differential form is shorthand for the integral equation

Xt=X0+∫0tf(Xs,s) ds+∫0tg(Xs,s) dWs,X_t = X_0 + \int_0^t f(X_s, s)\,ds + \int_0^t g(X_s, s)\,dW_s,

an ordinary integral plus an Itô integral. Over a short step, with the coefficients frozen at its start, E[dX]=f dt\mathbb{E}[dX] = f\,dt and Var(dX)=g2 dt\textrm{Var}(dX) = g^2\,dt. So the drift ff is the conditional mean velocity, and the diffusion coefficient gg sets the rate g2g^2 at which variance is pumped in. With g≡0g \equiv 0 the equation is the ODE of Section 26.1, and with f≡0f \equiv 0 and a constant gg it is the noise-only process Xt=X0+g WtX_t = X_0 + g\,W_t of Figure 26.4.1.

What is a solution? For an ODE it is one curve. Here it is one curve for each Brownian path. If ff and gg are Lipschitz in xx and grow at most linearly, ∣f(x,t)∣+∣g(x,t)∣≤C(1+∣x∣)|f(x, t)| + |g(x, t)| \le C(1 + |x|), and X0X_0 is independent of WW with E[X02]<∞\mathbb{E}[X_0^2] \lt \infty, then the start and the Brownian path determine the solution path uniquely (Øksendal (2003), Theorem 5.2.1). The proof is a stochastic version of the Picard iteration of Section 26.1.1.3, with the isometry in place of the integral bound. All the randomness therefore sits in WW. Running the equation many times from the same start with fresh noise gives a cloud of paths like those of Figure 26.4.1, and what a generative model cares about is the law of the cloud at each time.

One distinction will matter when we simulate. Noise is additive if its amplitude depends on time only, g=g(t)g = g(t), as in the template (26.4.1) and in every diffusion-model SDE of this chapter, the running example among them. It is multiplicative if the amplitude depends on the state, g=g(X,t)g = g(X, t), as for a stock price whose fluctuations are proportional to its level. The difference looks cosmetic, but it decides how accurately a simulated path follows the true one (Section 26.4.4.2).

26.4.2.4 Itô's Lemma#

To compute with an SDE we need the chain rule: if XX solves an SDE, what equation does a function ϕ(Xt,t)\phi(X_t, t) solve? The ordinary chain rule would give d(W2)=2W dWd(W^2) = 2W\,dW, and we know that this is wrong, since ∫0t2W dW=Wt2−t\int_0^t 2W\,dW = W_t^2 - t by (26.4.7). Something is missing.

One step of a walk shows where the missing term comes from, and what it is for other functions. Take ϕ(x)=x2\phi(x) = x^2 and one step ±Δt\pm\sqrt{\Delta t} from xx, each sign with probability 12\tfrac12. The average of ϕ\phi after the step is

12[(x+Δt)2+(x−Δt)2]=x2+Δt=ϕ(x)+12ϕ′′(x) Δt,\tfrac12\left[(x + \sqrt{\Delta t})^2 + (x - \sqrt{\Delta t})^2\right] = x^2 + \Delta t = \phi(x) + \tfrac12\phi''(x)\,\Delta t ,

since ϕ′′=2\phi'' = 2. The rise comes from the square of the step, which for a coin flip is Δt\Delta t every time, and which the ordinary chain rule throws away.

With x=0.7x = 0.7 and Δt=0.25\Delta t = 0.25 the step lands at 0.20.2 or 1.21.2, where ϕ\phi is 0.040.04 or 1.441.44. The average 0.740.74 lies above ϕ(0.7)=0.49\phi(0.7) = 0.49 by 0.25=Δt0.25 = \Delta t (Figure 26.4.4). A convex function gains from symmetric noise, and because a squared Brownian increment is of order dtdt, the gain per unit time, 12ϕ′′\tfrac12\phi'', does not vanish in the limit.

Figure 26.4.4:

The Itô correction as Jensen's gap. One step of ±Δt\pm\sqrt{\Delta t} from xx moves a convex ϕ\phi to an average that lies above ϕ(x)\phi(x) by 12ϕ′′(x) Δt\tfrac12\phi''(x)\,\Delta t; here ϕ=x2\phi=x^2, x=0.7x=0.7, Δt=0.25\Delta t=0.25, and the gap is 0.250.25.

The general formula comes from the same computation. Let XX solve dX=f dt+g dWdX = f\,dt + g\,dW and let ϕ(x,t)\phi(x, t) be smooth. A small change of ϕ\phi, expanded to second order because first order is no longer enough, is

dϕ=ϕt dt+ϕx dX+12ϕxx (dX)2+⋯ .d\phi = \phi_t\,dt + \phi_x\,dX + \tfrac12 \phi_{xx}\,(dX)^2 + \cdots.

Substitute dX=f dt+g dWdX = f\,dt + g\,dW and multiply out (dX)2(dX)^2 with the table (26.4.4):

(dX)2=f2(dt)2+2fg dt dW+g2(dW)2=g2 dt.(dX)^2 = f^2 (dt)^2 + 2 f g\, dt\, dW + g^2 (dW)^2 = g^2\,dt.

The drift and cross terms vanish, and the noise term survives at first order. Collecting terms gives the chain rule of stochastic calculus. What we have given is a heuristic derivation, so we state the result as a theorem without proof.

Theorem (Itô's lemma). Let XX solve dX=f dt+g dWdX = f\,dt + g\,dW under the conditions of the previous subsection, and let ϕ∈C1,2\phi \in C^{1,2}, once continuously differentiable in time and twice in the state. Then Yt=ϕ(Xt,t)Y_t = \phi(X_t, t) satisfies

dϕ  =  (ϕt+f ϕx+12g2 ϕxx)dt  +  g ϕx dW.(26.4.9)d\phi \;=\; \left(\phi_t + f\,\phi_x + \tfrac12 g^2\,\phi_{xx}\right) dt \;+\; g\,\phi_x \, dW. \tag{26.4.9}

This is the working formula of the subject: the ordinary chain rule, plus half the second derivative times the rate at which variance arrives. The new term 12g2ϕxx dt\tfrac12 g^2 \phi_{xx}\,dt is called the Itô correction. The proof has to control the remainder terms of the Taylor expansion in mean square (Øksendal (2003), chapter 4), and it needs both derivatives in the state: the lemma does not apply as stated to a function with a kink, such as ϕ(x)=∣x∣\phi(x) = |x|.

Example 1: W2W^2. Take X=WX = W (so f=0f = 0, g=1g = 1) and ϕ(x)=x2\phi(x) = x^2. The lemma gives d(W2)=2W dW+dtd(W^2) = 2W\,dW + dt, with the dtdt that the ordinary chain rule dropped. In integrated form this is Wt2=∫0t2Ws dWs+tW_t^2 = \int_0^t 2W_s\,dW_s + t, which is (26.4.7) again. One line of the lemma has replaced the telescoping sums.

The correction can be watched as it accumulates. On any grid the gap between Wt2W_t^2 and the left-endpoint sum for ∫0t2W dW\int_0^t 2W\,dW telescopes, Wt2−∑ti<t2WtiΔWi=∑ti<t(ΔWi)2W_t^2 - \sum_{t_i \lt t} 2W_{t_i}\Delta W_i = \sum_{t_i \lt t} (\Delta W_i)^2, so it is the running quadratic variation and should follow the line tt. We reuse the path of the left and right sums.

ito = np.concatenate([[0.0], np.cumsum(2 * W_lr[:-1] * dW_lr)])  # int 2W dW
gap = W_lr**2 - ito
print(f'final gap W_1^2 - int 2W dW = {gap[-1]:.4f}   (Ito correction: t = 1)')
print(f'max |gap_t - t| along the path = {np.abs(gap - t_lr).max():.4f}')
d2l.plot(t_lr, [W_lr**2, ito, ito + t_lr], 't', 'value',
         legend=['$W_t^2$', 'naive $\\int_0^t 2W\\,dW$',
                 '$\\int_0^t 2W\\,dW + t$'], figsize=(5, 3))
final gap W_1^2 - int 2W dW = 0.9738   (Ito correction: t = 1)
max |gap_t - t| along the path = 0.0364

The curve labeled naive is the Itô integral ∫0t2W dW\int_0^t 2W\,dW, which the ordinary chain rule would take for Wt2W_t^2. It sags below Wt2W_t^2, and adding tt puts it back on top. Along the whole path the gap usually stays within about 0.10.1 of tt, since by the proof of (26.4.3) its standard deviation at time tt is 2t Δt≤0.045\sqrt{2t\,\Delta t} \le 0.045 on this grid. At t=1t = 1 the gap is the sum of squared increments that the earlier cell printed.

Example 2: geometric Brownian motion. The SDE dX=μX dt+νX dWdX = \mu X\,dt + \nu X\,dW with X0>0X_0 > 0 models a quantity that grows at mean rate μ\mu with noise proportional to its size. It is Samuelson's model of a stock price (Samuelson, 1965), on which the option-pricing formula of Black and Scholes rests (Black and Scholes, 1973). Its noise is multiplicative. Apply the lemma to ϕ(x)=log⁡x\phi(x) = \log x, with ϕ′=1/x\phi' = 1/x, ϕ′′=−1/x2\phi'' = -1/x^2, and g2=ν2x2g^2 = \nu^2 x^2. The logarithm is smooth only for x>0x > 0, so assume for the moment that XX stays positive:

dlog⁡X=(μ−12ν2)dt+ν dW,soXt=X0exp⁡ ⁣((μ−12ν2)t+νWt).(26.4.10)d\log X = \left(\mu - \tfrac12\nu^2\right) dt + \nu\, dW, \qquad\textrm{so}\qquad X_t = X_0 \exp\!\left(\left(\mu - \tfrac12\nu^2\right) t + \nu W_t\right). \tag{26.4.10}

The formula we obtain is positive, and the lemma applied to it as a function of WtW_t and tt confirms that it solves the equation, which removes the assumption. The −12ν2-\tfrac12\nu^2 is the correction with ϕ′′<0\phi'' \lt 0: a concave function loses from symmetric noise. Noise therefore lowers the typical growth rate μ−12ν2\mu - \tfrac12\nu^2 below the mean growth rate μ\mu, and Exercise 3 shows how far the mean and the median of XtX_t drift apart. We will measure simulation errors against this exact solution.

In several dimensions the pattern is the same, and we take the result as given (Øksendal, 2003). For dX=f dt+G dWd\mathbf{X} = \mathbf{f}\,dt + \mathbf{G}\,d\mathbf{W} with X∈Rd\mathbf{X} \in \mathbb{R}^d, a matrix G\mathbf{G}, and a Brownian motion W\mathbf{W} whose coordinates are independent scalar Brownian motions, the multiplication table reads dWi dWj=δij dtdW_i\, dW_j = \delta_{ij}\,dt, and a twice continuously differentiable ϕ\phi satisfies

dϕ(X)  =  ∇ϕ⋅dX  +  12∑i,j(GG⊤)ij∂2ϕ∂xi ∂xj  dt.(26.4.11)d\phi(\mathbf{X}) \;=\; \nabla\phi \cdot d\mathbf{X} \;+\; \tfrac12 \sum_{i,j} \left(\mathbf{G} \mathbf{G}^\top\right)_{ij} \frac{\partial^2\phi}{\partial x_i\, \partial x_j} \; dt . \tag{26.4.11}

The correction weights each second derivative by the covariance rate GG⊤\mathbf{G}\mathbf{G}^\top of the noise. The diffusion-model SDEs of this chapter treat every coordinate alike, G=g(t) I\mathbf{G} = g(t)\,\mathbf{I}, and for them it reduces to 12g(t)2∑i∂2ϕ/∂xi2  dt\tfrac12 g(t)^2 \sum_i \partial^2\phi/\partial x_i^2\; dt, a multiple of the Laplacian of ϕ\phi. The next section needs the lemma in this form.

26.4.3 The Ornstein--Uhlenbeck Process#

The running example now gets its name and its solution. The Ornstein--Uhlenbeck (OU) process (Uhlenbeck and Ornstein, 1930) is

dX=−θX dt+g dW,θ>0,(26.4.12)dX = -\theta X \, dt + g\,dW, \qquad \theta > 0, \tag{26.4.12}

the decay ODE x˙=−θx\dot{x} = -\theta x of Section 26.1.2 with noise of constant amplitude gg added. Its authors wrote it for the velocity of a particle that is kicked by molecules and slowed by friction. The drift pulls excursions back toward zero at rate θ\theta, and the noise keeps producing new ones (Figure 26.4.5). The recurrence of the opening is this process with θ=1\theta = 1 and g=2g = \sqrt{2}, advanced on a grid with step h=0.01h = 0.01 by freezing the drift over each step: one step multiplies by 1−θh=0.991 - \theta h = 0.99 and adds noise of standard deviation gh=0.02g\sqrt{h} = \sqrt{0.02}.

Figure 26.4.5:

Mean reversion. The drift −θx-\theta x (arrows) pulls every path toward 00 at rate θ=1\theta=1 while noise of amplitude g=2g=\sqrt2 keeps it moving; paths from three starts forget them within a few multiples of 1/θ1/\theta and then fluctuate inside the band of two standard deviations of the limiting law, ±2g2/2θ=±2\pm2\sqrt{g^2/2\theta}=\pm2 (labeled stationary). Right: its density.

26.4.3.1 The Transition Kernel#

The OU process is one of the few SDEs whose law is known in closed form at every time, and this is why diffusion models are built on it. The law of XtX_t given the start X0=x0X_0 = x_0, read as a function of the start, is called the transition kernel (no relation to the kernels of a convolution). For the OU process it is Gaussian. Of all the results of this section, this is the one the rest of the book uses most.

Proposition (OU transition kernel). For the OU process (26.4.12),

Xt∣X0=x0  ∼  N ⁣(x0 e−θt,  g22θ(1−e−2θt)).(26.4.13)X_t \mid X_0 = x_0 \;\sim\; \mathcal{N}\!\left( x_0\, e^{-\theta t},\; \frac{g^2}{2\theta}\left(1 - e^{-2\theta t}\right) \right). \tag{26.4.13}

Proof. The integrating factor eθte^{\theta t} solved the decay equation with an input in Section 26.1.2.3 (by the variation of constants formula), and it works when the input is noise. Apply Itô's lemma (26.4.9) to ϕ(x,t)=eθtx\phi(x, t) = e^{\theta t} x, for which ϕt=θeθtx\phi_t = \theta e^{\theta t} x, ϕx=eθt\phi_x = e^{\theta t}, and ϕxx=0\phi_{xx} = 0, so there is no correction:

d ⁣(eθtXt)=(θeθtXt−θeθtXt)dt+eθtg dWt=g eθt dWt.d\!\left(e^{\theta t} X_t\right) = \left(\theta e^{\theta t} X_t - \theta e^{\theta t} X_t\right) dt + e^{\theta t} g\,dW_t = g\, e^{\theta t}\,dW_t.

The drift cancels, and integrating from 00 to tt and multiplying by e−θte^{-\theta t} gives

Xt=X0 e−θt+g∫0te−θ(t−s) dWs.(26.4.14)X_t = X_0\, e^{-\theta t} + g \int_0^t e^{-\theta (t - s)}\, dW_s . \tag{26.4.14}

The remaining integral has a deterministic integrand, so it is a limit of weighted sums of independent Gaussian increments and is Gaussian with mean zero (we grant that mean-square limits of Gaussians are Gaussian). The Itô isometry (26.4.6) gives its variance:

Var(Xt∣X0)=g2∫0te−2θ(t−s) ds=g22θ(1−e−2θt).■\textrm{Var}(X_t \mid X_0) = g^2 \int_0^t e^{-2\theta (t - s)}\, ds = \frac{g^2}{2\theta}\left(1 - e^{-2\theta t}\right). \qquad \blacksquare

The solution (26.4.14) reads as a fading memory. The start decays at rate θ\theta, and a noise kick from time ss survives with weight e−θ(t−s)e^{-\theta(t-s)}: recent noise counts, old noise is forgotten. In the recurrence a kick from kk steps ago survives with weight 0.99k≈e−0.01k0.99^k \approx e^{-0.01k}. Every later section, and Section 17.2 in the main text, writes the kernel with two coefficients, the mean factor and the standard deviation,

αt=e−θt,σt2=g22θ(1−e−2θt),so thatXt∣X0=x0∼N ⁣(αtx0, σt2).(26.4.15)\alpha_t = e^{-\theta t}, \qquad \sigma_t^2 = \frac{g^2}{2\theta}\left(1 - e^{-2\theta t}\right), \qquad\textrm{so that}\qquad X_t \mid X_0 = x_0 \sim \mathcal{N}\!\left(\alpha_t x_0,\, \sigma_t^2\right). \tag{26.4.15}

The data enter only through the mean αtx0\alpha_t x_0, and the noise only through the variance σt2\sigma_t^2. For the running example (θ=1\theta = 1, g=2g = \sqrt{2}, σt2=1−e−2t\sigma_t^2 = 1 - e^{-2t}) started at x0=2x_0 = 2, the kernel is N(1.213, 0.7952)\mathcal{N}(1.213,\, 0.795^2) at t=0.5t = 0.5, N(0.736, 0.9302)\mathcal{N}(0.736,\, 0.930^2) at t=1t = 1, and N(0.037, 1.0002)\mathcal{N}(0.037,\, 1.000^2) at t=4t = 4. Because the kernel is Gaussian at every time and for every start, everything about the noised state given its start is known in closed form, including the gradient of its log-density, −(x−αtx0)/σt2-(x-\alpha_t x_0)/\sigma_t^2. Generation will turn out to depend on this gradient.

The recurrence is only a discretization of the process, so its cloud should follow the kernel closely but need not follow it exactly. We run it as a plain loop on 10,00010{,}000 copies of x0=2x_0 = 2, save the cloud at five times, and print its mean and standard deviation next to 2αt2\alpha_t and σt\sigma_t, together with the fraction of the cloud inside 2αt±2σt2\alpha_t \pm 2\sigma_t.

theta_ou, g_ou, h_ou = 1.0, np.sqrt(2.0), 0.01
X_ou = np.full(10000, 2.0)                    # 10,000 copies of x_0 = 2
snap_steps = {round(t / h_ou): t for t in (0.5, 1.0, 1.5, 2.0, 4.0)}
X_ou_snap = {}
for k in range(1, 401):
    xi = rng.standard_normal(X_ou.size)
    X_ou = (1 - theta_ou * h_ou) * X_ou + g_ou * np.sqrt(h_ou) * xi
    if k in snap_steps:
        X_ou_snap[snap_steps[k]] = X_ou.copy()
print(f'{"t":>4} {"mean":>6} {"2alpha_t":>8} {"std":>6} {"sigma_t":>7}'
      f' {"inside":>7}')
for t, X in X_ou_snap.items():
    alpha_t = np.exp(-theta_ou * t)
    sigma_t = np.sqrt(g_ou**2 / (2 * theta_ou) * (1 - alpha_t**2))
    inside = np.mean(np.abs(X - 2 * alpha_t) < 2 * sigma_t)
    print(f'{t:4.1f} {X.mean():6.3f} {2 * alpha_t:8.3f} {X.std():6.3f} '
          f'{sigma_t:7.3f} {inside:7.3f}')
   t   mean 2alpha_t    std sigma_t  inside
 0.5  1.209    1.213  0.803   0.795   0.951
 1.0  0.733    0.736  0.943   0.930   0.955
 1.5  0.433    0.446  0.968   0.975   0.957
 2.0  0.259    0.271  0.992   0.991   0.953
 4.0  0.042    0.037  1.012   1.000   0.952

Means and standard deviations agree with the kernel within two or three standard errors (about 0.010.01 for a mean and 0.0070.007 for a standard deviation from 10,00010{,}000 samples). About 95%95\% of the cloud lies inside the band at every time, as for any Gaussian. The cloud that began as the single point 22 widens quickly and then stops widening. At this sample size the discretization cannot be seen.

26.4.3.2 The Stationary Distribution#

A forward process must forget its start, and the kernel (26.4.13) shows that the OU process does. As t→∞t \to \infty the mean x0e−θtx_0 e^{-\theta t} decays to zero and the variance approaches g2/(2θ)g^2/(2\theta), so

Xt∣X0=x0  ⟶  N ⁣(0, g22θ)in distribution, for every x0.X_t \mid X_0 = x_0 \;\longrightarrow\; \mathcal{N}\!\left(0, \, \frac{g^2}{2\theta}\right) \quad \textrm{in distribution, for every } x_0 .

This is the endpoint, the same for all data, that Section 26.4.1.1 asked for. The limit is more than an endpoint. A process started in it stays in it, and a law with this property is called stationary. To see it, let X0∼N(0,g2/2θ)X_0 \sim \mathcal{N}(0, g^2/2\theta) be independent of WW. By (26.4.14), XtX_t is then the sum of X0e−θtX_0 e^{-\theta t} and an independent Gaussian of variance σt2\sigma_t^2, hence Gaussian with mean zero and variance

e−2θt g22θ+g22θ(1−e−2θt)=g22θ.e^{-2\theta t}\,\frac{g^2}{2\theta} + \frac{g^2}{2\theta}\left(1 - e^{-2\theta t}\right) = \frac{g^2}{2\theta}.

The decay of the start removes as much variance as the noise adds. How long does the forgetting take? Its time scale is 1/θ1/\theta, the relaxation time, and after a few multiples of it e−θte^{-\theta t} is negligible. For the running example g2/2θ=1g^2/2\theta = 1 and 1/θ=11/\theta = 1, so the stationary law is N(0,1)\mathcal{N}(0, 1), and by t=4t = 4 the mean had fallen from 22 to 0.0370.037.

The recurrence has a stationary law of its own, and it is slightly different. One step maps a variance vv to (1−θh)2v+g2h(1 - \theta h)^2 v + g^2 h. From v=1v = 1 a step of the running example gives 0.9801+0.02=1.00010.9801 + 0.02 = 1.0001, slightly more than 11, and the fixed point of the map is

v∞=g2h1−(1−θh)2=0.020.0199=1.005(h=0.01).v_\infty = \frac{g^2 h}{1 - (1 - \theta h)^2} = \frac{0.02}{0.0199} = 1.005 \qquad (h = 0.01).

The recurrence therefore settles half a percent above the exact variance 11. No cloud of 10,00010{,}000 points can show half a percent, but the pencil can. This is the first sign of a discretization error, and Section 26.4.4.2 shows that it shrinks in proportion to hh.

26.4.3.3 The Variance-Preserving Normalization#

The stationary variance g2/2θg^2/2\theta is a free parameter, and diffusion models fix it to 11 by choosing

g2=2θ⟹Xt∣X0=x0  ⟶  N(0,1).g^2 = 2\theta \qquad \Longrightarrow \qquad X_t \mid X_0 = x_0 \;\longrightarrow\; \mathcal{N}(0, 1).

The running example makes this choice. With it σt2=1−αt2\sigma_t^2 = 1 - \alpha_t^2, so one coefficient describes the kernel.

So far the start was a single number x0x_0. Let it now be a draw X0X_0 from the data. In distribution, the solution (26.4.14) then reads Xt=αtX0+1−αt2 ϵX_t = \alpha_t X_0 + \sqrt{1 - \alpha_t^2}\,\epsilon with a standard Gaussian ϵ\epsilon independent of X0X_0. The law of this XtX_t, which is the kernel averaged over the data, is called the marginal at time tt. Most papers write it with another letter. They follow the denoising diffusion probabilistic model (DDPM) of Ho et al. (2020), the discrete-time diffusion model of Section 17.2, which writes αˉt\bar{\alpha}_t for our αt2=e−2θt\alpha_t^2 = e^{-2\theta t}. The main text does the same, and so do we:

Xt  =  αˉt  X0  +  1−αˉt  ϵ,ϵ∼N(0,1) independent of X0.(26.4.16)X_t \;=\; \sqrt{\bar{\alpha}_t}\; X_0 \;+\; \sqrt{1 - \bar{\alpha}_t}\; \epsilon, \qquad \epsilon \sim \mathcal{N}(0, 1) \textrm{ independent of } X_0 . \tag{26.4.16}

The marginal is a weighted mix of data and fresh noise, with weights whose squares add to one. Suppose the data have zero mean and unit variance. Taking variances of the two independent terms gives

Var(Xt)=αˉtVar(X0)+(1−αˉt)=αˉt+(1−αˉt)=1for all t.\textrm{Var}(X_t) = \bar{\alpha}_t \textrm{Var}(X_0) + (1 - \bar{\alpha}_t) = \bar{\alpha}_t + (1 - \bar{\alpha}_t) = 1 \qquad \textrm{for all } t.

This is what variance-preserving (VP) means. For unit-variance data the marginal variance equals 11 at every time, not only in the limit. The process moves variance from the data to the noise, a fraction αˉt\bar{\alpha}_t still carried by the data and 1−αˉt1 - \bar{\alpha}_t already replaced by noise, without changing the total. The name is accurate for unit-variance data only. For data of variance s2s^2 the marginal variance drifts from s2s^2 to 11 (Exercise 5), so the scale of the data matters. Images scaled to [−1,1][-1, 1], as in Section 17.1, have variance below one, while latent diffusion models rescale their latents to about unit variance (Rombach et al., 2022).

The variance is preserved; the shape of the distribution is not. Take data that are ±1\pm 1 with probability 12\tfrac12 each, the outcome of a coin flip. Their fourth moment is 11, against 33 for a standard Gaussian, and along the process it climbs as E[Xt4]=3−2αˉt2\mathbb{E}[X_t^4] = 3 - 2\bar{\alpha}_t^2, which is 2.2642.264 at t=0.25t = 0.25 and 2.9632.963 at t=1t = 1 for the running example (same exercise). The coin turns into a bell curve, and its variance never moves.

Diffusion models usually let the rate vary with time. The VP-SDE of score-based diffusion (Song et al., 2021),

dX=−12β(t) X dt+β(t)  dW,(26.4.17)dX = -\tfrac12 \beta(t)\, X \, dt + \sqrt{\beta(t)}\; dW, \tag{26.4.17}

is an OU process with time-dependent rate θ(t)=β(t)/2\theta(t) = \beta(t)/2 and noise amplitude g(t)2=β(t)g(t)^2 = \beta(t); the running example is the constant schedule β=2\beta = 2. It keeps g2=2θg^2 = 2\theta at every instant, so the variance stays at 11 along the whole schedule, and its marginal is (26.4.16) with αˉt=e−∫0tβ(s) ds\bar{\alpha}_t = e^{-\int_0^t \beta(s)\,ds} (Exercise 7 proves that its transitions are exact Gaussians). This is the variance-preserving path of Section 17.2, the forward process that a diffusion model learns to reverse.

26.4.4 Simulation#

26.4.4.1 The Euler--Maruyama Scheme#

Most SDEs have no closed-form solution, so we need to simulate them. Our recurrence already shows how. To simulate (26.4.8) on a grid tn=n Δtt_n = n\,\Delta t we freeze the coefficients over each step, as forward Euler does for ODEs (Section 26.2), and add a Gaussian kick whose size the increment form (26.4.2) dictates. The Euler--Maruyama update (Maruyama, 1955) is

X^n+1=X^n+f(X^n,tn) Δt+g(X^n,tn) Δt  ξn,ξn∼N(0,1) i.i.d.,(26.4.18)\hat{X}_{n+1} = \hat{X}_n + f(\hat{X}_n, t_n)\,\Delta t + g(\hat{X}_n, t_n)\,\sqrt{\Delta t}\;\xi_n, \qquad \xi_n \sim \mathcal{N}(0, 1) \textrm{ i.i.d.}, \tag{26.4.18}

where X^n\hat{X}_n approximates XtnX_{t_n} and X^0=X0\hat X_0 = X_0. The kick scales as Δt\sqrt{\Delta t}, since any other exponent makes the noise vanish or explode in the limit (Section 26.4.1.2), and with g=0g = 0 the scheme is forward Euler.

For the OU process the step reads

X^n+1=(1−θ Δt) X^n+gΔt  ξn=0.99 X^n+0.1414 ξn,(26.4.19)\hat{X}_{n+1} = (1 - \theta\,\Delta t)\,\hat{X}_n + g\sqrt{\Delta t}\;\xi_n = 0.99\,\hat{X}_n + 0.1414\,\xi_n , \tag{26.4.19}

where the second form is the running example at Δt=h=0.01\Delta t = h = 0.01, the recurrence we have been running all along.

For this process we could have done better. With g2=2θg^2 = 2\theta the kernel (26.4.13) over one interval is N(e−θΔtx,1−e−2θΔt)\mathcal{N}(e^{-\theta\Delta t}x, 1 - e^{-2\theta\Delta t}), and sampling from it is the forward step of DDPM (Section 17.2), xk=1−βk xk−1+βk ϵkx_k = \sqrt{1 - \beta_k}\, x_{k-1} + \sqrt{\beta_k}\,\epsilon_k with βk=1−e−2θΔt\beta_k = 1 - e^{-2\theta\Delta t}. DDPM's step is therefore exact at every step size, and Euler--Maruyama is its first-order approximation. At θ=1\theta = 1 and Δt=0.01\Delta t = 0.01 the exact step multiplies by e−0.01=0.99005e^{-0.01} = 0.99005 and adds noise of variance βk=1−e−0.02=0.0198\beta_k = 1 - e^{-0.02} = 0.0198, against 0.990.99 and 0.020.02. The two betas should not be confused. The βk\beta_k of the step is the fraction of the variance that one step replaces by noise. It is about β(t) Δt\beta(t)\,\Delta t for the rate β(t)\beta(t) of (26.4.17), here 0.01980.0198 against 2⋅0.012 \cdot 0.01. Exact steps need a linear equation (Exercise 7). A general drift has no transition law in closed form, and the approximate scheme is all we have.

The helper below implements (26.4.18) for a whole batch of paths. It takes the increments ΔW\Delta W as an argument, so that two simulations can be driven by the same noise. We need that at once: with the noise switched on, "error" itself needs a definition, and there are two.

def euler_maruyama(f, g, x0, t, dW):
    """Simulate dX = f dt + g dW on the grid t for a batch of paths."""
    X = np.empty((len(x0), len(t)))
    X[:, 0] = x0
    for k in range(len(t) - 1):
        h = t[k + 1] - t[k]
        X[:, k + 1] = (X[:, k] + f(X[:, k], t[k]) * h
                       + g(X[:, k], t[k]) * dW[:, k])
    return X

26.4.4.2 Strong and Weak Convergence#

A simulated path is random, and so is the true one. The two definitions of error differ in what they compare: paths or laws, the two kinds of statement that the band experiment told apart.

Let N=T/ΔtN = T/\Delta t be the number of steps, so that X^N\hat{X}_N approximates XTX_T. Drive the scheme and the exact solution with the same Brownian increments. The strong error E ∣X^N−XT∣\mathbb{E}\,|\hat{X}_N - X_T| measures pathwise tracking: does the simulated trajectory follow the true trajectory driven by that noise? The weak error ∣E φ(X^N)−E φ(XT)∣|\mathbb{E}\,\varphi(\hat{X}_N) - \mathbb{E}\,\varphi(X_T)|, for smooth test functions φ\varphi, measures only whether the law at time TT comes out right. A generative model needs weak accuracy, since it only uses the samples at the final time. Strong accuracy matters when paths are coupled across resolutions, as in multilevel Monte Carlo, which makes most of its estimate on cheap coarse paths and corrects it with a few expensive fine ones (Exercise 8). Figure 26.4.6 shows the two notions side by side.

Figure 26.4.6:

Strong versus weak convergence of Euler--Maruyama on the running example from X0=2X_0=2 on [0,1][0,1]. Left: one coarse path (1616 steps, orange) against a fine reference (40964096 steps, blue) driven by the same Brownian increments; the strip below magnifies their gap at the coarse grid times, which is what the strong error measures. Right: the terminal histograms of 20,00020{,}000 paths at the two step sizes nearly match each other and the exact law (black), although at 1616 steps the variance is about 4%4\% too large.

A scheme has strong order pp if its strong error is at most C ΔtpC\,\Delta t^{p}, and weak order pp likewise. The orders of Euler--Maruyama are classical.

Theorem (orders of Euler--Maruyama; stated without proof). Let ff, gg, and φ\varphi be smooth, with the growth conditions and the moments of X0X_0 that Kloeden and Platen (1992) make precise.

  1. Euler--Maruyama has strong order 12\tfrac12: E ∣X^N−XT∣≤C Δt1/2\mathbb{E}\,|\hat{X}_N - X_T| \le C\,\Delta t^{1/2}.
  2. If the noise is additive, g=g(t)g = g(t), the strong order is 11: E ∣X^N−XT∣≤C Δt\mathbb{E}\,|\hat{X}_N - X_T| \le C\,\Delta t.
  3. The weak order is 11, for additive and multiplicative noise alike: ∣E φ(X^N)−E φ(XT)∣≤C Δt|\mathbb{E}\,\varphi(\hat{X}_N) - \mathbb{E}\,\varphi(X_T)| \le C\,\Delta t.

A count of single-step errors explains the exponents. It is not a proof: it adds the errors of the steps as if each stayed where it was made, and the proofs must also control how the later dynamics carry an early error forward.

Euler--Maruyama freezes gg over the step, but gg moves with the path. At a time ss inside the step the path has moved by about g (Ws−Wtn)g\,(W_s - W_{t_n}), so the noise coefficient there is about g+g ∂xg (Ws−Wtn)g + g\,\partial_x g\,(W_s - W_{t_n}). The largest term that the scheme leaves out is the integral of the second part against dWdW, which is (26.4.7) for the Brownian motion Ws−WtnW_s - W_{t_n} that starts afresh at tnt_n:

g ∂xg∫tntn+1(Ws−Wtn)dWs=12 g ∂xg((ΔW)2−Δt).g\,\partial_x g \int_{t_n}^{t_{n+1}} \left(W_s - W_{t_n}\right) dW_s = \tfrac12\, g\,\partial_x g\left((\Delta W)^2 - \Delta t\right).

Milstein's scheme puts this term back (Milstein, 1975). Its random factor is the rule for squares seen a step at a time: (ΔW)2=Δt(\Delta W)^2 = \Delta t is true in sums, and off by something of mean zero and size Δt\Delta t in every step. The N=T/ΔtN = T/\Delta t steps contribute uncorrelated terms of this kind, which add like a random walk to N Δt=T Δt\sqrt{N}\,\Delta t = \sqrt{T\,\Delta t}. That is order 12\tfrac12.

For additive noise ∂xg=0\partial_x g = 0 removes this term. The next omitted stochastic terms have size Δt3/2\Delta t^{3/2} per step and add to N Δt3/2=O(Δt)\sqrt{N}\,\Delta t^{3/2} = O(\Delta t), as do the deterministic remainders, N Δt2=O(Δt)N\,\Delta t^2 = O(\Delta t), and the order is 11. For the weak error only means matter. Every omitted stochastic term has mean zero, the bias of one step is of order Δt2\Delta t^2, and NN steps give O(Δt)O(\Delta t), whatever the noise.

We measure both strong orders on the same noise. Geometric Brownian motion (μ=ν=1\mu = \nu = 1, X0=1.5X_0 = 1.5) is compared with its exact solution (26.4.10), and the running example from X0=2X_0 = 2 with a reference on a 40964096-step grid, since the noise integral in its exact solution (26.4.14) could itself be evaluated only on a grid. Coarse increments are sums of consecutive fine ones, so every resolution sees the same Brownian path.

T, n_ref, n_em = 1.0, 2**12, 2000
mu_g, nu_g = 1.0, 1.0                        # GBM: multiplicative noise
ou_f, ou_g = lambda x, s: -theta_ou * x, lambda x, s: g_ou
gbm_f, gbm_g = lambda x, s: mu_g * x, lambda x, s: nu_g * x
x0_ou, x0_gbm = np.full(n_em, 2.0), np.full(n_em, 1.5)
dW_f = np.sqrt(T / n_ref) * rng.standard_normal((n_em, n_ref))
t_f = np.linspace(0, T, n_ref + 1)
ou_ref = euler_maruyama(ou_f, ou_g, x0_ou, t_f, dW_f)[:, -1]
gbm_exact = 1.5 * np.exp((mu_g - nu_g**2 / 2) * T + nu_g * dW_f.sum(axis=1))
dts, errs_ou, errs_gbm = [], [], []
for n in [2**k for k in range(3, 9)]:        # 8 to 256 steps
    dW = dW_f.reshape(n_em, n, n_ref // n).sum(axis=2)
    t = np.linspace(0, T, n + 1)
    ou = euler_maruyama(ou_f, ou_g, x0_ou, t, dW)[:, -1]
    gbm = euler_maruyama(gbm_f, gbm_g, x0_gbm, t, dW)[:, -1]
    dts.append(T / n)
    errs_ou.append(np.abs(ou - ou_ref).mean())
    errs_gbm.append(np.abs(gbm - gbm_exact).mean())
print('strong-order slopes:  OU (additive) '
      f'{np.polyfit(np.log(dts), np.log(errs_ou), 1)[0]:.2f},'
      '  GBM (multiplicative) '
      f'{np.polyfit(np.log(dts), np.log(errs_gbm), 1)[0]:.2f}')
d2l.plot(dts, [errs_ou, errs_gbm, np.array(dts), 0.5 * np.sqrt(dts)],
         'step size', 'strong error', xscale='log', yscale='log',
         legend=['OU (additive)', 'GBM (multiplicative)',
                 'slope 1', 'slope 1/2'], figsize=(5, 3))
strong-order slopes:  OU (additive) 1.03,  GBM (multiplicative) 0.48

The measured slopes are close to 11 for the additive-noise OU process and close to 12\tfrac12 for the multiplicative-noise geometric Brownian motion, the two strong orders of the theorem. The two curves have different heights because the processes have different scales. What can be compared is how they fall: each halving of the step halves the OU error and divides the error of geometric Brownian motion by about 2\sqrt{2}.

For the weak order no sampling is needed. Euler--Maruyama applied to the OU process is a linear Gaussian recursion: if X^n\hat{X}_n is Gaussian, so is X^n+1\hat{X}_{n+1}. So the simulated marginal is Gaussian at every step, with mean and variance

mn+1=(1−θ Δt) mn,vn+1=(1−θ Δt)2 vn+g2 Δt.m_{n+1} = (1 - \theta\,\Delta t)\,m_n, \qquad v_{n+1} = (1 - \theta\,\Delta t)^2\, v_n + g^2\,\Delta t .

The mean is multiplied by 1−θΔt1 - \theta\Delta t per step where the exact solution multiplies by e−θΔte^{-\theta\Delta t} (0.9000.900 against 0.9050.905 at Δt=0.1\Delta t = 0.1). The variance recursion sums a geometric series, vn=g2Δt (1−a2n)/(1−a2)v_n = g^2\Delta t\,(1 - a^{2n})/(1 - a^2) with a=1−θΔta = 1 - \theta\Delta t and v0=0v_0 = 0, whose limit is the fixed point v∞v_\infty found earlier. Evaluating these closed forms at T=1T = 1 from X0=2X_0 = 2 for six step counts gives the errors of the simulated law with no sampling error to blur their slopes.

mean_T, var_T = 2 * np.exp(-1), 1 - np.exp(-2)  # exact OU kernel at T = 1
print(f'{"n":>4} {"EM mean":>8} {"EM var":>7}'
      f'   exact {mean_T:.4f}, {var_T:.4f}')
dts, errs = [], []
for n in [10, 20, 40, 80, 160, 320]:
    h = 1 / n
    a_h = 1 - theta_ou * h                   # one-step mean multiplier
    m = 2 * a_h**n                           # EM mean after n steps
    v = g_ou**2 * h * (1 - a_h**(2 * n)) / (1 - a_h**2)   # geometric series
    print(f'{n:4d} {m:8.4f} {v:7.4f}')
    dts.append(h)
    errs.append([abs(m - mean_T), abs(v - var_T)])
s_m, s_v = np.polyfit(np.log(dts), np.log(np.array(errs)), 1)[0]
print(f'weak-order slopes:  mean {s_m:.2f},  variance {s_v:.2f}')
   n  EM mean  EM var   exact 0.7358, 0.8647
  10   0.6974  0.9247
  20   0.7170  0.8938
  40   0.7265  0.8791
  80   0.7311  0.8718
 160   0.7335  0.8682
 320   0.7346  0.8664
weak-order slopes:  mean 1.01,  variance 1.01

At 1010 steps the simulated law has mean 0.6970.697 and variance 0.9250.925, against the exact 0.7360.736 and 0.8650.865 (the kernel N(0.736, 0.9302)\mathcal{N}(0.736,\, 0.930^2) from before), and at 320320 steps it has 0.7350.735 and 0.8660.866. Both fitted slopes are 1.011.01: halving the step halves the error of the simulated law, which is weak order 11.

The same computation for geometric Brownian motion also gives weak order 11, so multiplicative noise costs half an order in the paths and nothing in the law. A scheme that looks more accurate can even converge to the wrong equation. Heun's method, which averages the noise coefficient over the step, converges to the Stratonovich solution instead of the Itô one (Exercise 6).

Simulation produces samples of the forward process, and the kernel gives its law from a single start. Generation asks for more. Data are not a single point: the marginal ptp_t of the noised data is the kernel averaged over all starts, and for real data, which we know only through samples, it has no closed form. Yet running the process backward will require the gradient of log⁡pt\log p_t, the quantity that we could write down for one start, and so far we do not even have an equation that ptp_t obeys.

26.4.5 Summary#

A forward process must forget its start, which no deterministic flow can do, so it is driven by noise. Noise that survives the limit of small steps has size Δt\sqrt{\Delta t}, and the squares of its increments add up to the elapsed time, (dW)2=dt(dW)^2 = dt. An integral against dWdW therefore depends on where its integrand is evaluated, and Itô's left endpoint gives it mean zero and the isometry E[(∫H dW)2]=∫E[H2] ds\mathbb{E}[(\int H\,dW)^2] = \int \mathbb{E}[H^2]\,ds. For the same reason the chain rule gains the correction 12g2ϕxx dt\tfrac12 g^2 \phi_{xx}\,dt. With these tools the Ornstein--Uhlenbeck process dX=−θX dt+g dWdX = -\theta X\,dt + g\,dW is solved: given its start x0x_0 it is Gaussian at every time, with mean x0e−θtx_0 e^{-\theta t} and variance g22θ(1−e−2θt)\tfrac{g^2}{2\theta}(1 - e^{-2\theta t}), and with g2=2θg^2 = 2\theta it is the variance-preserving forward process of diffusion models. Equations without a closed form are simulated by Euler--Maruyama, Euler's step plus a Δt\sqrt{\Delta t} kick, which follows paths at order 11 for additive noise and 12\tfrac12 for multiplicative noise and reproduces laws at order 11 for both.

26.4.6 Exercises#

  1. Infinite total variation. The total variation of a path xx along a partition 0=t0<t1<⋯<tn=t0 = t_0 \lt t_1 \lt \cdots \lt t_n = t is Vn=∑i∣x(ti+1)−x(ti)∣V_n = \sum_i |x(t_{i+1}) - x(t_i)|. Pathwise integrals ∫H dx\int H\,dx, defined as limits of sums ∑iH(si) Δxi\sum_i H(s_i)\,\Delta x_i with arbitrary evaluation points si∈[ti,ti+1]s_i \in [t_i, t_{i+1}], exist for every continuous HH when VnV_n stays bounded as the mesh shrinks.

    1. Let x=Wx = W and use the uniform partition with Δti=t/n\Delta t_i = t/n. Compute E[Vn]\mathbb{E}[V_n] and Var(Vn)\textrm{Var}(V_n), using E∣ξ∣=2/π\mathbb{E}|\xi| = \sqrt{2/\pi} for ξ∼N(0,1)\xi \sim \mathcal{N}(0, 1), and conclude that Vn→∞V_n \to \infty in probability as n→∞n \to \infty.
    2. Let xx be continuous with Vn≤VV_n \le V for every partition. Show that max⁡i∣Δxi∣→0\max_i |\Delta x_i| \to 0 as the mesh shrinks, and deduce ∑i(Δxi)2≤max⁡i∣Δxi∣⋅V→0\sum_i (\Delta x_i)^2 \le \max_i |\Delta x_i| \cdot V \to 0: such a path has quadratic variation zero. Why does (26.4.3) then give a second proof that Brownian paths have unbounded total variation?
    3. Let xx be continuous with bounded total variation. Show that the left-endpoint sums ∑ix(ti) Δxi\sum_i x(t_i)\,\Delta x_i and the right-endpoint sums ∑ix(ti+1) Δxi\sum_i x(t_{i+1})\,\Delta x_i both converge to 12(x(t)2−x(0)2)\tfrac12\left(x(t)^2 - x(0)^2\right). Compare with the two limits for x=Wx = W in Section 26.4.2.2. Which step of your argument fails for a Brownian path?
  2. [code] Brownian scaling and exit times.

    1. For c>0c > 0 let W~t=Wct/c\tilde{W}_t = W_{ct}/\sqrt{c}. Check properties 1–4 of Section 26.4.1.2 and conclude that W~\tilde{W} is a Brownian motion. Which property of the ±Δt\pm\sqrt{\Delta t} walk does this make exact?
    2. Let τa\tau_a be the first time ∣Wt∣|W_t| reaches a level a>0a > 0. Use (a) to show that τa\tau_a has the same distribution as a2τ1a^2 \tau_1, so that E[τa]=a2 E[τ1]\mathbb{E}[\tau_a] = a^2\, \mathbb{E}[\tau_1].
    3. Granting E[τ1]=1\mathbb{E}[\tau_1] = 1 (it follows from E[Wτ2]=E[τ]\mathbb{E}[W_{\tau}^2] = \mathbb{E}[\tau], a consequence of (26.4.7) and the zero mean of the Itô integral up to a stopping time), simulate 4,0004{,}000 walks with Gaussian steps of variance Δt=10−3\Delta t = 10^{-3} and estimate E[τa]\mathbb{E}[\tau_a] for a=0.5,1,2a = 0.5, 1, 2, with Monte Carlo standard errors. The estimates tend to run slightly above a2a^2, most clearly for small aa. Explain the sign of the bias from the way a discrete walk crosses a level, and say how it scales with aa and Δt\Delta t.
    4. In the experiment of the text a little over half of the paths stay inside ±2t\pm 2\sqrt{t} at all 500500 grid times. Use (a) to show that this fraction depends only on the number nn of grid times and not on the length of the interval. Measure it for n=50n = 50, 500500, and 50005000. By what factor does it fall each time nn grows tenfold, and what does that say about the limit n→∞n \to \infty?
  3. Geometric Brownian motion and the −ν2/2-\nu^2/2 correction. Consider dX=μX dt+νX dWdX = \mu X\,dt + \nu X\,dW with X0>0X_0 > 0 and its solution, given by (26.4.10).

    1. Using E[eλZ]=eλ2v/2\mathbb{E}\big[e^{\lambda Z}\big] = e^{\lambda^2 v/2} for Z∼N(0,v)Z \sim \mathcal{N}(0, v), show that E[Xt]=X0eμt\mathbb{E}[X_t] = X_0 e^{\mu t} and E[Xt2]=X02e(2μ+ν2)t\mathbb{E}[X_t^2] = X_0^2 e^{(2\mu + \nu^2) t}. Find the median of XtX_t.
    2. Take μ=0.1\mu = 0.1 and ν=0.6\nu = 0.6. What happens to the mean, to the median, and to a typical path as t→∞t \to \infty? How can these statements hold at the same time?
    3. For which ν\nu, at a fixed μ>0\mu > 0, does the median stop growing? Check the condition for the parameters of (b) on 100100 paths simulated with euler_maruyama to t=20t = 20 at Δt=10−2\Delta t = 10^{-2}: what fraction of the paths ends below X0X_0, and what fraction does the exact solution (26.4.10) predict?

    Adapted from Simo Särkkä and Arno Solin, Applied Stochastic Differential Equations, Exercise 4.3.

  4. [code] Strong order and Milstein's correction. As Section 26.4.4.2 explains, Euler--Maruyama omits the Milstein term 12g ∂xg((ΔW)2−Δt)\tfrac12 g\,\partial_x g\left((\Delta W)^2 - \Delta t\right), and its strong order is 12\tfrac12 unless that term vanishes.

    1. By (26.4.10), Y=log⁡XY = \log X satisfies an SDE with additive noise. Apply euler_maruyama to that SDE with the increments dW_f and the step sizes of the strong-order experiment, exponentiate the endpoints, and measure the strong error against gbm_exact. Explain the result.
    2. Use Itô's lemma to show that for dX=f(X) dt+g(X) dWdX = f(X)\,dt + g(X)\,dW with g>0g > 0, the variable Y=ψ(X)Y = \psi(X) with ψ′=1/g\psi' = 1/g has unit additive noise, and find its drift.
    3. Milstein's scheme (Milstein, 1975) adds that term to the Euler--Maruyama step,
X^n+1=X^n+f Δt+g ΔWn+12 g ∂xg((ΔWn)2−Δt),(26.4.20)\hat X_{n+1} = \hat X_n + f\,\Delta t + g\,\Delta W_n + \tfrac12\, g\,\partial_x g\left((\Delta W_n)^2 - \Delta t\right), \tag{26.4.20}

with ff, gg, and ∂xg\partial_x g evaluated at (X^n,tn)(\hat X_n, t_n). Implement it for the geometric Brownian motion of the strong-order experiment, and measure its strong-order slope on the same increments and step sizes. By what factor does it reduce the strong error at Δt=2−8\Delta t = 2^{-8}?

  1. Variance preservation, exactly. Let XtX_t follow (26.4.16) with zero-mean data X0X_0.

    1. Assume unit-variance data and show that E[Xt4]=3+αˉt2(E[X04]−3)\mathbb{E}[X_t^4] = 3 + \bar{\alpha}_t^2\left(\mathbb{E}[X_0^4] - 3\right). Specialize to Rademacher data X0=±1X_0 = \pm 1 to obtain the prediction 3−2αˉt23 - 2\bar{\alpha}_t^2 of the text.
    2. The excess kurtosis E[X4]/Var(X)2−3\mathbb{E}[X^4]/\textrm{Var}(X)^2 - 3 is zero for every Gaussian. At what rate in tt does it decay for XtX_t? Compare this rate with the rate at which the conditional mean E[Xt∣X0]\mathbb{E}[X_t \mid X_0] forgets the data.
    3. Suppose instead that the data have variance s2s^2. Find Var(Xt)\textrm{Var}(X_t). For s=2s = 2 and θ=1\theta = 1, how long must the process run before Var(XT)\textrm{Var}(X_T) is within 1%1\% of the variance of the reference N(0,1)\mathcal{N}(0, 1)?
  2. [code] Itô versus Stratonovich. ● The left- and right-endpoint sums for ∫0tW dW\int_0^t W\,dW add up to Wt2W_t^2 on every partition (Section 26.4.2.2), so their average equals 12Wt2\tfrac12 W_t^2. Sums of this kind define the Stratonovich integral (Stratonovich, 1966), which obeys the ordinary chain rule.

    1. Heun's method of Section 26.2 carries over to SDEs if the noise term is treated like a second slope. The predictor is the Euler--Maruyama step X~=X^n+f(X^n) Δt+g(X^n) ΔWn\tilde{X} = \hat X_n + f(\hat X_n)\,\Delta t + g(\hat X_n)\,\Delta W_n, and the corrector averages both coefficients:
X^n+1=X^n+12[f(X^n)+f(X~)]Δt+12[g(X^n)+g(X~)]ΔWn.(26.4.21)\hat X_{n+1} = \hat X_n + \tfrac12\left[f(\hat X_n) + f(\tilde{X})\right]\Delta t + \tfrac12\left[g(\hat X_n) + g(\tilde{X})\right]\Delta W_n. \tag{26.4.21}

Implement it for the geometric Brownian motion of the strong-order experiment, on its increments dW_f and step sizes. Compare its endpoints with the exact Itô solution (26.4.10) and with X0exp⁡(μT+νWT)X_0 \exp(\mu T + \nu W_T). To which of the two does the scheme converge?

  1. Expand the corrector's noise term to first order in X~−X^n\tilde{X} - \hat X_n, and use (ΔWn)2≈Δt(\Delta W_n)^2 \approx \Delta t to show that the scheme behaves like Euler--Maruyama for an Itô SDE with a modified drift. Find that drift and check that it explains the limit in (a).
  2. Why do the two conventions give the same solution for every SDE of the form (26.4.1)?
  1. Exact transitions of linear SDEs. ● Consider the linear SDE dX=−θ(t)X dt+g(t) dWdX = -\theta(t) X\,dt + g(t)\,dW with continuous θ\theta and gg, and let Θ(t)=∫0tθ(u) du\Theta(t) = \int_0^t \theta(u)\,du.

    1. Apply Itô's lemma to ϕ(x,t)=eΘ(t)x\phi(x, t) = e^{\Theta(t)} x and show that, given XsX_s, the state XtX_t at a later time tt is Gaussian with mean e−(Θ(t)−Θ(s))Xse^{-(\Theta(t) - \Theta(s))} X_s and variance ∫ste−2(Θ(t)−Θ(u)) g(u)2 du\int_s^t e^{-2(\Theta(t) - \Theta(u))}\, g(u)^2\, du.
    2. Specialize (a) to the VP-SDE (26.4.17) and show that a transition of any length maps a unit-variance state to a unit-variance state. Conversely, assume for the linear SDE of (a) that Var(Xt)=1\textrm{Var}(X_t) = 1 for every t≥0t \ge 0 whenever Var(X0)=1\textrm{Var}(X_0) = 1, and show that g(t)2=2 θ(t)g(t)^2 = 2\,\theta(t) at every tt.
    3. Consider the semilinear SDE dX=(−θX+q(X))dt+g dWdX = \left(-\theta X + q(X)\right)dt + g\,dW with constants θ,g>0\theta, g > 0 and a smooth function qq. The exponential Euler--Maruyama step holds qq at the value q(X^n)q(\hat X_n) over a step of length Δt\Delta t and solves the remaining linear SDE exactly, by the method of (a). Write the step. Show that it agrees with the Euler--Maruyama step (26.4.18) to first order in Δt\Delta t and that it is exact when qq is constant. For q≡0q \equiv 0, Euler--Maruyama damps the mean only for Δt<2/θ\Delta t \lt 2/\theta (Section 26.2.3). What limits the step size of the exponential step?
    4. Exact steps have zero weak error at every step size. Which property of the drift and the diffusion of the VP-SDE makes them available? The generative sampler of a diffusion model simulates, backward in time, an SDE whose drift is −12β(t) x−β(t) ∇log⁡pt(x)-\tfrac12\beta(t)\,x - \beta(t)\,\nabla \log p_t(x): the VP drift plus a multiple of ∇log⁡pt\nabla \log p_t, the score of the forward marginal, which is a nonlinear function of xx for non-Gaussian data. Why can its steps not be made exact? Exponential integrators for diffusion samplers (Zhang and Chen, 2023) apply the idea of (c) to the deterministic counterpart of this sampler, an ODE whose drift has the same two parts with half the score term (Section 26.5). Which part of such a drift does the step treat exactly, and which part does it approximate?
  2. [extended] Multilevel Monte Carlo. ●● Estimating E[φ(XT)]\mathbb{E}[\varphi(X_T)] by averaging over Euler--Maruyama paths has two sources of error, the weak bias of the scheme and the sampling noise. Multilevel Monte Carlo (Giles, 2008) reduces the cost by coupling paths across step sizes, which is one of the uses of strong accuracy named in Section 26.4.4.2. Throughout, the mean-square strong error E[(X^N−XT)2]1/2\mathbb{E}\big[(\hat X_N - X_T)^2\big]^{1/2} has the same orders as the strong error of that section.

    1. Plain Monte Carlo averages φ(X^N)\varphi(\hat X_N) over MM independent paths at a cost of MT/ΔtMT/\Delta t steps. Assume a weak bias c1 Δtc_1\,\Delta t and a variance vv of φ(X^N)\varphi(\hat X_N). Show that a mean squared error below ε2\varepsilon^2 costs order ε−3\varepsilon^{-3} steps at the best choice of MM and Δt\Delta t.
    2. Let PlP_l be φ\varphi of the Euler--Maruyama endpoint with step hl=T 2−lh_l = T\,2^{-l}, and write E[PL]=E[P0]+∑l=1LE[Pl−Pl−1]\mathbb{E}[P_L] = \mathbb{E}[P_0] + \sum_{l=1}^{L}\mathbb{E}[P_l - P_{l-1}]. Each term is estimated from MlM_l independent pairs in which the coarse path uses the sums of consecutive pairs of fine increments, as in the strong-order experiment. For Lipschitz φ\varphi, bound Vl=Var(Pl−Pl−1)V_l = \textrm{Var}(P_l - P_{l-1}) by the mean-square strong error. Take φ(x)=x\varphi(x) = x, and predict how VlV_l scales with hlh_l for the OU process and the geometric Brownian motion of that experiment, with its parameters. Measure VlV_l for l=1,…,8l = 1, \ldots, 8 with euler_maruyama, and fit the slopes on the finer levels.
    3. Minimize the total cost ∑lMl/hl\sum_l M_l / h_l subject to ∑lVl/Ml≤ε2/2\sum_l V_l / M_l \le \varepsilon^2 / 2 with a Lagrange multiplier. Choose the finest level LL so that the bias stays below ε/2\varepsilon / \sqrt{2}. Assuming Vl∝hlrV_l \propto h_l^{r}, show that the cost is of order ε−2\varepsilon^{-2} for r=2r = 2 and of order ε−2(log⁡ε)2\varepsilon^{-2} (\log \varepsilon)^2 for r=1r = 1.
    4. Implement both estimators for E[XT]=X0eμT\mathbb{E}[X_T] = X_0 e^{\mu T} of the geometric Brownian motion of the strong-order experiment, with MlM_l from (c) and your measured VlV_l. For ε∈{0.2,0.1,0.05,0.02}\varepsilon \in \{0.2, 0.1, 0.05, 0.02\}, record the total number of steps each estimator uses and check its error over a few repetitions. Plot ε2\varepsilon^2 times the cost against ε\varepsilon. Repeat with the Milstein scheme of Exercise 4c in place of Euler--Maruyama. euler_maruyama stores every path in full, so simulate large MlM_l in batches.
    5. Repeat the measurement of (b) for the discontinuous φ(x)=1(x>X0)\varphi(x) = \mathbf{1}(x > X_0). Why does VlV_l decay more slowly, and what does the slower decay do to the cost?

Discussions