Skip to content
Part IA Easter Term

Random Variables

What is a Random Variable?

A random variable XX is a function from the sample space to the real numbers:

X:ΩRX: \Omega \to \mathbb{R}

Formally, it assigns a numerical value to each outcome.

Why Functions?

Outcomes (like “three coin flips”) are often complex or qualitative. Random variables let us work numerically: counting heads, summing dice, measuring time.

Example: Coin Flips

Flip three coins. The outcome is a sequence like {H,T,H}\{H, T, H\}. Define XX = number of heads.

OutcomeXX
HHH3
HHT2
HTH2
HTT1
THH2
THT1
TTH1
TTT0

Discrete vs Continuous Random Variables

Discrete

XX is discrete if it takes values from a finite or countably infinite set.

Examples:

  • Number of heads in 10 flips: {0,1,2,,10}\{0, 1, 2, \ldots, 10\}
  • Rolls until first 6: {1,2,3,}\{1, 2, 3, \ldots\}
  • Number of customers arriving: {0,1,2,}\{0, 1, 2, \ldots\}

Continuous

XX is continuous if it can take any value in an interval.

Examples:

  • Time until failure: [0,)[0, \infty)
  • Temperature: R\mathbb{R}
  • Height: [0,300][0, 300] cm

Indicator Variables

An indicator variable (or Bernoulli variable) for event EE is:

IE={1if E occurs0otherwiseI_E = \begin{cases} 1 & \text{if } E \text{ occurs} \\ 0 & \text{otherwise} \end{cases}

Properties

E[IE]=0P(Ec)+1P(E)=P(E)E[I_E] = 0 \cdot P(E^c) + 1 \cdot P(E) = P(E)

The expected value of an indicator equals the probability of the event.

This is incredibly useful: to compute P(E)P(E), compute E[IE]E[I_E].

Example: Matching Cards

Shuffle a deck. What is the expected number of cards in their original position?

Let IiI_i indicate card ii is in position ii. Then X=i=152IiX = \sum_{i=1}^{52} I_i counts matches.

E[Ii]=P(card i in position i)=152E[I_i] = P(\text{card } i \text{ in position } i) = \frac{1}{52}

By linearity:

E[X]=i=152E[Ii]=52×152=1E[X] = \sum_{i=1}^{52} E[I_i] = 52 \times \frac{1}{52} = 1

On average, exactly one card is in its original position, regardless of deck size.

Types of Random Variables

TypeValuesExample
Indicator{0,1}\{0, 1\}Success/failure
Binary{0,1}\{0, 1\} (or {1,1}\{-1, 1\})Yes/no outcomes
Count{0,1,2,}\{0, 1, 2, \ldots\}Number of arrivals
ContinuousReal intervalTime, weight

Random Variables in Programming

Think of a random variable like a variable in a programming language:

  • It has a type (discrete or continuous)
  • It has a range of possible values
  • It has a distribution describing likelihood of each value
  • It can be sampled to produce a concrete value

Example in Context

# X is a random variable
# - Type: discrete
# - Range: {1, 2, 3, 4, 5, 6}
# - Distribution: uniform
import random
x = random.randint(1, 6)  # Sample a value

Functions of Random Variables

If XX is a random variable and gg is a function, then Y=g(X)Y = g(X) is also a random variable.

Example: If XX is the roll of a die, then Y=2X+1Y = 2X + 1 takes values {3,5,7,9,11,13}\{3, 5, 7, 9, 11, 13\}.

Summary

ConceptDefinition
Random variableFunction ΩR\Omega \to \mathbb{R}
Discrete RVTakes countably many values
Continuous RVTakes values in an interval
IndicatorIE={1E occurs0otherwiseI_E = \begin{cases} 1 & E \text{ occurs} \\ 0 & \text{otherwise} \end{cases}