21. Probability densities: integrals as probability

📖 Reading · 10 min
💡 Every code box below is live — edit it and hit Run.

Every application in this module has interpreted \int f as a physical quantity — area, volume, length, work, mass, average. This closing lesson gives it one more meaning, arguably the most consequential: a probability. The mechanics are calculus you already know; only the interpretation is new.

What makes a function a density

A probability density function (PDF) f(x) describes a continuous random quantity X — a measurement that could land anywhere in a range, like a height, a wait time, or a measurement error. It must satisfy two conditions:

f(x)\ge0\text{ for all }x,\qquad\int_{-\infty}^{\infty}f(x)\,dx=1

Nonnegative, because probability can't be negative; total area exactly 1, because X must land somewhere. That second condition is frequently an improper integral (§4.10) — many useful densities, like the exponential one below, are defined on an infinite domain, and it's the same "does the tail integrate to a finite number" question §4.10 already trained you to check.

Crucially, f(x) itself is not a probability — it can exceed 1 (a narrow, tall density is perfectly valid, as long as the area is 1). What has meaning is area under the curve over a range:

P(a\le X\le b)=\int_a^bf(x)\,dx

The probability that X lands in [a,b] is exactly the area under the density curve there — nothing more than §5.0's area-under-a-curve, applied to a function that happens to model likelihood instead of geometry.

Expected value: probability's center of mass

The expected value (mean) of X is

\mu=E[X]=\int_{-\infty}^{\infty}x\,f(x)\,dx

This is exactly §5.5's center-of-mass formula, $\bar x=\int x\rho(x)\,dx$, with the density f(x) playing the role of \rho(x) and the "total mass" denominator equal to 1 automatically, since \int f=1. A probability distribution is a mass distribution with total mass 1, and its expected value is its centroid — the same weighted average, the same moment integral, just relabeled.

Variance, which measures spread around that center, extends the analogy one step further:

\sigma^2=\text{Var}(X)=\int_{-\infty}^{\infty}(x-\mu)^2f(x)\,dx

— the average squared distance from the centroid, structurally the same "weighted average of a squared quantity" that produced \bar y in §5.5's lamina centroid.

Two standard densities

Uniform on [a,b]: every outcome in the interval is equally likely, f(x)=\frac1{b-a} — constant, so \int_a^b\frac1{b-a}dx=1 automatically, and E[X]=\frac{a+b}2, the interval's midpoint, exactly as you'd expect for a flat density.

Exponential, modelling wait times (time until the next event, at constant rate \lambda):

f(x)=\lambda e^{-\lambda x},\qquad x\ge0

\int_0^\infty\lambda e^{-\lambda x}dx=\Big[-e^{-\lambda x}\Big]_0^\infty=(0)-(-1)=1

using the same infinite-tail limit from §4.10. Its expected value, E[X]=\int_0^\infty x\lambda e^{-\lambda x}dx=\frac1\lambda, needs integration by parts (§4.6) — a wait-time density built entirely from tools this module already has.

Doing it in Python

A triangular density f(x)=\frac x2 on [0,2] — verifying it's a valid density, then computing a probability and the expected value:

import sympy as sp

x = sp.Symbol('x')
f = x / 2

total = sp.integrate(f, (x, 0, 2))
print(f"total area (must be 1) = {total}")

P = sp.integrate(f, (x, 0, 1))
print(f"P(0 <= X <= 1) = {P}")

E = sp.integrate(x * f, (x, 0, 2))
print(f"E[X] = {E}")

The exponential density, its infinite-tail normalization, and its mean — both improper integrals, both handled the §4.10 way:

import sympy as sp

x, lam = sp.symbols('x lambda', positive=True)
f = lam * sp.exp(-lam * x)

total = sp.integrate(f, (x, 0, sp.oo))
print(f"total area (symbolic, any lambda > 0) = {total}")

E = sp.integrate(x * f, (x, 0, sp.oo))
print(f"E[X] (symbolic) = {sp.simplify(E)}")

# concrete: lambda = 2 (average wait 0.5)
f2 = f.subs(lam, 2)
E2 = sp.integrate(x * f2, (x, 0, sp.oo))
print(f"lambda=2: E[X] = {E2}")

Confirming the center-of-mass analogy directly — expected value computed via the probability formula and via the §5.5 centroid formula give the same number, because they're the same integral:

import sympy as sp

x = sp.Symbol('x')
f = x / 2       # this density IS a "density" in the §5.5 sense too

mass = sp.integrate(f, (x, 0, 2))          # = 1, as any valid PDF must be
moment = sp.integrate(x * f, (x, 0, 2))
centroid_style = moment / mass
probability_style = sp.integrate(x * f, (x, 0, 2))   # E[X] directly

