from edumath.probability import expected_value, validate_pmf
values = [0, 1]
probabilities = [0.4, 0.6]
expected_value(values, probabilities), validate_pmf(values, probabilities).message(0.6, 'Correct.')
A random experiment can have outcomes like heads, tails, red card, blue card, or rain tomorrow. A random variable turns those outcomes into numbers. This is powerful because once outcomes are numbers, we can compute averages, spreads, probability tables, and distributions.
After this lesson you should be able to:
edumath, and SymPy to check probability tables.A random variable is a rule that assigns a number to each outcome of a random experiment.
Example: flip one coin. The sample space is
S = {H, T}
Define a random variable X by:
X(H) = 1
X(T) = 0
Now X is the number of heads in one flip.
Random variables are often named with capital letters such as X, Y, or T. A particular value is often written with a lowercase letter, such as X = x.
These three ideas are related but different:
| Idea | Meaning | Example |
|---|---|---|
| Outcome | one result of the experiment | HT from two coin flips |
| Event | a set of outcomes | “exactly one head” = {HT, TH} |
| Random variable | number assigned to outcomes | X = number of heads |
If X counts heads in two flips, then:
X(HH) = 2
X(HT) = 1
X(TH) = 1
X(TT) = 0
A discrete random variable has possible values that can be listed, such as:
Discrete random variables are often described with a table.
A continuous random variable can take values along an interval, such as:
For continuous variables, the probability of one exact value is usually 0. We ask for probabilities over intervals, such as “between 2 and 3 minutes.”
A probability mass function, or PMF, gives probabilities for a discrete random variable.
Example:
x |
0 | 1 |
|---|---|---|
P(X=x) |
0.4 | 0.6 |
This table is valid because:
The cumulative distribution function, or CDF, gives P(X <= x). It adds all probabilities up to a value.
For the PMF above:
P(X <= 0) = 0.4
P(X <= 1) = 1.0
CDFs are useful because many probability questions ask for “at most” or “no more than” a value.
Expected value is the long-run average of a random variable:
E[X] = sum of value * probability
For the table above:
E[X] = 0(0.4) + 1(0.6) = 0.6
A game pays $3 for heads and $0 for tails. The coin has probability 0.5 of heads and 0.5 of tails. Let X be the payout.
| outcome | H | T |
|---|---|---|
X |
3 | 0 |
| probability | 0.5 | 0.5 |
The expected payout is
E[X] = 3(0.5) + 0(0.5) = 1.5
This does not mean one play pays $1.50. It means the long-run average payout is $1.50 per play.
A proposed PMF is:
x |
0 | 1 | 2 |
|---|---|---|---|
P(X=x) |
0.2 | 0.5 | 0.4 |
Check it:
0.2 + 0.5 + 0.4 = 1.1
The probabilities add to more than 1, so this is not a valid PMF.
X be the number of heads in two coin flips. What is X(HT)?{0: 0.2, 1: 0.8} a valid PMF?{0: 0.2, 1: 0.9} a valid PMF?0, 10 with probabilities 0.7, 0.3.X(HT)=1 because there is one head.0(0.7)+10(0.3)=3.Random variables become easier when you say the rule out loud: “This outcome gets this number.”
Guess first. In two coin flips, what number should HH get if X counts heads?
Guided exercise.
Count heads in each outcome:
HH -> 2
HT -> 1
TH -> 1
TT -> 0
The random variable is not the coin flip itself. It is the number assigned to the result.
Checkpoint. Let Y be the number of tails in two coin flips. Find Y(HH), Y(HT), and Y(TT).
Guess first. Is “number of calls in one hour” discrete or continuous?
Guided exercise.
A count is discrete because possible values are separate numbers:
0, 1, 2, 3, ...
A measurement like time or length is usually continuous.
Checkpoint. Classify each as discrete or continuous: shoe size, exact height, number of defective parts, time until a bus arrives.
Guess first. Can probabilities in a PMF add to less than 1?
Guided exercise.
For a valid PMF, probabilities must cover all possible values and add to 1. For
P(X=0)=0.25, P(X=1)=0.25, P(X=2)=0.50
the total is
0.25 + 0.25 + 0.50 = 1
so the PMF is valid.
Checkpoint. Check whether P(X=0)=0.1, P(X=1)=0.2, P(X=2)=0.6 is a valid PMF.
Guess first. If the table says P(X=2)=0.30, does that mean X always equals 2?
Guided exercise.
A probability of 0.30 means value 2 occurs in about 30% of many repetitions, not always.
Checkpoint. If P(X=0)=0.2, P(X=1)=0.5, and P(X=2)=0.3, what is P(X >= 1)?
Guess first. Which is bigger: the value with highest probability or the expected value?
Guided exercise.
For values 0, 5, 10 with probabilities 0.2, 0.5, 0.3:
E[X] = 0(0.2) + 5(0.5) + 10(0.3)
= 0 + 2.5 + 3
= 5.5
Expected value is a weighted average, not necessarily the most likely value.
Checkpoint. Compute expected value for values 1, 2, 3 with probabilities 0.2, 0.5, 0.3.
Y(HH)=0, Y(HT)=1, and Y(TT)=2.0.9, not 1.P(X >= 1)=P(X=1)+P(X=2)=0.5+0.3=0.8.1(0.2)+2(0.5)+3(0.3)=0.2+1.0+0.9=2.1.Use this checkpoint to practice classifying random variables and checking PMFs.
Use edumath to compute expected value.
(0.6, 'Correct.')
Use Python to build a PMF table.
Use SymPy for exact weighted averages.