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 distinct points there is exactly one polynomial of degree at most . For with continuous derivatives,
for some in the span of the points and . Two things control the error: how fast the derivatives grow, and the size of the node polynomial .
Runge's phenomenon. For on , the derivatives grow like (the function has complex poles at , close to the interval), and on equally spaced nodes is enormous near the ends. Raising the degree makes things worse.
Chebyshev nodes cluster near the ends and minimise , which then equals . For any analytic near , interpolation at Chebyshev nodes converges geometrically. Evaluate the interpolant with the barycentric formula (stable, 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 knots that leaves two free conditions, fixed by an end condition: natural ( at the ends), clamped (given end slopes) or not-a-knot. The unknowns satisfy a tridiagonal system, solved in . For with a good end condition, the error is , 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 on with and intervals by (a) a polynomial on equally spaced nodes, (b) a polynomial on 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 solutionHide solution
Maximum errors (barycentric evaluation for the polynomials, SciPy's CubicSpline for the spline, 2,001 test points):
| Equispaced polynomial | Chebyshev polynomial | Natural cubic spline | |
|---|---|---|---|
| 10 | 1.92 | 0.109 | 0.022 |
| 20 | 59.8 | 0.0153 | 0.00318 |
The equispaced polynomial explodes near : doubling the degree multiplies the error by 30. The Chebyshev interpolant converges (roughly geometrically, slowly here because the poles at are close). The spline is best at these sizes and never oscillates wildly.
Refining the not-a-knot spline: gives errors . The ratios are : once resolves the peak (width about ), halving divides the error by about , confirming .
In practice. For curves from market data, interpolate the quantity whose smoothness you trust: a common yield-curve choice is linear interpolation in 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 and you get the composite trapezoid rule; integrate piecewise quadratics over pairs of intervals and you get Simpson's rule. Their errors are
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 with . Eliminating the term,
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 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 . For , 4, 8 and 16 points give errors , and .
- Singular derivatives: the formulas assume or is bounded. If it is not, the order drops, as the next example shows.
Worked example
measuring trapezoid and Simpson orders
Compute with the composite trapezoid and Simpson rules for and report the error ratios. Then repeat Simpson for . Explain both sets of ratios.
Show solutionHide 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 and confirm orders 2 and 4. The constants match too: for , (using the average of over , which is ), and .
Singular case. For , Simpson's errors are , with ratios every time. That is : the order has dropped to , because is unbounded at 0 and the error is dominated by the first interval, where .
Fixes. Remove the singularity analytically: with , , 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 nodes and weights there are free parameters, enough to integrate every polynomial of degree up to exactly. For the nodes are the roots of the Legendre polynomial , and all weights are positive (so the rule is stable).
- Two points: nodes , weights 1. Check: and the rule gives ; for the rule gives instead of , as degree 4 exceeds .
- Accuracy: for , Gauss–Legendre with 2, 3 and 5 points has errors , and . Five evaluations beat Simpson with 65 evaluations () by three orders of magnitude.
Other weight functions give other families. Gauss–Hermite with weight (NumPy's hermite_e.hermegauss) computes for as , the natural tool for expectations under a normal or lognormal model. The catch: exactness for polynomials only translates into fast convergence when is smooth.
Worked example
pricing a call by Gaussian quadrature
Price a European call with , , , as with . Compare Gauss–Hermite with nodes against Gauss–Legendre with nodes on the region where the payoff is positive. The Black–Scholes price is .
Show solutionHide solution
The payoff is positive for .
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 , 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 , and Gauss–Legendre converges geometrically: 16 nodes give , 32 give machine precision. The truncation at discards a tail far below .
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 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 halves, the difference is about 15 times the error of . If , accept (a Richardson correction); otherwise recurse on each half with .
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 , 985 evaluations reach . Uniform Simpson, with its error (fitted from the table above), would need about points just to reach . Library routines (scipy.integrate.quad, QUADPACK) use adaptive Gauss–Kronrod rules on the same principle.
When quadrature loses: dimension
A tensor-product rule with points per axis in dimensions costs evaluations, and an order- rule then has error . Monte Carlo's error is in any dimension. For Simpson () the crossover is ; 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 gives errors that fall by a factor of about 2.83 each time 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 . Equispaced high-degree polynomials diverge on Runge's function; Chebyshev nodes (max ) or splines () fix it. Evaluate with the barycentric formula.
- Trapezoid is , Simpson ; check by halving 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 nodes is exact to degree ; 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 wins.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).