Skip to main content
Back to module
Probability & Statistical Inference · Probability Models

Random variables

Reading32mLesson 1 of 3 in this module

Describe an uncertain quantity with a random variable and compute its expected value and variance.

Why this matters

Statistics Fundamentals taught you to summarise data you already have. Probability goes the other way: it describes data you have not seen yet. How many defective parts will tomorrow's batch contain? How much will an insurance policy pay out next year? How long until the next server request arrives? Each of these is a random variable, and two numbers, its expected value and its variance, drive pricing, staffing, quality limits and model evaluation in every career this course serves.

The core idea

A random variable is a rule that turns the outcome of a random process into a number. Roll a die and record the face: that number is a random variable, usually written with a capital letter, X. A particular value it takes is written in lower case, x.

A discrete random variable takes separate, countable values (0, 1, 2, ... defects). A continuous one can take any value in a range (a waiting time of 2.37 seconds). This lesson focuses on discrete ones; continuous variables come in lesson 3.

A discrete random variable is fully described by its probability mass function (PMF): a list of each possible value and its probability. Two rules always hold: every probability is between 0 and 1, and the probabilities add up to exactly 1.

X = face of a fair die
x      1    2    3    4    5    6
P(x)  1/6  1/6  1/6  1/6  1/6  1/6     (sum = 1)

Expected value

The expected value (or mean) E[X] is the probability-weighted average of the values:

E[X] = sum of x * P(x)
die: (1 + 2 + 3 + 4 + 5 + 6) / 6 = 3.5

You can never roll 3.5. The expected value is the long-run average over many repetitions, not a value you expect to see on one try.

Variance and standard deviation

The variance measures how far values typically land from the mean, weighting each squared distance by its probability:

Var(X) = sum of P(x) * (x - E[X])^2
die: [(1-3.5)^2 + (2-3.5)^2 + ... + (6-3.5)^2] / 6 = 17.5 / 6 = 35/12 ≈ 2.917
SD(X)  = sqrt(Var(X)) ≈ 1.708

A handy equivalent form is Var(X) = E[X^2] - (E[X])^2. It is quicker by hand but can lose precision on a computer when the two terms are large and nearly equal, so code usually uses the first form.

Worked example: an insurance policy

A one-year policy pays $10,000 if a claim happens (probability 0.01) and $0 otherwise.

x        0       10,000
P(x)   0.99       0.01

E[X]   = 0 * 0.99 + 10,000 * 0.01 = $100
Var(X) = 0.99 * (0 - 100)^2 + 0.01 * (10,000 - 100)^2
       = 9,900 + 980,100 = 990,000
SD(X)  ≈ $995

The insurer's expected cost is $100 per policy, but the standard deviation is almost ten times larger. A single policy is very risky; a portfolio of thousands is not, because (as you will see in module 2) averages are far less variable than single values. This is the basic logic of actuarial pricing, and it is the opening topic of the Society of Actuaries' Exam P (Probability). Exam details change, so read the current syllabus at soa.org. Checked on 2026-10-23.

Rules you will use constantly

For constants a and b and random variables X and Y:

  • E[aX + b] = a E[X] + b: shifting and scaling move the mean the same way.
  • Var(aX + b) = a^2 Var(X): adding a constant does not change spread; scaling by a multiplies variance by a^2.
  • E[X + Y] = E[X] + E[Y], always, even if X and Y are related.
  • Var(X + Y) = Var(X) + Var(Y) only when X and Y are independent.

Example: the sum of two independent dice has mean 3.5 + 3.5 = 7 and variance 35/12 + 35/12 ≈ 5.83.

In Python

A dictionary from value to probability is a natural PMF:

die = {face: 1 / 6 for face in range(1, 7)}
mean = sum(x * p for x, p in die.items())
print(mean)   # 3.5

In data work you will also meet numpy arrays of values and probabilities, and scipy.stats objects such as scipy.stats.binom(n, p).mean() that compute these for named distributions.

Common mistakes

  • Forgetting to check the PMF. If the probabilities do not add to 1 (within rounding), every result built on them is wrong. Validate first.
  • Adding standard deviations. Variances of independent variables add; standard deviations do not. Two independent dice have SD sqrt(5.83) ≈ 2.42, not 1.71 + 1.71.
  • Reading E[X] as "most likely". The most likely value is the mode. For the insurance policy the mode is $0, while the mean is $100.
  • Confusing population and sample variance. A PMF describes the whole population, so there is no n - 1 here.

Recap

  • A random variable maps random outcomes to numbers; a PMF lists values and probabilities that sum to 1.
  • E[X] is the long-run average; Var(X) is the probability-weighted squared distance from it.
  • Means always add; variances add only for independent variables.

Try it

Open the Do tab and write the variance and validity check for a PMF stored as a Python dictionary.

Also in this lesson: