Skip to content
LeetQuant
E04 Numerical Methods

E04 · Numerical Methods · Lesson 4 of 4

Finite Differences

Truncation versus rounding error in numerical derivatives and the optimal step sizes, the explicit scheme for the heat equation and its stability limit, Crank–Nicolson, and a European call priced by finite differences against Black–Scholes.

20 min

Objective

Finite differences appear twice in quant work: computing Greeks by bumping, and solving pricing PDEs on a grid. In both, two errors compete, truncation from the Taylor series and rounding from floating point (for derivatives), or truncation and instability (for PDEs). By the end you should be able to choose a bump size, state and derive a stability condition, and explain why Crank–Nicolson is the default pricing scheme and when it misbehaves.

Difference quotients and their truncation error

Taylor expansion gives the three workhorse formulas:

f(x+h)−f(x)h=f′(x)+h2f′′(x)+O(h2),\frac{f(x + h) - f(x)}{h} = f'(x) + \frac h2 f''(x) + O(h^2), f(x+h)−f(x−h)2h=f′(x)+h26f′′′(x)+O(h4),\frac{f(x + h) - f(x - h)}{2h} = f'(x) + \frac{h^2}{6} f'''(x) + O(h^4), f(x+h)−2f(x)+f(x−h)h2=f′′(x)+h212f(4)(x)+O(h4).\frac{f(x + h) - 2f(x) + f(x - h)}{h^2} = f''(x) + \frac{h^2}{12} f^{(4)}(x) + O(h^4).

The central difference is second order because the even-order terms cancel by symmetry.

The optimal step size

Truncation error falls as h→0h \to 0, but the numerator is a difference of nearly equal numbers. If each computed value of ff has absolute error up to about ε∣f∣\varepsilon |f| (correct rounding plus a little error inside ff), the numerator of the forward difference has error up to 2ε∣f∣2\varepsilon|f|, which the division by hh amplifies. The total error model is

Efwd(h)≈h2∣f′′∣+2ε∣f∣h,Ecen(h)≈h26∣f′′′∣+ε∣f∣h.E_{\text{fwd}}(h) \approx \frac h2 |f''| + \frac{2\varepsilon |f|}{h}, \qquad E_{\text{cen}}(h) \approx \frac{h^2}{6}|f'''| + \frac{\varepsilon |f|}{h} .

Setting the derivative in hh to zero:

  • Forward: h∗=2ε∣f∣/∣f′′∣∼ε1/2≈10−8h^* = 2\sqrt{\varepsilon |f|/|f''|} \sim \varepsilon^{1/2} \approx 10^{-8}, with E∗=2ε∣ff′′∣∼10−8E^* = 2\sqrt{\varepsilon |f f''|} \sim 10^{-8}. Half the digits are lost.
  • Central: h∗3=3ε∣f∣/∣f′′′∣h^{*3} = 3\varepsilon |f|/|f'''|, so h∗∼ε1/3≈6×10−6h^* \sim \varepsilon^{1/3} \approx 6 \times 10^{-6}, with E∗=32ε∣f∣/h∗∼ε2/3≈10−11E^* = \tfrac32 \varepsilon |f|/h^* \sim \varepsilon^{2/3} \approx 10^{-11}. A third of the digits are lost.
  • Second difference: the rounding term is about 4ε∣f∣/h24\varepsilon |f|/h^2, so h∗∼ε1/4≈10−4h^* \sim \varepsilon^{1/4} \approx 10^{-4} and E∗∼ε1/2≈10−8E^* \sim \varepsilon^{1/2} \approx 10^{-8}.

The scales assume xx, ff and its derivatives are of order 1. In general scale the bump with the variable, h=ε1/3max⁡(∣x∣,1)h = \varepsilon^{1/3}\max(|x|, 1) for a central difference, and make the step exactly representable (xph = x + h; h = xph - x) so the difference in xx is what you divide by.

Worked example

the V-shaped error curve

Differentiate f(x)=exf(x) = e^x at x=1x = 1 (exact answer ee) by forward and central differences for h=10−1h = 10^{-1} down to 10−1210^{-12}. Locate the best hh for each and compare with the predictions.

Show solution
import math

x, exact = 1.0, math.exp(1.0)
print("    h      forward err   central err")
for k in (1, 2, 4, 5, 6, 8, 10, 12):
    h = 10.0**-k
    fwd = (math.exp(x + h) - math.exp(x)) / h
    cen = (math.exp(x + h) - math.exp(x - h)) / (2 * h)
    print(f"1e-{k:02d}   {abs(fwd - exact):.2e}      {abs(cen - exact):.2e}")
    h      forward err   central err
1e-01   1.41e-01      4.53e-03
1e-02   1.36e-02      4.53e-05
1e-04   1.36e-04      4.53e-09
1e-05   1.36e-05      5.86e-11
1e-06   1.36e-06      1.63e-10
1e-08   6.60e-09      6.60e-09
1e-10   1.55e-06      6.73e-07
1e-12   4.32e-04      2.10e-04

Truncation regime (large hh): the forward error falls by 10 per decade of hh (first order) and matches h2e=0.136\tfrac h2 e = 0.136 at h=0.1h = 0.1 up to the h2h^2 term; the central error falls by 100 per decade (second order) and matches h26e=4.53×10−3\tfrac{h^2}{6} e = 4.53 \times 10^{-3}.

Rounding regime (small hh): both errors grow like 1/h1/h, and at h=10−12h = 10^{-12} only three or four digits remain.

Optima. With ε=2.2×10−16\varepsilon = 2.2 \times 10^{-16} and f=f′′=f′′′=ef = f'' = f''' = e: forward h∗=2ε=3.0×10−8h^* = 2\sqrt\varepsilon = 3.0 \times 10^{-8} with predicted worst case 8.1×10−88.1 \times 10^{-8}; observed best 6.6×10−96.6 \times 10^{-9} at h=10−8h = 10^{-8}. Central h∗=(3ε)1/3=8.7×10−6h^* = (3\varepsilon)^{1/3} = 8.7 \times 10^{-6} with predicted worst case 1.0×10−101.0 \times 10^{-10}; observed best 5.9×10−115.9 \times 10^{-11} at h=10−5h = 10^{-5}. The observed errors are at or below the bounds because individual rounding errors partly cancel. The second difference, run the same way, is best near h=10−4h = 10^{-4} with error 3.8×10−83.8 \times 10^{-8}, as ε1/4\varepsilon^{1/4} and ε1/2\varepsilon^{1/2} predict.

