Quantitative Finance · Book 4 · Methods

Quantitative Methods

Quantitative Methods · Methods

4Stochastic Differential Equations

At 02:00 the overnight risk run stops. A square-root variance process, stepped forward one day at a time with the plain Euler scheme, produced a negative variance, the code took its square root, and a NaN propagated from one path into the book’s value and every limit that depends on it. It was not one unlucky path: with the desk’s calibrated parameters, three paths in four go negative within a year, and halving the time step four times over changes nothing. The model was not wrong, the scheme was, and the number that says so, the Feller ratio, is a one-line function of the parameters. This chapter sets out what a stochastic differential equation is and when it has a solution, the three diffusions the series uses most (geometric, mean-reverting and square-root), the generator that links them to partial differential equations, and the Feynman–Kac formula that turns an expectation into a PDE and back.

4.1 Existence, uniqueness and the Euler scheme

Definition 4.1 (Stochastic differential equation, strong and weak solutions)

A stochastic differential equation is

dXt=μ(t,Xt) dt+σ(t,Xt) dWt,X0=x0,dX_t = \mu(t, X_t)\,dt + \sigma(t, X_t)\,dW_t, \qquad X_0 = x_0,

for measurable μ,σ\mu, \sigma. A strong solution on a given probability space with a given Brownian motion WW is an adapted continuous process with Xt=x0+∫0tμ(s,Xs) ds+∫0tσ(s,Xs) dWsX_t = x_0 + \int_0^t\mu(s, X_s)\,ds + \int_0^t\sigma(s, X_s)\,dW_s. A weak solution is a pair (X,W)(X, W) on some filtered probability space satisfying the equation: the Brownian motion is part of the answer.

Theorem 4.2 (Existence and uniqueness)

If ∣μ(t,x)−μ(t,y)∣+∣σ(t,x)−σ(t,y)∣≤K∣x−y∣|\mu(t,x) - \mu(t,y)| + |\sigma(t,x) - \sigma(t,y)| \le K|x - y| and ∣μ(t,x)∣+∣σ(t,x)∣≤K(1+∣x∣)|\mu(t,x)| + |\sigma(t,x)| \le K(1 + |x|), the equation has a unique strong solution, with E[sup⁡t≤TXt2]<∞\E[\sup_{t\le T}X_t^2] < \infty.

Partial proof. Picard iteration Xt(n+1)=x0+∫0tμ(X(n)) ds+∫0tσ(X(n)) dWX^{(n+1)}_t = x_0 + \int_0^t\mu(X^{(n)})\,ds + \int_0^t\sigma(X^{(n)})\,dW: by the isometry and Doob’s inequality, E[sup⁡s≤t∣Xs(n+1)−Xs(n)∣2]≤C∫0tE[sup⁡u≤s∣Xu(n)−Xu(n−1)∣2] ds\E[\sup_{s\le t}|X^{(n+1)}_s - X^{(n)}_s|^2] \le C\int_0^t \E[\sup_{u\le s}|X^{(n)}_u - X^{(n-1)}_u|^2]\,ds, so the differences are bounded by Cntn/n!C^nt^n/n! and the iterates converge; Gronwall’s lemma gives uniqueness (Karatzas and Shreve, 1991, §5.2). ∎

The square-root coefficient ηv\eta\sqrt v is not Lipschitz at zero, which is why the square-root process needs its own theory below. Weak solutions matter because measure changes (chapter 5) produce them: the equation dX=sign⁡(X) dWdX = \operatorname{sign}(X)\,dW has a weak solution, a Brownian motion, but no strong one.

Definition 4.3 (Euler–Maruyama scheme)

The Euler–Maruyama scheme on the grid tk=kΔtt_k = k\Delta t is X^k+1=X^k+μ(tk,X^k)Δt+σ(tk,X^k)Δt Zk\hat X_{k+1} = \hat X_k + \mu(t_k, \hat X_k)\Delta t + \sigma(t_k, \hat X_k)\sqrt{\Delta t}\,Z_k, with ZkZ_k independent standard normals.

It is the discrete Itô integral of Chapter 3: coefficients frozen at the left point. Under the Lipschitz conditions it converges as Δt→0\Delta t \to 0, pathwise at order 12\tfrac12 and in law at order 1 (chapter 26 makes both orders precise). Its weakness is that it ignores the geometry of the state space: a Gaussian increment can carry a positive process below zero.

4.2 The workhorse diffusions

Definition 4.4 (Geometric Brownian motion)

A geometric Brownian motion solves dS=μS dt+σS dWdS = \mu S\,dt + \sigma S\,dW; by Example 3.8, St=S0exp⁡((μ−12σ2)t+σWt)S_t = S_0\exp((\mu - \tfrac12\sigma^2)t + \sigma W_t), lognormal.

