Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 57 ยท Random Variables

Expected Value and Variance of Random Variables

Compute the expected value, variance and standard deviation of a discrete random variable, use linearity of expectation, and apply them to games, insurance and decisions.

A random variable's PMF tells you everything about it, but you often want a short summary: what is the long-run average, and how much does the outcome fluctuate? The expected value and the variance are the probabilistic counterparts of the mean and variance you computed from data in the chapter on describing data, with probabilities playing the role of weights.

Expected value

The expected value (or mean) of a discrete random variable X is

E[X] = sum of x * p(x) over all values x.

It is a weighted average of the possible values, using probabilities as weights (compare the weighted mean). For a fair die:

E[X] = 1/6 * (1 + 2 + 3 + 4 + 5 + 6) = 3.5.

You will never roll 3.5, yet it is the long-run average of many rolls (law of large numbers). The expected value is the balance point of the PMF, not necessarily a likely value.

Python Expected value of a die, by formula and simulation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Expected value in decisions

A game: pay 3 dollars to roll a die; you win the number of dollars shown. Your profit G = X - 3 has E[G] = 3.5 - 3 = 0.5. In the long run you make 50 cents per play. A lottery ticket costing 2 dollars with a 1 in a million chance at 1 million has E = 1 - 2 = -1: you lose a dollar on average. Insurance, casinos and pricing all rest on expected values.

Python Is the game worth playing?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Linearity of expectation

Two properties make expected values easy to work with:

  • E[aX + b] = a * E[X] + b
  • E[X + Y] = E[X] + E[Y], always, even when X and Y are dependent.

The second one is remarkable. A binomial variable with n trials is a sum of n Bernoulli variables, each with expected value p, so E[X] = n * p with no further work. For 100 flips of a fair coin the expected number of heads is 50.

The expected value of a function is E[g(X)] = sum of g(x) * p(x). Note that E[X^2] is generally not (E[X])^2.

Variance and standard deviation

The variance measures spread around the mean:

Var(X) = E[(X - mu)^2] = sum of (x - mu)^2 * p(x), where mu = E[X].

A convenient equivalent: Var(X) = E[X^2] - (E[X])^2.

For a die: E[X^2] = (1 + 4 + 9 + 16 + 25 + 36) / 6 = 91/6 โ‰ˆ 15.17, so Var = 15.17 - 3.5^2 = 15.17 - 12.25 โ‰ˆ 2.92, and the standard deviation is sqrt(2.92) โ‰ˆ 1.71.

Rules for a constant a: Var(aX + b) = a^2 * Var(X) (shifting changes nothing; scaling squares). For independent variables, Var(X + Y) = Var(X) + Var(Y). Standard deviations do not add: sd(X + Y) = sqrt(sd(X)^2 + sd(Y)^2) when independent.

Python Variance of a PMF

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Binomial mean and variance

For a binomial X with n trials and success probability p:

  • E[X] = n * p
  • Var(X) = n * p * (1 - p)

These come from summing n independent Bernoulli variables, each with variance p(1 - p).

Python Binomial formulas versus a simulation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Risk: same mean, different variance

Two investments can have the same expected return and very different risk. Variance quantifies that risk. Investment A returns 5% for sure. Investment B returns 0% or 10% with equal probability. Both have E = 5%, but B has a standard deviation of 5 points. Many decisions weigh mean against variance (the mean-variance tradeoff).

flowchart LR
    X["Random variable X<br/>with PMF"] --> M["E[X]<br/>center, balance point"]
    X --> V["Var(X)<br/>spread around E[X]"]
    V --> S["sd = sqrt(Var)<br/>same units as X"]

Mean and variance reduce a whole distribution to two descriptive numbers.

Properties at a glance

Quantity Rule
E[c] c
E[aX + b] a E[X] + b
E[X + Y] E[X] + E[Y]
Var(aX + b) a^2 Var(X)
Var(X + Y) (independent) Var(X) + Var(Y)
Var(X) E[X^2] - (E[X])^2

Common mistakes

  • Expecting E[X] to be a possible value of X.
  • Using Var(X + Y) = Var(X) + Var(Y) for dependent variables (a covariance term appears; see joint distributions).
  • Forgetting to square the constant: Var(3X) = 9 Var(X).
  • Taking E[X^2] = (E[X])^2.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. X takes 0, 10 with probabilities 0.8 and 0.2. What is E[X]?

    0 * 0.8 + 10 * 0.2 = 2.
  2. 2. Var(X) = 4. What is Var(3X + 5)?

    Adding 5 does not change variance, and multiplying by 3 multiplies variance by 9: 9 * 4 = 36.
  3. 3. A binomial variable has n = 50 and p = 0.2. What is E[X]?

    E[X] = n * p = 10.
  4. 4. Which statement holds even for dependent variables?

    Linearity of expectation needs no independence assumption.

Summary

  • E[X] = sum of x * p(x) is the probability-weighted average and the long-run mean.
  • Linearity: E[aX + b] = aE[X] + b and E[X + Y] = E[X] + E[Y] always.
  • Var(X) = E[(X - mu)^2] = E[X^2] - mu^2; the standard deviation is its square root.
  • Var(aX + b) = a^2 Var(X), and variances of independent variables add.
  • Binomial: mean np, variance np(1 - p).

Next: CDFs and Continuous Random Variables