Skip to main content

Random Variables

A random variable is a function that assigns a number to each outcome of a random experiment. It lets us replace statements about outcomes, such as "the die shows a 20", with statements about numbers, such as X=20X = 20, and then use algebra and calculus to reason about them.

info

This chapter covers

  • sample spaces, events, and the axioms that every probability satisfies,
  • the definition of a random variable and the notation used throughout these notes,
  • the difference between discrete and continuous random variables, and
  • the cumulative distribution function, which describes any random variable.
A running example from Dungeons & Dragons 1
What is Dungeons & Dragons (D&D)?

Dungeons & Dragons (D&D) is a fantasy tabletop role-playing game where players assume the roles of characters embarking on adventures in an imaginary world. The outcomes of actions, such as combat or solving puzzles, are determined by dice rolls, making probability and randomness key aspects of the game.

These notes use scenarios from D&D as running examples, so you don't need to have played to follow them.

Imagine you're playing D&D, leading a group of adventurers in a fierce battle against a dragon. One of the players, a mighty paladin, attempts to land a critical hit with their weapon. To decide whether the attack succeeds, the player rolls a 20-sided die, commonly known as a d20.

A roll of 20 is a critical hit (an extraordinary success), and a roll of 1 is a critical miss (an extraordinary failure). Any other roll is compared against the dragon's armor class, which we'll come back to below.

Try it

Roll the d20 a few times and see how often you get a critical hit.

Sample Spaces and Events​

A random experiment is a process whose result can't be predicted with certainty, such as rolling a die or training a neural network from a random initialization. Each possible result is an outcome ω\omega, and the set of all outcomes is the sample space Ω\Omega. For a single d20 roll,

Ω={1,2,3,…,20}.\Omega = \{1, 2, 3, \dots, 20\}.

An event is a subset of the sample space, that is, a collection of outcomes we may want to ask about. The event "critical hit" is {20}\{20\}. If the dragon has an armor class of 15, the attack hits on a roll of 15 or more, so the event "hit" is {15,16,…,20}\{15, 16, \dots, 20\}.

Probability​

A probability is a function Pr⁡\Pr that assigns a number to each event. Every probability satisfies three axioms.

Pr⁡(A)≥0for every event A,Pr⁡(Ω)=1,Pr⁡(A1∪A2∪⋯ )=Pr⁡(A1)+Pr⁡(A2)+⋯if A1,A2,… are pairwise disjoint.\begin{gather} \Pr(A) \ge 0 \quad \text{for every event } A, \\ \Pr(\Omega) = 1, \\ \Pr(A_1 \cup A_2 \cup \cdots) = \Pr(A_1) + \Pr(A_2) + \cdots \quad \text{if } A_1, A_2, \dots \text{ are pairwise disjoint.} \end{gather}

The first says probabilities are never negative, the second says that some outcome always happens, and the third says that probabilities of events that can't happen together add up. Every other rule follows from these three. Two that we'll use often are the complement rule and the inclusion–exclusion rule,

Pr⁡(Ac)=1−Pr⁡(A),Pr⁡(A∪B)=Pr⁡(A)+Pr⁡(B)−Pr⁡(A∩B).\Pr(A^c) = 1 - \Pr(A), \qquad \Pr(A \cup B) = \Pr(A) + \Pr(B) - \Pr(A \cap B).

When all outcomes are equally likely, as with a fair die, the probability of an event is the fraction of outcomes it contains,

Pr⁡(A)=∣A∣∣Ω∣.\Pr(A) = \frac{|A|}{|\Omega|}.

So for the fair d20, the probability of a critical hit is 1/20=0.051/20 = 0.05, and the probability of hitting armor class 15 is 6/20=0.36/20 = 0.3.

Random Variables​

Formally, a random variable XX is a function from the sample space to the real numbers,

X:Ω→R.X: \Omega \rightarrow \mathbb{R}.

