Skip to content

06-01: Random Variables and Expected Value

A random variable attaches a number to every outcome of a probability experiment. Once outcomes are numbers, you can take a mean and a standard deviation of a probability distribution — exactly as you did for data in Chapters 03 and 04, but now for what should happen rather than what did.


Random Variables

Random variable X   —  a variable whose value is determined by chance

Discrete    countable values, with gaps           number of heads in 3 flips
Continuous  any value in an interval              time until a bus arrives
Experiment Random variable X Type Possible values
Flip 3 coins Number of heads Discrete 0, 1, 2, 3
Roll 2 dice Sum Discrete 2, 3, …, 12
Inspect 20 items Number defective Discrete 0, 1, …, 20
Call centre Calls per hour Discrete 0, 1, 2, …
Time a task Minutes taken Continuous any t > 0

This note covers discrete random variables; continuous ones start in 07-01.


Discrete Probability Distributions

A table (or formula) listing every value of X with its probability.

Two requirements — check BOTH every time:

1.   0 ≤ P(X = x) ≤ 1     for every x
2.   Σ P(X = x) = 1        summed over all x

Example — flip 3 fair coins, X = number of heads.

Sample space: HHH HHT HTH HTT THH THT TTH TTT      (8 equally likely)

x      0      1      2      3
P(x)  1/8    3/8    3/8    1/8      Σ = 1  ✓
      .125   .375   .375   .125

A probability histogram plots P(x) against x — the theoretical counterpart of the frequency histogram in 02-02.


Expected Value (the Mean of a Distribution)

μ  =  E(X)  =  Σ  x · P(x)

This is the weighted mean of 03-02, with probabilities as the weights. It is the long-run average value of X over many repetitions — not necessarily a value X can actually take.

E(X) for 3 coin flips = 0(.125) + 1(.375) + 2(.375) + 3(.125) = 1.5 heads

You never see 1.5 heads on one trial; you average 1.5 over many trials.


Variance and Standard Deviation of a Distribution

Definition form       σ²  =  Σ (x − μ)² · P(x)

Computational form    σ²  =  Σ x² · P(x)  −  μ²          [usually easier]
                                                  ____
Standard deviation    σ   =  √σ²

Note

These are population parameters — the distribution is the entire theoretical population, so there is no n − 1 correction here.


Worked Example — Three Coin Flips

x    P(x)     x·P(x)    x²·P(x)
0    0.125    0.000     0.000
1    0.375    0.375     0.375
2    0.375    0.750     1.500
3    0.125    0.375     1.125
──   ─────    ─────     ─────
     1.000    1.500     3.000

μ  = Σ x·P(x)  = 1.500
σ² = Σ x²·P(x) − μ² = 3.000 − 1.500² = 3.000 − 2.250 = 0.750
σ  = √0.750 = 0.866

Check against the binomial shortcut of 06-02: μ = np = 3(0.5) = 1.5 ✓ and σ² = npq = 3(0.5)(0.5) = 0.75 ✓.


Worked Example — Expected Value in Decisions

A raffle

1,000 tickets at $5 each. One prize of $2,000, five prizes of $100. X = net gain per ticket.

x         P(x)              x·P(x)
+1995     1/1000  = 0.001   +1.995      (2000 prize − 5 cost)
  +95     5/1000  = 0.005   +0.475
   −5   994/1000  = 0.994   −4.970
                            ───────
                  E(X)  =   −2.500

Expected loss of $2.50 per ticket. Over 1,000 tickets the organizer collects $5,000 and pays out $2,500 — the expected value is exactly the house edge.

An insurance policy

A company sells a one-year $100,000 life policy to a 30-year-old for a $250 premium. The probability of death in the year is 0.001.

x          P(x)      x·P(x)
 +250      0.999     +249.75      (policyholder survives)
−99,750    0.001     −99.75       (pays out 100,000, keeps the 250)
                     ────────
             E(X) =  +150.00

