Advanced level

Random variables and expected value

A random variable attaches a number to every outcome of an experiment — a payout, a waiting time, a count of faults. Its distribution is written as a table, and two numbers summarise that whole table: the expected value says what the results centre on, the variance says how far from it they wander.

Before you start

This topic builds on earlier ideas. Before you start, it's worth working through the lessons below — they'll make everything click:

Where this is used

Real situations where you count exactly the way this lesson teaches:

  • A lottery ticket
    A ticket costs 5. The top prize of 1000 comes up with probability 1/2000 and a consolation 50 with probability 1/200. The expected payout is 1000/2000 + 50/200 = 0.50 + 0.25 = 0.75, so the player’s expected result is 0.75 − 5 = −4.25 per ticket. A hundred tickets a year means an expected loss of 425, and no run of luck changes that — it only changes when it shows.
  • Pricing an insurance premium
    An insurer estimates that a claim occurs for 2 per cent of customers per year and that its average cost is 30,000. The expected payout per policy is 0.02 · 30,000 = 600, so a premium of 900 leaves 300 for costs and profit. The whole actuarial pricing of a policy is the computation of the expected value of the random variable “payout”.
  • European roulette
    A stake of 1 on a single number pays 35 to 1, and there are 37 numbers counting the zero. The expected value is 35 · 1/37 − 1 · 36/37 = −1/37, about −0.027 per unit staked. That is the house edge: 2.7 per cent of turnover, whatever the betting strategy and however many times the game is played.
  • A flat-rate service contract
    A repair shop knows that a given machine breaks down within a year with probability 0.15 and that a repair costs 800. The expected cost is 0.15 · 800 = 120 per machine, so a flat annual fee of 200 covers it with room to spare. It is worth taking for the customer whenever they prefer a certain 200 to a lottery that costs 800 with probability 15 per cent — and that is the whole difference between expected value and risk.

All formulas

  • Random variable

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

    a function attaching a number to an outcome

  • Distribution

    ipi=1,pi=P(X=xi)\sum_{i} p_i = 1, \qquad p_i = P(X = x_i)

    a table of values and their probabilities

  • Expected value

    E(X)=ixipiE(X) = \sum_{i} x_i \, p_i

    a mean weighted by probabilities

  • Variance of a variable

    σ2(X)=i(xiE(X))2pi\sigma^2(X) = \sum_{i} \big(x_i - E(X)\big)^2 p_i

    mean squared deviation from E(X)

  • Computational form

    σ2(X)=E(X2)(E(X))2\sigma^2(X) = E(X^2) - \big(E(X)\big)^2

    one pass over the table instead of two

  • Standard deviation

    σ(X)=σ2(X)\sigma(X) = \sqrt{\sigma^2(X)}

    in the same unit as X

  • Distribution function

    F(x)=P(Xx)F(x) = P(X \leq x)

    non-decreasing, from 0 to 1

  • Linear transformation

    E(aX+b)=aE(X)+b,σ2(aX+b)=a2σ2(X)E(aX + b) = a\,E(X) + b, \qquad \sigma^2(aX + b) = a^2 \sigma^2(X)

    a shift does not change the spread

The outcome of rolling two dice is a pair of faces, but a problem almost always asks about their sum. The outcome of a game is the whole play, but what interests us is the payout. In both cases an event is translated into a number — and that recipe for translating is what a random variable is.

A random variable is a function

A random variable is a function attaching a number to every elementary outcome:

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

The name misleads twice. It is not a variable in the algebraic sense — it does not stand in an equation and there is nothing to solve for. Nor is it a random number — the function is entirely determined; only its argument is random.

For a roll of two dice Ω\Omega has 3636 elements, and XX = the sum of the faces assigns the number 77 to the pair (3,4)(3, 4). The variable “simplifies” the experiment: instead of 3636 pairs we have 1111 numbers — at the price that they are no longer equally likely.

The distribution as a table

The distribution of a discrete variable is the list of its values together with their probabilities. It is written as a table:

