Density Functions
For continuous random variables, probability is spread out like mass along a line. The density function describes how thickly it is spread: probabilities are areas under .
Definition
A random variable has density if , , and for every interval
A density is not a probability: can exceed 1 (the uniform density on is 2). What is true is that for a small , : density times width.
Examples
| Distribution | Density | Typical use |
|---|---|---|
| Uniform on | on | a random point, rounding errors |
| Exponential, rate | for | waiting times, lifetimes |
| Normal | (standard) | sums of many small effects |
Histograms approach the density
Draw many samples and plot a histogram scaled so its total area is 1. As the sample grows and the bins shrink, the histogram approaches the density. Here samples of (with uniform on ) are compared with its density, which is on (derived in the next lesson):
import random
import math
rng = random.Random(1)
n = 100_000
xs = [rng.random() ** 2 for _ in range(n)]
bins = 10
counts = [0] * bins
for x in xs:
counts[min(int(x * bins), bins - 1)] += 1
for i, c in enumerate(counts):
a, b = i / bins, (i + 1) / bins
hist = c / n / (b - a) # height = fraction / width
exact = (math.sqrt(b) - math.sqrt(a)) / (b - a) # average of f over the bin
print(f"[{a:.1f},{b:.1f}) histogram {hist:.3f} density average {exact:.3f}")[0.0,0.1) histogram 3.155 density average 3.162
[0.1,0.2) histogram 1.314 density average 1.310
[0.2,0.3) histogram 1.019 density average 1.005
[0.3,0.4) histogram 0.850 density average 0.847
[0.4,0.5) histogram 0.733 density average 0.747
[0.5,0.6) histogram 0.668 density average 0.675
[0.6,0.7) histogram 0.610 density average 0.621
[0.7,0.8) histogram 0.582 density average 0.578
[0.8,0.9) histogram 0.547 density average 0.543
[0.9,1.0) histogram 0.522 density average 0.513
The density is unbounded near 0 (squaring pushes small numbers even closer to 0), yet its total area is 1.
Computing probabilities by integration
For the exponential density with rate , . If a component fails at rate per year, the probability that it lasts more than 5 years is . SymPy can do these integrals exactly:
from sympy import symbols, exp, integrate, oo, Rational, sqrt, pi, N
x = symbols("x", real=True)
lam = Rational(1, 10)
f = lam * exp(-lam * x)
print("total area:", integrate(f, (x, 0, oo)))
print("P(X > 5):", integrate(f, (x, 5, oo)), "=", N(integrate(f, (x, 5, oo)), 4))
phi = exp(-x ** 2 / 2) / sqrt(2 * pi)
print("normal total:", integrate(phi, (x, -oo, oo)))
print("P(-1 <= Z <= 1):", N(integrate(phi, (x, -1, 1)), 4))total area: 1
P(X > 5): exp(-1/2) = 0.6065
normal total: 1
P(-1 <= Z <= 1): 0.6827
Where densities come from
A density is a modelling choice. Uniform densities come from symmetry, exponential ones from "memoryless" waiting (Uniform and Exponential), normal ones from adding up many independent effects (the CLT). The Statistics course shows how to check a density against data.
In software
Kernel density estimation (KDE) smooths a histogram into an estimated density; it is behind violin plots and many anomaly detectors. In machine learning, density estimation is the task of learning from data, and the loss of a probabilistic model is often the negative log-density of the data.
Exercises
Exercise 1: Find the constant
on is a density. Find and .
Answer
; .
Solution
. Then .
Exercise 2: Density above 1
Give a density that takes the value 10 somewhere, and explain why that is allowed.
Answer
The uniform density on equals 10 there.
Solution
Its area is . Probabilities are areas; a density only needs to be non-negative with total area 1.
Exercise 3: Lifetimes (code)
Lifetimes are exponential with rate per year. Simulate 100,000 lifetimes with rng.expovariate(0.1) and estimate and ; compare with exact values.
Answer
and .
Solution
import math
import random
rng = random.Random(5)
xs = [rng.expovariate(0.1) for _ in range(100_000)]
print(round(sum(x > 5 for x in xs) / len(xs), 4), round(math.exp(-0.5), 4))
print(round(sum(3 <= x <= 7 for x in xs) / len(xs), 4), round(math.exp(-0.3) - math.exp(-0.7), 4))0.6044 0.6065
0.2464 0.2442
Test yourself
For a continuous random variable with density , equals…
Can a density function take values greater than 1?
- No, never
- Yes, as long as the total area is 1
- Only for discrete variables
- Only at one point