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:
The central difference is second order because the even-order terms cancel by symmetry.
The optimal step size
Truncation error falls as , but the numerator is a difference of nearly equal numbers. If each computed value of has absolute error up to about (correct rounding plus a little error inside ), the numerator of the forward difference has error up to , which the division by amplifies. The total error model is
Setting the derivative in to zero:
- Forward: , with . Half the digits are lost.
- Central: , so , with . A third of the digits are lost.
- Second difference: the rounding term is about , so and .
The scales assume , and its derivatives are of order 1. In general scale the bump with the variable, for a central difference, and make the step exactly representable (xph = x + h; h = xph - x) so the difference in is what you divide by.
Worked example
the V-shaped error curve
Differentiate at (exact answer ) by forward and central differences for down to . Locate the best for each and compare with the predictions.
Show solutionHide 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 ): the forward error falls by 10 per decade of (first order) and matches at up to the term; the central error falls by 100 per decade (second order) and matches .
Rounding regime (small ): both errors grow like , and at only three or four digits remain.
Optima. With and : forward with predicted worst case ; observed best at . Central with predicted worst case ; observed best at . 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 with error , as and predict.
The heat equation and the explicit scheme
The Black–Scholes PDE becomes the heat equation after a change of variables, so the heat equation is where the numerical theory lives. On a grid , , replace by a forward difference and by the central second difference:
Each new value is an explicit combination of old ones, so a step costs for grid points. The local truncation error is .
Stability (von Neumann analysis). Errors on the grid can be written as Fourier modes . Substituting, and using ,
The scheme is stable only if for every mode. The worst mode, the grid-scale oscillation with , has , so the condition is
If , that mode grows by 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 forces down by 4, so the work (points × steps) rises by 8, and in space dimensions by .
Worked example
crossing the stability limit
Solve on with at both ends and the hat initial condition (which has a kink, like a payoff), using intervals and the explicit scheme to for . Compare with the Fourier-series solution .
Show solutionHide 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 , which differs slightly from 0.1 when is not an integer.)
- : errors around , the discretisation error.
- : still stable but the highest modes are barely damped ( for the top mode on this grid), so the kink's grid-scale content persists and the error is ten times larger.
- and : the top mode has and per step. After 77 and 67 steps that is growth of about and from an initial amplitude that the kink seeds at the level: an error of , then . The answer is garbage although every individual operation is accurate.
Implicit schemes and Crank–Nicolson
Evaluating the space derivative at the new time level, ( the second-difference matrix), gives implicit Euler, with : always in , so stable for every , but only first order in time. Each step solves a tridiagonal system, which the Thomas algorithm does in , so an implicit step costs about the same as an explicit one.
Crank–Nicolson averages the two, centring the scheme at :
for all (unconditionally stable) and the error is , so you can take instead of .
The catch. For large and high-frequency modes, : 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 log-space intervals and only 2 time steps on the call below, the computed gamma at the seven nodes around the strike is , , , , , , against a true value of ; with 400 time steps it is down to 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 , , , (Black–Scholes ) by solving the Black–Scholes PDE in and time to maturity :
Use a grid of around with intervals, boundary values at the bottom and at the top. Compare Crank–Nicolson ( steps) with the explicit scheme (smallest with ), and show an unstable explicit run.
Show solutionHide 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 per doubling of (with ): second order, as predicted. Almost all of that error is spatial: at , running Crank–Nicolson with 50, 100, 400 and 1,600 steps gives , , and .
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 (28, 112, 445, 1,778), while Crank–Nicolson's grows like . With 10% fewer steps than the limit allows (), the explicit price is .
Richardson. Because the Crank–Nicolson error is to leading order, , within 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 , central , and rounding adds (or for second differences). Optimal steps: , , , losing a half, a third and a half of the digits respectively. Scale with .
- Complex-step and algorithmic differentiation avoid the subtraction altogether.
- The explicit heat scheme has and is stable iff ; 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).