Random variables and their distributions, expected value and its properties, variance, and conditional expectation — all through the lens of coins.
What is a Random Variable?
A random variable is technically a mapping between outcomes in the sample space to a real number. The first random variable we examine is X, which is 1 when heads is flipped and 0 when tails is flipped. We have mapped both possible outcomes in our sample space to real numbers, so X is a random variable.
We frequently categorize random variables into distributions. There are many families of distributions, each useful for describing different kinds of random variables. Our random variable X belongs to the Bernoulli Distribution: the distribution of a “Bernoulli Trial,” an experiment with only two outcomes, success or failure, with some probability p. We formalize this as:
X={10with probability pwith probability 1−p
Because we are focusing on coins, most of the random variables we work with will be discrete, meaning they take countably many values. For discrete random variables, we can define the probability mass function (pmf). Commonly denoted f, the pmf of a random variable X taking values {x1,x2,x3,…} is
fX(x)=P(X=x)
Applying this to our Bernoulli random variable gives fX(1)=p and fX(0)=1−p. Another random variable we have already encountered is the one that counts the number of heads flipped in n coin flips. This belongs to the Binomial Distribution.
Question 1: What is the pmf of X∼Binom(n,p)?
Answer
Left to reader.
These two distributions, the Bernoulli and Binomial, are called parameterized because a set of parameters determines the shape of the distribution.
Question 2: How many parameters do the Bernoulli and Binomial distributions have?
Answer
One and two respectively: p for the Bernoulli distribution, and (n,p) for the Binomial.
We write X∼Bern(p) to denote that X comes from the Bernoulli distribution with parameter p, and X∼Binom(n,p) for the Binomial with parameters n and p. A collection of random variables drawn from the same distribution independently are called i.i.d. (independent and identically distributed).
Question 3: Given i.i.d. random variables X1,X2,…,Xn∼Bern(p), prove that ∑i=1nXi∼Binom(n,p).
Answer
Left to reader.
Joint Distributions
Sometimes we look at the distribution of two random variables simultaneously using a joint distribution. A joint pmf describes the probability of two random variables taking specific values at the same time:
fX,Y(x,y)=P(X=x,Y=y)
The same rules that apply to ordinary distributions apply here: fX,Y(x,y)≥0 and ∑x∑yfX,Y(x,y)=1. Note, when I write ∑x or ∑y, it means I want to sum over all possible outputs of the random variable Y
The explorer below lets you choose marginal distributions for X and Y independently and visualize the joint PMF that results when they are independent.
X
Y
Right-drag to rotate · Left-click a bar to inspect
Click a bar to see its probabilities
Question 4: Prove that random variables X and Y are independent if and only if fX,Y(x,y)=fX(x)⋅fY(y).
The expected value of a discrete random variable is a weighted average of its possible values, where each value is weighted by the probability it occurs. Formally, for X:Ω↦R:
E[X]=∑x∈Rx⋅f(x)=∑x∈Rx⋅P(X=x)
This connects to physics: the “first moment” of an object, its center of mass, is calculated in exactly the same way. The expected value is the point where, if you balanced the probability distribution on a fulcrum, it would sit perfectly level.
Applying this to a fair coin flip, with X=1 for heads and X=0 for tails:
E[X]=0⋅P(X=0)+1⋅P(X=1)=21
Question 7: You go to a casino where two games each cost \5.GameAflipsacoinwithp = 1/100ofheads,payingout$500onheads.GameBflipsafaircoin,payingout$4$ on heads. What is the expected value of each game, and of playing both once?
Answer
Framing as random variables:
A={−5500with probability 10099with probability 1001B={−54with probability 21with probability 21
Then E[A]=−5⋅10099+500⋅1001=0.05 and E[B]=−5⋅21+4⋅21=−0.5.
Let X1,X2,…,Xn∼Bern(p) be the i.i.d. Bernoulli trials comprising X. By linearity of expectation:
E[X]=E[∑i=1nXi]=∑i=1nE[Xi]=n⋅p
Question 10: Let X∼Binom(n,p) and let f(x)=2x. (To be added.)
Answer
(To be added.)
Question 11: What is the expected number of flips until you get your first heads, in terms of p?
Answer
Let X be the number of flips until the first heads. The probability that the first heads occurs on flip k requires all prior flips to be tails. Since flips are independent:
For a fair coin (p=1/2), the expected number of flips is 2.
LOTUS and Jensen’s Inequality
LOTUS
The humorously named Law of the Unconscious Statistician (LOTUS) states that for any function g:
E[g(X)]=∑xg(x)⋅f(x)
This feels like it should be obvious, but the reason it’s true is not. Let Y={y:∃x∈ΩX,g(x)=y} be the range of g, and let Xy={x∈ΩX∣g(x)=y}. For any yi=yj, the sets Xyi and Xyj are disjoint, which enables:
E[g(X)]=y∈Y∑y⋅P(g(X)=y)=y∈Y∑y⋅x∈Xy∑P(X=x)=y∈Y∑x∈Xy∑g(x)P(X=x)=x∑g(x)P(X=x)(y=g(x) for all x∈Xy)
Jensen’s Inequality
Given any convex function ϕ, Jensen’s Inequality states that for any random variable X:
ϕ(E[X])≤E[ϕ(X)]
For a concave ϕ, the inequality reverses. The interactive lab below lets you build geometric intuition: drag the control points to reshape ϕ, click the x-axis to place probability mass, then click “Show Jensen’s” to compare ϕ(E[X]) and E[ϕ(X)] directly.
Neither
Challenges
○
Convex φ — drag the ◆ control points until the curve is convex (U-shaped). Jensen's inequality then guarantees φ(𝔼[X]) ≤ 𝔼[φ(X)] for any placement of X.
○
Concave φ — make the curve concave (∩-shaped). Jensen's inequality reverses: φ(𝔼[X]) ≥ 𝔼[φ(X)].
0/2
Both directions — find a curve that is neither convex nor concave. Then, with that same curve, find two different placements of X: one where φ(𝔼[X]) ≤ 𝔼[φ(X)] and one where the inequality flips. Click Show Jensen's for each placement.
Drag ◆ to reshape φ · Click the x-axis to place probability mass · Right-click axis to remove a point
Variance
The variance captures how spread out a distribution is around its mean:
V(X)=E[(X−E[X])2]
We center X by subtracting its mean, square the result (so deviations above and below both contribute positively), then take the expected value of that squared distance. In physics, this is the centered second moment.
For independent random variables X1,…,Xn and constants a1,…,an:
V(∑i=1naiXi)=∑i=1nai2V(Xi)
Proof:
V(i=1∑naiXi)=E(i=1∑naiXi)2−E[i=1∑naiXi]2=i=1∑nai2E[Xi2]+i=j∑aiajE[XiXj]−i=1∑nai2E[Xi]2−i=j∑aiajE[Xi]E[Xj]=i=1∑nai2E[Xi2]−i=1∑nai2E[Xi]2=i=1∑nai2V(Xi)E[XiXj]=E[Xi]E[Xj] by independence
Question 13: What is the variance of X∼Binom(n,p)?
Answer
Write X=∑i=1nXi where X1,…,Xn∼Bern(p) are independent. By the formula above (with ai=1):
V(X)=∑i=1nV(Xi)=n⋅V(X1)
For a single Bernoulli trial: E[X12]=02⋅(1−p)+12⋅p=p, so:
V(X1)=E[X12]−E[X1]2=p−p2=p(1−p)
Therefore V(X)=np(1−p).
Conditional Expected Value and Variance
Given random variables X and Y, we can define conditional expectations E[Y∣X] and conditional variance V(Y∣X).
Question 14: You have two coins: coin 1 has p=0.25 and coin 2 has p=0.50. You pick one at random and flip it 100 times. What is the expected number of heads?
Answer
Let X∈{1,2} indicate which coin was chosen, and Y be the number of heads. We have E[Y∣X=1]=25 and E[Y∣X=2]=50. Since both coins are equally likely:
E[Y]=21⋅E[Y∣X=1]+21⋅E[Y∣X=2]=21(25+50)=37.5
Question 15: You flip a fair coin 100 times, recording h1 heads. You then build a weighted coin with p=h1/100 and flip it 100 more times. What is the expected number of heads from the second experiment?
Answer
Let X be the number of heads from the first (fair) experiment, and Y the number of heads from the second. Since the second coin has probability X/100 of heads and is flipped 100 times, we have Y∣X∼Binom(100,X/100). Therefore:
E[Y∣X]=100⋅100X=X
By the law of total expectation (Question 16):
E[Y]=E[E[Y∣X]]=E[X]
Since X∼Binom(100,0.5), we have E[X]=50, and therefore E[Y]=50.
Question 16: Prove that E[E[Y∣X]]=E[Y]. (Law of Total Expectation)
Answer
(To be added.)
Question 17: Prove that V(Y)=E[V(Y∣X)]+V(E[Y∣X]). (Law of Total Variance)
Answer
(To be added.)
Covariance
(To be added.)
Normal and Poisson Distributions
We are already familiar with the Bernoulli and Binomial distributions. With small modifications to the Binomial, we get two more distributions that appear throughout science.
Normal Distribution
If we let n→∞ in Binom(n,p) while keeping p fixed, the distribution converges to the Normal distribution, parameterized by its mean μ and variance σ2, written N(μ,σ2). Its importance cannot be overstated: the Normal distribution underlies much of modern statistical inference.
Poisson Distribution
If we let n→∞ while simultaneously letting p→0 such that np=λ stays constant, we obtain the Poisson distribution, written Pois(λ). It appears frequently in nature, often modeling the number of discrete events in a fixed time interval. The parameter λ is both the mean and the variance.
Extra problem: Let X∼Pois(λ) and Y∼Pois(μ) be independent. Show that the conditional distribution of X given X+Y=n is Binom(n,λ+μλ).