Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 57 ยท Random Variables

Random Variables and Probability Mass Functions

Understand random variables, discrete versus continuous, the probability mass function and how to build, check and simulate a discrete distribution, with Bernoulli and binomial previews.

So far you have worked with events such as "heads" or "sum is 7". Real problems want numbers: how many defects, what total, how long. A random variable attaches a number to each outcome of a random experiment, so that you can use algebra on chance. It is the central object of probability theory and the bridge to data.

What is a random variable?

A random variable (usually a capital letter like X) is a function from the sample space to the real numbers. It does not "vary randomly" by itself: the experiment is random and the variable reads off a number.

Flip a coin three times and let X = the number of heads. The outcomes HHH, HHT, ... map to numbers 0 to 3:

Outcome HHH HHT, HTH, THH HTT, THT, TTH TTT
X 3 2 1 0

Lowercase x stands for a particular value. P(X = 2) is the probability that the variable takes the value 2: 3 outcomes out of 8, so 3/8.

Discrete and continuous

  • A discrete random variable takes countable values: the number of heads, goals, customers per hour, or the result of a die.
  • A continuous random variable takes any value in an interval: height, time, temperature. Individual values have probability zero; only ranges have positive probability. See continuous random variables.

This lesson covers the discrete case.

The probability mass function

The probability mass function (PMF) lists the probability of each possible value:

p(x) = P(X = x)

A valid PMF satisfies two conditions: every p(x) โ‰ฅ 0, and the probabilities add up to 1.

For the three flips: p(0) = 1/8, p(1) = 3/8, p(2) = 3/8, p(3) = 1/8. They sum to 8/8.

Python Build a PMF by enumeration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Plot a PMF

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Checking and using a PMF

Suppose a shop records how many items a customer buys: x = 1, 2, 3, 4 with probabilities 0.4, 0.3, 0.2, 0.1. Probabilities of events are sums of PMF values: P(X โ‰ฅ 3) = 0.2 + 0.1 = 0.3; P(X < 3) = 0.4 + 0.3 = 0.7 (also 1 - 0.3).

Python Validate and query a PMF

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Simulating a random variable

A PMF is a recipe for generating random numbers. Sampling many times and comparing the relative frequencies with the PMF is the empirical check that the model behaves as stated.

Python Simulate from a PMF

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two famous PMFs

Bernoulli. One trial with two outcomes: success (1) with probability p, failure (0) with probability 1 - p. Examples: a coin flip, a customer clicking an ad.

Binomial. The number of successes X in n independent Bernoulli trials, each with success probability p:

P(X = k) = C(n, k) * p^k * (1 - p)^(n - k), for k = 0, 1, ..., n.

The three-flip example is binomial with n = 3 and p = 0.5: P(X = 2) = C(3, 2) * 0.5^3 = 3/8. The coefficient C(n, k) counts the arrangements (counting lesson), and p^k (1-p)^(n-k) is the probability of each one. The Common Distributions chapter explores these in depth.

Python Binomial PMF from the formula

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Functions of a random variable

If X is a random variable, so is Y = 2X + 1 or Y = X^2. To get the PMF of Y, map each value and add probabilities of values that land on the same result. For a fair die roll X, the variable Y = X mod 2 takes the value 1 (odd rolls: 1, 3, 5) and 0 (even rolls), each with probability 1/2.

flowchart LR
    E["Random experiment"] --> O["Outcome"]
    O --> X["Random variable X<br/>assigns a number"]
    X --> P["PMF p(x)<br/>probability of each value"]

The random variable turns outcomes into numbers; the PMF says how likely each number is.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is a valid PMF over x = 0, 1, 2?

    Only 0.2 + 0.5 + 0.3 gives 1 with no negative values.
  2. 2. A discrete random variable has P(1)=0.2, P(2)=0.5, P(3)=0.3. What is P(X >= 2)?

    0.5 + 0.3 = 0.8.
  3. 3. Which of these is a discrete random variable?

    A count has countable values; the others are measured on a continuous scale.
  4. 4. A coin lands heads with p = 0.5. X = heads in 4 flips. P(X = 2) is...

    C(4, 2) * 0.5^4 = 6/16 = 0.375.

Summary

  • A random variable assigns a number to each outcome; capital X is the variable, lowercase x a value.
  • Discrete variables have countable values; continuous ones fill intervals.
  • A PMF gives P(X = x) for each value, with non-negative entries that sum to 1.
  • Event probabilities come from adding PMF values.
  • Bernoulli and binomial variables model counts of successes.

Next: Expected Value and Variance