Skip to content
AI360XpertBeta

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.

An integral is the area trapped under a curve, approximated by rectangles that get thinner until the staircase becomes the curve itself
An integral is the area trapped under a curve, approximated by rectangles that get thinner until the staircase becomes the curve itself

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 11. 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 11. 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 6060 km per h for two hours it is easy: 120120 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 ff from aa to bb. Chop into nn slices of width Δx\Delta x, freeze ff on each slice and make rectangles. The total is a Riemann sum:

∑i=1nf(xi) Δx\sum_{i=1}^{n} f(x_i)\, \Delta x

Height times width, totalled. Thinner slices converge to the definite integral:

∫abf(x) dx\int_a^b f(x)\, dx

The sign is a picture of the sum. The ∫\int is an stretched S for sum, f(x)f(x) is height, dxdx is thin width. Two properties get used constantly: additivity ∫ac=∫ab+∫bc\int_a^c = \int_a^b + \int_b^c, and area below the axis counts negative.

The antiderivative, and the theorem that connects them

An antiderivative FF of ff is any function with F′=fF' = f. Since ddxx3/3=x2\frac{d}{dx}x^3/3 = x^2, x3/3x^3/3 is an antiderivative of x2x^2.

The fundamental theorem says area is the change in any antiderivative:

∫abf(x) dx=F(b)−F(a)\int_a^b f(x)\, dx = F(b) - F(a)

Find a function whose slope is your curve, subtract its endpoint values. Infinitely many rectangles collapse to one subtraction. Constants cancel, so the +C+C never touches a definite answer.

The rules you actually need

Three cover most ML uses. Powers: ∫xndx=xn+1/(n+1)+C\int x^n dx = x^{n+1}/(n+1) + C for n≠−1n \neq -1. Exponential: ∫exdx=ex+C\int e^x dx = e^x + C, which keeps Gaussians tractable. Reciprocal: ∫1/x dx=ln⁡∣x∣+C\int 1/x\, dx = \ln\lvert x \rvert + C, 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 e−x2e^{-x^2} 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 1/N1/\sqrt{N} with no dimension term, so it is the only option in high dimensions and why sampling runs Bayesian ML.

Worked example

Integrate f(x)=x2f(x) = x^2 from 00 to 33.

Antiderivative F(x)=x3/3F(x) = x^3/3. Check: ddxx3/3=x2\frac{d}{dx}x^3/3 = x^2. Then:

∫03x2 dx=273−0=9\int_0^3 x^2\, dx = \frac{27}{3} - 0 = 9

Nine square units. Check against six right-edge slices of width 0.50.5. Heights 0.250.25, 11, 2.252.25, 44, 6.256.25 and 99 total 22.7522.75, times 0.50.5 gives 11.37511.375.

That overshoots 99 because on a rising curve each right edge is the tallest point, so every box pokes above the curve. Width 0.250.25 drops the estimate near 10.1610.16, width 0.010.01 gives 9.079.07. It converges slowly, error proportional to width, which is why plain right sums are never used for real work.

The diagram shades f(x)=x2/2f(x) = x^2/2 and shows 4.54.5, exactly half of 99, 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 approx

Exact is 99 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 nn 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 100100 points in 22D, 101010^{10} in 1010D and 1010010^{100} in 100100D, beyond atoms in the observable universe. A 100100D latent space is small today.

Monte Carlo error is 1/N1/\sqrt{N} with no dimension term. A thousand samples give similar accuracy in 100100D as in 22D. It converges slowly, 100100 times the samples for 1010 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 ∫abf=F(b)−F(a)\int_a^b f = F(b) - F(a). Infinite boxes collapse to one subtraction.
  • Integration undoes differentiation, which is why calculus is one subject.
  • Three rules cover most ML needs: powers, exe^x and 1/x1/x giving ln⁡∣x∣\ln\lvert x \rvert.
  • Most integrals have no closed form. The Gaussian provably does not, so norm.cdf is numerical.
  • Grid quadrature costs kdk^d and fails past a few dimensions. Monte Carlo is 1/N1/\sqrt{N} in any dimension.