Search

Search papers and lessons

Probability Density Function

Level 2 · 9 min read

A probability density function gives each value of a continuous variable a height, and the probability of an interval is the area under the curve over that interval.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Sign in to see your progress

TL;DR

A continuous random variable can take any value in a range, such as a height or a waiting time. For such a variable, the probability of hitting one exact value is zero, so we describe it with a density instead: a function f(x)f(x) whose height tells you how concentrated the probability is near xx. The probability of landing in an interval is the area under ff over that interval.

Where you'll see it: in every model of a continuous quantity, from the bell curve of measurement error to the waiting-time models in queueing and the noise distributions inside generative models.

Intuition

Density is probability per unit width

A histogram of heights is a row of bars. Each bar's area is the fraction of people in that bin. Make the bins narrower and more numerous, and the staircase of bar tops smooths into a curve. That curve is the density. Its height at a point is not a probability. It is a probability per unit of xx, the same way a mass density is mass per unit of volume.

Physically: a thin metal rod has a density in kilograms per metre. The mass of a short piece is density times length. No single point of the rod has mass, but a piece of it does. A probability density works the same way. A point has zero width, so it holds zero probability. A small interval of width dxdx holds about f(x) dxf(x)\,dx.

This is also why the density can exceed 1. A density of 5 at some point just means that, locally, probability is packed five times tighter than one unit per unit width. What must equal 1 is the total area.

Why we need it

For a die roll the outcomes are discrete, and each face has a probability you can list. For a height or a waiting time, there are uncountably many possible values. Assigning each one a positive probability is impossible, since they would add up to more than 1. The density sidesteps this. It assigns zero to every point and a meaningful area to every interval, and the area is what we measure.

01234567800.10.20.30.40.50.6x (minutes)density f(x)
  • exponential, rate 0.5
The density is a curve, not a list of probabilities. The probability of waiting between 1 and 2 minutes is the area under the curve between those two points.

The math

Definition

Definition: Probability density function

A function ff is a probability density function for a continuous random variable XX if f(x)≥0f(x) \geq 0 for every xx, and for every interval [a,b][a, b]

P(a≤X≤b)=∫abf(x) dx,P(a \leq X \leq b) = \int_a^b f(x)\,dx,

where the total area is one:

∫−∞∞f(x) dx=1.\int_{-\infty}^{\infty} f(x)\,dx = 1.

Here xx is a value the variable can take, and f(x)f(x) is the density at that value. The integral is the definite integral from aa to bb, which adds up tiny slices f(x) dxf(x)\,dx of area. The two conditions say that ff is never negative and that the probabilities over the whole line add to 1.

Because a single point has no width, the integral from aa to aa is zero:

P(X=a)=∫aaf(x) dx=0.P(X = a) = \int_a^a f(x)\,dx = 0.

So for a continuous variable, P(a≤X≤b)P(a \leq X \leq b), P(a<X<b)P(a \lt X \lt b) and P(a<X≤b)P(a \lt X \leq b) are all the same number. The endpoints do not matter.

The cumulative distribution function

The probability of being at or below xx is the cumulative distribution function:

F(x)=P(X≤x)=∫−∞xf(t) dt.F(x) = P(X \leq x) = \int_{-\infty}^{x} f(t)\,dt.

The density is the rate at which FF grows. By the fundamental theorem of calculus, wherever ff is continuous,

ddxF(x)=f(x).\frac{d}{dx} F(x) = f(x).

This gives a second way to find ff from FF: differentiate. It also explains why P(a≤X≤b)=F(b)−F(a)P(a \leq X \leq b) = F(b) - F(a), which is often the easiest way to compute an interval probability by hand.

Expectation and spread

Averages of a continuous variable replace sums with integrals. The expected value of g(X)g(X) is

E[g(X)]=∫−∞∞g(x) f(x) dx.\mathbb{E}[g(X)] = \int_{-\infty}^{\infty} g(x)\, f(x)\,dx.

Taking g(x)=xg(x) = x gives the mean μ=E[X]\mu = \mathbb{E}[X], and g(x)=(x−μ)2g(x) = (x - \mu)^2 gives the variance σ2=E[(X−μ)2]\sigma^2 = \mathbb{E}[(X-\mu)^2]. These are the same expectations and mean and variance you know from discrete variables, with the sum swapped for an integral.

Worked examples

Example
A uniform density on [0, 2]

Let XX be uniform on the interval from 0 to 2. Its density is constant on that interval and zero outside it:

