Skip to content
AI360Xpert
Beta

Numerical Integration

Estimating integrals without antiderivatives: weighted sums of function values that converge to the area. Most ML integrals end here, not in closed form.

On x squared from 0 to 1 the trapezoid overshoots at 0.375 with straight tops while Simpson rides the curve through the midpoint and lands on 0.3333
On x squared from 0 to 1 the trapezoid overshoots at 0.375 with straight tops while Simpson rides the curve through the midpoint and lands on 0.3333

Why Does This Exist?

Most integrals in ML have no closed form: Bayesian posteriors, expected losses over continuous noise, partition functions. Analytic integration, taught in integration and area, handles the textbook cases; everything else needs numerical integration (quadrature): approximate ∫abf\int_a^b f by a weighted sum ∑iwif(xi)\sum_i w_i f(x_i) of function values at chosen points.

The design space is one trade: where to put the points and how to weight them. Trapezoids join values with straight lines (cheap, robust). Simpson fits parabolas (exact for cubics and below). Gaussian quadrature optimises both points and weights (maximum exactness per evaluation). Monte Carlo scatters points randomly (dimension-proof, slow 1/N1/\sqrt{N} convergence). Expectations over distributions connect in Monte Carlo methods.

The one idea you need first

A Riemann sum slices [a,b][a, b] into panels and totals rectangle areas: the integral is its limit as panels thin. Quadrature rules are smarter panels: trapezoid averages the two endpoint heights per panel, Simpson weights endpoint-midpoint-endpoint as 11-44-11 per double panel. Same limit, far fewer panels.

Think of It Like This

Surveying a curved field with straight fences

A field's boundary curves, but your measuring tape runs straight between posts. Plant posts at intervals and join them with straight fence runs (trapezoid): each run cuts or overshoots the true boundary, and the errors shrink as posts multiply.

Simpson upgrades the fence to flexible rails bent through three posts at a time, hugging the curve. Gaussian quadrature hires the surveyor to choose post positions optimally for the terrain instead of spacing them evenly. Monte Carlo throws darts and counts landings: crude per dart, unbeatable when the field has a hundred dimensions and fences stop working.

How It Actually Works

On [0,1][0, 1] with f(x)=x2f(x) = x^2 and two panels (h=0.5h = 0.5, points 0,0.5,10, 0.5, 1):

  • Trapezoid: h⋅(f0/2+f1+f2/2)=0.5⋅(0+0.25+0.5)=0.375h \cdot (f_0/2 + f_1 + f_2/2) = 0.5 \cdot (0 + 0.25 + 0.5) = 0.375. Error O(h2)O(h^2): overshoots because straight tops sit above the convex curve.
  • Simpson: (f0+4f1+f2)/6=(0+1+1)/6=1/3≈0.3333(f_0 + 4f_1 + f_2)/6 = (0 + 1 + 1)/6 = 1/3 \approx 0.3333. Exact: parabolas reproduce quadratics, so the only error is rounding.

Same three function values, 0.375 versus 0.3333 against truth 1/31/3. The weights did all the work. Gaussian quadrature with nn points integrates degree-(2n−1)(2n-1) polynomials exactly; Monte Carlo's 1/N1/\sqrt{N} rate ignores dimension, which is why hundred-dimensional expectations sample rather than grid.

Worked example

Integrate f(x)=x2f(x) = x^2 on [0,1][0, 1] (truth ∫01x2dx=1/3\int_0^1 x^2 dx = 1/3). The trapezoid computation above gives 0.3750.375, off by 0.04170.0417. Halve hh to 0.250.25 (points 0,0.25,0.5,0.75,10, 0.25, 0.5, 0.75, 1): 0.25⋅(0/2+0.0625+0.25+0.5625+1/2)=0.25⋅1.375=0.343750.25 \cdot (0/2 + 0.0625 + 0.25 + 0.5625 + 1/2) = 0.25 \cdot 1.375 = 0.34375. Error falls 0.0417→0.01040.0417 \to 0.0104, a factor of 4 for halved hh: the O(h2)O(h^2) rate made visible. Simpson needed no refinement at all.

Code

The point is the check: implement both rules and confirm the error ratio on refinement.

def trapezoid(f: list[float], h: float) -> float:    return h * (f[0] / 2 + sum(f[1:-1]) + f[-1] / 2)

def simpson(f0: float, f1: float, f2: float, h: float) -> float:    return h / 3 * (f0 + 4 * f1 + f2)  # one double panel

vals = [0.0, 0.25, 1.0]  # x squared at 0, 0.5, 1print(trapezoid(vals, 0.5))  # -> 0.375print(round(simpson(0.0, 0.25, 1.0, 0.5), 4))  # -> 0.3333

Watch Out For

Pushing Newton-Cotes to high order

Simpson (order 2) behaves; order-8 Newton-Cotes on evenly spaced points oscillates wildly between nodes (Runge's phenomenon) with exploding weights that amplify function noise. High accuracy comes from composite low-order rules on more panels, or Gaussian points, never from cranking one panel's degree.

Gridding a high-dimensional integral

A 10-point grid in 20 dimensions needs 102010^{20} evaluations: the curse in its purest form. Quadrature grids die past 4 or 5 dimensions. Switch to Monte Carlo (or quasi-Monte Carlo, or variational bounds) when dimensions pile up, and distrust any grid-based estimate that "worked in 2D".

The Quick Version

  • Quadrature replaces integrals with weighted sums of function values at chosen points.
  • Trapezoid on x2x^2 gives 0.375; Simpson gives exactly 1/31/3 from the same three values.
  • Halving trapezoid panels quarters the error: the O(h2)O(h^2) rate in action.
  • Gaussian quadrature maximises exactness per evaluation; Monte Carlo ignores dimension at 1/N1/\sqrt{N}.
  • Composite low-order rules beat single high-order ones; grids die in high dimensions.