← Articles

3. Distributions, Expectation, and Variance

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 XX, which is 11 when heads is flipped and 00 when tails is flipped. We have mapped both possible outcomes in our sample space to real numbers, so XX 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 XX belongs to the Bernoulli Distribution: the distribution of a “Bernoulli Trial,” an experiment with only two outcomes, success or failure, with some probability pp. We formalize this as:

X={1with probability p0with probability 1pX = \begin{cases}1 & \text{with probability } p \\ 0 & \text{with probability } 1-p\end{cases}

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 ff, the pmf of a random variable XX taking values {x1,x2,x3,}\{x_1, x_2, x_3, \ldots\} is

fX(x)=P(X=x)f_X(x)=\mathbb{P}(X=x)

Applying this to our Bernoulli random variable gives fX(1)=pf_X(1) = p and fX(0)=1pf_X(0) = 1-p. Another random variable we have already encountered is the one that counts the number of heads flipped in nn coin flips. This belongs to the Binomial Distribution.

Question 1: What is the pmf of XBinom(n,p)X \sim \text{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: pp for the Bernoulli distribution, and (n,p)(n, p) for the Binomial.

We write XBern(p)X \sim \text{Bern}(p) to denote that XX comes from the Bernoulli distribution with parameter pp, and XBinom(n,p)X \sim \text{Binom}(n, p) for the Binomial with parameters nn and pp. 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,,XnBern(p)X_1, X_2, \ldots, X_n \sim \text{Bern}(p), prove that i=1nXiBinom(n,p)\sum_{i=1}^{n} X_i \sim \text{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)f_{X,Y}(x,y) = \mathbb{P}(X=x, Y=y)

The same rules that apply to ordinary distributions apply here: fX,Y(x,y)0f_{X,Y}(x,y) \geq 0 and xyfX,Y(x,y)=1\sum_{x} \sum_{y} f_{X,Y}(x,y) = 1. Note, when I write x\sum_x or y\sum_{y}, it means I want to sum over all possible outputs of the random variable YY

The explorer below lets you choose marginal distributions for XX and YY independently and visualize the joint PMF that results when they are independent.

Question 4: Prove that random variables XX and YY are independent if and only if fX,Y(x,y)=fX(x)fY(y)f_{X,Y}(x,y) = f_X(x) \cdot f_Y(y).

Answer

First, suppose XX and YY are independent. Then:

fX,Y(x,y)=P(X=x,Y=y)=P(X=x)P(Y=y)by independence=fX(x)fY(y)\begin{align*} f_{X,Y}(x,y) &= \mathbb{P}(X=x, Y=y) \\ &= \mathbb{P}(X=x) \cdot \mathbb{P}(Y=y) && \text{by independence} \\ &= f_X(x) \cdot f_Y(y) \end{align*}

This proves the forward direction. The reverse direction is similar and is left to the reader.

We have marginal distributions fX(x)=P(X=x)f_X(x) = \mathbb{P}(X=x) and joint distributions fX,Y(x,y)=P(X=x,Y=y)f_{X,Y}(x,y) = \mathbb{P}(X=x, Y=y). There is also the conditional probability mass function:

fXY(x,y)=P(X=xY=y)f_{X|Y}(x, y) = \mathbb{P}(X=x \mid Y=y)

Question 5: Write fX,Y(x,y)f_{X,Y}(x,y) in terms of fY(y)f_Y(y) and fXY(x,y)f_{X|Y}(x,y).

AnswerfX,Y(x,y)=P(X=x,Y=y)=P(X=xY=y)P(Y=y)=fXY(x,y)fY(y)\begin{align*} f_{X,Y}(x,y) &= \mathbb{P}(X=x, Y=y) \\ &= \mathbb{P}(X=x \mid Y=y) \cdot \mathbb{P}(Y=y) \\ &= f_{X|Y}(x,y) \cdot f_Y(y) \end{align*}

Question 6: Write fX(x)f_X(x) in terms of fX,Y(x,y)f_{X,Y}(x,y).

AnswerfX(x)=P(X=x)=yP(X=x,Y=y)=yfX,Y(x,y)\begin{align*} f_X(x) &= \mathbb{P}(X=x) \\ &= \sum_{y} \mathbb{P}(X=x, Y=y) \\ &= \sum_{y} f_{X,Y}(x,y) \end{align*}

Expected Value

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:ΩRX: \Omega \mapsto \mathbb{R}:

E[X]=xRxf(x)=xRxP(X=x)\mathbb{E}[X]=\sum_{x \in \mathbb{R}} x \cdot f(x) = \sum_{x \in \mathbb{R}} x \cdot \mathbb{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=1X = 1 for heads and X=0X = 0 for tails:

E[X]=0P(X=0)+1P(X=1)=12\begin{align*} \mathbb{E}[X] &= 0 \cdot \mathbb{P}(X=0) + 1 \cdot \mathbb{P}(X=1) = \frac{1}{2} \end{align*}

Question 7: You go to a casino where two games each cost \5.Game. Game Aflipsacoinwithflips a coin withp = 1/100ofheads,payingoutof heads, paying out$500onheads.Gameon heads. GameBflipsafaircoin,payingoutflips a fair coin, paying out$4$ on heads. What is the expected value of each game, and of playing both once?

Answer

Framing as random variables:

A={5with probability 99100500with probability 1100B={5with probability 124with probability 12\begin{align*} A &= \begin{cases} -5 & \text{with probability } \tfrac{99}{100} \\ 500 & \text{with probability } \tfrac{1}{100}\end{cases} & B &= \begin{cases} -5 & \text{with probability } \tfrac{1}{2} \\ 4 & \text{with probability } \tfrac{1}{2}\end{cases} \end{align*}

Then E[A]=599100+5001100=0.05\mathbb{E}[A] = -5 \cdot \tfrac{99}{100} + 500 \cdot \tfrac{1}{100} = 0.05 and E[B]=512+412=0.5\mathbb{E}[B] = -5 \cdot \tfrac{1}{2} + 4 \cdot \tfrac{1}{2} = -0.5.

Expanding E[A+B]\mathbb{E}[A+B] over all joint outcomes:

E[A+B]=((5)+(5))P(A=tails,B=tails)+((500)+(5))P(A=heads,B=tails)+((5)+(4))P(A=tails,B=heads)+((500)+(4))P(A=heads,B=heads)=0.45\begin{align*} \mathbb{E}[A + B] &= ((-5) + (-5)) \cdot \mathbb{P}(A=\text{tails}, B=\text{tails}) \\ &+ ((500) + (-5)) \cdot \mathbb{P}(A=\text{heads}, B=\text{tails}) \\ &+ ((-5) + (4)) \cdot \mathbb{P}(A=\text{tails}, B=\text{heads}) \\ &+ ((500) + (4)) \cdot \mathbb{P}(A=\text{heads}, B=\text{heads}) \\ &= -0.45 \end{align*}

Observe that E[A+B]=E[A]+E[B]=0.05+(0.5)=0.45\mathbb{E}[A+B] = \mathbb{E}[A] + \mathbb{E}[B] = 0.05 + (-0.5) = -0.45.

Question 8: Prove that for any random variables XX, YY and constants aa, bb: E[aX+bY]=aE[X]+bE[Y]\mathbb{E}[aX+bY]=a\mathbb{E}[X]+b\mathbb{E}[Y].

Answer

This is Linearity of Expectation:

E[aX+bY]=x,y(ax+by)P(X=x,Y=y)=axyxP(X=x,Y=y)+byxyP(X=x,Y=y)=axxP(X=x)+byyP(Y=y)=aE[X]+bE[Y]\begin{align*} \mathbb{E}[aX+bY] &= \sum_{x, y}(ax+by)\,\mathbb{P}(X=x,Y=y)\\ &= a\sum_{x}\sum_{y}x\,\mathbb{P}(X=x,Y=y)+b\sum_{y}\sum_{x}y\,\mathbb{P}(X=x,Y=y)\\ &= a\sum_{x}x\,\mathbb{P}(X=x)+b\sum_{y}y\,\mathbb{P}(Y=y)\\ &= a\mathbb{E}[X] + b\mathbb{E}[Y] \end{align*}

Question 9: Let XBinom(n,p)X \sim \text{Binom}(n,p). Compute E[X]\mathbb{E}[X].

Answer

Let X1,X2,,XnBern(p)X_1, X_2, \ldots, X_n \sim \text{Bern}(p) be the i.i.d. Bernoulli trials comprising XX. By linearity of expectation:

E[X]=E ⁣[i=1nXi]=i=1nE[Xi]=np\mathbb{E}[X] = \mathbb{E}\!\left[\sum_{i=1}^{n} X_i\right] = \sum_{i=1}^{n} \mathbb{E}[X_i] = n \cdot p

Question 10: Let XBinom(n,p)X \sim \text{Binom}(n,p) and let f(x)=2xf(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 pp?

Answer

Let XX be the number of flips until the first heads. The probability that the first heads occurs on flip kk requires all prior flips to be tails. Since flips are independent:

P(X=k)=(1p)k1p\mathbb{P}(X = k) = (1-p)^{k-1} \cdot p

Therefore:

E[X]=i=1i(1p)i1p\mathbb{E}[X] = \sum_{i=1}^\infty i \cdot (1-p)^{i-1} \cdot p

To simplify, note that:

E[X]=p+i=2i(1p)i1p=p+i=1(i+1)(1p)ip\begin{align*} \mathbb{E}[X] &= p + \sum_{i=2}^\infty i \cdot (1-p)^{i-1} \cdot p \\ &= \textcolor{royalblue}{p} + \textcolor{green}{\sum_{i=1}^\infty (i+1) \cdot (1-p)^{i} \cdot p} \end{align*}

Note also that (1p)E[X]=i=1i(1p)ip(1-p)\mathbb{E}[X] = \textcolor{green}{\displaystyle\sum_{i=1}^\infty i \cdot (1-p)^{i} \cdot p}. Subtracting:

E[X](1p)E[X]=i=1(i+1)(1p)ipi=1i(1p)ip+ppE[X]=i=1(1p)ip+ppE[X]=1p+pE[X]=1p\begin{align*} \textcolor{purple}{\mathbb{E}[X] - (1-p)\mathbb{E}[X]} &= \textcolor{green}{\sum_{i=1}^\infty (i+1) \cdot (1-p)^{i} \cdot p} - \textcolor{green}{\sum_{i=1}^\infty i \cdot (1-p)^{i} \cdot p} + \textcolor{royalblue}{p} \\[4pt] \textcolor{purple}{p \cdot \mathbb{E}[X]} &= \textcolor{green}{\sum_{i=1}^\infty (1-p)^{i} \cdot p} + \textcolor{royalblue}{p} \\[4pt] \textcolor{purple}{p \cdot \mathbb{E}[X]} &= \textcolor{green}{1-p} + \textcolor{royalblue}{p} \\[4pt] \mathbb{E}[X] &= \dfrac{1}{p} \end{align*}

For a fair coin (p=1/2p = 1/2), the expected number of flips is 22.

LOTUS and Jensen’s Inequality

LOTUS

The humorously named Law of the Unconscious Statistician (LOTUS) states that for any function gg:

E[g(X)]=xg(x)f(x)\mathbb{E}[g(X)] = \sum_{x} g(x) \cdot 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}Y = \{y : \exists\, x \in \Omega_X,\ g(x) = y\} be the range of gg, and let Xy={xΩXg(x)=y}X_y = \{x \in \Omega_X \mid g(x) = y\}. For any yiyjy_i \neq y_j, the sets XyiX_{y_i} and XyjX_{y_j} are disjoint, which enables:

E[g(X)]=yYyP(g(X)=y)=yYyxXyP(X=x)=yYxXyg(x)P(X=x)(y=g(x) for all xXy)=xg(x)P(X=x)\begin{align*} \mathbb{E}[g(X)] &= \sum_{y \in Y} y \cdot \mathbb{P}(g(X)=y) \\ &= \sum_{y \in Y} y \cdot \sum_{x \in X_y} \mathbb{P}(X=x) \\ &= \sum_{y \in Y} \sum_{x \in X_y} g(x)\, \mathbb{P}(X=x) && (y = g(x) \text{ for all } x \in X_y) \\ &= \sum_{x} g(x)\, \mathbb{P}(X=x) \end{align*}

Jensen’s Inequality

Given any convex function ϕ\phi, Jensen’s Inequality states that for any random variable XX:

ϕ(E[X])E[ϕ(X)]\phi(\mathbb{E}[X]) \leq \mathbb{E}[\phi(X)]

For a concave ϕ\phi, the inequality reverses. The interactive lab below lets you build geometric intuition: drag the control points to reshape ϕ\phi, click the xx-axis to place probability mass, then click “Show Jensen’s” to compare ϕ(E[X])\phi(\mathbb{E}[X]) and E[ϕ(X)]\mathbb{E}[\phi(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 ⁣[(XE[X])2]\mathbb{V}(X) = \mathbb{E}\!\left[(X - \mathbb{E}[X])^2\right]

We center XX 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.

Question 12: Show that V(X)=E[X2]E[X]2\mathbb{V}(X) = \mathbb{E}[X^2] - \mathbb{E}[X]^2.

AnswerV(X)=E ⁣[(XE[X])2]=E ⁣[X22E[X]X+E[X]2]=E[X2]2E[X]E[X]+E[X]2=E[X2]E[X]2\begin{align*} \mathbb{V}(X) &= \mathbb{E}\!\left[(X-\mathbb{E}[X])^2\right] \\ &= \mathbb{E}\!\left[X^2 - 2\mathbb{E}[X]\,X + \mathbb{E}[X]^2\right] \\ &= \mathbb{E}[X^2] - 2\mathbb{E}[X]\,\mathbb{E}[X] + \mathbb{E}[X]^2 \\ &= \mathbb{E}[X^2] - \mathbb{E}[X]^2 \end{align*}

For independent random variables X1,,XnX_1, \ldots, X_n and constants a1,,ana_1, \ldots, a_n:

V ⁣(i=1naiXi)=i=1nai2V(Xi)\mathbb{V}\!\left(\sum_{i=1}^{n} a_i X_i\right) = \sum_{i=1}^{n} a_i^2\, \mathbb{V}(X_i)

Proof:

V ⁣(i=1naiXi)=E ⁣[ ⁣(i=1naiXi) ⁣2]E ⁣[i=1naiXi] ⁣2=i=1nai2E[Xi2]+ijaiajE[XiXj]i=1nai2E[Xi]2ijaiajE[Xi]E[Xj]=i=1nai2E[Xi2]i=1nai2E[Xi]2E[XiXj]=E[Xi]E[Xj] by independence=i=1nai2V(Xi)\begin{align*} \mathbb{V}\!\left(\sum_{i=1}^{n}a_iX_i\right) &= \textcolor{green}{\mathbb{E}\!\left[\!\left(\sum_{i=1}^{n}a_iX_i\right)^{\!2}\right]} - \textcolor{royalblue}{\mathbb{E}\!\left[\sum_{i=1}^{n}a_iX_i\right]^{\!2}} \\ &= \textcolor{limegreen}{\sum_{i=1}^{n}a_i^2\mathbb{E}[X_i^2]} + \textcolor{darkgreen}{\sum_{i\neq j}a_ia_j\mathbb{E}[X_iX_j]} - \textcolor{cornflowerblue}{\sum_{i=1}^{n}a_i^2\mathbb{E}[X_i]^2} - \textcolor{navy}{\sum_{i\neq j}a_ia_j\mathbb{E}[X_i]\mathbb{E}[X_j]} \\ &= \textcolor{limegreen}{\sum_{i=1}^{n}a_i^2\mathbb{E}[X_i^2]} - \textcolor{cornflowerblue}{\sum_{i=1}^{n}a_i^2\mathbb{E}[X_i]^2} && \textcolor{darkgreen}{\mathbb{E}[X_iX_j]} = \textcolor{navy}{\mathbb{E}[X_i]\mathbb{E}[X_j]} \text{ by independence} \\ &= \sum_{i=1}^{n}a_i^2\mathbb{V}(X_i) \end{align*}

Question 13: What is the variance of XBinom(n,p)X \sim \text{Binom}(n, p)?

Answer

Write X=i=1nXiX = \sum_{i=1}^n X_i where X1,,XnBern(p)X_1, \ldots, X_n \sim \text{Bern}(p) are independent. By the formula above (with ai=1a_i = 1):

V(X)=i=1nV(Xi)=nV(X1)\mathbb{V}(X) = \sum_{i=1}^n \mathbb{V}(X_i) = n \cdot \mathbb{V}(X_1)

For a single Bernoulli trial: E[X12]=02(1p)+12p=p\mathbb{E}[X_1^2] = 0^2 \cdot (1-p) + 1^2 \cdot p = p, so:

V(X1)=E[X12]E[X1]2=pp2=p(1p)\mathbb{V}(X_1) = \mathbb{E}[X_1^2] - \mathbb{E}[X_1]^2 = p - p^2 = p(1-p)

Therefore V(X)=np(1p)\mathbb{V}(X) = np(1-p).

Conditional Expected Value and Variance

Given random variables XX and YY, we can define conditional expectations E[YX]\mathbb{E}[Y \mid X] and conditional variance V(YX)\mathbb{V}(Y \mid X).

Question 14: You have two coins: coin 1 has p=0.25p = 0.25 and coin 2 has p=0.50p = 0.50. You pick one at random and flip it 100100 times. What is the expected number of heads?

Answer

Let X{1,2}X \in \{1, 2\} indicate which coin was chosen, and YY be the number of heads. We have E[YX=1]=25\mathbb{E}[Y \mid X=1] = 25 and E[YX=2]=50\mathbb{E}[Y \mid X=2] = 50. Since both coins are equally likely:

E[Y]=12E[YX=1]+12E[YX=2]=12(25+50)=37.5\mathbb{E}[Y] = \frac{1}{2} \cdot \mathbb{E}[Y \mid X=1] + \frac{1}{2} \cdot \mathbb{E}[Y \mid X=2] = \frac{1}{2}(25 + 50) = 37.5

Question 15: You flip a fair coin 100100 times, recording h1h_1 heads. You then build a weighted coin with p=h1/100p = h_1/100 and flip it 100100 more times. What is the expected number of heads from the second experiment?

Answer

Let XX be the number of heads from the first (fair) experiment, and YY the number of heads from the second. Since the second coin has probability X/100X/100 of heads and is flipped 100100 times, we have YXBinom(100,X/100)Y \mid X \sim \text{Binom}(100, X/100). Therefore:

E[YX]=100X100=X\mathbb{E}[Y \mid X] = 100 \cdot \frac{X}{100} = X

By the law of total expectation (Question 16):

E[Y]=E[E[YX]]=E[X]\mathbb{E}[Y] = \mathbb{E}[\mathbb{E}[Y \mid X]] = \mathbb{E}[X]

Since XBinom(100,0.5)X \sim \text{Binom}(100, 0.5), we have E[X]=50\mathbb{E}[X] = 50, and therefore E[Y]=50\mathbb{E}[Y] = 50.

Question 16: Prove that E[E[YX]]=E[Y]\mathbb{E}[\mathbb{E}[Y \mid X]] = \mathbb{E}[Y]. (Law of Total Expectation)

Answer

(To be added.)

Question 17: Prove that V(Y)=E[V(YX)]+V(E[YX])\mathbb{V}(Y) = \mathbb{E}[\mathbb{V}(Y \mid X)] + \mathbb{V}(\mathbb{E}[Y \mid 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 nn \to \infty in Binom(n,p)\text{Binom}(n, p) while keeping pp fixed, the distribution converges to the Normal distribution, parameterized by its mean μ\mu and variance σ2\sigma^2, written N(μ,σ2)\mathcal{N}(\mu, \sigma^2). Its importance cannot be overstated: the Normal distribution underlies much of modern statistical inference.

Poisson Distribution

If we let nn \to \infty while simultaneously letting p0p \to 0 such that np=λnp = \lambda stays constant, we obtain the Poisson distribution, written Pois(λ)\text{Pois}(\lambda). It appears frequently in nature, often modeling the number of discrete events in a fixed time interval. The parameter λ\lambda is both the mean and the variance.

Extra problem: Let XPois(λ)X \sim \text{Pois}(\lambda) and YPois(μ)Y \sim \text{Pois}(\mu) be independent. Show that the conditional distribution of XX given X+Y=nX + Y = n is Binom ⁣(n,λλ+μ)\text{Binom}\!\left(n,\, \dfrac{\lambda}{\lambda + \mu}\right).