☰

Probability Density Function

Probability Density Function (PDF): Definition, Formula and Examples

A clock stops at a random moment during the day. What is the chance that it stopped at exactly 10:00:00? The answer is zero. It sounds odd, but it is true, and it is the key to understanding the probability density function. Time is continuous, so there are endless possible moments, and each single moment has no probability of its own.

What you can ask is the chance that the clock stopped between 6 and 9 o’clock. That is a real, non zero probability, and the probability density function is the tool that gives it to you. It describes how probability is spread over a continuous range, such as time, height, weight, rainfall or waiting time.

This tutorial explains what a PDF is, the two rules it must follow, the formula, how to read it as an area, two worked examples with all the numbers, how it differs from the CDF and the PMF, and how to calculate it in Python.

What is the Probability Density Function?

A probability density function, or PDF, describes the distribution of a continuous random variable. A continuous variable can take any value in a range, such as 1.7 or 1.7001 metres. This is different from a discrete variable, such as the number on a die, which takes separate values.

The PDF is a function, f(x), whose curve shows where the values are most concentrated. A high curve means that values near there are more likely. A low curve means that they are less likely.

probability density function tutorial

Two Rules Every PDF Must Follow

  1. It is never negative. f(x) is 0 or more everywhere.
  2. The total area under the curve is 1. The variable must take some value, so all the probability adds up to 1.

Density is Not Probability

The live page calls the PDF the relative likelihood of a given value. That is close, but it hides an important point. The height of the curve at x is a density, not a probability. It can even be bigger than 1. For example, a variable spread evenly between 0 and 0.5 has a density of 2 everywhere. The area is 2 times 0.5 = 1, which is what must hold.

Why do We Need a Probability Density Function?

For a continuous variable, you cannot list the probability of every value, because there are infinitely many. The PDF lets you find the probability of any range.

Probability density function tutorial

The live page gives the example of the chance of rain in July, using past data that shows rainfall between limits a and b. It says that the aim is to find the nearest exact value. That is not the idea. The aim is to find the probability that rainfall falls in a range, and this is the area under the PDF between the two limits.

  • Weather and climate. The chance that rainfall or temperature falls in a range.
  • Quality control. The chance that a part’s length is within tolerance.
  • Waiting times. The chance that a customer waits between 2 and 5 minutes.
  • Machine learning. Many models describe data with PDFs, such as the normal curve.

The live page also says that PDFs measure the risk of a stock or ETF. In finance, a PDF can model returns, but it is a general tool for any continuous variable, and this page is not investment advice.

Probability Density Function Formula

Probability density function formula

The probability that X lies between a and b is the area under the PDF between a and b:

P(a is at most X, and X is at most b) = the integral of f(x) from a to b

In words: the probability of a range is the area under the curve over that range. The total area is 1, and the probability of any single exact value is 0, because a single point has no width.

The live page shows the formula only as an image. Here are the most common PDFs in text:

Common probability density functions
Distribution PDF f(x) Where it applies
Uniform from a to b 1 divided by (b minus a) a is at most x, and x is at most b
Normal with mean mu and standard deviation sigma (1 divided by (sigma times the square root of 2 times pi)) times e to the power of minus (x minus mu) squared divided by (2 times sigma squared) All x
Exponential with rate r r times e to the power minus r times x x is 0 or more

Probability Density Function Example

Example 1: The Clock That Stops

Probability density function example

The live page: a clock stops at a random time during the day, and the time of stopping is x. The PDF is shown only in an image. If every moment of the day is equally likely, the PDF is uniform on 0 to 24 hours.

  • PDF: f(x) = 1 divided by 24 = 0.0417, for x between 0 and 24, and 0 elsewhere.
  • Check the area: 0.0417 times 24 = 1. The rule holds.
  • Chance the clock stops between 6 and 9: the area is (9 minus 6) times 0.0417 = 3 divided by 24 = 0.125.
  • Chance it stops at exactly 10:00: 0.
  • Chance it stops between 9.99 and 10.01: 0.02 times 0.0417 = 0.000833.
  • Mean = 12 hours. Standard deviation = 24 divided by the square root of 12 = 6.93 hours.

Meaning: a three hour window has a one in eight chance. A single instant has none, and the chance for a tiny window grows with its width. The value 0.0417 is a density, in probability per hour, and not a probability.

Example 2: July Rainfall

The live page describes the rain example without numbers. Here is a version with made up numbers. Suppose July rainfall in a city is normal, with a mean of 200 mm and a standard deviation of 50 mm.

July rainfall example with a normal distribution, mean 200 mm and standard deviation 50 mm
Question Answer How
Density at 200 mm 0.00798 The height of the curve at the peak
Chance of rain between 150 and 250 mm 0.6827 The area within 1 standard deviation
Chance of more than 300 mm 0.0228 The right tail beyond 2 standard deviations
Chance of exactly 200 mm 0 A single point has no area
Chance between 199 and 201 mm 0.016 A narrow strip around the peak

