Type at least 2 letters. Enter opens the first result, Esc closes.

    Probability and Statistics · Lesson 4 · Many random variables

    Joint and conditional distributions, conditional expectation and covariance

    Intermediate 80 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 02 (expectation, variance, LOTUS) and lesson 03 (common distributions). For the last part: symmetric matrices and the spectral theorem (linear algebra 06) and positive semidefinite matrices (linear algebra 07). Time: about 80 minutes, plus the notebook.


    Why this matters #

    • Regression and the MSE loss. A model trained with the squared error learns (in the limit of a lot of data and a flexible model) the conditional expectation E[Y∣X=x]\mathbb{E}[Y \mid X = x]. Section 3 proves that it is the best predictor in mean squared error.
    • Uncertainty of ensembles. The predictive variance of a deep ensemble or a mixture model splits, by the law of total variance, into "average noise of the members" plus "disagreement between the members".
    • Averaging and SGD. The variance of a sum needs the covariances. A mini-batch gradient averages BB per-example gradients; its variance drops like 1/B1/B only when they are uncorrelated.
    • PCA, whitening, Kalman filters. All of them are statements about a covariance matrix: its eigenvectors, its square root, or how it changes under a linear map.

    1. Predict first #

    2. The picture #

    A joint table. Take two discrete random variables. In our running example, a student does two homework problems. XX is the number of problems the student solves (0, 1 or 2), and Y=1Y = 1 if the student went to the tutorial, Y=0Y = 0 if not. The joint PMF is a table: cell (x,y)(x, y) holds P(X=x,Y=y)P(X = x, Y = y), and all cells add up to 1.

    Y=0Y = 0 (no tutorial) Y=1Y = 1 (tutorial) row sum pX(x)p_X(x)
    X=0X = 0 0.2 0.1 0.3
    X=1X = 1 0.2 0.2 0.4
    X=2X = 2 0.0 0.3 0.3
    column sum pY(y)p_Y(y) 0.4 0.6 1

    Three operations on the table give everything in this lesson:

    • Marginal = add up. The row sums are the distribution of XX alone; the column sums are the distribution of YY alone. They are written in the margins of the table, hence the name.
    • Conditional = keep one column and rescale it. Keep the column Y=1Y = 1: (0.1,0.2,0.3)(0.1, 0.2, 0.3). It sums to 0.60.6, so divide by 0.60.6: p(x∣Y=1)=(1/6,1/3,1/2)p(x \mid Y = 1) = (1/6, 1/3, 1/2). This is the distribution of XX in the world where you know that the student went to the tutorial.
    • Independence = every column has the same shape. If XX and YY are independent, knowing YY changes nothing, so every rescaled column equals the marginal pXp_X. Then the table is the outer product pX pY⊤p_X\,p_Y^\top of its two marginals, a matrix of rank 1.
    Interactive figure: loading…

    Click a column to see its conditional distribution. The cells are the joint PMF (blue, darker = more mass); the bars on the right and at the bottom are the marginals. The side panel compares the conditional p(x∣y)p(x \mid y) with the marginal pX(x)p_X(x). Click a cell and move the slider to change its mass (the other cells rescale so the total stays 1). Press Make independent: the table becomes pX pY⊤p_X\,p_Y^\top, every column gets the same shape and the rank drops to 1.

    The conditional mean and the two parts of the spread. The conditional mean E[X∣Y=y]\mathbb{E}[X \mid Y = y] is the balance point of column yy. In the table it is 0.50.5 for Y=0Y = 0 and 4/34/3 for Y=1Y = 1. It is a number for each yy, so it is a function of yy. Averaging it over yy (with weights pYp_Y) gives back the overall mean.

    The overall spread of XX has two sources. Inside each group the values scatter around the group mean (the within-group part). And the group means themselves differ (the between-group part). The law of total variance in section 3 says that the variance is exactly the sum of these two parts.

    Interactive figure: loading…

    Move the sliders and watch the bar. Three classes with their own scores (blue dots) and their own means (orange marks). The bar splits Var⁡(X)\operatorname{Var}(X) into the within-class part E[Var⁡(X∣Y)]\mathbb{E}[\operatorname{Var}(X \mid Y)] and the between-class part Var⁡(E[X∣Y])\operatorname{Var}(\mathbb{E}[X \mid Y]). Set the gap between the means to 0: the between part disappears. New sample draws new students; the sample variance (black tick) stays close to the exact total.

    Covariance as a picture. Draw a cloud of points (x,y)(x, y) and the cross through the means (xˉ,yˉ)(\bar x, \bar y). For each point, the product (x−xˉ)(y−yˉ)(x - \bar x)(y - \bar y) is positive in the upper-right and lower-left quadrants and negative in the other two. The covariance is the average of these products. When most points lie in the positive quadrants, the cloud tilts up and the covariance is positive.

    Interactive figure: loading…

    Move the slider ρ\rho and watch the colours. Each point is coloured by the sign of (x−xˉ)(y−yˉ)(x - \bar x)(y - \bar y): aqua = positive, magenta = negative. The readouts show the sample covariance and correlation. Then drag the yellow point far away: one outlier can change the correlation a lot, because its product is large.

    Quick check

    In the homework table, what is P(X=2∣Y=0)P(X = 2 \mid Y = 0), and what does it say about independence?

    3. Definitions and main results #

    Joint, marginal and conditional distributions #

    Let XX and YY be two random variables defined on the same sample space.

    • The joint PMF (discrete case) is p(x,y)=P(X=x,Y=y)p(x, y) = P(X = x, Y = y).
    • A joint PDF (continuous case) is a function p(x,y)≥0p(x, y) \ge 0 with P((X,Y)∈B)=∬Bp(x,y) dx dyP((X, Y) \in B) = \iint_B p(x, y)\,dx\,dy for every (measurable) set B⊆R2B \subseteq \mathbb{R}^2. As in one dimension, p(x,y)p(x, y) is a density, not a probability: it can be larger than 1.
    • The marginals are pX(x)=∑yp(x,y)p_X(x) = \sum_y p(x, y) and pY(y)=∑xp(x,y)p_Y(y) = \sum_x p(x, y); for densities, replace the sums by integrals, pX(x)=∫p(x,y) dyp_X(x) = \int p(x, y)\,dy.
    • The conditional PMF or PDF of XX given Y=yY = y (where pY(y)>0p_Y(y) > 0) is
    p(x∣y)=p(x,y)pY(y).p(x \mid y) = \frac{p(x, y)}{p_Y(y)} .
    • Multiplying back gives the chain rule: p(x,y)=p(x∣y) pY(y)=p(y∣x) pX(x)p(x, y) = p(x \mid y)\,p_Y(y) = p(y \mid x)\,p_X(x).
    • XX and YY are independent if p(x,y)=pX(x) pY(y)p(x, y) = p_X(x)\,p_Y(y) for all xx and yy. Equivalently, p(x∣y)=pX(x)p(x \mid y) = p_X(x) for every yy with pY(y)>0p_Y(y) > 0: knowing YY does not change the distribution of XX.

    Factorisation criterion. If p(x,y)=g(x) h(y)p(x, y) = g(x)\,h(y) for all x,yx, y, for some functions gg and hh, then XX and YY are independent.

    Proof (densities; for PMFs replace integrals by sums).

    1. Let G=∫g(x) dxG = \int g(x)\,dx and H=∫h(y) dyH = \int h(y)\,dy. The joint density integrates to 1, so GH=1GH = 1.
    2. Integrate out one variable: pX(x)=∫g(x)h(y) dy=g(x) Hp_X(x) = \int g(x)h(y)\,dy = g(x)\,H, and in the same way pY(y)=h(y) Gp_Y(y) = h(y)\,G.
    3. So pX(x) pY(y)=g(x)h(y) GH=p(x,y)p_X(x)\,p_Y(y) = g(x)h(y)\,GH = p(x, y). ■\blacksquare

    For a table this says: XX and YY are independent exactly when the joint table has rank 1. A rank-1 table is an outer product uv⊤uv^\top, so p(x,y)=uxvyp(x, y) = u_x v_y has the form g(x)h(y)g(x)h(y); conversely an independent table is pX pY⊤p_X\,p_Y^\top.

    The support matters. The condition "for all x,yx, y" includes the points where the density is 0, so the set where p>0p > 0 must be a rectangle (a product set). Take p(x,y)=2p(x, y) = 2 on the triangle 0<x<y<10 < x < y < 1 and 0 elsewhere. The formula looks constant, but XX and YY are dependent: X>0.9X > 0.9 forces Y>0.9Y > 0.9. For this density:

    • pX(x)=∫x12 dy=2(1−x)p_X(x) = \int_x^1 2\,dy = 2(1 - x) and pY(y)=∫0y2 dx=2yp_Y(y) = \int_0^y 2\,dx = 2y, both on (0,1)(0, 1);
    • given X=xX = x, p(y∣x)=2/(2(1−x))=1/(1−x)p(y \mid x) = 2 / (2(1 - x)) = 1/(1 - x) on (x,1)(x, 1): YY is uniform on (x,1)(x, 1).

    Conditional expectation, the tower rule and total variance #

    The conditional expectation E[X∣Y=y]=∑xx p(x∣y)\mathbb{E}[X \mid Y = y] = \sum_x x\,p(x \mid y) (or ∫x p(x∣y) dx\int x\,p(x \mid y)\,dx) is a number for each yy. Call this function m(y)m(y). Now put the random variable YY into it:

    E[X∣Y]:=m(Y).\mathbb{E}[X \mid Y] := m(Y).

    This is a random variable, a function of YY. It takes the value m(y)m(y) when Y=yY = y. In the same way, Var⁡(X∣Y=y)=E[X2∣Y=y]−m(y)2\operatorname{Var}(X \mid Y = y) = \mathbb{E}[X^2 \mid Y = y] - m(y)^2, and Var⁡(X∣Y)\operatorname{Var}(X \mid Y) is that function evaluated at YY.

    Theorem (tower rule, also called Adam's law or the law of iterated expectations). If E∣X∣<∞\mathbb{E}|X| < \infty, then E[E[X∣Y]]=E[X]\mathbb{E}\big[\mathbb{E}[X \mid Y]\big] = \mathbb{E}[X].

    Proof (discrete case; the continuous case is the same with integrals).

    1. m(Y)m(Y) is a function of YY, so by LOTUS E[m(Y)]=∑ypY(y) m(y)=∑ypY(y)∑xx p(x,y)pY(y)\mathbb{E}[m(Y)] = \sum_y p_Y(y)\,m(y) = \sum_y p_Y(y) \sum_x x\,\dfrac{p(x, y)}{p_Y(y)}.
    2. The factors pY(y)p_Y(y) cancel: ∑y∑xx p(x,y)\sum_y \sum_x x\,p(x, y).
    3. Sum over yy first: ∑xx pX(x)=E[X]\sum_x x\,p_X(x) = \mathbb{E}[X]. ■\blacksquare

    Theorem (law of total variance, also called Eve's law). If E[X2]<∞\mathbb{E}[X^2] < \infty, then

    Var⁡(X)=E[Var⁡(X∣Y)]⏟within groups+Var⁡(E[X∣Y])⏟between groups.\operatorname{Var}(X) = \underbrace{\mathbb{E}\big[\operatorname{Var}(X \mid Y)\big]}_{\text{within groups}} + \underbrace{\operatorname{Var}\big(\mathbb{E}[X \mid Y]\big)}_{\text{between groups}} .

    Proof. Write m(Y)=E[X∣Y]m(Y) = \mathbb{E}[X \mid Y].

    1. Use Var⁡(X∣Y)=E[X2∣Y]−m(Y)2\operatorname{Var}(X \mid Y) = \mathbb{E}[X^2 \mid Y] - m(Y)^2 and the tower rule for X2X^2: E[Var⁡(X∣Y)]=E[X2]−E[m(Y)2]\mathbb{E}[\operatorname{Var}(X \mid Y)] = \mathbb{E}[X^2] - \mathbb{E}[m(Y)^2].
    2. Use the definition of variance and the tower rule for XX: Var⁡(m(Y))=E[m(Y)2]−(E[m(Y)])2=E[m(Y)2]−(E[X])2\operatorname{Var}(m(Y)) = \mathbb{E}[m(Y)^2] - (\mathbb{E}[m(Y)])^2 = \mathbb{E}[m(Y)^2] - (\mathbb{E}[X])^2.
    3. Add the two lines. The terms E[m(Y)2]\mathbb{E}[m(Y)^2] cancel, and what is left is E[X2]−(E[X])2=Var⁡(X)\mathbb{E}[X^2] - (\mathbb{E}[X])^2 = \operatorname{Var}(X). ■\blacksquare

    Both parts are ≥0\ge 0. So, on average, knowing YY cannot increase the variance: E[Var⁡(X∣Y)]≤Var⁡(X)\mathbb{E}[\operatorname{Var}(X \mid Y)] \le \operatorname{Var}(X).

    The conditional expectation is the best predictor

    Claim. For every function hh, E[(X−m(Y))2]≤E[(X−h(Y))2]\mathbb{E}[(X - m(Y))^2] \le \mathbb{E}[(X - h(Y))^2], where m(Y)=E[X∣Y]m(Y) = \mathbb{E}[X \mid Y].

    Proof.

    1. Write X−h(Y)=(X−m(Y))+(m(Y)−h(Y))X - h(Y) = (X - m(Y)) + (m(Y) - h(Y)) and expand the square.
    2. The cross term is 2 E[(X−m(Y)) (m(Y)−h(Y))]2\,\mathbb{E}[(X - m(Y))\,(m(Y) - h(Y))]. Condition on YY: given YY, the factor m(Y)−h(Y)m(Y) - h(Y) is a constant, and E[X−m(Y)∣Y]=0\mathbb{E}[X - m(Y) \mid Y] = 0. By the tower rule the cross term is 0.
    3. So E[(X−h(Y))2]=E[(X−m(Y))2]+E[(m(Y)−h(Y))2]≥E[(X−m(Y))2]\mathbb{E}[(X - h(Y))^2] = \mathbb{E}[(X - m(Y))^2] + \mathbb{E}[(m(Y) - h(Y))^2] \ge \mathbb{E}[(X - m(Y))^2]. ■\blacksquare

    This is why a regression model trained with the squared loss estimates E[target∣input]\mathbb{E}[\text{target} \mid \text{input}], and why the smallest possible MSE is E[Var⁡(X∣Y)]\mathbb{E}[\operatorname{Var}(X \mid Y)], the within-group part.

    Quick check

    Two classes have the same size. Class A has mean score 60 and variance 25; class B has mean 80 and variance 25. What is the variance of the scores of all students together?

    Covariance and correlation #

    The covariance of XX and YY, with means μX\mu_X and μY\mu_Y, is

    Cov⁡(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−E[X] E[Y].\operatorname{Cov}(X, Y) = \mathbb{E}\big[(X - \mu_X)(Y - \mu_Y)\big] = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y].

    (Expand the product and use linearity: E[XY]−μXμY−μXμY+μXμY\mathbb{E}[XY] - \mu_X\mu_Y - \mu_X\mu_Y + \mu_X\mu_Y.) The correlation is the covariance of the standardised variables,

    ρ=Corr⁡(X,Y)=Cov⁡(X,Y)σXσY,σX,σY>0.\rho = \operatorname{Corr}(X, Y) = \frac{\operatorname{Cov}(X, Y)}{\sigma_X \sigma_Y}, \qquad \sigma_X, \sigma_Y > 0 .

    Properties you will use all the time:

    • Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X), Cov⁡(X,Y)=Cov⁡(Y,X)\operatorname{Cov}(X, Y) = \operatorname{Cov}(Y, X), and Cov⁡(X,c)=0\operatorname{Cov}(X, c) = 0 for a constant cc.
    • Covariance is bilinear: Cov⁡(aX+bY,Z)=aCov⁡(X,Z)+bCov⁡(Y,Z)\operatorname{Cov}(aX + bY, Z) = a\operatorname{Cov}(X, Z) + b\operatorname{Cov}(Y, Z), and the same in the second slot.
    • So Cov⁡(aX+b,cY+d)=acCov⁡(X,Y)\operatorname{Cov}(aX + b, cY + d) = ac\operatorname{Cov}(X, Y), and ρ(aX+b,cY+d)=sign⁡(ac) ρ(X,Y)\rho(aX + b, cY + d) = \operatorname{sign}(ac)\,\rho(X, Y): correlation has no units and ignores shifts and scales, but a negative factor flips its sign.

    Theorem. ∣ρ∣≤1|\rho| \le 1, that is, Cov⁡(X,Y)2≤Var⁡(X)Var⁡(Y)\operatorname{Cov}(X, Y)^2 \le \operatorname{Var}(X)\operatorname{Var}(Y). Equality holds if and only if Y=aX+bY = aX + b with probability 1, for some constants a≠0a \ne 0 and bb.

    Proof.

    1. Standardise: X~=(X−μX)/σX\tilde X = (X - \mu_X)/\sigma_X and Y~=(Y−μY)/σY\tilde Y = (Y - \mu_Y)/\sigma_Y. Both have variance 1, and Cov⁡(X~,Y~)=ρ\operatorname{Cov}(\tilde X, \tilde Y) = \rho.
    2. A variance is never negative. By bilinearity, 0≤Var⁡(X~+Y~)=1+1+2ρ0 \le \operatorname{Var}(\tilde X + \tilde Y) = 1 + 1 + 2\rho and 0≤Var⁡(X~−Y~)=1+1−2ρ0 \le \operatorname{Var}(\tilde X - \tilde Y) = 1 + 1 - 2\rho.
    3. So −1≤ρ≤1-1 \le \rho \le 1.
    4. If ρ=1\rho = 1, then Var⁡(X~−Y~)=0\operatorname{Var}(\tilde X - \tilde Y) = 0, so X~−Y~\tilde X - \tilde Y is a constant with probability 1, and YY is an affine function of XX with a>0a > 0. The case ρ=−1\rho = -1 is the same with X~+Y~\tilde X + \tilde Y and gives a<0a < 0. ■\blacksquare
    Quick check

    Corr⁡(X,Y)=0.6\operatorname{Corr}(X, Y) = 0.6. What is Corr⁡(−2X+1, Y)\operatorname{Corr}(-2X + 1,\ Y)?

    Uncorrelated does not mean independent #

    Independent implies uncorrelated. If XX and YY are independent, then E[XY]=∑x,yxy pX(x) pY(y)=(∑xx pX(x))(∑yy pY(y))=E[X] E[Y]\mathbb{E}[XY] = \sum_{x, y} xy\,p_X(x)\,p_Y(y) = \big(\sum_x x\,p_X(x)\big)\big(\sum_y y\,p_Y(y)\big) = \mathbb{E}[X]\,\mathbb{E}[Y], so Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0.

    The converse is false. Let XX be uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2.

    1. E[X]=0\mathbb{E}[X] = 0 and E[XY]=E[X3]=13(−1+0+1)=0\mathbb{E}[XY] = \mathbb{E}[X^3] = \tfrac13(-1 + 0 + 1) = 0.
    2. So Cov⁡(X,Y)=0−0⋅E[Y]=0\operatorname{Cov}(X, Y) = 0 - 0 \cdot \mathbb{E}[Y] = 0: uncorrelated.
    3. But YY is a function of XX. For example P(Y=0∣X=0)=1P(Y = 0 \mid X = 0) = 1, while P(Y=0)=1/3P(Y = 0) = 1/3: dependent.

    The same happens for Z∼N(0,1)Z \sim \mathcal N(0, 1) and Z2Z^2, since all odd moments of a symmetric density are 0. Correlation measures only the linear part of a dependence. A U-shape, a circle or an X-shape can have correlation 0.

    Interactive figure: loading…

    Switch the examples. Each cloud has correlation ρ^(X,Y)≈0\hat\rho(X, Y) \approx 0, but the orange curve (the mean of YY in narrow bins of XX) or the orange band (± one standard deviation in each bin) changes with XX, so YY depends on XX. The second readout, the correlation of X2X^2 and Y2Y^2, is far from 0 for every dependent example. Only "independent" has both readouts near 0.

    The variance of a sum #

    Theorem.

    Var⁡(∑i=1nXi)=∑i=1nVar⁡(Xi)+2∑i<jCov⁡(Xi,Xj).\operatorname{Var}\Big(\sum_{i=1}^n X_i\Big) = \sum_{i=1}^n \operatorname{Var}(X_i) + 2\sum_{i<j} \operatorname{Cov}(X_i, X_j).

    Proof.

    1. Var⁡(S)=Cov⁡(S,S)\operatorname{Var}(S) = \operatorname{Cov}(S, S) for S=∑iXiS = \sum_i X_i.
    2. By bilinearity, Cov⁡(∑iXi,∑jXj)=∑i∑jCov⁡(Xi,Xj)\operatorname{Cov}\big(\sum_i X_i, \sum_j X_j\big) = \sum_i \sum_j \operatorname{Cov}(X_i, X_j): n2n^2 terms.
    3. The nn terms with i=ji = j are the variances. Every pair i<ji < j appears twice, as (i,j)(i, j) and (j,i)(j, i). ■\blacksquare

    For uncorrelated XiX_i the covariances vanish and the variances simply add. For two variables: Var⁡(X±Y)=Var⁡(X)+Var⁡(Y)±2Cov⁡(X,Y)\operatorname{Var}(X \pm Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) \pm 2\operatorname{Cov}(X, Y).

    The covariance matrix #

    For a random vector X=(X1,…,Xd)⊤X = (X_1, \dots, X_d)^\top with mean μ=E[X]\mu = \mathbb{E}[X], the covariance matrix is

    Σ=Cov⁡(X)=E[(X−μ)(X−μ)⊤],Σij=Cov⁡(Xi,Xj).\Sigma = \operatorname{Cov}(X) = \mathbb{E}\big[(X - \mu)(X - \mu)^\top\big], \qquad \Sigma_{ij} = \operatorname{Cov}(X_i, X_j).

    Its diagonal holds the variances. The correlation matrix is D−1/2 Σ D−1/2D^{-1/2}\,\Sigma\,D^{-1/2} with D=diag⁡(Σ11,…,Σdd)D = \operatorname{diag}(\Sigma_{11}, \dots, \Sigma_{dd}); its entries are the correlations ρij\rho_{ij}.

    Theorem (affine maps). For a fixed matrix A∈Rm×dA \in \mathbb{R}^{m \times d} and vector b∈Rmb \in \mathbb{R}^m,

    Cov⁡(AX+b)=A Σ A⊤.\operatorname{Cov}(AX + b) = A\,\Sigma\,A^\top .

    Proof.

    1. By linearity, E[AX+b]=Aμ+b\mathbb{E}[AX + b] = A\mu + b. So (AX+b)−E[AX+b]=A(X−μ)(AX + b) - \mathbb{E}[AX + b] = A(X - \mu): the shift bb drops out.
    2. Cov⁡(AX+b)=E[A(X−μ)(X−μ)⊤A⊤]=A E[(X−μ)(X−μ)⊤] A⊤=AΣA⊤\operatorname{Cov}(AX + b) = \mathbb{E}\big[A(X - \mu)(X - \mu)^\top A^\top\big] = A\,\mathbb{E}\big[(X - \mu)(X - \mu)^\top\big]\,A^\top = A\Sigma A^\top, because expectation is linear, entry by entry. ■\blacksquare

    With one row, A=a⊤A = a^\top, this gives the variance of any linear combination: Var⁡(a⊤X)=a⊤Σ a\operatorname{Var}(a^\top X) = a^\top \Sigma\, a. With a=(1,…,1)a = (1, \dots, 1) it is again the variance of a sum.

    Theorem. Every covariance matrix is symmetric and positive semidefinite (PSD): a⊤Σa≥0a^\top \Sigma a \ge 0 for every aa. It is singular if and only if some combination a⊤Xa^\top X with a≠0a \ne 0 is constant with probability 1.

    Proof.

    1. Symmetric: Σij=Cov⁡(Xi,Xj)=Cov⁡(Xj,Xi)=Σji\Sigma_{ij} = \operatorname{Cov}(X_i, X_j) = \operatorname{Cov}(X_j, X_i) = \Sigma_{ji}.
    2. PSD: a⊤Σa=Var⁡(a⊤X)≥0a^\top \Sigma a = \operatorname{Var}(a^\top X) \ge 0, by the previous theorem.
    3. Singular: for a PSD matrix, Σa=0\Sigma a = 0 exactly when a⊤Σa=0a^\top \Sigma a = 0 (write a⊤Σa=∥Σ1/2a∥2a^\top \Sigma a = \|\Sigma^{1/2} a\|^2, linear algebra 07). And Var⁡(a⊤X)=0\operatorname{Var}(a^\top X) = 0 exactly when a⊤Xa^\top X is constant: the data lie on a hyperplane. ■\blacksquare

    So a symmetric matrix with a negative eigenvalue is never a covariance matrix: it would give a "variance" a⊤Σa<0a^\top \Sigma a < 0.

    Quick check

    Which matrix can be a covariance matrix?

    Bessel's correction: why the sample variance divides by n−1n - 1 #

    Theorem. Let X1,…,XnX_1, \dots, X_n (n≥2n \ge 2) be i.i.d. with mean μ\mu and variance σ2\sigma^2. Let Xˉ=1n∑iXi\bar X = \frac1n \sum_i X_i and

    S2=1n−1∑i=1n(Xi−Xˉ)2.S^2 = \frac{1}{n - 1} \sum_{i=1}^n (X_i - \bar X)^2 .

    Then E[S2]=σ2\mathbb{E}[S^2] = \sigma^2: S2S^2 is unbiased.

    Proof.

    1. Write Xi−Xˉ=(Xi−μ)−(Xˉ−μ)X_i - \bar X = (X_i - \mu) - (\bar X - \mu), square, and sum. Use ∑i(Xi−μ)=n(Xˉ−μ)\sum_i (X_i - \mu) = n(\bar X - \mu):
    ∑i(Xi−Xˉ)2=∑i(Xi−μ)2−2(Xˉ−μ)⋅n(Xˉ−μ)+n(Xˉ−μ)2=∑i(Xi−μ)2−n(Xˉ−μ)2.\sum_i (X_i - \bar X)^2 = \sum_i (X_i - \mu)^2 - 2(\bar X - \mu) \cdot n(\bar X - \mu) + n(\bar X - \mu)^2 = \sum_i (X_i - \mu)^2 - n(\bar X - \mu)^2 .
    1. Each term (Xi−μ)2(X_i - \mu)^2 has mean σ2\sigma^2, so E[∑i(Xi−μ)2]=nσ2\mathbb{E}\big[\sum_i (X_i - \mu)^2\big] = n\sigma^2.
    2. The XiX_i are independent, so by the variance of a sum Var⁡(Xˉ)=1n2⋅nσ2=σ2/n\operatorname{Var}(\bar X) = \frac{1}{n^2} \cdot n\sigma^2 = \sigma^2/n. Hence E[n(Xˉ−μ)2]=nVar⁡(Xˉ)=σ2\mathbb{E}[n(\bar X - \mu)^2] = n\operatorname{Var}(\bar X) = \sigma^2.
    3. So E[∑i(Xi−Xˉ)2]=nσ2−σ2=(n−1)σ2\mathbb{E}\big[\sum_i (X_i - \bar X)^2\big] = n\sigma^2 - \sigma^2 = (n - 1)\sigma^2. Divide by n−1n - 1. ■\blacksquare

    Why. Xˉ\bar X is the number cc that makes ∑i(Xi−c)2\sum_i (X_i - c)^2 smallest. So the deviations from Xˉ\bar X are, on average, smaller than the deviations from the true μ\mu. Dividing by nn gives the mean n−1nσ2\frac{n-1}{n}\sigma^2, which is too small: "one degree of freedom is spent to estimate μ\mu". The same proof, entry by entry, shows that the sample covariance matrix C=1n−1∑i(Xi−Xˉ)(Xi−Xˉ)⊤C = \frac{1}{n-1} \sum_i (X_i - \bar X)(X_i - \bar X)^\top is unbiased for Σ\Sigma.

    Note: SS is a biased estimator of σ\sigma. The square root is concave, so E[S]<σ\mathbb{E}[S] < \sigma by Jensen's inequality (information theory 03) (unless S2S^2 is constant).

    The covariance matrix and PCA #

    Σ\Sigma is symmetric, so by the spectral theorem (linear algebra 06) Σ=QΛQ⊤\Sigma = Q \Lambda Q^\top, with orthonormal eigenvectors q1,…,qdq_1, \dots, q_d (the columns of QQ) and eigenvalues λ1≥⋯≥λd≥0\lambda_1 \ge \dots \ge \lambda_d \ge 0 (they are ≥0\ge 0 because Σ\Sigma is PSD). In this section only, Λ\Lambda is the diagonal matrix of eigenvalues; from lesson 05 on, Λ\Lambda is the precision matrix.

    • The direction of largest variance. For a unit vector uu, write c=Q⊤uc = Q^\top u; it is also a unit vector. Then Var⁡(u⊤X)=u⊤Σu=∑kλkck2≤λ1∑kck2=λ1\operatorname{Var}(u^\top X) = u^\top \Sigma u = \sum_k \lambda_k c_k^2 \le \lambda_1 \sum_k c_k^2 = \lambda_1, with equality at u=q1u = q_1. So q1q_1 is the direction of largest variance, and that variance is λ1\lambda_1.
    • The principal components are uncorrelated. Y=Q⊤(X−μ)Y = Q^\top (X - \mu) has Cov⁡(Y)=Q⊤ΣQ=Λ\operatorname{Cov}(Y) = Q^\top \Sigma Q = \Lambda, a diagonal matrix.
    • Total variance. ∑iVar⁡(Xi)=tr⁡Σ=∑kλk\sum_i \operatorname{Var}(X_i) = \operatorname{tr}\Sigma = \sum_k \lambda_k. The first kk components explain the fraction (λ1+⋯+λk)/tr⁡Σ(\lambda_1 + \dots + \lambda_k)/\operatorname{tr}\Sigma.

    In two dimensions, Σ\Sigma is drawn as an ellipse: its axes point along q1q_1 and q2q_2, with half-lengths λ1\sqrt{\lambda_1} and λ2\sqrt{\lambda_2} (one standard deviation). PCA on data does the same with the sample covariance CC (linear algebra 09).

    Interactive figure: loading…

    Drag the yellow handle to turn the direction uu, and watch the variance of the projections u⊤xu^\top x. The samples are blue; the orange ellipses are 1 and 2 standard deviations of Σ\Sigma, and the orange arrows are the eigenvectors scaled by λk\sqrt{\lambda_k}. The projected variance u⊤Σuu^\top \Sigma u is largest (=λ1= \lambda_1) when uu points along the long axis, and smallest (=λ2= \lambda_2) along the short axis. Set ρ=0.8\rho = 0.8 and σ1=σ2\sigma_1 = \sigma_2 to get the two-thermometer matrix up to scale.

    Answers to section 1. (1) No. The average is a⊤Xa^\top X with a=(12,12)a = (\tfrac12, \tfrac12), so its variance is a⊤Σa=14(5+5+2⋅4)=4.5a^\top \Sigma a = \tfrac14 (5 + 5 + 2 \cdot 4) = 4.5, only a little less than 5. The difference has variance 5+5−2⋅4=25 + 5 - 2 \cdot 4 = 2. Most of the variance is shared by the two readings, and averaging cannot remove a shared part. (2) (a) Yes, by the tower rule. (b) No. The weighted average of the class variances is only E[Var⁡(X∣Y)]\mathbb{E}[\operatorname{Var}(X \mid Y)]. It misses the between part Var⁡(E[X∣Y])\operatorname{Var}(\mathbb{E}[X \mid Y]), which is positive unless all class means are equal.

    4. Worked example #

    (a) The homework table. We use the table of section 2: rows X=0,1,2X = 0, 1, 2, columns Y=0,1Y = 0, 1.

    1. Marginals. Row sums: pX=(0.3,0.4,0.3)p_X = (0.3, 0.4, 0.3). Column sums: pY=(0.4,0.6)p_Y = (0.4, 0.6).
    2. Mean and variance of XX. E[X]=0⋅0.3+1⋅0.4+2⋅0.3=1\mathbb{E}[X] = 0 \cdot 0.3 + 1 \cdot 0.4 + 2 \cdot 0.3 = 1. E[X2]=0+1⋅0.4+4⋅0.3=1.6\mathbb{E}[X^2] = 0 + 1 \cdot 0.4 + 4 \cdot 0.3 = 1.6. So Var⁡(X)=1.6−12=0.6\operatorname{Var}(X) = 1.6 - 1^2 = 0.6.
    3. Not independent. p(2,0)=0p(2, 0) = 0, but pX(2) pY(0)=0.3⋅0.4=0.12p_X(2)\,p_Y(0) = 0.3 \cdot 0.4 = 0.12. One cell is enough to show dependence.
    4. Conditionals. Column Y=0Y = 0 divided by 0.40.4: p(x∣Y=0)=(0.5,0.5,0)p(x \mid Y = 0) = (0.5, 0.5, 0). Its mean is 0.50.5, and its variance is E[X2∣Y=0]−0.52=0.5−0.25=0.25\mathbb{E}[X^2 \mid Y = 0] - 0.5^2 = 0.5 - 0.25 = 0.25. Column Y=1Y = 1 divided by 0.60.6: p(x∣Y=1)=(1/6,1/3,1/2)p(x \mid Y = 1) = (1/6, 1/3, 1/2). Its mean is 1/3+2⋅1/2=4/31/3 + 2 \cdot 1/2 = 4/3. E[X2∣Y=1]=1/3+4⋅1/2=7/3\mathbb{E}[X^2 \mid Y = 1] = 1/3 + 4 \cdot 1/2 = 7/3, so the variance is 7/3−16/9=5/97/3 - 16/9 = 5/9.
    5. Tower rule. E[E[X∣Y]]=0.4⋅0.5+0.6⋅43=0.2+0.8=1=E[X]\mathbb{E}[\mathbb{E}[X \mid Y]] = 0.4 \cdot 0.5 + 0.6 \cdot \tfrac43 = 0.2 + 0.8 = 1 = \mathbb{E}[X]. ✓
    6. Total variance. Within: E[Var⁡(X∣Y)]=0.4⋅0.25+0.6⋅59=0.1+13=1330≈0.433\mathbb{E}[\operatorname{Var}(X \mid Y)] = 0.4 \cdot 0.25 + 0.6 \cdot \tfrac59 = 0.1 + \tfrac13 = \tfrac{13}{30} \approx 0.433. Between: Var⁡(E[X∣Y])=0.4⋅0.52+0.6⋅(43)2−12=0.1+1615−1=16≈0.167\operatorname{Var}(\mathbb{E}[X \mid Y]) = 0.4 \cdot 0.5^2 + 0.6 \cdot (\tfrac43)^2 - 1^2 = 0.1 + \tfrac{16}{15} - 1 = \tfrac16 \approx 0.167. Sum: 1330+530=1830=0.6=Var⁡(X)\tfrac{13}{30} + \tfrac{5}{30} = \tfrac{18}{30} = 0.6 = \operatorname{Var}(X). ✓
    7. Covariance and correlation. E[XY]\mathbb{E}[XY] only gets contributions from the column Y=1Y = 1: E[XY]=1⋅1⋅0.2+2⋅1⋅0.3=0.8\mathbb{E}[XY] = 1 \cdot 1 \cdot 0.2 + 2 \cdot 1 \cdot 0.3 = 0.8. With E[Y]=0.6\mathbb{E}[Y] = 0.6: Cov⁡(X,Y)=0.8−1⋅0.6=0.2\operatorname{Cov}(X, Y) = 0.8 - 1 \cdot 0.6 = 0.2. YY is Bernoulli(0.6), so Var⁡(Y)=0.6⋅0.4=0.24\operatorname{Var}(Y) = 0.6 \cdot 0.4 = 0.24, and ρ=0.2/0.6⋅0.24≈0.527\rho = 0.2/\sqrt{0.6 \cdot 0.24} \approx 0.527. Going to the tutorial goes together with solving more problems. (This is an association, not a proof that the tutorial causes it.)

    (b) Two thermometers. Model: the true temperature TT has variance 4, and thermometer ii reads Xi=T+eiX_i = T + e_i, where the errors e1,e2e_1, e_2 have variance 1 and are independent of each other and of TT.

    1. Var⁡(Xi)=Var⁡(T)+Var⁡(ei)=4+1=5\operatorname{Var}(X_i) = \operatorname{Var}(T) + \operatorname{Var}(e_i) = 4 + 1 = 5.
    2. Cov⁡(X1,X2)=Cov⁡(T+e1,T+e2)=Var⁡(T)=4\operatorname{Cov}(X_1, X_2) = \operatorname{Cov}(T + e_1, T + e_2) = \operatorname{Var}(T) = 4, by bilinearity (all other terms are 0).
    3. So Σ=(5445)\Sigma = \begin{pmatrix} 5 & 4 \\ 4 & 5 \end{pmatrix} and ρ=4/5=0.8\rho = 4/5 = 0.8.
    4. Average and difference at once. Take A=(1/21/21−1)A = \begin{pmatrix} 1/2 & 1/2 \\ 1 & -1 \end{pmatrix}. Then AΣ=(4.54.51−1)A\Sigma = \begin{pmatrix} 4.5 & 4.5 \\ 1 & -1 \end{pmatrix} and AΣA⊤=(4.5002)A\Sigma A^\top = \begin{pmatrix} 4.5 & 0 \\ 0 & 2 \end{pmatrix}. The average has variance 4.5, the difference 2, and they are uncorrelated. The average removes half of the error variance (4+124 + \tfrac12), but none of the variance of TT: that part is signal, not noise.
    5. PCA. The eigenvalues of Σ\Sigma are 99 (eigenvector (1,1)/2(1, 1)/\sqrt 2) and 11 (eigenvector (1,−1)/2(1, -1)/\sqrt 2). Check: Σ(1,1)⊤=(9,9)⊤\Sigma (1, 1)^\top = (9, 9)^\top and Σ(1,−1)⊤=(1,−1)⊤\Sigma (1, -1)^\top = (1, -1)^\top. The first component, the "common temperature" direction, explains 9/10=90%9/10 = 90\% of the total variance tr⁡Σ=10\operatorname{tr}\Sigma = 10.

    (c) Bessel's correction by hand. Data 2,4,92, 4, 9. The mean is xˉ=15/3=5\bar x = 15/3 = 5. The squared deviations are 9,1,169, 1, 16, with sum 2626. So S2=26/2=13S^2 = 26/2 = 13. Dividing by n=3n = 3 would give 8.678.67.

    5. In code #

    NumPy checks the hand computations. The notebook for this lesson has these cells and the exercises.

    import numpy as np
    
    # (a) joint PMF: rows X = 0, 1, 2; columns Y = 0, 1
    P = np.array([[0.2, 0.1],
                  [0.2, 0.2],
                  [0.0, 0.3]])
    x, y = np.array([0, 1, 2]), np.array([0, 1])
    pX, pY = P.sum(axis=1), P.sum(axis=0)        # marginals: row and column sums
    print(pX, pY, np.linalg.matrix_rank(P))      # [0.3 0.4 0.3] [0.4 0.6] 2  -> rank 2: dependent
    cond = P / pY                                # column j is p(x | Y = y_j)
    m = x @ cond                                 # E[X | Y = y]
    v = x**2 @ cond - m**2                       # Var(X | Y = y)
    print(m.round(4), pY @ m)                    # [0.5    1.3333] 1.0   (tower rule)
    print(round(pY @ v, 4), round(pY @ m**2 - (pY @ m)**2, 4))   # 0.4333 0.1667  (sum 0.6)
    EX = x @ pX
    print(round(x @ P @ y - EX * (pY @ y), 4))   # 0.2 = Cov(X, Y)
    
    # (b) Cov(AX + b) = A Σ Aᵀ, exactly and on samples
    Sigma = np.array([[5.0, 4.0], [4.0, 5.0]])
    A, b = np.array([[0.5, 0.5], [1.0, -1.0]]), np.array([3.0, -1.0])
    print(A @ Sigma @ A.T)                       # [[4.5 0. ] [0.  2. ]]
    rng = np.random.default_rng(1)
    X = rng.multivariate_normal([0.0, 0.0], Sigma, size=200_000)   # rows are samples
    print(np.cov((X @ A.T + b).T).round(2))      # about [[4.5 0.] [0. 2.]]
    print(np.linalg.eigvalsh(Sigma))             # [1. 9.]: the PCA variances
    
    # uncorrelated but dependent: X uniform on {-1, 0, 1}, Y = X^2
    xs = rng.choice([-1, 0, 1], size=200_000)
    print(round(np.corrcoef(xs, xs**2)[0, 1], 2))   # about 0.0, yet Y is a function of X
    
    # (c) Bessel: divide by n - 1
    d = np.array([2.0, 4.0, 9.0])
    print(d.var(ddof=1), round(d.var(ddof=0), 2))   # 13.0 8.67
    

    np.cov treats each row as one variable unless you pass rowvar=False; that is why the code passes the transpose. It divides by n−1n - 1 by default.

    6. Where it shows up #

    • Deep ensembles and mixtures. An ensemble of MM Gaussian heads with means μm\mu_m and variances σm2\sigma_m^2 predicts with variance 1M∑mσm2+1M∑m(μm−μˉ)2\frac1M \sum_m \sigma_m^2 + \frac1M \sum_m (\mu_m - \bar\mu)^2. This is the law of total variance with YY = the member. The first part is often called aleatoric (noise in the data), the second epistemic (disagreement of the models).
    • Regression with the squared loss estimates the conditional mean E[Y∣X]\mathbb{E}[Y \mid X] (the best predictor above). The irreducible error is E[Var⁡(Y∣X)]\mathbb{E}[\operatorname{Var}(Y \mid X)].
    • Mini-batch gradients. The mean of BB uncorrelated per-example gradients has 1/B1/B times the variance of one. Correlated examples (duplicates, consecutive frames of a video) reduce less, because of the covariance terms.
    • PCA, whitening and EOFs. PCA uses the eigenvectors of the (sample) covariance. Whitening multiplies by Λ−1/2Q⊤\Lambda^{-1/2} Q^\top, which gives the identity covariance by Cov⁡(AX)=AΣA⊤\operatorname{Cov}(AX) = A\Sigma A^\top. In climate science, empirical orthogonal functions (EOFs) are PCA of space–time anomalies.
    • Ensemble Kalman filter. Weather forecasting estimates the forecast covariance by the sample covariance of an ensemble of model runs, with 1/(N−1)1/(N - 1) (ML for science 06). With few members it has rank at most N−1N - 1 and is noisy.
    • Sums of correlated errors. The error of a total (a portfolio, rainfall over a region, a sum of pixel errors) needs the covariance terms. Positive correlation makes the total much more uncertain than the sum of variances suggests.

    7. Common mistakes #

    • Factorising the formula but not the support. p(x,y)=2p(x, y) = 2 on 0<x<y<10 < x < y < 1 looks like a product, but XX and YY are dependent. The set where p>0p > 0 must be a rectangle.
    • Treating E[X∣Y]\mathbb{E}[X \mid Y] as a number. It is a random variable, a function of YY. Only E[X∣Y=y]\mathbb{E}[X \mid Y = y], for a fixed yy, is a number.
    • Averaging only the group variances. This misses the between part Var⁡(E[X∣Y])\operatorname{Var}(\mathbb{E}[X \mid Y]).
    • Reading zero correlation as independence. XX and X2X^2 are uncorrelated (for symmetric XX) and fully dependent. A correlation near 0 means "no linear relation", not "no relation".
    • Forgetting the covariance terms in Var⁡(X+Y)\operatorname{Var}(X + Y). The variances add only for uncorrelated variables.
    • Dividing by nn and calling the result unbiased, or claiming that SS (not S2S^2) is unbiased for σ\sigma.
    • The rowvar default of np.cov. A data matrix of shape (samples, features) needs rowvar=False or a transpose. Otherwise you get an n×nn \times n matrix of covariances between samples.

    8. Check yourself #

    1. (X,Y)(X, Y) has density p(x,y)=2p(x, y) = 2 on 0<x<y<10 < x < y < 1. Find pXp_X and E[Y∣X=x]\mathbb{E}[Y \mid X = x], and check the tower rule for E[Y]\mathbb{E}[Y].
      AnswerpX(x)=∫x12 dy=2(1−x)p_X(x) = \int_x^1 2\,dy = 2(1 - x) on (0,1)(0, 1). Given X=xX = x, YY is uniform on (x,1)(x, 1), so E[Y∣X=x]=(1+x)/2\mathbb{E}[Y \mid X = x] = (1 + x)/2. E[X]=∫01x⋅2(1−x) dx=1/3\mathbb{E}[X] = \int_0^1 x \cdot 2(1 - x)\,dx = 1/3, so E[(1+X)/2]=(1+1/3)/2=2/3\mathbb{E}[(1 + X)/2] = (1 + 1/3)/2 = 2/3. Directly: pY(y)=2yp_Y(y) = 2y and E[Y]=∫01y⋅2y dy=2/3\mathbb{E}[Y] = \int_0^1 y \cdot 2y\,dy = 2/3. ✓
    2. A deep ensemble has 5 members with predicted means 1.0,1.2,0.8,1.1,0.91.0, 1.2, 0.8, 1.1, 0.9, each with predicted variance 0.040.04. What is the variance of the ensemble's predictive mixture?
      AnswerWithin part: E[Var⁡]=0.04\mathbb{E}[\operatorname{Var}] = 0.04. The means average to 1.01.0; their variance (weights 1/51/5) is (0+0.04+0.04+0.01+0.01)/5=0.02(0 + 0.04 + 0.04 + 0.01 + 0.01)/5 = 0.02. Total: 0.04+0.02=0.060.04 + 0.02 = 0.06.
    3. Var⁡(X)=1\operatorname{Var}(X) = 1, Var⁡(Y)=2\operatorname{Var}(Y) = 2 and Cov⁡(X,Y)=0.5\operatorname{Cov}(X, Y) = 0.5. Find Var⁡(2X−3Y+5)\operatorname{Var}(2X - 3Y + 5).
      AnswerThe shift 5 does not matter. Var⁡(2X−3Y)=4⋅1+9⋅2+2⋅2⋅(−3)⋅0.5=4+18−6=16\operatorname{Var}(2X - 3Y) = 4 \cdot 1 + 9 \cdot 2 + 2 \cdot 2 \cdot (-3) \cdot 0.5 = 4 + 18 - 6 = 16. Or with a=(2,−3)a = (2, -3): a⊤Σaa^\top \Sigma a.
    4. Var⁡(X)=Var⁡(Y)=1\operatorname{Var}(X) = \operatorname{Var}(Y) = 1 and ρ=0.9\rho = 0.9. Find Var⁡(X−Y)\operatorname{Var}(X - Y).
      Answer1+1−2⋅0.9=0.21 + 1 - 2 \cdot 0.9 = 0.2. The difference of two strongly correlated readings removes most of the shared variance.
    5. Can (1221)\begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} be a covariance matrix? Show it with one vector aa.
      AnswerNo. With a=(1,−1)a = (1, -1): a⊤Σa=1+1−2⋅2=−2a^\top \Sigma a = 1 + 1 - 2 \cdot 2 = -2. This would be Var⁡(X1−X2)<0\operatorname{Var}(X_1 - X_2) < 0, which is impossible. (Its eigenvalues are 33 and −1-1; and "ρ\rho" would be 2>12 > 1.)
    6. You compute the sample variance of 5 numbers with np.var(x). On average, by what factor is it too small?
      Answer`np.var` divides by nn by default (`ddof=0`), so E=n−1nσ2=0.8 σ2\mathbb{E} = \frac{n-1}{n}\sigma^2 = 0.8\,\sigma^2. Use `np.var(x, ddof=1)` for the unbiased S2S^2.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. How do you get the marginal pXp_X and the conditional p(x∣y)p(x \mid y) from a joint PMF table?

    2. What does independence look like in a joint PMF table?

    3. Why is E[X∣Y]\mathbb{E}[X \mid Y] a random variable and not a number?

    4. State the tower rule.

    5. State the law of total variance and name its two parts.

    6. Give a pair of variables that are uncorrelated but dependent.

    7. Write Var⁡(X+Y)\operatorname{Var}(X + Y) and Var⁡(X−Y)\operatorname{Var}(X - Y).

    8. What is Cov⁡(AX+b)\operatorname{Cov}(AX + b) when Cov⁡(X)=Σ\operatorname{Cov}(X) = \Sigma?

    9. Why is every covariance matrix positive semidefinite?

    10. Why does the sample variance divide by n−1n - 1?

    Open the review →

    9. Interview angle #

    • "Are uncorrelated variables independent?" — No: XX and X2X^2 for symmetric XX. They are for jointly Gaussian variables (lesson 05); Gaussian marginals alone are not enough.
    • "Why divide by n−1n - 1?" — The deviations from Xˉ\bar X are on average smaller than those from μ\mu; E∑i(Xi−Xˉ)2=(n−1)σ2\mathbb{E}\sum_i (X_i - \bar X)^2 = (n - 1)\sigma^2.
    • "nn variables all have variance σ2\sigma^2 and the same pairwise correlation ρ\rho. How negative can ρ\rho be?" — Var⁡(∑iXi)=nσ2(1+(n−1)ρ)≥0\operatorname{Var}(\sum_i X_i) = n\sigma^2\big(1 + (n - 1)\rho\big) \ge 0, so ρ≥−1/(n−1)\rho \ge -1/(n - 1).
    • "Your ensemble members agree but each is unsure. What does the predictive variance look like?" — Large within part, small between part; the law of total variance gives the split.
    • "What does PCA compute, and what does the top eigenvalue mean?" — The eigenvectors of the sample covariance; λ1\lambda_1 is the largest variance of any unit-length projection u⊤Xu^\top X.

    10. Practice #

    The notebook for this lesson has five exercises, each with a check cell and a folded solution:

    1. From a joint PMF array: marginals, the conditional columns, and an independence test.
    2. Verify the law of total variance on simulated grouped data (three classes).
    3. Write the sample covariance matrix from scratch (no np.cov) and check Bessel's correction on 20,000 samples of size 5.
    4. Check Cov⁡(AX+b)=AΣA⊤\operatorname{Cov}(AX + b) = A\Sigma A^\top on samples.
    5. PCA from the covariance matrix: eigen-decomposition compared with sklearn.decomposition.PCA.

    11. Watch and read #

    Joint, marginal and conditional distributions, discrete and continuous, with many small examples.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The conditional expectation as a random variable, the tower rule and the law of total variance.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Properties of covariance, the variance of a sum, and why correlation lies between −1 and 1.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A short visual summary of the covariance matrix and how it describes the shape of a data cloud.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    • Joseph Blitzstein and Jessica Hwang, Introduction to Probability, 2nd edition (free PDF from the authors): §7.1 (joint, marginal and conditional), §7.3 (covariance and correlation), §9.1–9.5 (conditional expectation, Adam's and Eve's laws).
    • Kevin Murphy, Probabilistic Machine Learning: An Introduction, free PDF: §3.1 (covariance, correlation, uncorrelated does not imply independent).
    • NumPy documentation of np.cov (rowvar and ddof).