Integration and Area Under a Curve
Adding up infinitely many infinitely thin slices to get a total — and the surprising fact that doing so is just differentiation run backwards.
Why Does This Exist?
Every continuous probability you use is an integral. The bell curve means nothing until you learn that area under it between two points is the chance of landing there, and total area is exactly . AUC spells it out: area under the curve. The hard term that makes variational autoencoders tricky is an integral. Expected values over continuous variables are integrals.
So this page exists because continuous probability needs it. Probability distributions says a density integrates to . This is the page that makes that sentence mean something.
There is a second reason, and it is practical. Most integrals you meet in ML have no closed form, and recognising one is the skill. If you can spot the integral, see why it resists solving and reach for sampling, you can read a VAE paper. If not, you cannot.
Think of It Like This
A car with a broken odometer
Your odometer is dead but the speedometer works. How far did you travel.
At steady km per h for two hours it is easy: km, a rectangle two wide and sixty tall.
Now speed varies constantly. Cheat a little: glance each minute and pretend speed held for that minute. Sixty small rectangles give a decent total. Check each second and it gets better. Thinner slices cost less pretence.
An integral is where that estimate lands as slices turn infinitely thin. Speed is the derivative of distance, so integrating speed must recover distance. That round trip is the fundamental theorem, not a coincidence.
How It Actually Works
Two ideas that turn out to be one idea.
Integration as accumulated area
Take from to . Chop into slices of width , freeze on each slice and make rectangles. The total is a Riemann sum:
Height times width, totalled. Thinner slices converge to the definite integral:
The sign is a picture of the sum. The is an stretched S for sum, is height, is thin width. Two properties get used constantly: additivity , and area below the axis counts negative.
The antiderivative, and the theorem that connects them
An antiderivative of is any function with . Since , is an antiderivative of .
The fundamental theorem says area is the change in any antiderivative:
Find a function whose slope is your curve, subtract its endpoint values. Infinitely many rectangles collapse to one subtraction. Constants cancel, so the never touches a definite answer.
The rules you actually need
Three cover most ML uses. Powers: for . Exponential: , which keeps Gaussians tractable. Reciprocal: , where logs come from.
For several variables integrate one at a time while holding the rest fixed, the same freeze move behind derivatives and partial derivatives. A double integral over a region is an integral of an integral.
The honest part: most integrals cannot be solved
Differentiation is mechanical. Chain and product rules crank out any standard derivative.
Integration is not. Many ordinary functions have no elementary antiderivative, and is the famous one. No formula gives bell-curve area between two points. This is proven, not missing knowledge, which is why tables existed and why norm.cdf is numerical.
That shapes practice more than any technique:
- Quadrature: clever Riemann placement. Great in one or two dimensions, hopeless in a thousand.
- Monte Carlo: random samples averaged. Error falls as with no dimension term, so it is the only option in high dimensions and why sampling runs Bayesian ML.
Worked example
Integrate from to .
Antiderivative . Check: . Then:
Nine square units. Check against six right-edge slices of width . Heights , , , , and total , times gives .
That overshoots because on a rising curve each right edge is the tallest point, so every box pokes above the curve. Width drops the estimate near , width gives . It converges slowly, error proportional to width, which is why plain right sums are never used for real work.
The diagram shades and shows , exactly half of , same integral with the function halved.
Code
import numpy as np
def riemann_right(f, a: float, b: float, n: int) -> float: dx = (b - a) / n x = a + dx * np.arange(1, n + 1) return float(np.sum(f(x) * dx))
f = lambda x: x**2print(round(riemann_right(f, 0, 3, 6), 4)) # -> 11.375print(round(riemann_right(f, 0, 3, 300), 4)) # -> 9.0451print(round(riemann_right(f, 0, 3, 30000), 4)) # -> 9.0005rng = np.random.default_rng(0)xs = rng.uniform(0, 3, size=100000)print(round(float(np.mean(f(xs)) * 3), 2)) # -> 9.0 approxExact is and both routes approach it. Grids die in high dimensions while Monte Carlo only needs samples.
Watch Out For
Shrinking the step size past the point where it helps
Finer slices cut truncation error but double the terms you add, and each float addition rounds. Sum ten million values and round-off accumulates while each term shrinks toward nothing against a large running total. Past some the answer gets worse.
Use a better rule and a better sum instead: trapezoid or Simpson for faster convergence, math.fsum or pairwise summation to tame accumulation. scipy.integrate.quad does both and reports an error estimate worth reading.
Reaching for quadrature in high dimensions
Grids die exponentially. Ten slices per axis is points in D, in D and in D, beyond atoms in the observable universe. A D latent space is small today.
Monte Carlo error is with no dimension term. A thousand samples give similar accuracy in D as in D. It converges slowly, times the samples for times the accuracy, but it converges at all. When you meet a latent or posterior integral, ask what you can sample, not which grid to use.
The Quick Version
- A definite integral is the limit of a Riemann sum: heights times widths as widths go to zero.
- It is signed, below axis subtracts, and additive over adjacent intervals.
- An antiderivative is a function whose derivative is your function.
- The fundamental theorem says . Infinite boxes collapse to one subtraction.
- Integration undoes differentiation, which is why calculus is one subject.
- Three rules cover most ML needs: powers, and giving .
- Most integrals have no closed form. The Gaussian provably does not, so
norm.cdfis numerical. - Grid quadrature costs and fails past a few dimensions. Monte Carlo is in any dimension.