Definition 4.5 (Mean reversion, Ornstein–Uhlenbeck process, half-life)

A process shows mean reversion when its drift pulls it toward a level. The Ornstein–Uhlenbeck process is the Gaussian case dX=κ(xˉ−X) dt+σ dWdX = \kappa(\bar x - X)\,dt + \sigma\,dW, κ>0\kappa > 0. The half-life of a mean-reverting process is the time ln⁡2/κ\ln 2/\kappa after which the expected deviation from the level has halved.

Proposition 4.6 (The Ornstein–Uhlenbeck transition)

Xt=xˉ+(X0−xˉ)e−κt+σ∫0te−κ(t−s) dWsX_t = \bar x + (X_0 - \bar x)e^{-\kappa t} + \sigma\int_0^te^{-\kappa(t-s)}\,dW_s; given X0X_0, XtX_t is normal with mean xˉ+(X0−xˉ)e−κt\bar x + (X_0 - \bar x)e^{-\kappa t} and variance σ2(1−e−2κt)/(2κ)\sigma^2(1 - e^{-2\kappa t})/(2\kappa), tending to N(xˉ,σ2/2κ)\mathcal N(\bar x, \sigma^2/2\kappa).

Proof. Itô’s formula on eκt(Xt−xˉ)e^{\kappa t}(X_t - \bar x) gives d(eκt(Xt−xˉ))=σeκt dWtd(e^{\kappa t}(X_t - \bar x)) = \sigma e^{\kappa t}\,dW_t; integrate, and use the isometry for the variance of the Wiener integral. ∎

The exact transition makes the Ornstein–Uhlenbeck process free to simulate at any step with no error (Figure 4.1), and its discrete sampling is an autoregression with coefficient e−κΔte^{-\kappa\Delta t} (chapter 17). Spreads, funding bases and, in One Quant Book 6, short rates are modelled this way.

Definition 4.7 (Square-root process, Feller condition)

The square-root process is dv=κ(vˉ−v) dt+ηv dWdv = \kappa(\bar v - v)\,dt + \eta\sqrt v\,dW with κ,vˉ,η>0\kappa, \bar v, \eta > 0 and v0>0v_0 > 0. The Feller condition is 2κvˉ≥η22\kappa\bar v \ge \eta^2; the ratio 2κvˉ/η22\kappa\bar v/\eta^2 is the Feller ratio.

Theorem 4.8 (Feller)

The square-root process has a unique strong nonnegative solution. It never reaches zero if the Feller condition holds, and reaches zero with positive probability (and is instantly reflected) if it fails. Given vsv_s, vt=c Yv_t = c\,Y with YY noncentral chi-square with d=4κvˉ/η2d = 4\kappa\bar v/\eta^2 degrees of freedom and noncentrality vse−κ(t−s)/cv_se^{-\kappa(t-s)}/c, where c=η2(1−e−κ(t−s))/(4κ)c = \eta^2(1 - e^{-\kappa(t-s)})/(4\kappa); its stationary law is Gamma(2κvˉ/η2, η2/2κ)\mathrm{Gamma}(2\kappa\bar v/\eta^2,\ \eta^2/2\kappa).

Proof. Admitted here. ∎

Uniqueness uses the Yamada–Watanabe argument for Hölder-12\tfrac12 coefficients; the boundary classification is Feller’s (1951), the transition law is in Cox, Ingersoll and Ross (1985). The square-root process is the variance of the Heston model and the short rate of the CIR model (One Quant Books 5 and 6); stochastic-volatility fits to equity smiles typically want a high vol-of-vol η\eta and so violate the condition. The desk’s process has κ=2\kappa = 2, vˉ=v0=0.04\bar v = v_0 = 0.04 and η=0.6\eta = 0.6: a Feller ratio of 0.44, a stationary law with mean 0.04 and standard deviation 0.06, and 27.9% of its stationary mass below one tenth of the mean.

Left: three Ornstein–Uhlenbeck paths from 0.5 with = 2 (half-life 0.35 years), x = 0, = 0.2, and their expected path 0.5e-2t (dashed). Right: three paths of the desk’s square-root process (= 2, v = 0.04, = 0.6), sampled exactly each day; with a Feller ratio of 0.44 they spend long spells near zero. Data: the chapter’s tutorial, seeded.
Figure 4.1. Left: three Ornstein–Uhlenbeck paths from 0.5 with κ=2\kappa = 2 (half-life 0.35 years), xˉ=0\bar x = 0, σ=0.2\sigma = 0.2, and their expected path 0.5e−2t0.5e^{-2t} (dashed). Right: three paths of the desk’s square-root process (κ=2\kappa = 2, vˉ=0.04\bar v = 0.04, η=0.6\eta = 0.6), sampled exactly each day; with a Feller ratio of 0.44 they spend long spells near zero. Data: the chapter’s tutorial, seeded.

