Random Variables, PMF, CDF, and Expectation: From Intuition to Implementation
A complete guide to the building blocks of probability theory with NumPy examples and practice problems
Probability
Statistics
NumPy
Tutorial
Author
Rishabh Mondal
Published
April 2, 2026
Probability Theory Fundamentals
Random Variables, PMF, CDF, and Expectation
Part 1: Random Variables
1.1 Thinking Lens
When you flip a coin, the outcome is “Heads” or “Tails”. When you roll a die, the outcome is a face with dots. These are words and images, not numbers. But mathematics works with numbers.
A random variable is a translator. It takes outcomes from the real world and converts them into numbers that we can compute with.
Think of it like a scoring system:
Coin flip: Heads → 1, Tails → 0
Die roll: Each face → its dot count (1, 2, 3, 4, 5, 6)
Customer arrival: Each customer → time they arrived
The randomness comes from the underlying experiment (the coin flip, the die roll). The “variable” part is the numerical assignment.
A random variable is a function that assigns a real number to each outcome of a random experiment.
1.2 Definition
Formally, a random variable\(X\) is a function:
\[
X: \Omega \rightarrow \mathbb{R}
\]
Where:
\(\Omega\) is the sample space (set of all possible outcomes)
\(\mathbb{R}\) is the set of real numbers
For each outcome \(\omega \in \Omega\), \(X(\omega)\) gives us a real number.
Types of Random Variables:
Type
Values
Example
Discrete
Countable (finite or infinite)
Die roll: {1, 2, 3, 4, 5, 6}
Continuous
Uncountable (any value in an interval)
Height: any value in [0, 300] cm
A random variable maps outcomes from the sample space to real numbers, enabling mathematical analysis of random phenomena.
1.3 Code: Random Variables in NumPy
import numpy as npimport matplotlib.pyplot as pltfrom scipy import statsnp.random.seed(42)# Example 1: Coin flip as a random variable# Sample space: {Heads, Tails}# Random variable X: Heads -> 1, Tails -> 0def coin_flip_rv(n_flips=10):"""Simulate coin flips as random variable values.""" outcomes = np.random.choice(['Heads', 'Tails'], size=n_flips)# Map to numbers X = np.array([1if o =='Heads'else0for o in outcomes])return outcomes, Xoutcomes, X = coin_flip_rv(10)print("Coin Flip Random Variable:")print(f" Outcomes: {outcomes}")print(f" X values: {X}")print(f" Sum (count of heads): {X.sum()}")
Coin Flip Random Variable:
Outcomes: ['Heads' 'Tails' 'Heads' 'Heads' 'Heads' 'Tails' 'Heads' 'Heads' 'Heads'
'Tails']
X values: [1 0 1 1 1 0 1 1 1 0]
Sum (count of heads): 7
# Example 2: Die roll as a random variable# The random variable IS the face valuedef die_roll_rv(n_rolls=10):"""Die roll - the outcome is already a number.""" X = np.random.randint(1, 7, size=n_rolls)return XX_die = die_roll_rv(10)print("\nDie Roll Random Variable:")print(f" X values: {X_die}")print(f" Mean: {X_die.mean():.2f}")
Die Roll Random Variable:
X values: [3 3 3 5 4 3 6 5 2 4]
Mean: 3.80
# Example 3: Sum of two dice# Random variable Y = X1 + X2def two_dice_sum_rv(n_rolls=10):"""Sum of two dice - a function of two random variables.""" X1 = np.random.randint(1, 7, size=n_rolls) X2 = np.random.randint(1, 7, size=n_rolls) Y = X1 + X2return X1, X2, YX1, X2, Y = two_dice_sum_rv(10)print("\nSum of Two Dice:")print(f" Die 1: {X1}")print(f" Die 2: {X2}")print(f" Sum Y: {Y}")print(f" Possible values of Y: {set(range(2, 13))}")
Sum of Two Dice:
Die 1: [6 6 2 4 5 1 4 2 6 5]
Die 2: [4 1 1 3 3 2 4 4 6 6]
Sum Y: [10 7 3 7 8 3 8 6 12 11]
Possible values of Y: {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Key Insight
A random variable is NOT random by itself. The randomness comes from the underlying experiment. The variable is just a systematic way to assign numbers to outcomes.
# Visualize PMFfig, axes = plt.subplots(1, 2, figsize=(12, 4))# Fair die PMFax = axes[0]ax.bar(values, pmf, color='#6366f1', alpha=0.8, edgecolor='white', linewidth=2)ax.axhline(y=1/6, color='red', linestyle='--', label='Theoretical (1/6)')ax.set_xlabel('Value (x)', fontsize=12)ax.set_ylabel('P(X = x)', fontsize=12)ax.set_title('PMF of Fair Die', fontsize=14)ax.set_xticks(range(1, 7))ax.legend()ax.set_ylim(0, 0.25)# Sum of two dice PMFX1 = np.random.randint(1, 7, size=10000)X2 = np.random.randint(1, 7, size=10000)Y = X1 + X2values_y, pmf_y = compute_pmf(Y)ax = axes[1]ax.bar(values_y, pmf_y, color='#ec4899', alpha=0.8, edgecolor='white', linewidth=2)ax.set_xlabel('Value (y)', fontsize=12)ax.set_ylabel('P(Y = y)', fontsize=12)ax.set_title('PMF of Sum of Two Dice', fontsize=14)ax.set_xticks(range(2, 13))plt.tight_layout()plt.show()
# Theoretical PMF for sum of two dicedef theoretical_two_dice_pmf():"""Compute exact PMF for sum of two fair dice.""" pmf = {}for x1 inrange(1, 7):for x2 inrange(1, 7): s = x1 + x2 pmf[s] = pmf.get(s, 0) +1/36return pmftheoretical = theoretical_two_dice_pmf()print("Theoretical PMF for Sum of Two Dice:")print("-"*40)for value insorted(theoretical.keys()): prob = theoretical[value] bar ="█"*int(prob *60)print(f" P(Y = {value:2d}) = {prob:.4f} ({int(prob*36)}/36) {bar}")
When rolling two dice, 7 has the most ways to occur: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1) = 6 ways out of 36. That is why 7 appears most often in board games!
Part 3: Cumulative Distribution Function (CDF)
3.1 Thinking Lens
The PMF tells us the probability of exact values. But often we want to know: what is the probability of getting at most some value?
The CDF answers: “What is the probability that X is less than or equal to x?”
Think of it as accumulating probabilities:
CDF at x = 1: P(X ≤ 1) = P(X = 1)
CDF at x = 2: P(X ≤ 2) = P(X = 1) + P(X = 2)
CDF at x = 3: P(X ≤ 3) = P(X = 1) + P(X = 2) + P(X = 3)
… and so on
The CDF is a “running total” of probabilities.
Key property: CDF always goes from 0 to 1, never decreasing.
3.2 Definition
The Cumulative Distribution Function (CDF) of a random variable \(X\) is:
\[
F_X(x) = P(X \leq x)
\]
Properties of CDF:
Non-decreasing: If \(a < b\), then \(F_X(a) \leq F_X(b)\)
Use PMF when: - You want P(X = specific value) - You are working with discrete distributions - You want to visualize the probability distribution shape
Use CDF when: - You want P(X ≤ x) or P(X > x) or P(a < X ≤ b) - You are comparing distributions (CDFs are easier to compare) - You are generating random samples (inverse CDF method)
Part 4: Expectation (Expected Value)
4.1 Thinking Lens
If you rolled a fair die infinitely many times and averaged the results, what would you get?
The expectation (or expected value) is this theoretical “long-run average”. It is the center of mass of the probability distribution.
Think of it like this:
Place weights on a number line
Each value gets a weight equal to its probability
The balance point (center of mass) is the expected value