Part III — Probability, Information Theory & Bayesian Inference · Chapter 2

Expectation, variance & covariance

Hook

A distribution has a center and a spread, and two distributions can move together or apart. Three numbers capture all of that — but only if you actually accumulate them term by term instead of eyeballing the table.

Intuition
outcomexyp
1120.1
2210.3
3340.2
4430.3
5550.1

Each row of the table is one outcome; each bar is that outcome's own contribution pixip_i \cdot x_i (or piyip_i \cdot y_i) to the mean. Toggle between XX and YY — the bars change, but they always add up to exactly the number printed underneath.

Formalize

The expectation (mean) of a discrete random variable weights every outcome by how likely it is:

E[X]=ipixi\mathbb{E}[X] = \sum_i p_i\, x_i
  • E[X]\mathbb{E}[X] — the expected value of XX: its probability-weighted average.
  • pip_i — the probability of outcome ii.
  • xix_i — the value XX takes on outcome ii.

Variance measures spread around that mean, and covariance measures whether two variables move together:

Var(X)=ipi(xiE[X])2Cov(X,Y)=ipi(xiE[X])(yiE[Y])\mathrm{Var}(X) = \sum_i p_i\,(x_i - \mathbb{E}[X])^2 \qquad \mathrm{Cov}(X,Y) = \sum_i p_i\,(x_i - \mathbb{E}[X])(y_i - \mathbb{E}[Y])
  • Var(X)\mathrm{Var}(X) — the expected squared deviation of XX from its own mean.
  • Cov(X,Y)\mathrm{Cov}(X,Y) — the expected product of XX's and YY's deviations from their own means.
  1. Variance is a special case of covariance

    Var(X)=Cov(X,X)\mathrm{Var}(X) = \mathrm{Cov}(X,X) — pairing a variable with itself just squares the deviation.

  2. The sign of covariance is the whole story

    A positive Cov(X,Y)\mathrm{Cov}(X,Y) means XX and YY tend to sit on the same side of their means together; negative means they tend to sit on opposite sides. The magnitude alone is hard to read without normalizing by spread (which is what correlation does).

  3. Every outcome pulls in one direction or another

    An outcome where both XX and YY are above their means, or both below, contributes positively to the covariance sum; an outcome where one is above and the other below contributes negatively.

Play

Switch between Var(X)\mathrm{Var}(X), Var(Y)\mathrm{Var}(Y), and Cov(X,Y)\mathrm{Cov}(X,Y): the bars are the exact terms being summed, and outcome 3 — where x=3x=3 sits right at E[X]\mathbb{E}[X] — contributes nothing to either variance or covariance, because its deviation is zero.

Worked example
  1. Find E[X] and E[Y]
    E[X]=0.1(1)+0.3(2)+0.2(3)+0.3(4)+0.1(5)=3.0\mathbb{E}[X] = 0.1(1) + 0.3(2) + 0.2(3) + 0.3(4) + 0.1(5) = 3.0E[Y]=0.1(2)+0.3(1)+0.2(4)+0.3(3)+0.1(5)=2.7\mathbb{E}[Y] = 0.1(2) + 0.3(1) + 0.2(4) + 0.3(3) + 0.1(5) = 2.7
  2. Find Var(X)
    Var(X)=0.1(13)2+0.3(23)2+0.2(0)2+0.3(1)2+0.1(2)2=0.4+0.3+0+0.3+0.4=1.4\mathrm{Var}(X) = 0.1(1{-}3)^2 + 0.3(2{-}3)^2 + 0.2(0)^2 + 0.3(1)^2 + 0.1(2)^2 = 0.4+0.3+0+0.3+0.4 = 1.4
  3. Find Cov(X, Y)

    The five outcomes are (xi,yi)(x_i,y_i): (1,2)(1,2), (2,1)(2,1), (3,4)(3,4), (4,3)(4,3), (5,5)(5,5), with the same probabilities 0.1,0.3,0.2,0.3,0.10.1, 0.3, 0.2, 0.3, 0.1 as above. Subtract each mean and multiply the deviations:

    • (13)(22.7)=(2)(0.7)=1.4(1{-}3)(2{-}2.7)=(-2)(-0.7)=1.4
    • (23)(12.7)=(1)(1.7)=1.7(2{-}3)(1{-}2.7)=(-1)(-1.7)=1.7
    • (33)(42.7)=(0)(1.3)=0(3{-}3)(4{-}2.7)=(0)(1.3)=0
    • (43)(32.7)=(1)(0.3)=0.3(4{-}3)(3{-}2.7)=(1)(0.3)=0.3
    • (53)(52.7)=(2)(2.3)=4.6(5{-}3)(5{-}2.7)=(2)(2.3)=4.6

    Weighting each by its outcome's probability and summing:

    Cov(X,Y)=0.1(1.4)+0.3(1.7)+0.2(0)+0.3(0.3)+0.1(4.6)=1.2\mathrm{Cov}(X,Y) = 0.1(1.4) + 0.3(1.7) + 0.2(0) + 0.3(0.3) + 0.1(4.6) = 1.2

    Positive — when XX swings away from its mean, YY tends to swing the same direction.

Checkpoint

Using the same outcome table, compute Var(Y) by hand.

outcomexyp
1120.1
2210.3
3340.2
4430.3
5550.1
Pick a value to try it
Summary
E[X]=ipixiVar(X)=ipi(xiE[X])2Cov(X,Y)=ipi(xiE[X])(yiE[Y])\mathbb{E}[X] = \sum_i p_i x_i \qquad \mathrm{Var}(X) = \sum_i p_i (x_i-\mathbb{E}[X])^2 \qquad \mathrm{Cov}(X,Y) = \sum_i p_i (x_i-\mathbb{E}[X])(y_i-\mathbb{E}[Y])

These three sums are the entire vocabulary for "where a distribution sits," "how spread out it is," and "whether two variables move together" — every model that reports a mean, a variance, or a correlation is just this arithmetic in disguise.