Numerical Integration: Quadrature#

A price is an integral. For a European option it is the discounted payoff times the density of the final price, integrated over all prices. When the integral has one or two dimensions, quadrature computes it directly with far fewer function evaluations than Monte Carlo, and with an error that can be driven to machine precision. This page sets out the main rules, what a kink in the payoff does to them, and where Monte Carlo becomes the better choice.

The integral#

For the benchmark call, with the standard normal variable \(Z\) driving the final price \(S_T(Z)\):

\[V = \int_{-\infty}^{\infty} e^{-rT}\max\left(S_T(z) - K, 0\right)\,\varphi(z)\,dz\]

where \(\varphi\) is the normal density. The integrand is smooth except at one point, \(z^*\), where the payoff starts. Below \(z^*\) it is exactly zero. The kink is where the final price equals the strike,

\[z^* = \frac{\ln(K/S_0) - (r - \tfrac12\sigma^2)T}{\sigma\sqrt{T}}, \qquad V = \int_{z^*}^{\infty} e^{-rT}\left(S_T(z) - K\right)\varphi(z)\,dz\]

which is \(-0.05\) for the benchmark. In practice the infinite limit is cut at \(L\), and the probability that is lost is \(1 - \Phi(L)\), which is \(6 \times 10^{-16}\) for \(L = 8\). The error of a quadrature rule for this integral has two parts, the truncation of the range and the discretisation of what is left, and the second is what the choice of rule controls.

The rules#

Every rule approximates the integral by a weighted sum of function values,

\[\int_a^b f(x)\,\omega(x)\,dx \approx \sum_{i=1}^{n} w_i\,f(x_i)\]

with nodes \(x_i\), weights \(w_i\) and a weight function \(\omega\) that is 1 for the simple rules. The degree of exactness of a rule is the highest degree \(m\) for which every polynomial of degree up to \(m\) is integrated exactly. A rule with a high degree is accurate for functions that a polynomial approximates well.

Trapezoid. On \(n\) intervals of width \(h = (b - a)/n\) with \(f_i = f(a + ih)\),

\[T_n = h\left[ \tfrac12 f_0 + f_1 + f_2 + \dots + f_{n-1} + \tfrac12 f_n \right], \qquad \int_a^b f\,dx - T_n = -\frac{(b - a)\,h^2}{12}\,f''(\xi)\]

for some \(\xi\) in \([a, b]\), so the error falls as \(1/n^2\). It is exact for straight lines. By the Euler-Maclaurin formula the error is a series in \(h^2, h^4, \dots\) with coefficients that depend on the derivatives of \(f\) at the two ends. If the integrand and its derivatives are negligible at both ends, as a normal density is in the tails, every term vanishes and the trapezoid rule converges faster than any power of \(h\). This is why it is the rule behind the Fourier methods (Transform Methods, Part 1: Characteristic Functions).

Simpson. On an even number \(n\) of intervals,

\[S_n = \frac{h}{3}\left[ f_0 + 4f_1 + 2f_2 + 4f_3 + \dots + 2f_{n-2} + 4f_{n-1} + f_n \right], \qquad \int_a^b f\,dx - S_n = -\frac{(b - a)\,h^4}{180}\,f^{(4)}(\xi)\]

The rule is exact for cubics, and falls as \(1/n^4\). It is a combination of two trapezoid rules, \(S_{2n} = (4\,T_{2n} - T_n)/3\), in which the \(h^2\) terms of the two errors cancel (Richardson extrapolation).

Gaussian quadrature. The nodes and the weights are both free, \(2n\) numbers, and they are chosen together so that the rule is exact for polynomials up to degree \(2n - 1\). The nodes turn out to be the roots of the degree-\(n\) polynomial \(P_n\) that is orthogonal to all lower degrees under the weight \(\omega\), \(\int P_n\,q\,\omega\,dx = 0\) for every polynomial \(q\) of degree below \(n\), and the weights are \(w_i = \int \ell_i(x)\,\omega(x)\,dx\), where \(\ell_i\) is the Lagrange polynomial that is 1 at \(x_i\) and 0 at the other nodes. For a function with \(2n\) continuous derivatives on \([a, b]\) and \(\omega = 1\) (Gauss-Legendre),

\[\int_a^b f\,dx - \sum_i w_i f(x_i) = \frac{(b - a)^{2n+1}\,(n!)^4}{(2n + 1)\,[(2n)!]^3}\,f^{(2n)}(\xi)\]

