Velamuvi AIAcademy
Probability

Lesson 10 of 16 · Random Variables

Continuous Random Variables

Continuous random variables: why any exact value has probability 0, how a density function turns probability into area, the uniform distribution and the CDF.

  • 8 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • Explain what makes a random variable continuous
  • Explain why P(X = x) is 0 for any single value of a continuous random variable
  • Use a probability density function (PDF) to find probabilities as areas
  • Work with the uniform distribution
  • Use the CDF to find the probability of a range

Measurements, not counts

Waiting times, heights, temperatures and model scores can take any value in a range, not just whole numbers. A wait might be 3 minutes, or 3.1, or 3.14159 minutes.

That creates a puzzle. Between 0 and 10 there are infinitely many possible values. If each had a positive probability, the total would be more than 1. So for a continuous random variable, the probability of any single exact value is 0.

Try it yourselfPython
import random

random.seed(6)
waits = [random.uniform(0, 10) for _ in range(100_000)]    # a wait of 0 to 10 minutes, any value equally likely

print("first three waits:", [round(w, 6) for w in waits[:3]])
print(f"share between 2 and 5 minutes: {sum(2 <= w <= 5 for w in waits) / len(waits):.3f}")
print(f"waits of exactly 3 minutes:    {sum(w == 3.0 for w in waits)}")

In 100,000 random waits, not one is exactly 3.0, yet about 30% fall between 2 and 5 minutes. For continuous random variables, only ranges have probabilities.

Probability as area

Instead of a PMF, a continuous random variable has a density.

The simplest density is flat. A wait that's equally likely to be anywhere between 0 and 10 minutes has a uniform distribution: its density is a flat line at height 1/10, so the whole area is 10 × 1/10 = 1. Move the sliders to choose a range and see its probability:

024681000.050.10.15
uniform density, 0 to 10
area = 0.3
P(a ≤ X ≤ b) is the shaded area: (b − a) × 1/10. From 2 to 5 that's 0.3, matching the simulation.

Density is not probability

The height of a density is not a probability, and it can be bigger than 1. Shrink the range of the uniform distribution below and watch the density rise above 1, while the area stays exactly 1:

0123401234
uniform density, 0 to hi
area = 1
A uniform distribution from 0 to 0.25 has density 4 everywhere in that range. The probability, the area, is still 1.

Densities don't have to be flat. If X is the larger of two random numbers between 0 and 1, big values are more likely than small ones, and its density is the line f(x) = 2x. To find a probability, find an area. Here the area is calculated with thin rectangles, as in the calculus course, and checked by simulation:

Try it yourselfPython
import random

def area(f, a, b, n=100_000):
    """Area under f from a to b, using n thin midpoint rectangles."""
    width = (b - a) / n
    return sum(f(a + (i + 0.5) * width) for i in range(n)) * width

# X = the larger of two random numbers between 0 and 1. Its density is f(x) = 2x, for x from 0 to 1.
f = lambda x: 2 * x

print(f"total area under f: {area(f, 0, 1):.4f}")
print(f"P(X <= 0.5) by area: {area(f, 0, 0.5):.4f}")

random.seed(3)
samples = [max(random.random(), random.random()) for _ in range(100_000)]
print(f"P(X <= 0.5) by simulation: {sum(x <= 0.5 for x in samples) / len(samples):.4f}")

The continuous CDF

The CDF works just as before: F(x) = P(X ≤ x), which for a continuous random variable is the area to the left of x. Then any range is a subtraction:

P(a ≤ X ≤ b) = F(b) − F(a)

For the uniform wait from 0 to 10, F(x) = x/10, so P(2 ≤ X ≤ 5) = 0.5 − 0.2 = 0.3. And because single values have probability 0, P(X < 5) and P(X ≤ 5) are the same for continuous random variables, unlike in lesson 9.

Common mistakes

Practice exercise

A bus arrives at a uniformly random time in the next 15 minutes. Complete prob_between, then find the chance of waiting more than 10 minutes, and of waiting between 3 and 6 minutes.

Your solutionPython
lo, hi = 0, 15      # the bus comes at a uniformly random time in the next 15 minutes

def prob_between(a, b):
    """P(a <= wait <= b) for this uniform wait."""
    return 0.0      # replace: the area of a rectangle under the flat density

# Print P(wait > 10) and P(3 <= wait <= 6), each rounded to 3 decimals, on one line.
Knowledge check

X is uniform between 0 and 20. What is P(5 ≤ X ≤ 9)?

Summary

  • A continuous random variable can take any value in a range, and every exact value has probability 0.
  • A probability density function gives probabilities as areas under its curve; the total area is 1.
  • Density heights are not probabilities and can be greater than 1.
  • The uniform distribution has a flat density: 1 ÷ (upper − lower).
  • The CDF gives P(X ≤ x), and P(a ≤ X ≤ b) = F(b) − F(a).

Next: expected value.

Finished reading? Tick it off to track your progress.