The plain Euler step from a small vv is Gaussian with mean v+κ(vˉ−v)Δtv + \kappa(\bar v - v)\Delta t and standard deviation ηvΔt\eta\sqrt{v\Delta t}: from v=0.004v = 0.004 one daily step goes negative with probability 3.6%, and a path spends many days near zero. Figure 4.2 counts the paths that produce a negative variance within a year: 75% at the desk’s parameters, and, what surprises most, the same 75% with four or sixteen steps a day. Refining cannot help, because the exact process itself comes arbitrarily close to zero on those paths; only a scheme that respects the boundary can. Even at a Feller ratio of exactly one, a quarter of the daily Euler paths go negative: the condition protects the process, not the scheme.

Share of plain-Euler paths of the square-root process that produce a negative variance within one year, against the Feller ratio (vol-of-vol  from 0.2 to 1.0). Refining the step does not remove the failures; exact and full-truncation schemes have none. Data: 20 000 and 10 000 seeded paths per point.
Figure 4.2. Share of plain-Euler paths of the square-root process that produce a negative variance within one year, against the Feller ratio (vol-of-vol η\eta from 0.2 to 1.0). Refining the step does not remove the failures; exact and full-truncation schemes have none. Data: 20 000 and 10 000 seeded paths per point.

Two fixes are standard. The exact scheme samples the noncentral chi-square transition of Theorem 4.8 and is positive by construction. Full truncation (Lord, Koekkoek and van Dijk, 2010) keeps the Euler step but uses v+=max⁡(v,0)v^+ = \max(v, 0) in both coefficients, reporting v+v^+: it never takes the square root of a negative number, and its bias vanishes as Δt→0\Delta t \to 0. One Quant Book 5, chapter 23, adds the quadratic-exponential scheme used for Heston in production.

4.3 Generators and the Kolmogorov equations

Definition 4.9 (Infinitesimal generator)

The infinitesimal generator of a time-homogeneous diffusion dX=μ(X) dt+σ(X) dWdX = \mu(X)\,dt + \sigma(X)\,dW is the operator Lf(x)=lim⁡t↓0(Ex[f(Xt)]−f(x))/t=μ(x)f′(x)+12σ2(x)f′′(x)\mathcal Lf(x) = \lim_{t\downarrow 0} (\E_x[f(X_t)] - f(x))/t = \mu(x)f'(x) + \tfrac12\sigma^2(x)f^{\prime\prime}(x) on C2C^2 functions.

Proposition 4.10 (Dynkin’s formula)

For f∈C2f \in C^2 with compact support and a stopping time τ\tau with Ex[τ]<∞\E_x[\tau] < \infty, Ex[f(Xτ)]=f(x)+Ex∫0τLf(Xs) ds\E_x[f(X_\tau)] = f(x) + \E_x\int_0^\tau\mathcal Lf(X_s)\,ds.

Proof. By Itô’s formula, f(Xt)−f(x)−∫0tLf(Xs) ds=∫0tf′(Xs)σ(Xs) dWsf(X_t) - f(x) - \int_0^t\mathcal Lf(X_s)\,ds = \int_0^tf'(X_s)\sigma(X_s)\,dW_s, a martingale with bounded integrand; apply optional stopping at τ∧n\tau \wedge n and let n→∞n \to \infty. ∎

Definition 4.11 (Kolmogorov equations, stationary distribution)

For u(t,x)=E[g(XT)∣Xt=x]u(t, x) = \E[g(X_T) \mid X_t = x], the Kolmogorov backward equation is ∂tu+Lu=0\partial_tu + \mathcal Lu = 0 with u(T,⋅)=gu(T, \cdot) = g. The transition density p(t,y)p(t, y) of XtX_t solves the Kolmogorov forward equation, also called the Fokker–Planck equation, ∂tp=L∗p=−∂y(μp)+12∂yy(σ2p)\partial_tp = \mathcal L^*p = -\partial_y(\mu p) + \tfrac12\partial_{yy}(\sigma^2p). A stationary distribution of a Markov process is a law that, taken as the law of X0X_0, is the law of every XtX_t; for a diffusion its density solves L∗p=0\mathcal L^*p = 0.