print(f"total mass (should be 1)         = {mass}")
print(f"moment / mass  (centroid formula) = {centroid_style}")
print(f"E[X]           (probability formula) = {probability_style}")
print(f"identical: {centroid_style == probability_style}")

Worked example

A density is given by f(x)=\frac x2 on [0,2] (and 0 elsewhere). Verify it's a valid density, find P(0\le X\le1), and find E[X].

Validity: f(x)\ge0 on [0,2] since x\ge0 there, and

\int_0^2\frac x2dx=\left[\frac{x^2}4\right]_0^2=1\quad\checkmark

Probability:

P(0\le X\le1)=\int_0^1\frac x2dx=\left[\frac{x^2}4\right]_0^1=\boxed{\frac14}

Expected value:

E[X]=\int_0^2x\cdot\frac x2dx=\frac12\int_0^2x^2dx=\frac12\left[\frac{x^3}3\right]_0^2=\frac12\cdot\frac83=\boxed{\frac43}

Sanity check. P(0\le X\le1)=\frac14 is well under \frac12 — reasonable, since this density is higher near x=2 than near x=0 (it's rising linearly), so less than half the probability mass sits in the lower half of the interval [0,2]. And E[X]=\frac43\approx1.33 sits past the interval's midpoint of 1, consistent with the density leaning toward larger x — exactly the same "bulges toward x=2, so the centroid sits past the midpoint" reasoning used for the region under y=x^2 back in §5.5. ✓

Your turn

1. Verify that f(x)=\frac13 on [1,4] is a valid density (it's uniform), and find E[X] — check that it matches the interval's midpoint.

2. For the triangular density f(x)=\frac x2 on [0,2] from the worked example, find P(1\le X\le2), and check that it plus P(0\le X\le1) sum to 1.

3. A density is proposed as f(x)=cx^2 on [0,3] for some constant c. Find the value of c that makes this a valid density.

Solutions

1. \int_1^4\frac13dx=\frac13(4-1)=1\ \checkmark.

E[X]=\int_1^4x\cdot\frac13dx=\frac13\left[\frac{x^2}2\right]_1^4=\frac13\left(8-\frac12\right)=\frac13\cdot\frac{15}2=\boxed{\frac52}

— exactly \frac{1+4}2, the midpoint, matching the general uniform-density result from the concept section.

2.

P(1\le X\le2)=\int_1^2\frac x2dx=\left[\frac{x^2}4\right]_1^2=1-\frac14=\boxed{\frac34}

Check: \frac14+\frac34=1 — the two pieces of [0,2] account for all the probability, as they must, since P(0\le X\le2)=\int_0^2f=1 by validity.

3. Solve \int_0^3cx^2dx=1:

c\left[\frac{x^3}3\right]_0^3=c\cdot9=1\ \Longrightarrow\ c=\boxed{\frac19}

This is the same "solve for the constant that normalizes the total to 1" move behind every named density — uniform, exponential, or otherwise.

Check yourself in code

For the density f(x)=\frac x2 on [0,2]: compute the total area (should be 1), P(0\le X\le1), and E[X].

Print exactly this:

total area = 1
P(0 <= X <= 1) = 1/4
E[X] = 4/3
import sympy as sp

x = sp.Symbol('x')
f = x / 2

total = sp.integrate(f, (x, 0, 2))
print("total area = ...")

P = sp.integrate(f, (x, 0, 1))
print("P(0 <= X <= 1) = ...")

E = sp.integrate(x * f, (x, 0, 2))
print("E[X] = ...")
import sympy as sp

x = sp.Symbol('x')
f = x / 2

total = sp.integrate(f, (x, 0, 2))
print(f"total area = {total}")

P = sp.integrate(f, (x, 0, 1))
print(f"P(0 <= X <= 1) = {P}")

E = sp.integrate(x * f, (x, 0, 2))
print(f"E[X] = {E}")

A probability density is a nonnegative function with total area 1, and probability itself is nothing but area under it — P(a\le X\le b)=\int_a^bf — which makes every technique in Modules 4 and 5 immediately applicable, including §4.10's improper integrals whenever a density's domain runs to infinity. Expected value, E[X]=\int xf(x)\,dx, is §5.5's center-of-mass formula with a new name: a distribution's mean is its centroid, and variance is the same "average squared distance" idea that gave a lamina its \bar y.

That closes this module's tour of what a definite integral can compute — area, volume, length, work, mass, average, and probability, all built from the same slice-multiply-sum-limit pattern. Next: curves that trace out shapes a single function y=f(x) can't — described instead by a parameter, or by angle and radius.