The heat equation and the explicit scheme

The Black–Scholes PDE becomes the heat equation ut=uxxu_t = u_{xx} after a change of variables, so the heat equation is where the numerical theory lives. On a grid xi=iΔxx_i = i\Delta x, tn=nΔtt_n = n\Delta t, replace utu_t by a forward difference and uxxu_{xx} by the central second difference:

uin+1=uin+λ(ui−1n−2uin+ui+1n),λ=ΔtΔx2.u_i^{n+1} = u_i^n + \lambda\left(u_{i-1}^n - 2u_i^n + u_{i+1}^n\right), \qquad \lambda = \frac{\Delta t}{\Delta x^2} .

Each new value is an explicit combination of old ones, so a step costs O(M)O(M) for MM grid points. The local truncation error is O(Δt+Δx2)O(\Delta t + \Delta x^2).

Stability (von Neumann analysis). Errors on the grid can be written as Fourier modes uin=gneikiΔxu_i^n = g^n e^{\mathrm i k i \Delta x}. Substituting, and using eiθ−2+e−iθ=−4sin⁡2(θ/2)e^{\mathrm i\theta} - 2 + e^{-\mathrm i\theta} = -4\sin^2(\theta/2),

g(k)=1−4λsin⁡2 ⁣(kΔx2).g(k) = 1 - 4\lambda \sin^2\!\left(\frac{k\Delta x}{2}\right) .

The scheme is stable only if ∣g∣≤1|g| \le 1 for every mode. The worst mode, the grid-scale oscillation with sin⁡2=1\sin^2 = 1, has g=1−4λg = 1 - 4\lambda, so the condition is

λ=ΔtΔx2≤12.\lambda = \frac{\Delta t}{\Delta x^2} \le \frac12 .

If λ>1/2\lambda > 1/2, that mode grows by ∣1−4λ∣>1|1 - 4\lambda| > 1 per step from whatever seeds it (the data, or rounding), and it swamps the solution. By the Lax equivalence theorem, a consistent linear scheme converges if and only if it is stable, so stability is not a refinement; it is the whole question.

The cost of the condition: halving Δx\Delta x forces Δt\Delta t down by 4, so the work (points × steps) rises by 8, and in dd space dimensions by 2d+22^{d+2}.

Worked example

crossing the stability limit