Expected profit of $150 per policy. Sell 100,000 policies and the law of large numbers turns that expectation into near-certain revenue — the entire business model of insurance.


Rules for Expected Value and Variance

Useful for transforming and combining random variables:

E(aX + b)   =  a·E(X) + b
Var(aX + b) =  a²·Var(X)                 (adding b shifts, never spreads)
SD(aX + b)  =  |a|·SD(X)

E(X + Y)    =  E(X) + E(Y)               always, independent or not
Var(X + Y)  =  Var(X) + Var(Y)           ONLY if X and Y are independent
Var(X − Y)  =  Var(X) + Var(Y)           ONLY if independent — note the PLUS

Warning

Variances add even when you subtract the variables. Standard deviations never add: SD(X+Y) = √(Var X + Var Y), not SD(X) + SD(Y). This is exactly why the two-sample standard error in 11-02 has a + inside the square root.


Excel

' ── Check that it is a valid distribution ───────────────────────────
' x in A2:A5, P(x) in B2:B5
=SUM(B2:B5)                          ' must equal 1
=AND(MIN(B2:B5)>=0, MAX(B2:B5)<=1)   ' must be TRUE

' ── Mean, variance, standard deviation ──────────────────────────────
=SUMPRODUCT(A2:A5, B2:B5)                          ' μ   -> 1.5
=SUMPRODUCT(A2:A5^2, B2:B5) - D1^2                 ' σ²  (D1 holds μ) -> 0.75
=SUMPRODUCT((A2:A5-D1)^2, B2:B5)                   ' σ²  definition form
=SQRT(SUMPRODUCT((A2:A5-D1)^2, B2:B5))             ' σ   -> 0.866

' Helper-column version (clearer for coursework — show your work)
=A2*B2                               ' C2:  x · P(x)      then SUM(C2:C5) = μ
=A2^2*B2                             ' D2:  x² · P(x)     then SUM(D2:D5) − μ²
=(A2-$G$1)^2*B2                      ' E2:  (x−μ)² · P(x) then SUM(E2:E5) = σ²

' ── Cumulative probabilities ────────────────────────────────────────
=SUM($B$2:B2)                        ' P(X ≤ x)
=1-SUM($B$2:B3)                      ' P(X > x)

' ── Simulating from a discrete distribution ─────────────────────────
' Build a cumulative column C, then:
=INDEX($A$2:$A$5, MATCH(RAND(), $C$2:$C$5, 1) + 1)
' or:  Data ▸ Data Analysis ▸ Random Number Generation ▸ Discrete

R

x  <- 0:3
px <- c(0.125, 0.375, 0.375, 0.125)

# ── Validity check ─────────────────────────────────────────────────
sum(px) == 1                       # TRUE
all(px >= 0 & px <= 1)             # TRUE

# ── Mean, variance, SD ─────────────────────────────────────────────
mu    <- sum(x * px);              mu        # 1.5
sigma2 <- sum(x^2 * px) - mu^2;    sigma2    # 0.75   computational
sum((x - mu)^2 * px)                         # 0.75   definition
sigma <- sqrt(sigma2);             sigma     # 0.866

# Reusable helpers
E   <- function(x, p) sum(x * p)
Var <- function(x, p) sum(x^2 * p) - E(x, p)^2
SD  <- function(x, p) sqrt(Var(x, p))
c(mean = E(x, px), var = Var(x, px), sd = SD(x, px))

# ── Cumulative distribution ────────────────────────────────────────
cumsum(px)                         # P(X ≤ x): 0.125 0.500 0.875 1.000
1 - cumsum(px)                     # P(X > x)

# ── Probability histogram ──────────────────────────────────────────
barplot(px, names.arg = x, col = "#5B2A86", border = NA,
        xlab = "Number of heads", ylab = "P(x)",
        main = "Probability distribution")

