Trapezoidal Rule — $2M Options Pricing Failure
Fixed-step trapezoidal rule caused 0.5–1% error in OTM options, resulting in $2M misvaluation.
20+ years shipping performance-critical code where algorithms decide the bill. Drawn from code that ran under real load.
- Numerical integration approximates ∫f(x)dx when no closed form exists, using simple geometric shapes.
- Trapezoidal rule: O(h²) error — simple, works for any function, but needs many intervals for high accuracy.
- Simpson's rule: O(h⁴) error for smooth functions — same number of evaluations, 10⁴ more accurate than trapezoidal.
- Adaptive integration automatically refines intervals where error is high — essential for non-uniform functions.
- In production, prefer scipy.integrate.quad (adaptive Gauss-Kronrod) over hand-rolled methods for reliability.
- Biggest mistake: using a fixed number of intervals for functions with sharp peaks or oscillations.
Many functions have no closed-form integral — you can't solve ∫e^(-x²)dx analytically. Numerical integration approximates the area under a curve by dividing it into simple shapes: trapezoids or parabolic strips. More strips = better approximation. Simpson's rule uses parabolas instead of flat tops, achieving much better accuracy with the same number of evaluations.
The Gaussian normal distribution has no closed-form integral — there is no formula for erf(x). Machine learning models using Gaussian distributions (virtually all of them) require numerical integration constantly. Physics simulations, financial option pricing (Black-Scholes), and engineering stress analysis all reduce to integrals that cannot be solved analytically.
Numerical integration is where theory meets real engineering constraints: function evaluation is often expensive, so minimising the number of evaluations while achieving target precision is the practical problem. Simpson's rule achieves O(h^4) error versus Trapezoidal's O(h^2) — for the same number of function evaluations, you get roughly 10^4 more accuracy. That difference matters when each evaluation takes a second to compute.
Why Numerical Integration Methods Are Not Optional
Numerical integration methods approximate the definite integral of a function when an analytic solution is impossible or impractical. Instead of finding a closed-form antiderivative, they sample the function at discrete points and combine those values using a weighted sum. The core mechanic is replacing a continuous curve with a finite set of geometric shapes — rectangles, trapezoids, or polynomials — whose area can be computed exactly.
Every method introduces error proportional to the step size raised to some power. The Trapezoidal Rule, for example, uses linear interpolation between sample points, yielding O(h²) error for smooth functions. Halving the step size quadruples accuracy — but only if the function's second derivative is bounded. That assumption fails spectacularly near singularities or discontinuities, producing silent garbage.
Use numerical integration when the integrand is expensive to evaluate, comes from experimental data, or has no closed form. It's the backbone of pricing engines, physics simulations, and signal processing pipelines. Choosing the wrong method or step size doesn't just waste cycles — it can misprice a portfolio by millions.
Trapezoidal Rule
Approximate the area as a sum of trapezoids: h/2 × (f(x0) + 2f(x1) + 2f(x2) + ... + 2f(x_{n-1}) + f(xn)). Error: O(h²) per interval, O(h²) total.
The trapezoidal rule is the simplest numerical integration method. It approximates the integral by dividing the region into n strips of equal width h = (b-a)/n and summing the area of each trapezoid. The error term for a single interval is - (h³/12) f''(ξ), meaning the method is exact for linear functions (second derivative zero). For smooth functions, doubling n reduces the error by a factor of 4.
Simpson's Rule
Fit a parabola through each three consecutive points. Requires even n. h/3 × (f(x0) + 4f(x1) + 2f(x2) + 4f(x3) + ... + 4f(x_{n-1}) + f(xn)) Error: O(h⁴) — much better than Trapezoidal O(h²).
Simpson's rule uses quadratic interpolation over pairs of intervals. The pattern "4, 2, 4, 2, ..., 4" alternates coefficients. It is exact for polynomials up to degree 3 (cubic polynomials), because the error term involves the fourth derivative: - (h⁵/90) f^{(4)}(ξ). For smooth functions where the fourth derivative is bounded, Simpson's rule converges incredibly fast — notice with n=10, error is already 1e-7 for sin(x).
Error Analysis
| Method | Error Order | n=10 error (sin) | n=100 error |
|---|---|---|---|
| Trapezoidal | O(h²) | 1.65×10⁻² | 1.64×10⁻⁴ |
| Simpson's | O(h⁴) | 1.07×10⁻⁷ | 1.06×10⁻¹⁵ |
| Gaussian (n=5) | Exact for deg≤9 | ~0 | ~0 |
Simpson's with n=10 is more accurate than Trapezoidal with n=10,000 — same number of function evaluations, 1000x better.
The error analysis tells you more than just convergence rates. The constants matter: for trapezoidal, error ≈ (b-a)h²/12 max|f''|. For Simpson's, error ≈ (b-a)h⁴/180 max|f^{(4)}|. If your function has large second derivative but small fourth derivative, Simpson's wins dramatically. But if your function has huge fourth derivative (e.g., a sharp spike), Simpson's may actually be worse than a well-chosen trapezoidal with refined step near the spike.
Adaptive Integration
Fixed n doesn't work for all functions — some regions need more resolution. Adaptive integration refines intervals where the error is large.
Adaptive Simpson's method: compute the integral over [a,b] using Simpson's rule. Then split into two halves and apply Simpson's on each. The difference between the whole and the sum of halves gives an estimate of the error. If the error is larger than tolerance, recursively split each half. This concentrates evaluations where they're needed.
Romberg Integration
Romberg integration uses Richardson extrapolation on the trapezoidal rule. By computing the trapezoidal rule for a sequence of step sizes (h, h/2, h/4, ...) and combining them, you can eliminate lower-order error terms and achieve very high accuracy with relatively few evaluations.
The method builds a triangular array called Romberg's table. The first column is the trapezoidal rule with decreasing step sizes. Each subsequent column applies Richardson extrapolation to remove error terms of increasing order. For sufficiently smooth functions, Romberg integration can achieve machine epsilon with fewer function evaluations than adaptive Simpson's.
When to Use Which Method
Choosing the right method depends on the integrand's behaviour, accuracy requirements, and computational budget.
High-level guidance: - Trapezoidal rule: use for quick estimates, when function evaluations are very cheap, or when the function is only piecewise smooth and you can manually split intervals. - Simpson's rule: use for smooth, well-behaved functions where you need moderate accuracy (e.g., engineering approximations). It's the workhorse of numerical integration. - Adaptive Simpson's: use when you don't know the function's behaviour a priori — it's robust and widely used in production systems. - Romberg integration: use for extremely smooth functions where you need very high accuracy with minimal evaluations (e.g., reference solutions for benchmarks). - Gaussian quadrature: use when the function is smooth and you can afford to precompute nodes and weights. Gauss-Legendre quadrature with n points integrates exactly polynomials of degree 2n-1. For many applications, n=5 gives machine epsilon.
Production recommendation: Default to scipy.integrate.quad (adaptive Gauss-Kronrod). It handles discontinuities, infinite limits, and oscillatory integrands better than any simple method.
Gauss-Legendre Quadrature: When You Need Bloody Accuracy Without Bloody Intervals
Trapezoidal and Simpson's use evenly spaced points. That's fine if you're teaching undergrads. In production you want maximal accuracy per function evaluation. Gauss-Legendre quadrature picks the optimal abscissas — the roots of Legendre polynomials — and weights them to integrate polynomials of degree 2n-1 exactly with only n points.
Why does that matter? Because every function call to your simulation costs CPU time. Your 3D fluid solver calls this a million times per timestep. Chebyshev nodes help fight Runge's phenomenon, but Gauss-Legendre gives you spectral accuracy for smooth integrands with half the evaluations.
You still need to transform your integration limits [a, b] into [-1, 1]. That's a linear mapping, trivial code. The abscissas and weights are precomputed for given n — grab them from a table or compute on startup. For n=5 you integrate a 9th-degree polynomial exactly. Compare that to Trapezoidal's first-order convergence and weep.
Clenshaw-Curtis: Chebyshev Nodes for the Practical Engineer
Gauss-Legendre is the gold standard, but its nodes are irrational. Clenshaw-Curtis quadrature uses Chebyshev extrema — x_k = cos(kπ/n). That's cheaper to compute and gives you nested grids for adaptive refinement. The weights come from a cosine transform, which you can slam through an FFT.
Why use it? When your integrand has edge singularities or rapid oscillations, Chebyshev clustering at boundaries fights Runge's phenomenon better than Gauss-Legendre's open nodes. Engineers in signal processing and filter design reach for this first because they already have FFT libraries.
You lose a factor of two in polynomial exactness compared to Gauss-Legendre for the same n, but you gain nesting and FFT speed. For n=1024, Clenshaw-Curtis with an O(n log n) FFT beats Gauss-Legendre's O(n^2) node computation cold. The convergence is still exponential for analytic functions — just slightly slower.
Rule of thumb: if you're doing fixed-order quadrature and your function calls are the bottleneck, go Gauss-Legendre. If you're adapting order or already have an FFT, Clenshaw-Curtis wins.
Numerical Integration Failure in Options Pricing Led to $2M Misvaluation
- Never trust a fixed-step integration method for financial pricing without convergence verification.
- Always double the number of intervals and compare results — if they don't agree within expected tolerance, your method is inadequate.
- Use adaptive integration for functions with varying curvature; fixed-step methods are only safe for very smooth, slowly varying functions.
result1 = trapezoidal(f, a, b, n)
result2 = trapezoidal(f, a, b, 2*n)
abs(result1 - result2) / abs(result1)result_simpson = simpsons(f, a, b, n)
abs(result1 - result_simpson)Key takeaways
Common mistakes to avoid
4 patternsUsing fixed-step trapezoidal for oscillatory functions
Forgetting to make n even for Simpson's rule
Setting adaptive tolerance too tight (e.g., 1e-15) for double precision
Integrating across a discontinuity without splitting
Practice These on LeetCode
Interview Questions on This Topic
Why is Simpson's rule more accurate than the Trapezoidal rule for the same number of intervals?
Frequently Asked Questions
20+ years shipping performance-critical code where algorithms decide the bill. Drawn from code that ran under real load.
That's Numerical Analysis. Mark it forged?
6 min read · try the examples if you haven't