The backward equation looks at a payoff from where one stands; the forward equation pushes a density forward in time. For the Ornstein–Uhlenbeck process, L∗p=0\mathcal L^*p = 0 integrates once to κ(xˉ−y)p=12σ2p′\kappa(\bar x - y)p = \tfrac12\sigma^2p', whose solution is the N(xˉ,σ2/2κ)\mathcal N(\bar x, \sigma^2/2\kappa) density; for the square-root process, κ(vˉ−y)p=12η2(yp)′\kappa(\bar v - y)p = \tfrac12\eta^2(yp)' gives p∝y2κvˉ/η2−1e−2κy/η2p \propto y^{2\kappa\bar v/\eta^2 - 1}e^{-2\kappa y/\eta^2}, the Gamma law of Theorem 4.8. When the Feller ratio is below one the exponent is negative and the density is infinite at zero (Figure 4.3).

Stationary laws of the square-root process with = 2, v = 0.04: histograms of 50 000 exact paths after five years (steps) against the Gamma law that solves L*p = 0, averaged over each bin (dots). With the desk’s = 0.6 the density piles up at zero; with = 0.2 zero is unattainable. Data: the chapter’s tutorial, seeded.
Figure 4.3. Stationary laws of the square-root process with κ=2\kappa = 2, vˉ=0.04\bar v = 0.04: histograms of 50 000 exact paths after five years (steps) against the Gamma law that solves L∗p=0\mathcal L^*p = 0, averaged over each bin (dots). With the desk’s η=0.6\eta = 0.6 the density piles up at zero; with η=0.2\eta = 0.2 zero is unattainable. Data: the chapter’s tutorial, seeded.

4.4 Feynman–Kac

Theorem 4.12 (Feynman–Kac)

Let XX solve the equation of Definition 4.1, r≥0r \ge 0 and gg continuous and of polynomial growth, and let u∈C1,2u \in C^{1,2} of polynomial growth solve

∂tu+μ ∂xu+12σ2∂xxu−r(t,x)u=0,u(T,x)=g(x).\partial_tu + \mu\,\partial_xu + \tfrac12\sigma^2\partial_{xx}u - r(t,x)u = 0, \qquad u(T, x) = g(x).

Then u(t,x)=E[e−∫tTr(s,Xs) dsg(XT)∣Xt=x]u(t, x) = \E\bigl[e^{-\int_t^Tr(s, X_s)\,ds}g(X_T) \bigm| X_t = x\bigr].

Proof. With Ds=e−∫tsr(u,Xu) duD_s = e^{-\int_t^sr(u, X_u)\,du}, Itô’s product rule gives d(Dsu(s,Xs))=Ds(∂su+Lu−ru) ds+Dsσ∂xu dWs=Dsσ∂xu dWsd(D_su(s, X_s)) = D_s(\partial_su + \mathcal Lu - ru)\,ds + D_s\sigma\partial_xu\,dW_s = D_s\sigma\partial_xu\,dW_s. The growth conditions make the stochastic integral a martingale, so u(t,x)=E[DTu(T,XT)∣Xt=x]u(t, x) = \E[D_Tu(T, X_T) \mid X_t = x]. ∎

The theorem runs both ways: an expectation can be computed by solving a PDE (chapter 27), and a PDE by simulating paths (chapter 26). Figure 4.4 collects the links. With X=rX = r itself an Ornstein–Uhlenbeck short rate and g=1g = 1, uu is the price of a zero-coupon bond, and the PDE has the affine solution u=eA(τ)−B(τ)ru = e^{A(\tau) - B(\tau)r} with B(τ)=(1−e−κτ)/κB(\tau) = (1 - e^{-\kappa\tau})/\kappa and A(τ)=(rˉ−σ2/2κ2)(B−τ)−σ2B2/4κA(\tau) = (\bar r - \sigma^2/2\kappa^2)(B - \tau) - \sigma^2B^2/4\kappa, found by substituting and matching the terms in rr. With r0=3%r_0 = 3\%, rˉ=4%\bar r = 4\%, κ=0.5\kappa = 0.5 and σ=1%\sigma = 1\%, the five-year price is 0.8343, and 20 000 simulated paths give 0.8341±0.00020.8341 \pm 0.0002. One Quant Book 6, chapter 7, turns this calculation into a model of the curve.

How a diffusion’s equations fit together. The generator comes from Itô’s formula; expectations of payoffs solve the backward equation (Feynman–Kac, with discounting), densities solve the forward equation, and stationary laws solve L*p = 0.
Figure 4.4. How a diffusion’s equations fit together. The generator comes from Itô’s formula; expectations of payoffs solve the backward equation (Feynman–Kac, with discounting), densities solve the forward equation, and stationary laws solve L∗p=0\mathcal L^*p = 0.

4.5 Tutorial: the overnight NaN