I checked the area between 150 and 250 mm in two ways: by integrating the PDF, 0.6827, and by simulating 100,000 months, 0.6812. They agree.

PDF vs CDF vs PMF

PDF compared with CDF and PMF
Point PDF CDF PMF
For Continuous variables Continuous or discrete Discrete variables
Gives Density at x P(X at most x) P(X = x)
Probability of one value 0 Not applicable Can be above 0
Probability of a range Area under the curve CDF at b minus CDF at a Sum of the PMF
Total Area is 1 Rises from 0 to 1 Sum is 1

The CDF is the running total of the PDF. For the standard normal curve, the PDF at 0 is 0.3989, and the CDF at 1.96 is 0.975, which is the area to the left of 1.96.

Calculate the PDF in Python

I ran the code below, and the output is real.

python
import numpy as np
from scipy import stats
from scipy.integrate import quad

# Clock example: stops at a random time in 24 hours, uniform density 1/24
u = stats.uniform(0, 24)
print("Density f(x):", round(float(u.pdf(10)), 4), " Area over 0 to 24:", round(float(quad(u.pdf, 0, 24)[0]), 4))
print("P(6 < X < 9):", round(float(u.cdf(9) - u.cdf(6)), 4), " P(X = 10 exactly):", 0.0)
print("Mean:", u.mean(), " SD:", round(float(u.std()), 3))
print("Chance of stopping in a tiny interval 9.99 to 10.01:", round(float(u.cdf(10.01) - u.cdf(9.99)), 6))

# Rain example (made up): July rainfall in mm is normal, mean 200, sd 50
r = stats.norm(200, 50)
print("Density at 200 mm:", round(float(r.pdf(200)), 5))
print("P(150 < X < 250):", round(float(r.cdf(250) - r.cdf(150)), 4))
print("P(X > 300):", round(float(r.sf(300)), 4))
print("P(X = 200 exactly):", 0.0, " P(199 < X < 201):", round(float(r.cdf(201) - r.cdf(199)), 4))
print("Area from the pdf by integration, 150 to 250:", round(float(quad(r.pdf, 150, 250)[0]), 4))

# A density can be bigger than 1: uniform from 0 to 0.5
v = stats.uniform(0, 0.5)
print("Uniform 0 to 0.5: density", float(v.pdf(0.25)), " area", round(float(quad(v.pdf, 0, 0.5)[0]), 4))

# PDF vs CDF for a standard normal
z = stats.norm()
print("Standard normal pdf at 0:", round(float(z.pdf(0)), 4), " cdf at 1.96:", round(float(z.cdf(1.96)), 4))

# Simulation check of P(150 < X < 250)
rng = np.random.default_rng(10)
x = rng.normal(200, 50, 100000)
print("Simulated P(150 < X < 250):", round(float(((x > 150) & (x < 250)).mean()), 4))
output
Density f(x): 0.0417  Area over 0 to 24: 1.0
P(6 < X < 9): 0.125  P(X = 10 exactly): 0.0
Mean: 12.0  SD: 6.928
Chance of stopping in a tiny interval 9.99 to 10.01: 0.000833
Density at 200 mm: 0.00798
P(150 < X < 250): 0.6827
P(X > 300): 0.0228
P(X = 200 exactly): 0.0  P(199 < X < 201): 0.016
Area from the pdf by integration, 150 to 250: 0.6827
Uniform 0 to 0.5: density 2.0  area 1.0
Standard normal pdf at 0: 0.3989  cdf at 1.96: 0.975
Simulated P(150 < X < 250): 0.6812

Common Mistakes

Common mistakes and fixes
Mistake Fix
Treating the PDF value as a probability It is a density. Probability is an area
Asking for the probability of one exact value It is 0. Ask for a range
Expecting the density to be at most 1 It can exceed 1. The area must be 1
Forgetting the total area is 1 Check that your function integrates to 1
Using a PDF for a discrete variable Use the PMF
Mixing the PDF and the CDF The CDF is the area up to x
Using a negative function A PDF is never negative
Treating a PDF as investment advice It is a general tool for modelling

Practice Questions

  1. A variable is uniform between 0 and 10. What is the PDF, and what is the chance that it lies between 2 and 5?
  2. What is the probability that a continuous variable equals exactly 3?
  3. A uniform variable on 0 to 0.25 has what density?
  4. What two rules must a PDF follow?

Answers: 1) f(x) = 1 divided by 10 = 0.1, and the chance is 3 times 0.1 = 0.3. 2) 0. 3) 1 divided by 0.25 = 4. 4) It is never negative, and the total area under it is 1. These are my own answers, so please recheck them before publishing.

A probability density function is a map of where a continuous variable is likely to fall. Read it as an area, not as a height: the height is a density, and the area over a range is the probability. Remember that, and everything from the normal curve to a confidence interval becomes clearer.

Popular Tags:

probability density function example probability density function formula Probability Density Function Tutorial