and the factor in front of the derivative falls faster than exponentially in \(n\). For a function that is analytic the error falls exponentially in \(n\).

  • Gauss-Legendre has \(\omega = 1\) on \([-1, 1]\). For another interval the nodes are mapped by \(z = \tfrac12 (b - a)\,x + \tfrac12 (a + b)\), and \(\int_a^b f\,dz = \tfrac12 (b - a) \sum_i w_i f(z_i)\).

  • Gauss-Hermite has the weight \(e^{-z^2/2}\) on the whole line, whose nodes are the roots of the Hermite polynomial \(He_n\). With the normal density in the weight, \(\int f(z)\,\varphi(z)\,dz \approx \sum_i \hat{w}_i f(z_i)\), with \(\hat{w}_i = w_i/\sqrt{2\pi}\) summing to 1. It is built for integrals against the normal density, so it fits pricing under lognormal models.

  • Gauss-Laguerre has the weight \(e^{-x}\) on a half line.

The kink#

The fast convergence of these rules needs a smooth integrand. The error terms above contain derivatives of \(f\), and at a kink the second derivative does not exist. Write the integrand near the kink as a smooth function plus \(J\,(z - z^*)_+\), where \(J\) is the jump in the slope. The smooth part is integrated to the rule’s usual accuracy. The kink term is not a polynomial of any degree, and a rule with nodes at spacing \(h\) integrates it with an error of order \(J h^2\), whose size depends on where the kink falls between the nodes. A kink inside an interval therefore leaves every composite rule at order \(h^2\), so Simpson’s rule falls back from \(h^4\). For a global rule, such as Gauss-Hermite, the expansion of \((z - z^*)_+\) in orthogonal polynomials has coefficients that fall only like \(k^{-2}\), and the error falls like \(n^{-2}\), in place of exponentially. The sign of the error changes as the kink moves between nodes.

The remedy is to split the integral at the kink: integrate over the region where the option is in the money only, \([z^*, L]\), where the integrand is smooth, and treat the region where it is zero as zero. The kink is then the end of the interval, and Gaussian quadrature recovers its exponential rate. Here the integrand \(e^{\sigma\sqrt{T} z}\,\varphi(z)\) is an entire function, with no singularity anywhere in the complex plane, and the convergence is faster than any fixed exponential in \(n\). For a payoff with several kinks, such as a spread of calls or a barrier, the interval is split at each of them.

Dimension#

In two dimensions, a product rule uses \(n \times n\) points, and in \(d\) dimensions \(n^d\). For two independent normal drivers,

\[\iint f(z_1, z_2)\,\varphi(z_1)\,\varphi(z_2)\,dz_1\,dz_2 \approx \sum_{i=1}^{n} \sum_{j=1}^{n} \hat{w}_i\,\hat{w}_j\,f(z_i, z_j)\]

and correlated drivers are built from independent ones, \(w = \rho z_1 + \sqrt{1 - \rho^2}\,z_2\). With \(N = n^d\) evaluations, a rule whose one-dimensional error is \(n^{-s}\) has an error of \(N^{-s/d}\), and the exponent falls as the dimension rises. The Monte Carlo error is \(\sigma/\sqrt{N}\) whatever \(d\) is. The two cross where \(s/d = \tfrac12\), which is \(d = 2s\): for a payoff with a kink, \(s = 2\), and a product rule has no advantage over simulation beyond about 4 dimensions. For an analytic integrand the product rule keeps an exponential rate in \(n\), and has its advantage for longer, although the number of points needed rises as \(n^d\). This is the curse of dimensionality (Advanced Methods, Part 2: Dimension, Adjoints & Machine Learning). For one to three dimensions, quadrature is usually the better method.

The two-dimensional example is the option to exchange asset 2 for asset 1, with payoff \(\max(S_1 - S_2, 0)\). Its closed form, due to Margrabe, is

\[V = S_1\,\Phi(d_1) - S_2\,\Phi(d_2), \qquad \sigma^2 = \sigma_1^2 + \sigma_2^2 - 2\rho\,\sigma_1\sigma_2, \qquad d_{1,2} = \frac{\ln(S_1/S_2) \pm \tfrac12 \sigma^2 T}{\sigma\sqrt{T}}\]

Its kink is along the curve where \(S_1 = S_2\) in the \((z_1, z_2)\) plane, which a product rule cannot follow, so the kink problem is back and cannot be removed by splitting an interval.

Worked example#

The example computes the benchmark call by four rules with 8 to 256 points, compares quadrature with Monte Carlo for the same number of function evaluations, and then prices a two-asset exchange option, whose closed form (Margrabe) is known, by a two-dimensional Gauss-Hermite rule against Monte Carlo.