Solve ut=uxxu_t = u_{xx} on [0,1][0, 1] with u=0u = 0 at both ends and the hat initial condition u(x,0)=min⁡(x,1−x)u(x, 0) = \min(x, 1 - x) (which has a kink, like a payoff), using M=20M = 20 intervals and the explicit scheme to t=0.1t = 0.1 for λ=0.25,0.4,0.5,0.52,0.6\lambda = 0.25, 0.4, 0.5, 0.52, 0.6. Compare with the Fourier-series solution u=∑k odd4sin⁡(kπ/2)k2π2sin⁡(kπx) e−k2π2tu = \sum_{k \text{ odd}} \frac{4\sin(k\pi/2)}{k^2\pi^2}\sin(k\pi x)\,e^{-k^2\pi^2 t}.

Show solution
import math

def exact(x, t, terms=200):
    return sum(4 * math.sin(k * math.pi / 2) / (k * math.pi) ** 2
               * math.sin(k * math.pi * x) * math.exp(-(k * math.pi) ** 2 * t)
               for k in range(1, terms, 2))

def explicit_heat(M, lam, t_end):
    dx = 1.0 / M
    dt = lam * dx * dx
    steps = round(t_end / dt)
    u = [min(i * dx, 1 - i * dx) for i in range(M + 1)]     # hat initial condition
    for _ in range(steps):
        u = [0.0] + [u[i] + lam * (u[i - 1] - 2 * u[i] + u[i + 1])
                     for i in range(1, M)] + [0.0]
    return u, steps * dt

M = 20
for lam in (0.25, 0.4, 0.5, 0.52, 0.6):
    u, t = explicit_heat(M, lam, 0.1)
    err = max(abs(u[i] - exact(i / M, t)) for i in range(M + 1))
    print(f"lambda={lam:4.2f}  steps={round(t / (lam / M**2)):4d}  max error={err:.2e}")
# lambda=0.25  steps= 160  max error=1.57e-04
# lambda=0.40  steps= 100  max error=1.22e-04
# lambda=0.50  steps=  80  max error=1.23e-03
# lambda=0.52  steps=  77  max error=3.77e-01
# lambda=0.60  steps=  67  max error=7.65e+06

(Each run is compared with the exact solution at its own final time steps×Δt\text{steps} \times \Delta t, which differs slightly from 0.1 when 0.1/Δt0.1/\Delta t is not an integer.)

  • λ≤0.4\lambda \le 0.4: errors around 10−410^{-4}, the O(Δx2)O(\Delta x^2) discretisation error.
  • λ=0.5\lambda = 0.5: still stable but the highest modes are barely damped (g≈−0.99g \approx -0.99 for the top mode on this grid), so the kink's grid-scale content persists and the error is ten times larger.
  • λ=0.52\lambda = 0.52 and 0.60.6: the top mode has ∣g∣=∣1−4λsin⁡2∣≈1.07|g| = |1 - 4\lambda\sin^2| \approx 1.07 and 1.391.39 per step. After 77 and 67 steps that is growth of about 10210^2 and 10910^9 from an initial amplitude that the kink seeds at the 10−310^{-3} level: an error of 0.40.4, then 8×1068 \times 10^6. The answer is garbage although every individual operation is accurate.

Implicit schemes and Crank–Nicolson

Evaluating the space derivative at the new time level, un+1−λDun+1=unu^{n+1} - \lambda D u^{n+1} = u^n (DD the second-difference matrix), gives implicit Euler, with g=1/(1+4λsin⁡2)g = 1/(1 + 4\lambda\sin^2): always in (0,1](0, 1], so stable for every λ\lambda, but only first order in time. Each step solves a tridiagonal system, which the Thomas algorithm does in O(M)O(M), so an implicit step costs about the same as an explicit one.

Crank–Nicolson averages the two, centring the scheme at tn+1/2t_{n + 1/2}:

(I−λ2D)un+1=(I+λ2D)un,g=1−2λsin⁡2(kΔx/2)1+2λsin⁡2(kΔx/2).\left(I - \tfrac{\lambda}{2} D\right) u^{n+1} = \left(I + \tfrac{\lambda}{2} D\right) u^n, \qquad g = \frac{1 - 2\lambda \sin^2(k\Delta x/2)}{1 + 2\lambda\sin^2(k\Delta x/2)} .

∣g∣≤1|g| \le 1 for all λ\lambda (unconditionally stable) and the error is O(Δt2+Δx2)O(\Delta t^2 + \Delta x^2), so you can take Δt∝Δx\Delta t \propto \Delta x instead of Δx2\Delta x^2.