Goal. Reproduce the overnight failure, measure it against the Feller ratio, fix it two ways, and check the stationary law and a Feynman–Kac price. End state: Figures 4.1, 4.2 and 4.3 and the numbers of the weekend problem.

  1. The schemes. The running project samples the square-root process exactly and by Euler, plain or with full truncation.

    def sqrt_exact(v0: float, kappa: float, vbar: float, eta: float, T: float, n_steps: int, n_paths: int,
                   seed: int) -> np.ndarray:
        """dv = kappa (vbar - v) dt + eta sqrt(v) dW sampled exactly: v_{k+1} = c chi'^2_d(lambda) with
        c = eta^2 (1 - e^{-kappa dt}) / (4 kappa), d = 4 kappa vbar / eta^2, lambda = v_k e^{-kappa dt} / c."""
        rng = np.random.default_rng(seed)
        dt = T / n_steps
        c = eta**2 * (1 - math.exp(-kappa * dt)) / (4 * kappa)
        d = 4 * kappa * vbar / eta**2
        v = np.empty((n_paths, n_steps + 1))
        v[:, 0] = v0
        for k in range(n_steps):
            lam = v[:, k] * math.exp(-kappa * dt) / c
            v[:, k + 1] = c * rng.noncentral_chisquare(d, np.maximum(lam, 1e-300))
        return v
    
    
    def sqrt_euler(v0: float, kappa: float, vbar: float, eta: float, T: float, n_steps: int, n_paths: int,
                   seed: int, scheme: str = "plain") -> np.ndarray:
        """Euler steps of the square-root process. 'plain' takes sqrt(v) and yields NaN after a negative
        value; 'full_truncation' (Lord, Koekkoek and van Dijk) uses v^+ in drift and diffusion and
        reports v^+."""
        rng = np.random.default_rng(seed)
        dt = T / n_steps
        v = np.empty((n_paths, n_steps + 1))
        v[:, 0] = v0
        for k in range(n_steps):
            z = rng.standard_normal(n_paths)
            vk = v[:, k] if scheme == "plain" else np.maximum(v[:, k], 0.0)
            with np.errstate(invalid="ignore"):
                v[:, k + 1] = v[:, k] + kappa * (vbar - vk) * dt + eta * np.sqrt(vk) * math.sqrt(dt) * z
        return v if scheme == "plain" else np.maximum(v, 0.0)
    Listing 4.1. Exact and Euler steps of the square-root process. code/firm/mcengine/firm_mcengine.py
  2. The count: the share of plain-Euler paths that produce a negative variance, and a Feynman–Kac check by simulation.

    def bond_price_mc(r0=0.03, kappa=0.5, rbar=0.04, sigma=0.01, T=5.0, n_steps=500, n_paths=20_000, seed=6):
        """E[exp(-int_0^T r dt)] under an Ornstein-Uhlenbeck short rate, by simulation (trapezoid rule)."""
        r = ou_exact(r0, kappa, rbar, sigma, T, n_steps, n_paths, seed)
        integral = (r[:, :-1] + r[:, 1:]).sum(axis=1) * 0.5 * T / n_steps
        d = np.exp(-integral)
        return float(d.mean()), float(d.std() / math.sqrt(n_paths))
    
    
    def bond_price_pde(r0=0.03, kappa=0.5, rbar=0.04, sigma=0.01, T=5.0) -> float:
        """Feynman-Kac: u = exp(A(tau) - B(tau) r) solves u_t + kappa (rbar - r) u_r + sigma^2/2 u_rr - r u = 0,
        with B = (1 - e^{-kappa tau}) / kappa and A = (rbar - sigma^2 / (2 kappa^2)) (B - tau) - sigma^2 B^2 / (4 kappa)."""
        B = (1 - math.exp(-kappa * T)) / kappa
        A = (rbar - sigma**2 / (2 * kappa**2)) * (B - T) - sigma**2 * B**2 / (4 * kappa)
        return math.exp(A - B * r0)
    Listing 4.2. A zero-coupon price under an Ornstein–Uhlenbeck short rate, by simulation and by the PDE. code/methods/04-stochastic-differential-equations/python/qm_sde.py
  3. Run problem(), feller_table(), stationary_histograms() and fig_sde.py; the C++20 and Rust twins of the Euler and Ornstein–Uhlenbeck steps are in code/firm/mcengine/.

What to change next. Add the reflection scheme (v←∣v∣v \leftarrow |v|) and compare its one-year mean with the exact scheme’s; give the variance process a time-dependent level vˉ(t)\bar v(t) and check the generator’s prediction for E[vt]\E[v_t].

4.6 Build: the Monte Carlo engine, stage two

Purpose. Step the diffusions every later chapter simulates, with the scheme each one needs, and never return a NaN.

Interface. euler_maruyama(mu, sigma, x0, T, n_steps, n_paths, seed); ou_exact(x0, kappa, xbar, sigma, T, n_steps, n_paths, seed); sqrt_exact(v0, kappa, vbar, eta, …); sqrt_euler(…, scheme) with plain or full_truncation; feller_ratio(kappa, vbar, eta). C++20 and Rust: ou_exact_path and sqrt_euler_path over the stage-one NormalStream.