Python code
"""Numerical quadrature for a European call, and for an exchange option in two dimensions, against Monte Carlo."""
import numpy as np
from numpy.polynomial.hermite_e import hermegauss
from numpy.polynomial.legendre import leggauss
from scipy.integrate import simpson
from scipy.stats import norm

from common import K, R, S0, SIGMA, T, bs_price

DISC = np.exp(-R * T)


def terminal(z):
    return S0 * np.exp((R - 0.5 * SIGMA**2) * T + SIGMA * np.sqrt(T) * z)


def integrand(z):
    """Discounted call payoff times the standard normal density."""
    return DISC * np.maximum(terminal(z) - K, 0) * norm.pdf(z)


def trapezoid(n, width=8.0):
    z = np.linspace(-width, width, n)
    return np.trapezoid(integrand(z), z)


def simpson_rule(n, width=8.0):
    z = np.linspace(-width, width, n if n % 2 else n + 1)
    return simpson(integrand(z), x=z)


def gauss_hermite(n):
    nodes, weights = hermegauss(n)
    return np.sum(weights * DISC * np.maximum(terminal(nodes) - K, 0)) / np.sqrt(2 * np.pi)


def gauss_legendre_split(n, width=8.0):
    """Gauss-Legendre on the region where the option is in the money only: the kink is the end of the interval."""
    z_star = (np.log(K / S0) - (R - 0.5 * SIGMA**2) * T) / (SIGMA * np.sqrt(T))
    x, w = leggauss(n)
    z = 0.5 * (width - z_star) * x + 0.5 * (width + z_star)
    return 0.5 * (width - z_star) * np.sum(w * integrand(z))


EXCHANGE = dict(s1=100.0, s2=100.0, sigma1=0.20, sigma2=0.30, rho=0.5)


def margrabe(s1, s2, sigma1, sigma2, rho):
    """Closed form for the option to exchange asset 2 for asset 1."""
    sigma = np.sqrt(sigma1**2 + sigma2**2 - 2 * rho * sigma1 * sigma2)
    d1 = (np.log(s1 / s2) + 0.5 * sigma**2 * T) / (sigma * np.sqrt(T))
    return s1 * norm.cdf(d1) - s2 * norm.cdf(d1 - sigma * np.sqrt(T))


def exchange_payoff(z1, z2, s1, s2, sigma1, sigma2, rho):
    w = rho * z1 + np.sqrt(1 - rho**2) * z2
    a = s1 * np.exp((R - 0.5 * sigma1**2) * T + sigma1 * np.sqrt(T) * z1)
    b = s2 * np.exp((R - 0.5 * sigma2**2) * T + sigma2 * np.sqrt(T) * w)
    return DISC * np.maximum(a - b, 0)


def gauss_hermite_2d(n):
    nodes, weights = hermegauss(n)
    z1, z2 = np.meshgrid(nodes, nodes, indexing="ij")
    ww = np.outer(weights, weights) / (2 * np.pi)
    return np.sum(ww * exchange_payoff(z1, z2, **EXCHANGE))


def mc_exchange(n, rng):
    return exchange_payoff(rng.standard_normal(n), rng.standard_normal(n), **EXCHANGE).mean()


if __name__ == "__main__":
    exact = bs_price()
    print(f"One dimension: call = integral of the discounted payoff times the normal density. Black-Scholes {exact:.8f}\n")
    print(f"{'Points':>7} {'Trapezoid':>11} {'Simpson':>11} {'Gauss-Hermite':>14} {'Split Gauss-Legendre':>21}")
    for n in (8, 16, 32, 64, 128, 256):
        print(f"{n:>7} {trapezoid(n) - exact:+11.2e} {simpson_rule(n) - exact:+11.2e} {gauss_hermite(n) - exact:+14.2e} {gauss_legendre_split(n) - exact:+21.2e}")

    rng = np.random.default_rng(8)
    print("\nSame number of function evaluations: root-mean-square error over 100 Monte Carlo runs")
    print(f"{'Points':>7} {'Monte Carlo':>12} {'Gauss-Hermite':>14} {'Split Gauss-Legendre':>21}")
    for n in (16, 64, 256):
        rms = np.sqrt(np.mean([(np.mean(DISC * np.maximum(terminal(rng.standard_normal(n)) - K, 0)) - exact) ** 2 for _ in range(100)]))
        print(f"{n:>7} {rms:12.2e} {abs(gauss_hermite(n) - exact):14.2e} {abs(gauss_legendre_split(n) - exact):21.2e}")

    ex = margrabe(**EXCHANGE)
    print(f"\nTwo dimensions: option to exchange one asset for another, closed form (Margrabe) {ex:.8f}")
    print(f"{'Nodes per axis':>15} {'Evaluations':>12} {'Gauss-Hermite error':>20} {'Monte Carlo RMS error':>22}")
    for n in (8, 16, 32, 64, 128):
        rms = np.sqrt(np.mean([(mc_exchange(n * n, rng) - ex) ** 2 for _ in range(50)]))
        print(f"{n:>15} {n * n:>12} {gauss_hermite_2d(n) - ex:+20.2e} {rms:22.2e}")