The catch. For large λ\lambda and high-frequency modes, g→−1g \to -1: those modes are not damped but flip sign every step. A payoff's kink is full of high frequencies, so Crank–Nicolson with large time steps produces spurious oscillations near the strike, which show up in the gamma first. With M=400M = 400 log-space intervals and only 2 time steps on the call below, the computed gamma at the seven nodes around the strike is −0.09-0.09, −0.11-0.11, −0.13-0.13, +1.86+1.86, −0.12-0.12, −0.10-0.10, −0.09-0.09 against a true value of 0.01880.0188; with 400 time steps it is 0.01950.0195 down to 0.01800.0180 across the same nodes, smooth and correct. The standard fix is Rannacher smoothing: take the first few steps with implicit Euler, which damps the high modes, then switch to Crank–Nicolson.

Worked example

a European call by finite differences

Price the call with S0=K=100S_0 = K = 100, T=1T = 1, r=5%r = 5\%, σ=20%\sigma = 20\% (Black–Scholes 10.45058410.450584) by solving the Black–Scholes PDE in x=ln⁡Sx = \ln S and time to maturity τ\tau:

Vτ=12σ2Vxx+(r−12σ2)Vx−rV,V(x,0)=(ex−K)+.V_\tau = \tfrac12\sigma^2 V_{xx} + \left(r - \tfrac12\sigma^2\right)V_x - rV, \qquad V(x, 0) = (e^x - K)^+ .

Use a grid of ±5σT\pm 5\sigma\sqrt T around ln⁡S0\ln S_0 with MM intervals, boundary values V=0V = 0 at the bottom and V=Smax⁡−Ke−rτV = S_{\max} - Ke^{-r\tau} at the top. Compare Crank–Nicolson (Nt=MN_t = M steps) with the explicit scheme (smallest NtN_t with σ2Δt/Δx2≤0.9\sigma^2\Delta t/\Delta x^2 \le 0.9), and show an unstable explicit run.

Show solution
import math

S0, K, T, r, sig = 100.0, 100.0, 1.0, 0.05, 0.2

def N(x):
    return 0.5 * math.erfc(-x / math.sqrt(2))

d1 = (math.log(S0 / K) + (r + 0.5 * sig**2) * T) / (sig * math.sqrt(T))
BS = S0 * N(d1) - K * math.exp(-r * T) * N(d1 - sig * math.sqrt(T))

def thomas(a, b, c, d):
    """Solve a tridiagonal system: a = sub-, b = main, c = super-diagonal."""
    n = len(d)
    cp, dp = [0.0] * n, [0.0] * n
    cp[0], dp[0] = c[0] / b[0], d[0] / b[0]
    for i in range(1, n):
        m = b[i] - a[i] * cp[i - 1]
        cp[i] = c[i] / m if i < n - 1 else 0.0
        dp[i] = (d[i] - a[i] * dp[i - 1]) / m
    x = [0.0] * n
    x[-1] = dp[-1]
    for i in range(n - 2, -1, -1):
        x[i] = dp[i] - cp[i] * x[i + 1]
    return x