Rules. Parameters in the series notation (κ\kappa, xˉ\bar x, vˉ\bar v, η\eta); exact transitions where they exist; a scheme that can leave the state space is named as such and never the default.

Acceptance tests. Stationary mean and variance of both processes; Euler converges to the exact Ornstein–Uhlenbeck mean; the exact square-root scheme is nonnegative with the right moments; plain Euler fails at the desk’s parameters and full truncation never does, in all three languages.

Stretch. The quadratic-exponential scheme; a Milstein step (chapter 26).

Sources and further reading

  • W. Feller, “Two singular diffusion problems”, Annals of Mathematics 54, 1951.
  • J. C. Cox, J. E. Ingersoll and S. A. Ross, “A theory of the term structure of interest rates”, Econometrica 53, 1985.
  • G. E. Uhlenbeck and L. S. Ornstein, “On the theory of the Brownian motion”, Physical Review 36, 1930.
  • M. Kac, “On distributions of certain Wiener functionals”, Transactions of the AMS 65, 1949.
  • R. Lord, R. Koekkoek and D. van Dijk, “A comparison of biased simulation schemes for stochastic volatility models”, Quantitative Finance 10, 2010.

4.7 Exercises

Exercise 4.1 ★

A spread follows an Ornstein–Uhlenbeck process with κ=2\kappa = 2 per year. What is its half-life in years and in trading days?

Solution

Solution of Exercise 4.1.

ln⁡2/2=0.35\ln 2/2 = 0.35 years, 87 trading days.

Exercise 4.2 ★

A stock follows a geometric Brownian motion with μ=5%\mu = 5\% and σ=20%\sigma = 20\%. What is the probability that it is below its starting price after one year?

Solution

Solution of Exercise 4.2.

P(ln⁡S1<ln⁡S0)=Φ(−(0.05−0.02)/0.20)=Φ(−0.15)=44.0%\P(\ln S_1 < \ln S_0) = \Phi(-(0.05 - 0.02)/0.20) = \Phi(-0.15) = 44.0\%: the median grows at μ−σ2/2=3%\mu - \sigma^2/2 = 3\%.

Exercise 4.3 ★

Write the generator of the Ornstein–Uhlenbeck process, apply it to f(x)=xf(x) = x, and deduce the ODE satisfied by E[Xt]\E[X_t].

Solution

Solution of Exercise 4.3.

Lf=κ(xˉ−x)f′+12σ2f′′\mathcal Lf = \kappa(\bar x - x)f' + \tfrac12\sigma^2f^{\prime\prime}; for f(x)=xf(x) = x, Lf=κ(xˉ−x)\mathcal Lf = \kappa(\bar x - x), so ddtE[Xt]=κ(xˉ−E[Xt])\frac{d}{dt}\E[X_t] = \kappa(\bar x - \E[X_t]) and E[Xt]=xˉ+(X0−xˉ)e−κt\E[X_t] = \bar x + (X_0 - \bar x)e^{-\kappa t}.

Exercise 4.4 ★★

From the forward equation, find the stationary variance of the Ornstein–Uhlenbeck process with κ=2\kappa = 2, σ=0.4\sigma = 0.4.

Solution

Solution of Exercise 4.4.

κ(xˉ−y)p=12σ2p′\kappa(\bar x - y)p = \tfrac12\sigma^2p' gives a Gaussian density with variance σ2/2κ=0.16/4=0.04\sigma^2/2\kappa = 0.16/4 = 0.04.

Exercise 4.5 ★★

With Dynkin’s formula, compute E[v0.5]\E[v_{0.5}] for the square-root process with v0=0.09v_0 = 0.09, vˉ=0.04\bar v = 0.04, κ=2\kappa = 2.

Solution

Solution of Exercise 4.5.

Dynkin with f(v)=vf(v) = v gives ddtE[vt]=κ(vˉ−E[vt])\frac{d}{dt}\E[v_t] = \kappa(\bar v - \E[v_t]), so E[v0.5]=0.04+0.05e−1=0.0584\E[v_{0.5}] = 0.04 + 0.05e^{-1} = 0.0584.

Exercise 4.6 ★★

Check the five-year zero-coupon price of the chapter (0.8343) from the formula for AA and BB.

Solution

Solution of Exercise 4.6.

B=(1−e−2.5)/0.5=1.8358B = (1 - e^{-2.5})/0.5 = 1.8358; A=(0.04−0.0002)(1.8358−5)−0.0001×1.83582/2=−0.1261A = (0.04 - 0.0002)(1.8358 - 5) - 0.0001 \times 1.8358^2/2 = -0.1261; eA−0.03B=e−0.1812=0.8343e^{A - 0.03B} = e^{-0.1812} = 0.8343.

