A number for each outcome
Lesson 5.1.1 described an experiment by its sample space (omega), the set of all its outcomes. Often what matters is a number that each outcome produces: a count, a total, a score. A random variable is that number.
Toss a coin twice. The sample space is , four equally likely outcomes. Let be the number of heads.
| outcome | ||||
|---|---|---|---|---|
| , the number of heads | 2 | 1 | 1 | 0 |
| , heads minus tails | 2 | 0 | 0 |
Rule: Random variable
A rule that gives a real number to each outcome: a function (lesson 1.1.1) from to the real numbers . A random variable is written with a capital letter, ; a value it can take is written in lower case, .
The same experiment can carry many random variables. above is heads minus tails, and it takes the values , 0 and 2. A random variable has the units of what it counts or measures: heads, points on a die, dollars won.
Each statement about is an event, a set of outcomes. The event is , and is . So the probability rules of module 5.1 apply to them.
A random variable is discrete when its values can be listed: finitely many, or without end. This module is about discrete random variables. Module 5.4 treats continuous ones, which can take any value in an interval.
Try it
The cell lists the four outcomes and works out and for each one. Then it adds the probability of each outcome to the value it produces.
Fraction keeps the probabilities exact, so prints as 1/2.
The probability mass function
The probability mass function (pmf) of gives the probability of each value. To find , add the probabilities of all the outcomes that produce .
For two tosses, comes from two outcomes, and . So , while .
Rule: Probability mass function
The sum runs over every value of . The events for different values never overlap and together cover , so their probabilities add up to 1 (the axioms of lesson 5.1.1). At any number that is not a value, .
A pmf can be given by a formula. for is a pmf: the four values are positive and add up to . The same formula for is not one, because the five values add up to .
The larger of two dice
Roll two fair dice and let be the larger of the two numbers; on a double, it is that number. The 36 outcomes are equally likely. Each cell shows , with the first die down the side and the second across the top.
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 1 | 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | 2 | 2 | 3 | 4 | 5 | 6 |
| 3 | 3 | 3 | 3 | 4 | 5 | 6 |
| 4 | 4 | 4 | 4 | 4 | 5 | 6 |
| 5 | 5 | 5 | 5 | 5 | 5 | 6 |
| 6 | 6 | 6 | 6 | 6 | 6 | 6 |
Count the cells for each value. in 1 cell, in 3, and so on up to in 11.
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| cells | 1 | 3 | 5 | 7 | 9 | 11 |
The counts are the odd numbers, so . They add up to 36, so the pmf adds up to 1. The next section finds the same formula by algebra.
Try it
The cell counts the 36 cells for each value of . Fraction reduces to , so the count is printed too.
Then it rolls two dice 100,000 times with a fixed seed and prints the share of rolls for each value. In this seeded run every share is within 0.003 of the exact pmf.
The distribution function
The distribution function of , written , gives : the probability that is at most . It is also called the cumulative distribution function, or CDF. It is defined at every real number , not only at the values of .
For , add the pmf from the left. , , and so on.
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 1 | 4 | 9 | 16 | 25 | 36 |
The totals are squares, and algebra says why. means both dice show at most . Each does so with probability , and the dice are independent, so by lesson 5.1.3.
Rule: Distribution function
The sum runs over the values of up to . never decreases, is 0 to the left of every value and 1 to the right of them all. It is flat between values and jumps at each value by .
Between two values nothing is added, so the graph of is a staircase. , because cannot be 2.5. Below the smallest value , so ; from 6 on, .
At each value the staircase takes the upper level. counts itself, because includes the end. On the graph, a filled dot marks the level at the value and an open dot the level just before it.
The pmf of and its distribution function. The two highlighted bars, , are the rise of the staircase from to : both give .
From back to the pmf
The jump at each value is its probability. For a random variable whose values are whole numbers:
For , the algebra gives . That is the formula the counting found.
Try it
np.cumsum adds up the pmf from the left: the partial sums of lesson 1.5.2. Multiplied by 36, they are the squares of 1 to 6.
np.diff takes the jumps back. With a 0 in front, it returns the pmf.
Probabilities of intervals
counts every value up to . Subtract , everything up to , and what is left are the values above and up to .
Rule: Probability of an interval
The left end is left out and the right end is kept. The second line is the complement rule of lesson 5.1.1: is "not ".
For , . The bars at 3 and 4 give the same, . And , the chance that at least one die shows 5 or 6.
Which ends are included
For a discrete random variable, and give different answers: the end can carry probability. When the values are whole numbers, move to the neighbouring whole number.
| probability | from | for |
|---|---|---|
For a continuous random variable (module 5.4) a single value has probability 0, and the ends stop mattering.
Try it
rv_discrete from scipy.stats turns a list of values and their probabilities into a distribution. Its .pmf, .cdf and .sf give , and .
The third line gives and in 36ths, as in the table. The last line counts the simulated rolls with . In this seeded run the share is 0.3325, against the exact .
Lesson 5.2.2 meets the first named distributions, whose pmfs are formulas, and scipy.stats has them ready-made with the same .pmf, .cdf and .sf. Lesson 5.3.1 uses the pmf to define the average value of .
Common mistakes
- is the rule; is a value. Write , the probability that the random variable equals 3, not .
- Count every outcome that gives the value. comes from three outcomes, , and , not two.
- A pmf adds up to 1. If the values add up to anything else, a value is missing or a probability is wrong.
- is defined between values too. It is flat there: .
- Watch the ends. , not , and leaves out.
- is . is , which leaves out .
Random variables, PMFs and CDFs
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A random variable gives a number to each outcome. Statements about it, such as or , are events.
- Its pmf is : never negative, and adding up to 1 over the values.
- Its distribution function is a staircase, defined at every : flat between values, jumping by at each value .
- and . For whole-number values, , and the ends of an interval matter.