TExcelTAD
Home › Probability Theory › Density Functions

Density Functions

For continuous random variables, probability is spread out like mass along a line. The density function f(x)f(x) describes how thickly it is spread: probabilities are areas under ff.

Definition

A random variable XX has density ff if f(x)≥0f(x) \ge 0, ∫−∞∞f(x) dx=1\int_{-\infty}^{\infty} f(x)\,dx = 1, and for every interval

Pr⁡[a≤X≤b]=∫abf(x) dx\Pr[a \le X \le b] = \int_a^b f(x)\,dx

A density is not a probability: f(x)f(x) can exceed 1 (the uniform density on [0,0.5][0, 0.5] is 2). What is true is that for a small Δx\Delta x, Pr⁡[x≤X≤x+Δx]≈f(x) Δx\Pr[x \le X \le x + \Delta x] \approx f(x)\,\Delta x: density times width.

Examples

Distribution Density Typical use
Uniform on [a,b][a, b] 1b−a\frac{1}{b - a} on [a,b][a, b] a random point, rounding errors
Exponential, rate λ\lambda λe−λx\lambda e^{-\lambda x} for x≥0x \ge 0 waiting times, lifetimes
Normal 12πe−x2/2\frac{1}{\sqrt{2\pi}} e^{-x^2/2} (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 X=U2X = U^2 (with UU uniform on [0,1][0, 1]) are compared with its density, which is f(x)=12xf(x) = \frac{1}{2\sqrt{x}} on (0,1](0, 1] (derived in the next lesson):

python · try it yourself
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 λ\lambda, Pr⁡[X>t]=∫t∞λe−λx dx=e−λt\Pr[X > t] = \int_t^\infty \lambda e^{-\lambda x}\,dx = e^{-\lambda t}. If a component fails at rate λ=0.1\lambda = 0.1 per year, the probability that it lasts more than 5 years is e−0.5≈0.607e^{-0.5} \approx 0.607. SymPy can do these integrals exactly:

python · try it yourself
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 ff from data, and the loss of a probabilistic model is often the negative log-density of the data.

Exercises

Exercise 1: Find the constant

f(x)=cx2f(x) = c x^2 on [0,3][0, 3] is a density. Find cc and Pr⁡[X≤1]\Pr[X \le 1].

Answer

c=1/9c = 1/9; Pr⁡[X≤1]=1/27\Pr[X \le 1] = 1/27.

Solution

∫03cx2 dx=9c=1\int_0^3 c x^2\,dx = 9c = 1. Then ∫01x2/9 dx=1/27\int_0^1 x^2/9\,dx = 1/27.

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 [0,0.1][0, 0.1] equals 10 there.

Solution

Its area is 10⋅0.1=110 \cdot 0.1 = 1. Probabilities are areas; a density only needs to be non-negative with total area 1.

Exercise 3: Lifetimes (code)

Lifetimes are exponential with rate 0.10.1 per year. Simulate 100,000 lifetimes with rng.expovariate(0.1) and estimate Pr⁡[X>5]\Pr[X > 5] and Pr⁡[3≤X≤7]\Pr[3 \le X \le 7]; compare with exact values.

Answer

e−0.5≈0.6065e^{-0.5} \approx 0.6065 and e−0.3−e−0.7≈0.2442e^{-0.3} - e^{-0.7} \approx 0.2442.

Solution
python · try it yourself
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 ff, P(a≤X≤b)P(a \le X \le b) equals…

  1. f(b)−f(a)f(b) - f(a)
  2. ∫abf(x) dx\int_a^b f(x)\,dx
  3. f(a)⋅(b−a)f(a) \cdot (b - a)
  4. f′(b)f'(b)

Can a density function take values greater than 1?

  1. No, never
  2. Yes, as long as the total area is 1
  3. Only for discrete variables
  4. Only at one point