For the d20, the most natural random variable is the number rolled, X(ω)=ωX(\omega) = \omega. But many random variables can live on the same sample space. The indicator of a critical hit,

C(ω)={1if ω=20,0otherwise,C(\omega) = \begin{cases} 1 & \text{if } \omega = 20, \\ 0 & \text{otherwise,} \end{cases}

is another one, and so is the damage dealt, which depends on whether the attack hits.

These notes write random variables with capital letters (XX, YY, CC) and the values they take with lowercase letters (xx, yy). The expression X=xX = x is shorthand for the event {ω∈Ω:X(ω)=x}\{\omega \in \Omega : X(\omega) = x\}, so

Pr⁡(X=20)=Pr⁡({20})=0.05,Pr⁡(X≥15)=Pr⁡({15,…,20})=0.3.\Pr(X = 20) = \Pr(\{20\}) = 0.05, \qquad \Pr(X \ge 15) = \Pr(\{15, \dots, 20\}) = 0.3.
Where this shows up

In reinforcement learning, the state StS_t, action AtA_t and reward RtR_t at each time step are random variables, which is why the RL notes write them with capital letters (see Markov Decision Processes). In machine learning, the target yy is often modeled as a random variable whose distribution depends on the input (see Probabilistic Interpretation).

Discrete and Continuous Random Variables​

A discrete random variable takes values in a countable set. The set can be finite, like the 20 faces of a d20 or the number of heads in 10 coin flips, or infinite but listable, like the number of emails you receive in an hour, which can be any of 0,1,2,…0, 1, 2, \dots. A discrete random variable is described by its probability mass function (PMF),

p(x)=Pr⁡(X=x),p(x) = \Pr(X = x),

which gives the probability of each value.

A continuous random variable takes values in an interval of the real line, such as the time it takes to run a race. There are so many possible values that any single one has probability zero, Pr⁡(X=x)=0\Pr(X = x) = 0, so a PMF would be zero everywhere. Probabilities instead come from areas under a probability density function, which the next chapter on distributions introduces.

Cumulative Distribution Function​

The cumulative distribution function (CDF) of a random variable XX gives the probability that XX is at most xx,

F(x)=Pr⁡(X≤x).F(x) = \Pr(X \le x).

Unlike the PMF, the CDF is defined in the same way for discrete and continuous random variables. It never decreases, approaches 0 as x→−∞x \to -\infty and 1 as x→∞x \to \infty, and gives the probability of any interval,

Pr⁡(a<X≤b)=F(b)−F(a).\Pr(a < X \le b) = F(b) - F(a).

For the d20, F(x)=⌊x⌋/20F(x) = \lfloor x \rfloor / 20 for 0≤x≤200 \le x \le 20, a staircase that climbs by 0.050.05 at each face. The probability of rolling between 11 and 15 is F(15)−F(10)=0.75−0.5=0.25F(15) - F(10) = 0.75 - 0.5 = 0.25, which matches counting the five faces directly.

Recap​

  • A random experiment has a sample space of outcomes, and events are subsets of it.
  • A probability assigns a number to each event and satisfies three axioms, from which every other rule follows.
  • A random variable maps each outcome to a number, and many random variables can share one sample space.
  • Discrete random variables take countably many values and have a PMF, while continuous ones take values in an interval and give each single value probability zero.
  • The CDF F(x)=Pr⁡(X≤x)F(x) = \Pr(X \le x) describes both kinds.

1. A fair d20 is rolled. What is the probability of rolling 18 or higher?

2. Which of these is a discrete random variable?

3. If XX is a continuous random variable, what is Pr⁡(X=2)\Pr(X = 2)?

What's Next​

A chapter on joint and conditional probability, which describes how two random variables relate, is planned to come next. Until then, continue with Probability Distributions, which describes the common ways probability can be spread over the values of a random variable.


Footnotes​

  1. The idea of using D&D to explain statistical concepts came from Professor Marlos C. Machado's RL class (CMPUT 365) at the University of Alberta. ↩