Everything so far has been about events, which either happen or do not. Most questions worth asking concern a quantity instead: how many defective items, how long a wait, how much money. This lesson attaches numbers to outcomes, defines the average of such a number, and proves one property of that average that does more work than any other result in the subject.
A number attached to every outcome
A random variable is a function from the sample space to the real numbers. It assigns to each outcome a value . That is all it is, and the name is doubly misleading: it is not random, being a fixed function, and it is not a variable, being a function. The randomness lives in which outcome occurs, and merely reports a number about it.
Roll two dice, so is the ordered pairs, and let be the total. Then , and is defined on every outcome with no ambiguity. Once exists, statements about it become events in the old sense: "" names the set of six pairs that total seven, and "" names a set of six pairs, so both have probabilities by the rules already established.
A variable taking values in a finite or countable set is discrete, which covers everything in this lesson and the next three. Its behaviour is captured completely by the probability mass function
which is non-negative and sums to over all possible values, since the events partition . For the two-dice total, the mass function runs across the values to , and those eleven numbers sum to .
The mass function throws away the sample space, and that is its point. Two completely different experiments with the same mass function are interchangeable for every purpose that mentions only . From here on the underlying is rarely written down.
The average of a random variable
Suppose the experiment is repeated times and takes the value on about of them. The arithmetic mean of the observed values is then
with cancelling. That expression is taken as the definition of the expectation, also called the expected value or the mean:
It is a weighted average of the possible values, weighted by their probabilities, and it is a fixed number, not a random one. Whether the long-run average really converges to it is a theorem proved much later in this course, not an assumption made here.
For a single fair die, , a value the die can never show. Expectation is a centre of mass, not a prediction, and reading it as "what will happen" is the first of several ways it misleads. For the two-dice total, , which the sum can show, and which sits at the centre of the symmetric mass function.
Example. A European roulette wheel has pockets: red, black and one green zero. You stake unit on red, winning if red comes up and losing your stake otherwise. What is the expected gain per spin?
so the house keeps about percent of everything staked, on average, whatever the bet.
Now you. An American wheel has pockets, with both a zero and a double zero, so are red. What is the expected gain per unit staked on red?
Answer
, a house edge of percent, almost exactly twice the European figure. One extra pocket doubles the cost of playing.
Functions of a random variable
If is a random variable then so is for any function , since a function of a function is a function. Its expectation could be found by first working out the mass function of , and it never has to be, because
The proof is a regrouping: gather the outcomes according to the value of rather than the value of , and every outcome is still counted exactly once with its own probability. The result is often called the law of the unconscious statistician, from the habit of using it without noticing that it needs proof.
Two consequences are worth having on hand. First, for constants and , since the sum splits and . Expectation moves through a change of units unharmed: a temperature in Fahrenheit has times the expected Celsius value plus . Second, is generally not . For a fair die, , while . The gap of is not an error; the next lesson identifies it as the variance.
The practical consequence is that a payout must be averaged after the function is applied, never before. A stall charging units to roll a die and paying out the square of the result takes in and pays out , losing about units a play; pricing it from , or worse from , would have hidden that.
Linearity, which asks for nothing
Here is the result that earns its keep. For any random variables and defined on the same sample space,
The proof works outcome by outcome rather than value by value. Writing the expectation as a sum over ,
which is nothing but the associativity of addition. Notice what is absent: no independence, no assumption about how and relate, not even that they are different quantities. It holds when , when , when is a deliberately hostile function of . Extended to any finite number of terms it says the expectation of a sum is always the sum of the expectations.
This is stronger than it looks, because most quantities of interest are sums of simple pieces whose joint behaviour is a nightmare and whose individual behaviour is trivial. The two-dice total illustrates the pattern in miniature: rather than summing over the eleven values with their awkward weights, write and get immediately.
Example. Five cards are dealt from a shuffled deck. What is the expected number of hearts?
Let be if the card in position is a heart and otherwise, so the number of hearts is . Each individual card is equally likely to be any of the , so . Linearity gives hearts expected. The cards are emphatically not independent, since dealing a heart makes the next heart less likely, and linearity does not care.
Now you. Ten cards are dealt from the same deck. What is the expected number of aces?
Answer
Each position holds an ace with probability , so the expectation is .
Indicators, and the problems they crack
The technique in that example deserves a name. An indicator variable takes the value when the event occurs and when it does not, and its expectation is
so expectation and probability are the same operation on indicators. Any count is a sum of indicators, one per thing that might be counted, and by linearity its expectation is the sum of the separate probabilities. Nothing about dependence ever enters.
Take the classic: letters are put at random into addressed envelopes. How many arrive at the right address? The exact distribution of the number of matches is a genuinely awkward object involving alternating sums. Its mean is not. Let indicate that letter is in its own envelope. Any given letter is equally likely to be in any of the envelopes, so , and the expected number of matches is for every . One letter arrives correctly on average, whether there are ten letters or ten million.
Or the coupon collector. There are different coupons, one at random in each packet. Let be the number of packets bought while holding exactly distinct coupons. Each purchase in that stretch is new with probability , and a wait for a success with constant probability has expectation , proved in the lesson on the binomial family. So , and the total is
For , a full set of six needs packets on average, and for it is . Collecting the last few is most of the work: the final coupon alone takes packets on average.
Example. A fair die is rolled six times. What is the expected number of distinct faces seen?
Let indicate that face appears at least once. It fails to appear with probability , so . Summing over the six faces gives distinct faces, about four.
Now you. A fair coin is tossed times. What is the expected number of tosses that match the toss before them, out of the adjacent pairs?
Answer
Each adjacent pair matches with probability , so the expectation is .
Where the mean is not enough
Expectation compresses a whole distribution into one number, and the compression discards things that matter.
Consider two propositions. The first pays unit with certainty. The second pays units with probability and nothing otherwise. Both have expectation exactly , and nobody treats them as equivalent. Expectation cannot distinguish a certainty from a lottery, because it never asks how far the outcomes stray from their average.
The extreme case was posed by Nicolas Bernoulli in 1713. A coin is tossed until it lands heads; if this takes tosses, the payout is units. The probability of stopping at toss is , so each term of the expectation contributes , and the sum over infinitely many terms diverges. The expected payout is infinite, and no one will pay even units to play, since a payout above requires at least seven tosses, which happens with probability , about once in plays. Daniel Bernoulli's 1738 resolution, that people value money by something growing more slowly than the amount, launched utility theory, but the plain mathematical point stands on its own: an expectation can exist and be useless, or fail to exist while the variable is perfectly well behaved.
What is missing is a measure of how widely the values are spread around the mean. Building one, discovering that it adds when expectations of products do not, and extracting a guarantee about how far from the mean a variable can stray, is the next lesson.