def fd_call(M, Nt, theta):
    """theta = 0: explicit, theta = 0.5: Crank-Nicolson. Grid in x = ln S."""
    width = 5 * sig * math.sqrt(T)
    x0 = math.log(S0)
    dx = 2 * width / M                      # S0 sits on node M // 2
    xs = [x0 - width + i * dx for i in range(M + 1)]
    dt = T / Nt
    # V_tau = A V_xx + B V_x - r V, written as V_tau = L V
    A, B = 0.5 * sig**2, r - 0.5 * sig**2
    lo = A / dx**2 - B / (2 * dx)           # coefficient of V[i-1]
    mid = -2 * A / dx**2 - r                # coefficient of V[i]
    up = A / dx**2 + B / (2 * dx)           # coefficient of V[i+1]
    V = [max(math.exp(x) - K, 0.0) for x in xs]
    Smax = math.exp(xs[-1])
    for n in range(1, Nt + 1):
        tau = n * dt
        right = Smax - K * math.exp(-r * tau)
        LV = [lo * V[i - 1] + mid * V[i] + up * V[i + 1] for i in range(1, M)]
        rhs = [V[i] + (1 - theta) * dt * LV[i - 1] for i in range(1, M)]
        if theta == 0:
            inner = rhs
        else:
            k = theta * dt
            rhs[-1] += k * up * right       # known boundary value at the new time
            a = [-k * lo] * (M - 1)
            b = [1 - k * mid] * (M - 1)
            c = [-k * up] * (M - 1)
            inner = thomas(a, b, c, rhs)
        V = [0.0] + inner + [right]
    return V[M // 2]

print(f"Black-Scholes: {BS:.6f}")
for M in (50, 100, 200, 400):
    Nt_cn = M
    cn = fd_call(M, Nt_cn, 0.5)
    dx = 10 * sig * math.sqrt(T) / M
    Nt_ex = math.ceil(T * sig**2 / dx**2 / 0.9)   # explicit needs sig^2 dt / dx^2 <= 1
    ex = fd_call(M, Nt_ex, 0.0)
    print(f"M={M:3d}  CN (Nt={Nt_cn:3d}) {cn:.6f} err {cn - BS:+.2e}   "
          f"explicit (Nt={Nt_ex:5d}) {ex:.6f} err {ex - BS:+.2e}")
dx = 10 * sig * math.sqrt(T) / 100
nt_bad = math.floor(T * sig**2 / dx**2 / 1.1)
print("explicit M=100, Nt =", nt_bad, "->", f"{fd_call(100, nt_bad, 0.0):.3e}")
Black-Scholes: 10.450584
M= 50  CN (Nt= 50) 10.414331 err -3.63e-02   explicit (Nt=   28) 10.452249 err +1.67e-03
M=100  CN (Nt=100) 10.441575 err -9.01e-03   explicit (Nt=  112) 10.450957 err +3.74e-04
M=200  CN (Nt=200) 10.448335 err -2.25e-03   explicit (Nt=  445) 10.450689 err +1.05e-04
M=400  CN (Nt=400) 10.450022 err -5.62e-04   explicit (Nt= 1778) 10.450610 err +2.67e-05
explicit M=100, Nt = 90 -> -1.588e+06

Order. The Crank–Nicolson errors fall by 4.04.0 per doubling of MM (with Nt=MN_t = M): second order, as predicted. Almost all of that error is spatial: at M=100M = 100, running Crank–Nicolson with 50, 100, 400 and 1,600 steps gives −8.93×10−3-8.93 \times 10^{-3}, −9.01×10−3-9.01 \times 10^{-3}, −9.03×10−3-9.03 \times 10^{-3} and −9.03×10−3-9.03 \times 10^{-3}.

Why explicit looks more accurate here. Its time error is positive and partly cancels the (negative) spatial error that both schemes share, a coincidence of this setup, not a property to rely on. What is structural is the cost: the explicit scheme's step count grows like M2M^2 (28, 112, 445, 1,778), while Crank–Nicolson's grows like MM. With 10% fewer steps than the limit allows (σ2Δt/Δx2=1.11\sigma^2\Delta t/\Delta x^2 = 1.11), the explicit price is −1.6×106-1.6 \times 10^{6}.

Richardson. Because the Crank–Nicolson error is c Δx2c\,\Delta x^2 to leading order, (4V400−V200)/3=10.4505838(4V_{400} - V_{200})/3 = 10.4505838, within 3×10−73 \times 10^{-7} of Black–Scholes, from two cheap grids.

The strike sits on a grid node here, which keeps the convergence clean. With the kink between nodes the error ratio oscillates; practitioners place the strike on a node or average the payoff over the cell containing it.

Knowledge check

In double precision, for a smooth function with f, f', f'' and f''' all of order 1 near x = 1, roughly which step size minimises the error of the central difference (f(x+h) − f(x−h)) / (2h)?

Knowledge check

An explicit heat-equation solver runs at the stability limit. You halve the space step to improve accuracy. By what factor does the total work change?

Knowledge check

A Crank–Nicolson pricer with large time steps gives a reasonable price but a gamma that oscillates in sign near the strike. What is the cause and the standard fix?

Key takeaways

  • Forward differences are O(h)O(h), central O(h2)O(h^2), and rounding adds ∼ε/h\sim\varepsilon/h (or ε/h2\varepsilon/h^2 for second differences). Optimal steps: ε1/2\varepsilon^{1/2}, ε1/3\varepsilon^{1/3}, ε1/4\varepsilon^{1/4}, losing a half, a third and a half of the digits respectively. Scale hh with ∣x∣|x|.
  • Complex-step and algorithmic differentiation avoid the subtraction altogether.
  • The explicit heat scheme has g=1−4λsin⁡2(kΔx/2)g = 1 - 4\lambda\sin^2(k\Delta x/2) and is stable iff λ=Δt/Δx2≤1/2\lambda = \Delta t/\Delta x^2 \le 1/2; consistency plus stability equals convergence.
  • Crank–Nicolson is unconditionally stable and second order in time and space, at the cost of a tridiagonal solve per step, but it does not damp the payoff's kink: use Rannacher start-up steps.
  • Verify a pricer by refinement: errors should fall by 4 per halving of the grid, and Richardson extrapolation then gains several digits for free.

Practice problems

Finished reading?

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