xix_i00551010
pip_i14\tfrac{1}{4}12\tfrac{1}{2}14\tfrac{1}{4}

There is one validity condition, and it is always worth checking:

pi0,ipi=1p_i \geq 0, \qquad \sum_{i} p_i = 1

The table exhausts every possibility, so the probabilities must add up to exactly one. The same distribution drawn as bars:

−10123456789101100.20.40.6payoutp1/41/21/4game A
The distribution of the payout in game A. The bar heights add up to one — it is the same table, only drawn.

Expected value

The expected value of a random variable is the mean of its values weighted by their probabilities:

E(X)=ixipiE(X) = \sum_{i} x_i \, p_i

Physically it is the centre of mass of the distribution: the point where the bar chart could be balanced on a fingertip. For game A:

E(X)=014+512+1014=0+2.5+2.5=5E(X) = 0 \cdot \frac{1}{4} + 5 \cdot \frac{1}{2} + 10 \cdot \frac{1}{4} = 0 + 2.5 + 2.5 = 5

Two remarks, both more important than the arithmetic itself.

The expected value need not be possible. For a die roll E(X)=1+2++66=3.5E(X) = \tfrac{1+2+\dots+6}{6} = 3.5, though a die never shows 3.53.5. It is a centre of mass, not a forecast of a single result.

The expected value is not a sample mean. The arithmetic mean is computed from what came up and changes from sample to sample. The expected value is computed from the model, that is from the probabilities, and is single. After a thousand die rolls the mean will be close to 3.53.5; after ten it may well be 2.82.8 — and E(X)E(X) is still 3.53.5 in both cases.

A lottery ticket costs 5. A prize of 1000 occurs with probability 1/2000 and a prize of 50 with probability 1/200. Is it worth playing?

Variance: what the expected value does not say

E(X)E(X) on its own summarises a distribution too aggressively. Compare game A with game B, whose payout is 44, 55 or 66 with the same probabilities:

−10123456789101100.20.40.6payoutp1/41/21/4game B
Game B on the same scale as game A. The same expected value of 5, but the bars huddle around the centre instead of spreading across the whole axis.

Both games have E(X)=5E(X) = 5 and differ in everything that matters to a player. That difference is measured by the variance of the variable:

σ2(X)=i(xiE(X))2pi\sigma^2(X) = \sum_{i} \big(x_i - E(X)\big)^2 p_i

In practice the computational formula is handier, needing one pass over the table instead of two:

σ2(X)=E(X2)(E(X))2\sigma^2(X) = E(X^2) - \big(E(X)\big)^2
Compute the variance and standard deviation for both games.

Mind the units: the variance is in squared units, so what compares with the variable itself is the standard deviation σ(X)\sigma(X). It is the same relationship that appeared for the spread of a data set — with the difference that there the weights were counts, and here they are probabilities.

Two properties of linear transformations are also worth having:

E(aX+b)=aE(X)+b,σ2(aX+b)=a2σ2(X)E(aX + b) = a\,E(X) + b, \qquad \sigma^2(aX + b) = a^2 \sigma^2(X)

A shift by bb does not change the spread — it moves the whole distribution rather than stretching it. That is why bb does not appear in the second formula at all.

The distribution function

Instead of asking “what is XX”, we often ask “will XX stay below a given value”. That is answered by the distribution function:

F(x)=P(Xx)F(x) = P(X \leq x)

For a discrete variable it is a step function: constant between the values, jumping by pip_i at each of them. For game A:

intervalx<0x < 00x<50 \leq x < 55x<105 \leq x < 10x10x \geq 10
F(x)F(x)0014\tfrac{1}{4}34\tfrac{3}{4}11

The distribution function is always non-decreasing, starts at 00 and ends at 11 — because probabilities accumulate rather than cancel. Its convenience is that the probability of an interval is read off by subtraction: P(a<Xb)=F(b)F(a)P(a < X \leq b) = F(b) - F(a).

A note on continuous variables

Everything above assumed the values could be listed. Sometimes they cannot: a bus waiting time, the height of a randomly chosen person or a measurement error take every value in an interval.