# ── Raffle example ─────────────────────────────────────────────────
gain <- c(1995, 95, -5)
p    <- c(1/1000, 5/1000, 994/1000)
E(gain, p)                         # -2.5   expected loss per ticket

# ── Insurance example ──────────────────────────────────────────────
E(c(250, -99750), c(0.999, 0.001))    # 150

# ── Simulation ─────────────────────────────────────────────────────
set.seed(1)
sim <- sample(x, size = 100000, replace = TRUE, prob = px)
mean(sim); var(sim)                # ≈ 1.5 and ≈ 0.75
round(prop.table(table(sim)), 3)   # ≈ the theoretical px

Python

import numpy as np
import pandas as pd
from scipy import stats

x  = np.array([0, 1, 2, 3])
px = np.array([0.125, 0.375, 0.375, 0.125])

# ── Validity check ─────────────────────────────────────────────────
np.isclose(px.sum(), 1)                     # True
((px >= 0) & (px <= 1)).all()               # True

# ── Mean, variance, SD ─────────────────────────────────────────────
mu     = (x * px).sum()                     # 1.5
sigma2 = (x**2 * px).sum() - mu**2          # 0.75   computational
((x - mu)**2 * px).sum()                    # 0.75   definition
sigma  = np.sqrt(sigma2)                    # 0.866

# scipy's frozen discrete distribution does it all
dist = stats.rv_discrete(name="coins", values=(x, px))
dist.mean(), dist.var(), dist.std()         # 1.5, 0.75, 0.866
dist.cdf(2)                                 # P(X ≤ 2) -> 0.875
dist.pmf(2)                                 # P(X = 2) -> 0.375

# ── Cumulative distribution ────────────────────────────────────────
px.cumsum()                                 # 0.125 0.5 0.875 1.0

# ── Raffle example ─────────────────────────────────────────────────
gain = np.array([1995, 95, -5])
p    = np.array([1/1000, 5/1000, 994/1000])
(gain * p).sum()                            # -2.5

# ── Insurance example ──────────────────────────────────────────────
(np.array([250, -99_750]) * np.array([0.999, 0.001])).sum()   # 150.0

# ── Simulation ─────────────────────────────────────────────────────
rng = np.random.default_rng(1)
sim = rng.choice(x, size=100_000, p=px)
sim.mean(), sim.var()                       # ≈ 1.5, ≈ 0.75
pd.Series(sim).value_counts(normalize=True).sort_index()

Quick Reference

Quantity Formula Excel R Python
Validity Σ P(x) = 1 SUM(B:B) sum(px) px.sum()
Mean μ Σ x·P(x) SUMPRODUCT(x, p) sum(x*px) (x*px).sum()
Variance σ² Σ x²P(x) − μ² SUMPRODUCT(x^2,p)-μ^2 sum(x^2*px)-mu^2 (x**2*px).sum()-mu**2
SD σ √σ² SQRT(...) sqrt(sigma2) np.sqrt(sigma2)
CDF Σ_{t≤x} P(t) SUM($B$2:B2) cumsum(px) px.cumsum()
Simulate RAND() + lookup sample(x, n, TRUE, px) rng.choice(x, n, p=px)
E(aX+b) a·E(X)+b
Var(aX+b) a²·Var(X)

Common Mistakes

  • Forgetting to verify Σ P(x) = 1 before computing anything.
  • Expecting E(X) to be an attainable value — 1.5 heads and 2.4 children are perfectly valid expectations.
  • Using n − 1 in the variance of a probability distribution. There is no sample here.
  • Adding standard deviations instead of variances when combining independent variables.
  • In gain/loss problems, forgetting that the cost is already paid: the raffle winner's net gain is 2000 − 5, not 2000.

Exercises: 06-01: Exercises — Random Variables and Expected Value


⬅️ Previous: 05-03: Bayes' Theorem and Counting Rules ➡️ Next: 06-02: Binomial and Poisson Distributions