Velamuvi AIAcademy
Probability

Lesson 11 of 16 · Random Variables

Expected Value

Expected value, the long-run average of a random variable: how to calculate it, why linearity of expectation helps, and how expected loss guides decisions.

  • 6 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • Calculate the expected value of a discrete random variable
  • Explain expected value as a long-run average
  • Use linearity of expectation, even for dependent random variables
  • Find the expected value of a continuous uniform random variable
  • Use expected loss to make decisions

The long-run average

If you roll a die thousands of times and average the results, you'll get very close to 3.5. That number is the die's expected value.

For a die: E[X] = 1 × 1/6 + 2 × 1/6 + … + 6 × 1/6 = 21/6 = 3.5. You'll never roll a 3.5. The expected value is not the most likely value, but the average you'd get over many repeats.

Try it yourselfPython
import random
from itertools import product

die = range(1, 7)
print(f"E[one die] = {sum(x * 1 / 6 for x in die):.2f}")

# Linearity: E[X + Y] = E[X] + E[Y]
pairs = list(product(die, repeat=2))
print(f"E[total of two dice] = {sum(a + b for a, b in pairs) / 36:.2f}   (3.5 + 3.5)")
print(f"E[first die + total] = {sum(a + (a + b) for a, b in pairs) / 36:.2f}  (3.5 + 7, even though they're dependent)")

# The average of many rolls settles down near the expected value
random.seed(11)
for n in (10, 1_000, 100_000):
    rolls = [random.randint(1, 6) for _ in range(n)]
    print(f"average of {n:>7,} rolls: {sum(rolls) / n:.3f}")

After 10 rolls the average can be far off; after 100,000 it's within a whisker of 3.5. That's the law of large numbers from lesson 1, now applied to averages.

Linearity of expectation

Expected values follow two simple rules that make them easy to calculate:

The output above checks it: the total of two dice has expected value 3.5 + 3.5 = 7. And the first die plus the total has expected value 3.5 + 7 = 10.5, even though those two are clearly dependent.

For a continuous random variable, the sum becomes an area: E[X] = ∫ x f(x) dx. For a uniform distribution from lo to hi, the answer is simply the midpoint, (lo + hi)/2. A bus that arrives uniformly within 15 minutes has an expected wait of 7.5 minutes.

Decisions by expected value

Expected value turns uncertainty into a number you can act on. A payments company has to decide whether a person should check a suspicious payment. A check costs $20; a missed fraud costs $500. If the model says the payment is fraud with probability p, the expected loss from letting it through is p × $500:

Try it yourselfPython
review_cost = 20      # dollars to have a person check a payment
fraud_cost = 500      # dollars lost if a fraud gets through

for p_fraud in (0.01, 0.04, 0.10, 0.50):
    expected_loss = p_fraud * fraud_cost             # expected loss if we let it through
    decision = "review it" if expected_loss > review_cost else "let it through"
    print(f"P(fraud) = {p_fraud:.2f}: expected loss ${expected_loss:6.2f} vs review ${review_cost} -> {decision}")

The break-even point is p = 20/500 = 0.04. Above it, reviewing is cheaper on average; below it, letting the payment through is. The model's probability and the costs together decide the threshold, not a fixed 0.5.

Common mistakes

Practice exercise

Is this dice game worth playing? Calculate the expected winnings per game.

Your solutionPython
# A game: roll one die. Your winnings in dollars for each face (negative means you pay)
payoff = {1: -3, 2: -3, 3: -3, 4: 2, 5: 2, 6: 10}

# Each face has probability 1/6. Calculate the expected winnings per game,
# and print it rounded to 3 decimals.
Knowledge check

X has expected value 4. What is the expected value of 3X + 2?

Summary

  • The expected value E[X] = Σ x × P(X = x) is the long-run average of X.
  • It doesn't have to be a possible value: a die's is 3.5.
  • Linearity: E[X + Y] = E[X] + E[Y] and E[aX + b] = a × E[X] + b, even for dependent variables.
  • For a uniform distribution, the expected value is the midpoint.
  • Expected loss turns probabilities and costs into decisions, and training a model minimises an average loss.

Next: variance.

Finished reading? Tick it off to track your progress.