Exercise 4.7 ★★★

Coding. With negative_fraction, find the share of daily plain-Euler paths that go negative within a year when the Feller ratio is exactly one (η=0.4\eta = 0.4), and the largest η\eta in the table for which it is below 1%.

Solution

Solution of Exercise 4.7.

24.5% at a Feller ratio of one: the condition keeps the exact process away from zero, not the Gaussian Euler step. In the table, η=0.25\eta = 0.25 (ratio 2.56) gives 0.08%, and η=0.3\eta = 0.3 (ratio 1.78) already 1.6%.

Exercise 4.8 ★★★

Find the flaw. “We floor the variance at zero after every Euler step, so the scheme is now exact.”

Solution

Solution of Exercise 4.8.

Flooring removes the NaN but not the error: every step that would have crossed zero is replaced by zero, which then has zero diffusion, so the scheme adds mass at zero and biases the law near the boundary. It is still a first-order Euler scheme, not the exact transition; check its moments against E[vt]=vˉ+(v0−vˉ)e−κt\E[v_t] = \bar v + (v_0 - \bar v)e^{-\kappa t} and the Gamma stationary law, or use the exact or full-truncation scheme.

4.8 Problem: The NaN in the Overnight Run

Problem 4.1

Weekend problem — a variance process that violates the Feller condition

The desk’s variance process is dv=κ(vˉ−v) dt+ηv dWdv = \kappa(\bar v - v)\,dt + \eta\sqrt v\,dW with v0=vˉ=0.04v_0 = \bar v = 0.04, κ=2\kappa = 2 and η=0.6\eta = 0.6, simulated for one year of 252 days.

Part I — The process.

  1. What is the Feller ratio? Is zero attainable?
  2. What are the stationary law, its mean and standard deviation?
  3. What share of the stationary mass lies below 0.004?
  4. What vol-of-vol would make the Feller ratio exactly one?
  5. What is the half-life of the expected variance’s return to vˉ\bar v?

Part II — Plain Euler.

  1. From v=0.004v = 0.004, what is the probability that one daily Euler step goes negative?
  2. What share of paths produce a negative variance within the year?
  3. With four and sixteen steps a day, and with weekly steps?
  4. Why does refining the step not help?
  5. What does one NaN do to a risk run that sums over paths?

Part III — Fixes.

  1. What minimum and one-year mean does the exact scheme give?
  2. What one-year mean does full truncation give, and on what share of steps is it at zero?
  3. Which of the two is biased, and how does the bias behave as Δt→0\Delta t \to 0?
  4. What would reflection (v←∣v∣v \leftarrow |v|) do to the mean near zero?
  5. Why do production Heston engines use another scheme?

Part IV — Judgement.

  1. Should the desk impose the Feller condition in its calibration?
  2. What should an automated test of the simulation engine assert?
  3. What belongs in the model-review note about this incident?
  4. State the named result: the Feller ratio and the share of failing paths.
  5. In one sentence: what does the Feller condition protect, and what does it not?
Solution

Solution of Problem 4.1.

1. 2×2×0.04/0.36=0.442 \times 2 \times 0.04/0.36 = 0.44: below one, zero is attainable. 2. Gamma(0.44,0.09)\mathrm{Gamma}(0.44, 0.09): mean 0.04, standard deviation 0.04×0.36/4=0.06\sqrt{0.04 \times 0.36/4} = 0.06. 3. 27.9%. 4. η=2×2×0.04=0.4\eta = \sqrt{2 \times 2 \times 0.04} = 0.4. 5. ln⁡2/2=0.35\ln 2/2 = 0.35 years. 6. Φ(−(0.004+2×0.036/252)/(0.60.004/252))=3.6%\Phi(-(0.004 + 2 \times 0.036/252)/(0.6\sqrt{0.004/252})) = 3.6\%. 7. 75.2% of 40 000 paths. 8. 75.3% with four steps a day, 74.6% with sixteen, 75.2% with weekly steps. 9. The exact process comes arbitrarily close to zero on those paths; from a small vv a Gaussian step of any length has a fixed chance of crossing, and more steps near zero means more chances. 10. It propagates through every sum and average into the book’s value, and a comparison with a NaN is false, so limit checks can pass silently. 11. Minimum 0 (never negative); one-year mean 0.0394 against vˉ=0.04\bar v = 0.04. 12. Mean 0.0393; at zero on 5.2% of the steps. 13. The exact scheme has no discretisation bias; full truncation does, and its bias vanishes as Δt→0\Delta t \to 0. 14. Reflection turns every overshoot into a positive value of the same size, pushing mass up and biasing the mean upward near zero. 15. The exact sampler is slow and the Euler variants need small steps; the quadratic-exponential scheme (One Quant Book 5, chapter 23) is accurate at daily or larger steps. 16. No: the market’s smile asks for a high vol-of-vol, and imposing the condition would distort the fit to save a numerical scheme; fix the scheme. 17. No NaN and no negative variance on any seed; E[vt]\E[v_t] and Var⁡(vt)\Var(v_t) against their closed forms; the stationary law against the Gamma density; convergence as the step is refined. 18. The root cause (plain Euler with a Feller ratio of 0.44), the size of the failure (75% of paths), the fix (exact or full truncation) and its validation, and the tests added. 19. Named result: the NaN in the overnight run: with a Feller ratio of 0.44, 75% of daily plain-Euler paths produce a negative variance within a year, whatever the step; the exact and full-truncation schemes produce none. 20. It keeps the exact process away from zero; it does not keep a Gaussian step from crossing it.

