Skip to content
LeetQuant
E04 Numerical Methods

E04 · Numerical Methods · Lesson 3 of 4

Interpolation and Quadrature

Polynomial interpolation error and Runge's phenomenon, Chebyshev nodes and cubic splines, trapezoid and Simpson error orders measured numerically, Gaussian quadrature for option prices, and adaptive quadrature.

20 min

Objective

Interpolation reconstructs a function from samples (a yield curve from a few tenors, a vol smile from a few strikes); quadrature integrates it (an expectation, a price). Both are approximation by simple functions, and both have error formulas that tell you how fast refinement pays off and when it does not. By the end you should be able to predict an error order, confirm it numerically, and pick the right rule for a smooth, kinked or singular integrand.

Polynomial interpolation and its error

Through n+1n + 1 distinct points there is exactly one polynomial pnp_n of degree at most nn. For ff with n+1n + 1 continuous derivatives,

f(x)−pn(x)=f(n+1)(ξ)(n+1)!∏i=0n(x−xi)f(x) - p_n(x) = \frac{f^{(n+1)}(\xi)}{(n + 1)!} \prod_{i=0}^{n} (x - x_i)

for some ξ\xi in the span of the points and xx. Two things control the error: how fast the derivatives grow, and the size of the node polynomial ω(x)=∏(x−xi)\omega(x) = \prod (x - x_i).

Runge's phenomenon. For f(x)=1/(1+25x2)f(x) = 1/(1 + 25x^2) on [−1,1][-1, 1], the derivatives grow like (n+1)! 5n+1(n + 1)!\,5^{n+1} (the function has complex poles at ±i/5\pm i/5, close to the interval), and on equally spaced nodes ∣ω∣|\omega| is enormous near the ends. Raising the degree makes things worse.

Chebyshev nodes xi=cos⁡((2i+1)π/(2n+2))x_i = \cos\big((2i + 1)\pi/(2n + 2)\big) cluster near the ends and minimise max⁡[−1,1]∣ω∣\max_{[-1,1]} |\omega|, which then equals 2−n2^{-n}. For any ff analytic near [−1,1][-1, 1], interpolation at Chebyshev nodes converges geometrically. Evaluate the interpolant with the barycentric formula (stable, O(n)O(n) per point); never by solving a Vandermonde system, which is horribly ill-conditioned.

Cubic splines

A cubic spline is a different cubic on each interval, joined so that the value, slope and curvature are continuous. With n+1n + 1 knots that leaves two free conditions, fixed by an end condition: natural (S′′=0S'' = 0 at the ends), clamped (given end slopes) or not-a-knot. The unknowns satisfy a tridiagonal system, solved in O(n)O(n). For f∈C4f \in C^4 with a good end condition, the error is O(h4)O(h^4), and a perturbation of one data point decays rapidly away from it (unlike a high-degree polynomial, where it spreads everywhere).

Worked example

Runge's function three ways

Interpolate f(x)=1/(1+25x2)f(x) = 1/(1 + 25x^2) on [−1,1][-1, 1] with n=10n = 10 and n=20n = 20 intervals by (a) a polynomial on equally spaced nodes, (b) a polynomial on n+1n + 1 Chebyshev nodes and (c) a natural cubic spline on the equally spaced nodes. Report the maximum error on a fine grid, then check the spline's order by refining further.

Show solution

