Random Variables & Expected Value

Probability & Statistics Fundamentals

Chapter 5 · Random Variables & Expected Value

Every chapter so far has asked about the probability of a specific event. This chapter asks a slightly different question: given a whole range of numeric outcomes, each with its own probability, what's a single number that fairly summarizes the whole situation? That number is the expected value, and it's the mathematical foundation of essentially every risk, cost, and budgeting calculation built on top of uncertainty.

Random Variables & Probability Distributions

A random variable (conventionally written as a capital letter, like X) assigns a number to every outcome in a sample space. Its probability distribution (or probability mass function, for a discrete variable) lists every possible value it can take, together with the probability of each.

Baseline example: rolling a fair die
X = the value shown. Every value 1 through 6 has probability 1/6 — a genuinely uniform distribution.

Expected Value

The expected value E[X] is the probability-weighted average of every possible outcome:

Expected value formula
E[X] = Σ x · P(X = x) — sum over every possible value x

For the die: E[X] = (1+2+3+4+5+6)/6 = 21/6 = 3.5.

3.5 is never a real outcome — and that's expected
You can never actually roll a 3.5. Expected value is a long-run average, not a prediction of any single result — it's the number the average of many, many rolls converges toward, not a value any one roll can produce. This distinction matters constantly in practice: an "expected cost" of $1,500 doesn't mean any given week costs exactly $1,500 — it means that's the fair average across many weeks.

Worked Example: Expected Weekly Incident Cost

A service's weekly incident cost, X, has this distribution:

Outcome (cost)ProbabilityScenario
$00.70No incident
$5000.20Minor incident
$5,0000.08Major incident
$50,0000.02Catastrophic incident

(Probabilities sum to exactly 1.0, as any valid distribution must — Chapter 2's own certainty axiom.)

TermCalculation
0 × 0.700
500 × 0.20100
5,000 × 0.08400
50,000 × 0.021,000

E[X] = 0 + 100 + 400 + 1,000 = $1,500 — the expected weekly incident cost, useful directly for budgeting a reserve fund or setting an insurance premium, exactly the reasoning behind Technical Support's own backup1/incident1 material approached from a numeric angle.

Variance & Standard Deviation of a Random Variable

Expected value alone hides something important: how spread out the possible outcomes actually are. Variance measures that spread directly, for a known distribution — a genuinely different calculation from Chapter 9's own variance, which is computed from real, already-collected sample data rather than a theoretical distribution.

Variance & standard deviation of a random variable
Var(X) = E[X²] − (E[X])², and SD(X) = √Var(X)

For the incident-cost example: E[X²] = 0²(0.70) + 500²(0.20) + 5,000²(0.08) + 50,000²(0.02) = 0 + 50,000 + 2,000,000 + 50,000,000 = 52,050,000.

QuantityValue
Var(X)52,050,000 − 1,500² = 49,800,000
SD(X)√49,800,000 ≈ $7,057
A standard deviation far larger than the mean — a real risk signal
A standard deviation of roughly $7,057 against a mean of just $1,500 is enormous — it means the "typical" week doesn't look anything like $1,500 at all; most weeks cost $0, and the average is almost entirely dragged up by the rare, catastrophic 2% outcome. Expected value tells you what to budget on average; variance/standard deviation tells you how badly a single bad week could still blow that budget — both numbers matter, not just one.

Random Variables & Expected Value in Code

dist = {0: 0.70, 500: 0.20, 5000: 0.08, 50000: 0.02} assert abs(sum(dist.values()) - 1.0) < 1e-9 # a valid distribution sums to 1 def expected_value(dist): return sum(x * p for x, p in dist.items()) def variance(dist): ex = expected_value(dist) ex2 = sum((x ** 2) * p for x, p in dist.items()) return ex2 - ex ** 2 print(expected_value(dist)) # 1500.0 print(variance(dist) ** 0.5) # 7056.91... (standard deviation)

Hands-On Exercises

Exercise 1

A company's monthly new-signups count, X, has the distribution: P(X=100) = 0.5, P(X=200) = 0.3, P(X=500) = 0.2. Confirm the probabilities sum to 1, then compute E[X].

📄 View solution
Exercise 2

A feature-flag rollout has three possible cost outcomes: no issues (probability 0.85, cost $0), a minor rollback (probability 0.12, cost $2,000), a major outage (probability 0.03, cost $80,000). Compute E[X], Var(X), and SD(X). Given how large the standard deviation is relative to the mean, what does this chapter's own finding suggest about relying on the expected value alone to decide whether the rollout is "safe enough"?

📄 View solution
Exercise 3

Using this chapter's own incident-cost example (E[X] = $1,500 per week), explain in your own words why "the expected cost over the next 1,000 weeks is $1,500,000" is a reasonable statement to make, even though no single week is ever likely to cost exactly $1,500. Ground your answer in this chapter's own "long-run average, not a single prediction" distinction.

📄 View solution

Chapter 5 Quick Reference

  • Random variable: assigns a number to every outcome; its distribution lists every value with its probability, summing to exactly 1
  • Expected value: E[X] = Σ x · P(X=x) — a probability-weighted average, and a long-run average, not a prediction of any single outcome
  • Variance of a random variable: Var(X) = E[X²] − (E[X])²; standard deviation is its square root — distinct from Chapter 9's own sample-based variance, computed from real collected data rather than a known distribution
  • A large standard deviation relative to the mean signals a risky, skewed distribution — expected value alone can badly understate real risk
  • Next chapter: The binomial distribution