4.9 Interview questions

Interview question 4.1 ★ trader, researcher

Solve dS=μS dt+σS dWdS = \mu S\,dt + \sigma S\,dW. What is the median of STS_T?

Solution

Solution of Interview question 4.1.

ST=S0exp⁡((μ−12σ2)T+σWT)S_T = S_0\exp((\mu - \tfrac12\sigma^2)T + \sigma W_T); the median is S0e(μ−σ2/2)TS_0e^{(\mu - \sigma^2/2)T}, below the mean S0eμTS_0e^{\mu T}.

What the interviewer is looking for: Itô on ln⁡S\ln S and the lognormal law.

Interview question 4.2 ★ researcher

A spread mean-reverts with a half-life of 10 days. What is κ\kappa, and how much of today’s deviation is expected to remain after 30 days?

Solution

Solution of Interview question 4.2.

κ=ln⁡2/10=0.069\kappa = \ln 2/10 = 0.069 a day; after 30 days, three half-lives, 2−3=12.5%2^{-3} = 12.5\% remains.

What the interviewer is looking for: e−κte^{-\kappa t} and half-lives.

Interview question 4.3 ★★ researcher, bank

What is the Feller condition, and what happens to a square-root process and to its simulation when it fails?

Solution

Solution of Interview question 4.3.

2κvˉ≥η22\kappa\bar v \ge \eta^2: then the drift near zero beats the diffusion and zero is never reached; otherwise the process touches zero and reflects. The law stays nonnegative either way, but an Euler step from near zero can go negative, and more so when the condition fails.

What the interviewer is looking for: the boundary classification and its numerical consequence.

Interview question 4.4 ★★ researcher, bank

State the Feynman–Kac formula and use it to write the PDE for E[e−rTg(ST)]\E[e^{-rT}g(S_T)] under a geometric Brownian motion.

Solution

Solution of Interview question 4.4.

u(t,x)=E[e−∫tTrg(XT)∣Xt=x]u(t,x) = \E[e^{-\int_t^Tr}g(X_T) \mid X_t = x] solves ∂tu+Lu−ru=0\partial_tu + \mathcal Lu - ru = 0, u(T)=gu(T) = g. For dS=μS dt+σS dWdS = \mu S\,dt + \sigma S\,dW and constant rr: ∂tu+μS∂Su+12σ2S2∂SSu−ru=0\partial_tu + \mu S\partial_Su + \tfrac12\sigma^2S^2\partial_{SS}u - ru = 0.

What the interviewer is looking for: generator plus discounting, terminal condition.

Interview question 4.5 ★★ developer

The overnight Monte Carlo sometimes returns NaN. How do you find the cause and make it impossible?

Solution

Solution of Interview question 4.5.

Make the failure reproducible (log the seed and the path index), find the first non-finite value and the step that produced it, and read the scheme at that state. Then make it impossible: exact or boundary-respecting schemes, assertions on the state space inside the kernel, a test that runs many seeds and fails on any NaN, and aggregation that refuses non-finite inputs instead of propagating them.

What the interviewer is looking for: reproducibility, root cause, and invariants enforced in code.

Interview question 4.6 ★★★ researcher

What is the difference between a strong and a weak solution? Give an equation with one but not the other.

Solution

Solution of Interview question 4.6.

A strong solution is built on a given Brownian motion; a weak one only asks for some probability space carrying a process and a Brownian motion that satisfy the equation. Tanaka’s equation dX=sign⁡(X) dWdX = \operatorname{sign}(X)\,dW, X0=0X_0 = 0, has weak solutions (any Brownian motion XX with W=∫sign⁡(X) dXW = \int\operatorname{sign}(X)\,dX) but no strong one, because XX cannot be recovered from WW.

What the interviewer is looking for: the definitions and Tanaka’s example.

Terms defined in this chapter

See all 2333 terms in the glossary