Maximum errors (barycentric evaluation for the polynomials, SciPy's CubicSpline for the spline, 2,001 test points):

nnEquispaced polynomialChebyshev polynomialNatural cubic spline
101.920.1090.022
2059.80.01530.00318

The equispaced polynomial explodes near ±1\pm 1: doubling the degree multiplies the error by 30. The Chebyshev interpolant converges (roughly geometrically, slowly here because the poles at ±i/5\pm i/5 are close). The spline is best at these sizes and never oscillates wildly.

Refining the not-a-knot spline: n=10,20,40,80,160n = 10, 20, 40, 80, 160 gives errors 2.20×10−2,3.18×10−3,2.78×10−4,1.61×10−5,9.63×10−72.20 \times 10^{-2}, 3.18 \times 10^{-3}, 2.78 \times 10^{-4}, 1.61 \times 10^{-5}, 9.63 \times 10^{-7}. The ratios are 6.9,11.4,17.3,16.76.9, 11.4, 17.3, 16.7: once hh resolves the peak (width about 0.20.2), halving hh divides the error by about 16=2416 = 2^4, confirming O(h4)O(h^4).

In practice. For curves from market data, interpolate the quantity whose smoothness you trust: a common yield-curve choice is linear interpolation in ln⁡\ln of the discount factor, which gives piecewise-flat forward rates. A cubic spline on zero rates is smoother but can produce oscillating forwards, and a spline through implied vols can create a smile that admits arbitrage. The interpolation scheme is a modelling decision, not only a numerical one.

Newton–Cotes quadrature: trapezoid and Simpson

Integrate the piecewise-linear interpolant on a uniform grid of step h=(b−a)/nh = (b - a)/n and you get the composite trapezoid rule; integrate piecewise quadratics over pairs of intervals and you get Simpson's rule. Their errors are

Th−I=(b−a)h212f′′(ξ),Sh−I=(b−a)h4180f(4)(ξ).T_h - I = \frac{(b - a)h^2}{12} f''(\xi), \qquad S_h - I = \frac{(b - a)h^4}{180} f^{(4)}(\xi) .

Simpson is fourth order although it integrates quadratics: by symmetry it is also exact for cubics.

Richardson extrapolation explains the connection. The Euler–Maclaurin formula gives Th=I+c2h2+c4h4+⋯T_h = I + c_2 h^2 + c_4 h^4 + \cdots with c2=(f′(b)−f′(a))/12c_2 = (f'(b) - f'(a))/12. Eliminating the h2h^2 term,

4Th/2−Th3=I+O(h4),\frac{4T_{h/2} - T_h}{3} = I + O(h^4),

and this combination is exactly Simpson's rule on the finer grid. Repeating the elimination is Romberg integration.

Euler–Maclaurin also explains two practical surprises:

  • Smooth periodic integrands: every c2kc_{2k} involves differences of derivatives at the endpoints, which vanish for a periodic function over a full period. The trapezoid rule then converges faster than any power of hh. For ∫02πecos⁡x dx\int_0^{2\pi} e^{\cos x}\,dx, 4, 8 and 16 points give errors 3.4×10−23.4 \times 10^{-2}, 1.3×10−61.3 \times 10^{-6} and 2.7×10−152.7 \times 10^{-15}.
  • Singular derivatives: the formulas assume f′′f'' or f(4)f^{(4)} is bounded. If it is not, the order drops, as the next example shows.

Worked example

measuring trapezoid and Simpson orders

Compute ∫01ex dx=e−1\int_0^1 e^x\,dx = e - 1 with the composite trapezoid and Simpson rules for n=4,8,…,64n = 4, 8, \ldots, 64 and report the error ratios. Then repeat Simpson for ∫01x dx=2/3\int_0^1 \sqrt x\,dx = 2/3. Explain both sets of ratios.

Show solution
import math

def trapezoid(f, a, b, n):
    h = (b - a) / n
    return h * (0.5 * f(a) + sum(f(a + i * h) for i in range(1, n)) + 0.5 * f(b))

def simpson(f, a, b, n):          # n must be even
    h = (b - a) / n
    s = f(a) + f(b)
    s += 4 * sum(f(a + i * h) for i in range(1, n, 2))
    s += 2 * sum(f(a + i * h) for i in range(2, n, 2))
    return s * h / 3

exact = math.e - 1
prev_t = prev_s = None
print(" n   trap error   ratio   simpson error   ratio")
for n in (4, 8, 16, 32, 64):
    et = abs(trapezoid(math.exp, 0, 1, n) - exact)
    es = abs(simpson(math.exp, 0, 1, n) - exact)
    rt = f"{prev_t / et:5.2f}" if prev_t else "   - "
    rs = f"{prev_s / es:5.2f}" if prev_s else "   - "
    print(f"{n:3d}   {et:.3e}   {rt}   {es:.3e}       {rs}")
    prev_t, prev_s = et, es
 n   trap error   ratio   simpson error   ratio
  4   8.940e-03      -    3.701e-05          -
  8   2.237e-03    4.00   2.326e-06       15.91
 16   5.593e-04    4.00   1.456e-07       15.98
 32   1.398e-04    4.00   9.103e-09       15.99
 64   3.496e-05    4.00   5.690e-10       16.00

Smooth case. Ratios of 4=224 = 2^2 and 16=2416 = 2^4 confirm orders 2 and 4. The constants match too: for n=4n = 4, h2(e−1)/12=0.0625×1.718/12=8.95×10−3h^2 (e - 1)/12 = 0.0625 \times 1.718/12 = 8.95 \times 10^{-3} (using the average of f′′f'' over [0,1][0, 1], which is e−1e - 1), and h4(e−1)/180=3.73×10−5h^4 (e - 1)/180 = 3.73 \times 10^{-5}.

Singular case. For x\sqrt x, Simpson's errors are 1.01×10−2,3.59×10−3,1.27×10−3,4.49×10−4,1.59×10−41.01 \times 10^{-2}, 3.59 \times 10^{-3}, 1.27 \times 10^{-3}, 4.49 \times 10^{-4}, 1.59 \times 10^{-4}, with ratios 2.8282.828 every time. That is 21.52^{1.5}: the order has dropped to 1.51.5, because f(4)(x)∝x−7/2f^{(4)}(x) \propto x^{-7/2} is unbounded at 0 and the error is dominated by the first interval, where ∫0hx dx∝h1.5\int_0^h \sqrt x\,dx \propto h^{1.5}.

Fixes. Remove the singularity analytically: with x=t2x = t^2, ∫01x dx=∫012t2 dt\int_0^1 \sqrt x\,dx = \int_0^1 2t^2\,dt, a polynomial that Simpson integrates exactly. Or let an adaptive rule concentrate points near 0 (below).

Gaussian quadrature

Newton–Cotes fixes the nodes and chooses weights. Gaussian quadrature chooses both: with nn nodes and nn weights there are 2n2n free parameters, enough to integrate every polynomial of degree up to 2n−12n - 1 exactly. For ∫−11f(x) dx\int_{-1}^{1} f(x)\,dx the nodes are the roots of the Legendre polynomial PnP_n, and all weights are positive (so the rule is stable).

  • Two points: nodes ±1/3≈±0.5774\pm 1/\sqrt 3 \approx \pm 0.5774, weights 1. Check: ∫−11x2 dx=2/3\int_{-1}^1 x^2\,dx = 2/3 and the rule gives 2×(1/3)=2/32 \times (1/3) = 2/3; for x4x^4 the rule gives 2/92/9 instead of 2/52/5, as degree 4 exceeds 2n−1=32n - 1 = 3.
  • Accuracy: for ∫01ex dx\int_0^1 e^x\,dx, Gauss–Legendre with 2, 3 and 5 points has errors 3.9×10−43.9 \times 10^{-4}, 8.2×10−78.2 \times 10^{-7} and 6.5×10−136.5 \times 10^{-13}. Five evaluations beat Simpson with 65 evaluations (5.7×10−105.7 \times 10^{-10}) by three orders of magnitude.

Other weight functions give other families. Gauss–Hermite with weight e−z2/2e^{-z^2/2} (NumPy's hermite_e.hermegauss) computes E[g(Z)]\mathbb E[g(Z)] for Z∼N(0,1)Z \sim N(0, 1) as 12π∑iwig(zi)\frac{1}{\sqrt{2\pi}}\sum_i w_i g(z_i), the natural tool for expectations under a normal or lognormal model. The catch: exactness for polynomials only translates into fast convergence when gg is smooth.

Worked example

pricing a call by Gaussian quadrature

Price a European call with S0=K=100S_0 = K = 100, T=1T = 1, r=5%r = 5\%, σ=20%\sigma = 20\% as e−rT E[(ST−K)+]e^{-rT}\,\mathbb E[(S_T - K)^+] with ST=S0e(r−σ2/2)T+σTZS_T = S_0 e^{(r - \sigma^2/2)T + \sigma\sqrt T Z}. Compare Gauss–Hermite with nn nodes against Gauss–Legendre with nn nodes on the region where the payoff is positive. The Black–Scholes price is 10.450583572210.4505835722.

Show solution

The payoff is positive for Z>z∗=(ln⁡(K/S0)−(r−σ2/2)T)/(σT)=−0.15Z > z^* = \big(\ln(K/S_0) - (r - \sigma^2/2)T\big)/(\sigma\sqrt T) = -0.15.

import math
import numpy as np

S0, K, T, r, sig = 100.0, 100.0, 1.0, 0.05, 0.2
d1 = (math.log(S0 / K) + (r + 0.5 * sig**2) * T) / (sig * math.sqrt(T))
N = lambda x: 0.5 * math.erfc(-x / math.sqrt(2))
BS = S0 * N(d1) - K * math.exp(-r * T) * N(d1 - sig * math.sqrt(T))   # 10.4505835722

def payoff(z):
    ST = S0 * np.exp((r - 0.5 * sig**2) * T + sig * math.sqrt(T) * z)
    return np.maximum(ST - K, 0.0)

def phi(z):
    return np.exp(-0.5 * z**2) / math.sqrt(2 * math.pi)

z_star = (math.log(K / S0) - (r - 0.5 * sig**2) * T) / (sig * math.sqrt(T))
for n in (8, 16, 32, 64):
    x, w = np.polynomial.hermite_e.hermegauss(n)        # weight exp(-z^2/2)
    gh = math.exp(-r * T) * np.sum(w * payoff(x)) / math.sqrt(2 * math.pi)
    y, v = np.polynomial.legendre.leggauss(n)            # nodes on [-1, 1]
    a, b = z_star, z_star + 10.0                         # payoff smooth here
    z = 0.5 * (b - a) * y + 0.5 * (a + b)
    gl = math.exp(-r * T) * 0.5 * (b - a) * np.sum(v * payoff(z) * phi(z))
    print(f"n={n:2d}  Gauss-Hermite error {gh - BS:+.2e}   Gauss-Legendre past the kink {gl - BS:+.2e}")
n= 8  Gauss-Hermite error +3.34e-01   Gauss-Legendre past the kink -4.12e-02
n=16  Gauss-Hermite error +1.19e-01   Gauss-Legendre past the kink +1.36e-07
n=32  Gauss-Hermite error +1.55e-02   Gauss-Legendre past the kink -8.88e-15
n=64  Gauss-Hermite error -3.56e-02   Gauss-Legendre past the kink +1.07e-14

Gauss–Hermite over the whole line is poor and erratic (64 nodes is worse than 32): the integrand has a kink at z∗z^*, so no polynomial approximates it well, and the error depends on where the nodes happen to fall relative to the kink.

Splitting at the kink makes the integrand smooth (analytic) on [z∗,z∗+10][z^*, z^* + 10], and Gauss–Legendre converges geometrically: 16 nodes give 10−710^{-7}, 32 give machine precision. The truncation at z∗+10z^* + 10 discards a tail far below 10−1510^{-15}.

This is the general recipe for quadrature-based pricing: find the payoff's discontinuities and kinks (strikes, barriers), split the domain there, and use a high-order rule on each smooth piece. With 32 evaluations it beats Monte Carlo with millions of paths, in one dimension.

Adaptive quadrature

A uniform grid wastes points where ff is smooth and starves the regions where it is not. Adaptive Simpson compares Simpson on an interval with Simpson on its two halves. Since the error falls by about 16 when hh halves, the difference Δ=Shalves−Swhole\Delta = S_{\text{halves}} - S_{\text{whole}} is about 15 times the error of ShalvesS_{\text{halves}}. If ∣Δ∣≤15 tol|\Delta| \le 15\,\text{tol}, accept Shalves+Δ/15S_{\text{halves}} + \Delta/15 (a Richardson correction); otherwise recurse on each half with tol/2\text{tol}/2.

import math

def adaptive_simpson(f, a, b, tol):
    calls = 0
    def F(x):
        nonlocal calls
        calls += 1
        return f(x)
    def simpson(fa, fm, fb, a, b):
        return (b - a) / 6 * (fa + 4 * fm + fb)
    def recurse(a, b, fa, fm, fb, whole, tol, depth):
        m = (a + b) / 2
        lm, rm = (a + m) / 2, (m + b) / 2
        flm, frm = F(lm), F(rm)
        left = simpson(fa, flm, fm, a, m)
        right = simpson(fm, frm, fb, m, b)
        delta = left + right - whole
        if depth == 0 or abs(delta) <= 15 * tol:
            return left + right + delta / 15          # Richardson correction
        return (recurse(a, m, fa, flm, fm, left, tol / 2, depth - 1)
                + recurse(m, b, fm, frm, fb, right, tol / 2, depth - 1))
    fa, fm, fb = F(a), F((a + b) / 2), F(b)
    whole = simpson(fa, fm, fb, a, b)
    return recurse(a, b, fa, fm, fb, whole, tol, 50), calls

for tol in (1e-6, 1e-10):
    val, calls = adaptive_simpson(math.sqrt, 0.0, 1.0, tol)
    print(f"tol={tol:.0e}  error={abs(val - 2 / 3):.2e}  evaluations={calls}")
# tol=1e-06  error=1.95e-08  evaluations=105
# tol=1e-10  error=2.80e-14  evaluations=985

For ∫01x dx\int_0^1 \sqrt x\,dx, 985 evaluations reach 3×10−143 \times 10^{-14}. Uniform Simpson, with its error ≈0.081 n−1.5\approx 0.081\,n^{-1.5} (fitted from the table above), would need about 870,000870{,}000 points just to reach 10−1010^{-10}. Library routines (scipy.integrate.quad, QUADPACK) use adaptive Gauss–Kronrod rules on the same principle.

When quadrature loses: dimension

A tensor-product rule with mm points per axis in dd dimensions costs N=mdN = m^d evaluations, and an order-kk rule then has error O(m−k)=O(N−k/d)O(m^{-k}) = O(N^{-k/d}). Monte Carlo's error is O(N−1/2)O(N^{-1/2}) in any dimension. For Simpson (k=4k = 4) the crossover is d=8d = 8; for a basket or path-dependent option with dozens of dimensions, Monte Carlo (or quasi-Monte Carlo) wins. For one to three smooth dimensions, quadrature wins by a wide margin.

Knowledge check

Composite Simpson's rule for ∫01x dx\int_0^1 \sqrt x\,dx gives errors that fall by a factor of about 2.83 each time hh halves. Why not 16?

Knowledge check

A 3-point Gauss–Legendre rule integrates exactly all polynomials of degree up to:

Knowledge check

Gauss–Hermite quadrature prices a European call with an error of about 3e-2 even with 64 nodes. What is the best remedy?

Key takeaways

  • Interpolation error is f(n+1)(ξ) ω(x)/(n+1)!f^{(n+1)}(\xi)\,\omega(x)/(n + 1)!. Equispaced high-degree polynomials diverge on Runge's function; Chebyshev nodes (max ∣ω∣=2−n|\omega| = 2^{-n}) or splines (O(h4)O(h^4)) fix it. Evaluate with the barycentric formula.
  • Trapezoid is O(h2)O(h^2), Simpson O(h4)O(h^4); check by halving hh and looking for ratios 4 and 16. Simpson is Richardson-extrapolated trapezoid.
  • Orders assume smoothness: a singular derivative or a kink lowers them. Remove singularities by substitution, split at kinks, or go adaptive.
  • Gaussian quadrature with nn nodes is exact to degree 2n−12n - 1; on smooth integrands it converges geometrically. Gauss–Hermite computes normal expectations.
  • Quadrature dominates in low dimension; beyond a handful of dimensions, Monte Carlo's dimension-free N−1/2N^{-1/2} wins.

Practice problems

Finished reading?

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