f(x)={120≤x≤20otherwise.f(x) = \begin{cases} \tfrac{1}{2} & 0 \leq x \leq 2 \\ 0 & \text{otherwise.} \end{cases}

Question 1: is ff a valid density?

  1. It is never negative.
  2. The area is the base times the height: (2−0)×12=1(2 - 0) \times \tfrac{1}{2} = 1. Yes.

Question 2: what is P(0.5≤X≤1.5)P(0.5 \leq X \leq 1.5)?

  1. Integrate over the interval: ∫0.51.512 dx=12(1.5−0.5)=0.5\int_{0.5}^{1.5} \tfrac{1}{2}\,dx = \tfrac{1}{2}(1.5 - 0.5) = 0.5.
  2. Half the probability sits in the middle half of the range, which matches the picture of a flat density.

Question 3: what is P(X=1)P(X = 1)?

  1. It is a single point, so the integral from 1 to 1 is 0. Even though f(1)=12f(1) = \tfrac{1}{2} is positive, the probability of exactly 1 is zero.
Example
A density above 1

Let YY be uniform on [0,0.5][0, 0.5], so f(y)=2f(y) = 2 on that interval and zero elsewhere.

  1. Check the total area: (0.5−0)×2=1(0.5 - 0) \times 2 = 1. It is a valid density.
  2. Its height is 2, which is bigger than 1. That is fine, because a density is probability per unit of yy, and the interval is short.
  3. P(0≤Y≤0.25)=0.25×2=0.5P(0 \leq Y \leq 0.25) = 0.25 \times 2 = 0.5. The left half of the interval holds half the probability, as it must for a flat density.
Example
Waiting for a bus

Suppose the waiting time TT, in minutes, follows the exponential density f(t)=λe−λtf(t) = \lambda e^{-\lambda t} for t≥0t \geq 0, with rate λ=0.5\lambda = 0.5 per minute. The average wait is 1/λ=21/\lambda = 2 minutes.

Question 1: what is P(T>3)P(T \gt 3)?

  1. First find the cumulative function: F(t)=∫0tλe−λs ds=1−e−λtF(t) = \int_0^t \lambda e^{-\lambda s}\,ds = 1 - e^{-\lambda t}. Then use the complement: P(T>3)=1−F(3)=e−0.5×3=e−1.5P(T \gt 3) = 1 - F(3) = e^{-0.5 \times 3} = e^{-1.5}.
  2. e−1.5≈0.2231e^{-1.5} \approx 0.2231. About a 22% chance of waiting more than three minutes.

Question 2: what is P(1≤T≤2)P(1 \leq T \leq 2)?

  1. Subtract the cumulative values: F(2)−F(1)=(1−e−1)−(1−e−0.5)=e−0.5−e−1F(2) - F(1) = (1 - e^{-1}) - (1 - e^{-0.5}) = e^{-0.5} - e^{-1}.
  2. e−0.5≈0.6065e^{-0.5} \approx 0.6065 and e−1≈0.3679e^{-1} \approx 0.3679, so the answer is about 0.23870.2387.
  3. Interpretation: roughly a quarter of waits fall in that one-minute window, which is about what you would expect for a distribution whose mean is 2 minutes.

Tradeoffs and failure modes

A density is the right tool for continuous quantities and the wrong tool for counts. Most mistakes come from mixing the two up or from reading the height as a probability.

SituationProblemBetter choice
Reading f(x)f(x) as the probability of xxGives a number that can exceed 1 and has no meaning as a probabilityIntegrate ff over an interval, or use F(x)F(x)
Counting discrete outcomes such as dice or defectsThere is no density for a countable set, and integrals do not applyA probability mass function, with sums
Asking for the probability of an exact valueThe answer is always 0 for a continuous variableAsk for an interval, such as P(a≤X≤b)P(a \leq X \leq b)
Using a formula outside its supportAn exponential density is 00 for t<0t \lt 0; integrating the formula λe−λt\lambda e^{-\lambda t} over negative tt gives wrong areasCheck the domain of ff before integrating
Estimating a density from data with a histogramThe shape depends heavily on bin widthKernel density estimation, with a bandwidth you choose and check
Assuming a density exists for every random variableSome mixtures of discrete and continuous parts have no densityWork with the cumulative distribution FF, which always exists

The most common error in practice is the first row. A density reported as a probability above 1 is a sign that the integral was never taken.

History

The idea grew out of the eighteenth and nineteenth century work on measurement errors. Pierre-Simon Laplace and Carl Friedrich Gauss used continuous curves to describe errors in astronomical observations in the early 1800s, and those curves are densities in the modern sense. The formal framework came much later. In 1933 Andrey Kolmogorov put probability on a measure-theoretic footing, and in that setting a density is a derivative of one measure with respect to another. The density you use in practice is the special case where the measure is area under a curve.

It caught on because it turned a hard question about single points into an easy question about areas. That lets an engineer, a statistician and a machine learning researcher use the same tools, integrals, for very different continuous problems.

Check yourself

QuestionWhat does the area under a probability density function between aa and bb represent?Show answer

It is the probability that the random variable falls between aa and bb. Probabilities for a continuous variable are areas under the density, not heights of the density.

QuestionLet XX have density f(x)=3x2f(x) = 3x^2 on [0,1][0, 1] and zero elsewhere. What is P(X≤0.5)P(X \leq 0.5)?Show answer

P(X≤0.5)=∫00.53x2 dx=[x3]00.5=0.125P(X \leq 0.5) = \int_0^{0.5} 3x^2\,dx = \left[x^3\right]_0^{0.5} = 0.125. First check that ff is valid: ∫013x2 dx=1\int_0^1 3x^2\,dx = 1. So the answer is one eighth.

QuestionIf a continuous random variable has f(2)=0.4f(2) = 0.4, why is P(X=2)P(X = 2) still zero, even though the density at 2 is positive?Show answer

The density is probability per unit width, not probability at a point. The probability of an exact value is the integral over an interval of zero width, and that integral is 0. The density at 2 says how much probability sits in a small interval around 2, roughly 0.40.4 times the width of that interval, and that width shrinks to zero.

QuestionWaiting times at a service desk follow an exponential density with rate 0.10.1 per minute. What is the probability that a customer waits more than 10 minutes?Show answer

Using P(T>t)=e−λtP(T \gt t) = e^{-\lambda t} with λ=0.1\lambda = 0.1 and t=10t = 10, the answer is e−1≈0.368e^{-1} \approx 0.368. About 37% of customers wait longer than ten minutes.

Sources