The output is:

One dimension: call = integral of the discounted payoff times the normal density. Black-Scholes 9.41340338

 Points   Trapezoid     Simpson  Gauss-Hermite  Split Gauss-Legendre
      8   +3.24e+00   -1.79e+00      +4.11e-01             -2.51e-03
     16   +4.04e-01   +4.26e-01      +1.96e-01             -1.90e-09
     32   +7.92e-02   +6.46e-02      +9.19e-02             -2.43e-13
     64   +1.14e-02   +2.32e-02      +4.08e-02             -2.43e-13
    128   -4.53e-03   +6.53e-03      +1.55e-02             -1.78e-13
    256   +6.37e-04   -1.60e-03      +2.87e-03             -2.68e-13

Same number of function evaluations: root-mean-square error over 100 Monte Carlo runs
 Points  Monte Carlo  Gauss-Hermite  Split Gauss-Legendre
     16     3.75e+00       1.96e-01              1.90e-09
     64     1.54e+00       4.08e-02              2.43e-13
    256     9.92e-01       2.87e-03              2.68e-13

Two dimensions: option to exchange one asset for another, closed form (Margrabe) 10.52431578
 Nodes per axis  Evaluations  Gauss-Hermite error  Monte Carlo RMS error
              8           64            +2.79e-01               1.96e+00
             16          256            +5.77e-02               9.20e-01
             32         1024            -8.49e-04               4.90e-01
             64         4096            -6.65e-04               2.37e-01
            128        16384            +3.44e-04               1.14e-01
Absolute error of the call price against the number of function evaluations for four quadrature rules and Monte Carlo, on logarithmic axes

Reading the output:

  • The kink ruins the standard rules. Simpson’s rule, which is fourth order for a smooth function, has an error of 1.6e-03 with 256 points, like the trapezoid rule’s 6.4e-04. Gauss-Hermite is no better, at 2.9e-03. They all converge at the slow rate that the kink allows, and the errors change sign as the kink falls between points.

  • Splitting at the kink restores the rate. Gauss-Legendre on the in-the-money region has an error of 2.5e-03 with 8 points, 1.9e-09 with 16, and machine precision with 32. Sixteen function evaluations give an error of 1.9e-09, which a hundred million Monte Carlo paths cannot match.

  • Quadrature beats Monte Carlo at equal cost in one dimension, by a very wide margin. With 256 evaluations, the Monte Carlo error is 0.99 and the Gauss-Hermite error 0.003. The split rule is at 3e-13.

  • In two dimensions quadrature still wins, but by less. With 1,024 evaluations the Gauss-Hermite error is 8.5e-04 and the Monte Carlo error 0.49. The error of Gauss-Hermite here does not fall smoothly, because the payoff has a kink along a line in the plane and the rule cannot split at it easily. Monte Carlo falls at its usual \(1/\sqrt{N}\) rate, a factor of 2 for four times the points.

A valuation control view#

  • Quadrature is an excellent independent check on a simulation: for a European payoff in one or two dimensions it gives the price to many digits. A Monte Carlo price that differs from it by more than its standard error is wrong.

  • Look for kinks, jumps and barriers in the integrand: and see that they are handled, for example by splitting the integral. An engine that integrates a payoff with a kink without doing so has an error that no refinement will fix at the expected rate.

  • Check the range of integration: the finite limits must hold essentially all of the probability. For a long maturity or high volatility they need to widen, as for the truncation range of Transform Methods, Part 2: FFT & the COS Method.

  • Know the dimension at which the method stops being practical: a rule with 10 points per axis needs 10,000 points in 4 dimensions and ten billion in 10. A product with many underlyings needs simulation or a low-dimensional approximation.

  • Match the rule to the density: Gauss-Hermite for a normal driver, but a model with fat tails or a jump needs a rule that fits its density, or a transform.

Next, Numerical Methods: Interpolation covers how to fill in between and beyond quotes, which every curve and surface depends on.