For such a continuous variable the probability of hitting any particular number is zero — not because it is impossible, but because there are infinitely many possibilities. The sensible question is about an interval, and it is answered by the area under the graph of a density function:

P(aXb)=abf(x)dxP(a \leq X \leq b) = \int_a^b f(x)\,dx

The sum over a table turns into a definite integral, and the condition “the probabilities add up to one” turns into “the area under the whole density is one”. The most important continuous variable of all, the normal distribution, comes two lessons further on.

Exercises

The set asks for two numbers, both computed from the same distribution table. The E(X)E(X) prompt gives “value → probability” pairs and asks for the expected value. The σ2(X)\sigma^2(X) prompt gives a table of the same shape and asks for the variance of the variable, that is for E(X2)(E(X))2E(X^2) - \big(E(X)\big)^2.

The answer here is a number, not a fraction in lowest terms: an expected value is not a probability but a quantity on the variable’s own scale. The probabilities in the tables are chosen so that the result has at most two decimal places.

Practice

Work through a set of exercises — they get harder as you go. At the end you'll see your score and the mistakes worth reviewing.

Exercise 1 of 8Score: 0
E(X): 0 → 3/5, 2 → 1/5, 3 → 1/5

The generator does not ask you to state a distribution — the answer would be a whole table together with the names of the events, not a number. Nor does it ask about the distribution function, for the same reason. Both are worked through in the examples above.

Common mistakes

  • Confusing E(X)E(X) with the arithmetic mean of data — the first is a property of the model and is fixed, the second a property of the sample and changes with every repetition.
  • Expecting E(X)E(X) to be one of the possible values3.53.5 pips never comes up, and it is still the correct expected value.
  • A table whose probabilities do not add up to one — that is not a distribution, and every quantity computed from it is meaningless. It is worth starting with that check.
  • Squaring the probabilities instead of the values — in E(X2)E(X^2) the squares apply to the xix_i, while the weights pip_i stay untouched.
  • Comparing a variance with the variable itself — a variance is in squared units; what compares is the standard deviation.
  • Concluding that two variables with the same E(X)E(X) are equivalent — games A and B share an expected value and differ twenty-five-fold in variance, which for a player is the whole difference.

Formula card

Topic: Random variables

  • Random variable

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

    a function attaching a number to an outcome

  • Distribution

    ipi=1,pi=P(X=xi)\sum_{i} p_i = 1, \qquad p_i = P(X = x_i)

    a table of values and their probabilities

  • Expected value

    E(X)=ixipiE(X) = \sum_{i} x_i \, p_i

    a mean weighted by probabilities

  • Variance of a variable

    σ2(X)=i(xiE(X))2pi\sigma^2(X) = \sum_{i} \big(x_i - E(X)\big)^2 p_i

    mean squared deviation from E(X)

  • Computational form

    σ2(X)=E(X2)(E(X))2\sigma^2(X) = E(X^2) - \big(E(X)\big)^2

    one pass over the table instead of two

  • Standard deviation

    σ(X)=σ2(X)\sigma(X) = \sqrt{\sigma^2(X)}

    in the same unit as X

  • Distribution function

    F(x)=P(Xx)F(x) = P(X \leq x)

    non-decreasing, from 0 to 1

  • Linear transformation

    E(aX+b)=aE(X)+b,σ2(aX+b)=a2σ2(X)E(aX + b) = a\,E(X) + b, \qquad \sigma^2(aX + b) = a^2 \sigma^2(X)

    a shift does not change the spread

−10123456789101100.20.40.6payoutp1/41/21/4game A
The distribution of the payout in game A: 0, 5 or 10 units with probabilities 1/4, 1/2 and 1/4. The bars add up to one, and the centre of mass of this table — the expected value — sits at 5.
−10123456789101100.20.40.6payoutp1/41/21/4game B
The distribution of the payout in game B: 4, 5 or 6 with the same probabilities, drawn to the same scale. The same expected value of 5, a variance twenty-five times smaller.

Frequently asked questions

Related articles