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.
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]. 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 B
per-example gradients; its variance drops like 1/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.
A joint table. Take two discrete random variables. In our running example, a student does two homework
problems. X is the number of problems the student solves (0, 1 or 2), and Y=1 if the student went to
the tutorial, Y=0 if not. The joint PMF is a table: cell (x,y) holds P(X=x,Y=y), and all
cells add up to 1.
Y=0 (no tutorial)
Y=1 (tutorial)
row sum pX(x)
X=0
0.2
0.1
0.3
X=1
0.2
0.2
0.4
X=2
0.0
0.3
0.3
column sum pY(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 X alone; the column sums are the
distribution of Y alone. They are written in the margins of the table, hence the name.
Conditional = keep one column and rescale it. Keep the column Y=1:(0.1,0.2,0.3). It sums to
0.6, so divide by 0.6:p(x∣Y=1)=(1/6,1/3,1/2). This is the distribution of X in the world
where you know that the student went to the tutorial.
Independence = every column has the same shape. If X and Y are independent, knowing Y changes
nothing, so every rescaled column equals the marginal pX. Then the table is the outer productpXpY⊤ 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) with the marginal pX(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 pXpY⊤, every column gets the same shape and the rank drops to 1.
The conditional mean and the two parts of the spread. The conditional meanE[X∣Y=y]
is the balance point of column y. In the table it is 0.5 for Y=0 and 4/3 for Y=1. It is a number
for each y, so it is a function of y. Averaging it over y (with weights pY) gives back the overall mean.
The overall spread of X 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) into the
within-class partE[Var(X∣Y)] and the
between-class partVar(E[X∣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) and the cross through the means
(xˉ,yˉ). For each point, the product (x−xˉ)(y−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 ρ and watch the colours. Each point is coloured by the sign of
(x−xˉ)(y−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), and what does it say about independence?
Why.
Keep the column Y=0, which is (0.2,0.2,0) with sum 0.4, and rescale: (0.5,0.5,0). So P(X=2∣Y=0)=0. Knowing Y=0 changes the probability of X=2 from 0.3 to 0, so the variables are dependent. 0.12 is what the cell would hold if they were independent.
Let X and Y be two random variables defined on the same sample space.
The joint PMF (discrete case) is p(x,y)=P(X=x,Y=y).
A joint PDF (continuous case) is a function p(x,y)≥0 with
P((X,Y)∈B)=∬Bp(x,y)dxdy for every (measurable) set B⊆R2. As in one
dimension, p(x,y) is a density, not a probability: it can be larger than 1.
The marginals are pX(x)=∑yp(x,y) and pY(y)=∑xp(x,y); for densities, replace the
sums by integrals, pX(x)=∫p(x,y)dy.
The conditional PMF or PDF of X given Y=y (where pY(y)>0) is
p(x∣y)=pY(y)p(x,y).
Multiplying back gives the chain rule: p(x,y)=p(x∣y)pY(y)=p(y∣x)pX(x).
X and Y are independent if p(x,y)=pX(x)pY(y) for all x and y. Equivalently,
p(x∣y)=pX(x) for every y with pY(y)>0: knowing Y does not change the distribution of X.
Factorisation criterion. If p(x,y)=g(x)h(y) for all x,y, for some functions g and h, then
X and Y are independent.
Proof (densities; for PMFs replace integrals by sums).
Let G=∫g(x)dx and H=∫h(y)dy. The joint density integrates to 1, so GH=1.
Integrate out one variable: pX(x)=∫g(x)h(y)dy=g(x)H, and in the same way pY(y)=h(y)G.
So pX(x)pY(y)=g(x)h(y)GH=p(x,y).■
For a table this says: X and Y are independent exactly when the joint table has rank 1. A rank-1
table is an outer product uv⊤, so p(x,y)=uxvy has the form g(x)h(y); conversely an
independent table is pXpY⊤.
The support matters. The condition "for all x,y" includes the points where the density is 0, so the
set where p>0 must be a rectangle (a product set). Take p(x,y)=2 on the triangle 0<x<y<1
and 0 elsewhere. The formula looks constant, but X and Y are dependent: X>0.9 forces Y>0.9.
For this density:
pX(x)=∫x12dy=2(1−x) and pY(y)=∫0y2dx=2y, both on (0,1);
given X=x,p(y∣x)=2/(2(1−x))=1/(1−x) on (x,1):Y is uniform on (x,1).
Conditional expectation, the tower rule and total variance #
The conditional expectationE[X∣Y=y]=∑xxp(x∣y) (or ∫xp(x∣y)dx)
is a number for each y. Call this function m(y). Now put the random variable Y into it:
E[X∣Y]:=m(Y).
This is a random variable, a function of Y. It takes the value m(y) when Y=y. In the same way,
Var(X∣Y=y)=E[X2∣Y=y]−m(y)2, and Var(X∣Y) is
that function evaluated at Y.
Theorem (tower rule, also called Adam's law or the law of iterated expectations). If
E∣X∣<∞, then E[E[X∣Y]]=E[X].
Proof (discrete case; the continuous case is the same with integrals).
m(Y) is a function of Y, so by LOTUS
E[m(Y)]=∑ypY(y)m(y)=∑ypY(y)∑xxpY(y)p(x,y).
The factors pY(y) cancel: ∑y∑xxp(x,y).
Sum over y first: ∑xxpX(x)=E[X].■
Theorem (law of total variance, also called Eve's law). If E[X2]<∞, then
Use Var(X∣Y)=E[X2∣Y]−m(Y)2 and the tower rule for X2:E[Var(X∣Y)]=E[X2]−E[m(Y)2].
Use the definition of variance and the tower rule for X:Var(m(Y))=E[m(Y)2]−(E[m(Y)])2=E[m(Y)2]−(E[X])2.
Add the two lines. The terms E[m(Y)2] cancel, and what is left is
E[X2]−(E[X])2=Var(X).■
Both parts are ≥0. So, on average, knowing Y cannot increase the variance:
E[Var(X∣Y)]≤Var(X).
The conditional expectation is the best predictor
Claim. For every function h,E[(X−m(Y))2]≤E[(X−h(Y))2], where
m(Y)=E[X∣Y].
Proof.
Write X−h(Y)=(X−m(Y))+(m(Y)−h(Y)) and expand the square.
The cross term is 2E[(X−m(Y))(m(Y)−h(Y))]. Condition on Y: given Y, the factor
m(Y)−h(Y) is a constant, and E[X−m(Y)∣Y]=0. By the tower rule the cross term is 0.
So E[(X−h(Y))2]=E[(X−m(Y))2]+E[(m(Y)−h(Y))2]≥E[(X−m(Y))2].■
This is why a regression model trained with the squared loss estimates E[target∣input],
and why the smallest possible MSE is E[Var(X∣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?
Why.
Within part: E[Var(X∣Y)]=21⋅25+21⋅25=25. Between part: the class means 60 and 80 have mean 70 and variance 21⋅102+21⋅102=100. Total: 25+100=125. The average of the class variances misses the between part.
The covariance of X and Y, with means μX and μY, is
Cov(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−E[X]E[Y].
(Expand the product and use linearity: E[XY]−μXμY−μXμY+μXμY.) The
correlation is the covariance of the standardised variables,
ρ=Corr(X,Y)=σXσYCov(X,Y),σX,σY>0.
Properties you will use all the time:
Cov(X,X)=Var(X),Cov(X,Y)=Cov(Y,X), and
Cov(X,c)=0 for a constant c.
Covariance is bilinear: Cov(aX+bY,Z)=aCov(X,Z)+bCov(Y,Z),
and the same in the second slot.
So Cov(aX+b,cY+d)=acCov(X,Y), and ρ(aX+b,cY+d)=sign(ac)ρ(X,Y):
correlation has no units and ignores shifts and scales, but a negative factor flips its sign.
Theorem.∣ρ∣≤1, that is, Cov(X,Y)2≤Var(X)Var(Y).
Equality holds if and only if Y=aX+b with probability 1, for some constants a=0 and b.
Proof.
Standardise: X~=(X−μX)/σX and Y~=(Y−μY)/σY. Both have variance 1,
and Cov(X~,Y~)=ρ.
A variance is never negative. By bilinearity,
0≤Var(X~+Y~)=1+1+2ρ and 0≤Var(X~−Y~)=1+1−2ρ.
So −1≤ρ≤1.
If ρ=1, then Var(X~−Y~)=0, so X~−Y~ is a constant with
probability 1, and Y is an affine function of X with a>0. The case ρ=−1 is the same with
X~+Y~ and gives a<0.■
Quick check
Corr(X,Y)=0.6. What is Corr(−2X+1,Y)?
Why.
Cov(−2X+1,Y)=−2Cov(X,Y) and the standard deviation of −2X+1 is 2σX. The factor 2 cancels, the sign stays: ρ=−0.6. Correlation ignores the size of a scale factor and any shift, but not its sign. A correlation can never be −1.2.
Independent implies uncorrelated. If X and Y are independent, then
E[XY]=∑x,yxypX(x)pY(y)=(∑xxpX(x))(∑yypY(y))=E[X]E[Y],
so Cov(X,Y)=0.
The converse is false. Let X be uniform on {−1,0,1} and Y=X2.
E[X]=0 and E[XY]=E[X3]=31(−1+0+1)=0.
So Cov(X,Y)=0−0⋅E[Y]=0: uncorrelated.
But Y is a function of X. For example P(Y=0∣X=0)=1, while P(Y=0)=1/3: dependent.
The same happens for Z∼N(0,1) and Z2, 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, but the
orange curve (the mean of Y in narrow bins of X) or the
orange band (± one standard deviation in each bin) changes with X, so Y depends
on X. The second readout, the correlation of X2 and Y2, is far from 0 for every dependent example.
Only "independent" has both readouts near 0.
For a random vector X=(X1,…,Xd)⊤ with mean μ=E[X], the covariance matrix is
Σ=Cov(X)=E[(X−μ)(X−μ)⊤],Σij=Cov(Xi,Xj).
Its diagonal holds the variances. The correlation matrix is D−1/2ΣD−1/2 with
D=diag(Σ11,…,Σdd); its entries are the correlations ρij.
Theorem (affine maps). For a fixed matrix A∈Rm×d and vector b∈Rm,
Cov(AX+b)=AΣA⊤.
Proof.
By linearity, E[AX+b]=Aμ+b. So (AX+b)−E[AX+b]=A(X−μ): the shift b drops out.
Cov(AX+b)=E[A(X−μ)(X−μ)⊤A⊤]=AE[(X−μ)(X−μ)⊤]A⊤=AΣA⊤,
because expectation is linear, entry by entry. ■
With one row, A=a⊤, this gives the variance of any linear combination:
Var(a⊤X)=a⊤Σa. With a=(1,…,1) it is again the variance of a sum.
Theorem. Every covariance matrix is symmetric and positive semidefinite (PSD): a⊤Σa≥0
for every a. It is singular if and only if some combination a⊤X with a=0 is constant with
probability 1.
Proof.
Symmetric: Σij=Cov(Xi,Xj)=Cov(Xj,Xi)=Σji.
PSD: a⊤Σa=Var(a⊤X)≥0, by the previous theorem.
Singular: for a PSD matrix, Σa=0 exactly when a⊤Σa=0 (write
a⊤Σa=∥Σ1/2a∥2,linear algebra 07).
And Var(a⊤X)=0 exactly when a⊤X is constant: the data lie on a hyperplane. ■
So a symmetric matrix with a negative eigenvalue is never a covariance matrix: it would give a "variance"
a⊤Σa<0.
Quick check
Which matrix can be a covariance matrix?
Why.
A covariance matrix must be symmetric (rules out the second) and PSD. The first has eigenvalues 3 and −1 (and "ρ" =2). The fourth looks fine, but ρ=1.5/2⋅1≈1.06>1, and its determinant 2−2.25 is negative. The third has ρ=1/4=0.5, positive diagonal and determinant 3>0, so it is positive definite.
Bessel's correction: why the sample variance divides by n−1#
Theorem. Let X1,…,Xn(n≥2) be i.i.d. with mean μ and variance σ2. Let
Xˉ=n1∑iXi and
S2=n−11i=1∑n(Xi−Xˉ)2.
Then E[S2]=σ2:S2 is unbiased.
Proof.
Write Xi−Xˉ=(Xi−μ)−(Xˉ−μ), square, and sum. Use ∑i(Xi−μ)=n(Xˉ−μ):
Each term (Xi−μ)2 has mean σ2, so E[∑i(Xi−μ)2]=nσ2.
The Xi are independent, so by the variance of a sum Var(Xˉ)=n21⋅nσ2=σ2/n.
Hence E[n(Xˉ−μ)2]=nVar(Xˉ)=σ2.
So E[∑i(Xi−Xˉ)2]=nσ2−σ2=(n−1)σ2. Divide by n−1.■
Why.Xˉ is the number c that makes ∑i(Xi−c)2 smallest. So the deviations from Xˉ are,
on average, smaller than the deviations from the true μ. Dividing by n gives the mean
nn−1σ2, which is too small: "one degree of freedom is spent to estimate μ". The same proof,
entry by entry, shows that the sample covariance matrix
C=n−11∑i(Xi−Xˉ)(Xi−Xˉ)⊤ is unbiased for Σ.
Note: S is a biased estimator of σ. The square root is concave, so E[S]<σ by Jensen's
inequality (information theory 03) (unless S2 is constant).
Σ is symmetric, so by the spectral theorem (linear algebra 06)
Σ=QΛQ⊤, with orthonormal eigenvectors q1,…,qd (the columns of Q) and eigenvalues
λ1≥⋯≥λd≥0 (they are ≥0 because Σ is PSD). In this section only,
Λ is the diagonal matrix of eigenvalues; from lesson 05 on, Λ is the precision matrix.
The direction of largest variance. For a unit vector u, write c=Q⊤u; it is also a unit vector.
Then Var(u⊤X)=u⊤Σu=∑kλkck2≤λ1∑kck2=λ1,
with equality at u=q1. So q1 is the direction of largest variance, and that variance is λ1.
The principal components are uncorrelated.Y=Q⊤(X−μ) has
Cov(Y)=Q⊤ΣQ=Λ, a diagonal matrix.
Total variance.∑iVar(Xi)=trΣ=∑kλk. The first k
components explain the fraction (λ1+⋯+λk)/trΣ.
In two dimensions, Σ is drawn as an ellipse: its axes point along q1 and q2, with half-lengths
λ1 and λ2 (one standard deviation). PCA on data does the same with the sample
covariance C (linear algebra 09).
Interactive figure: loading…
Drag the yellow handle to turn the direction u, and watch the variance of the projections
u⊤x. The samples are blue; the orange ellipses are 1 and 2
standard deviations of Σ, and the orange arrows are the eigenvectors scaled by
λk. The projected variance u⊤Σu is largest (=λ1) when u points along the
long axis, and smallest (=λ2) along the short axis. Set ρ=0.8 and σ1=σ2 to get the
two-thermometer matrix up to scale.
Answers to section 1.
(1) No. The average is a⊤X with a=(21,21), so its variance is
a⊤Σa=41(5+5+2⋅4)=4.5, only a little less than 5. The difference has variance
5+5−2⋅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)]. It misses the between part Var(E[X∣Y]),
which is positive unless all class means are equal.
Mean and variance of X.E[X]=0⋅0.3+1⋅0.4+2⋅0.3=1.E[X2]=0+1⋅0.4+4⋅0.3=1.6. So Var(X)=1.6−12=0.6.
Not independent.p(2,0)=0, but pX(2)pY(0)=0.3⋅0.4=0.12. One cell is enough to show dependence.
Conditionals. Column Y=0 divided by 0.4:p(x∣Y=0)=(0.5,0.5,0). Its mean is 0.5, and its
variance is E[X2∣Y=0]−0.52=0.5−0.25=0.25.
Column Y=1 divided by 0.6:p(x∣Y=1)=(1/6,1/3,1/2). Its mean is 1/3+2⋅1/2=4/3.E[X2∣Y=1]=1/3+4⋅1/2=7/3, so the variance is 7/3−16/9=5/9.
Total variance. Within: E[Var(X∣Y)]=0.4⋅0.25+0.6⋅95=0.1+31=3013≈0.433.
Between: Var(E[X∣Y])=0.4⋅0.52+0.6⋅(34)2−12=0.1+1516−1=61≈0.167.
Sum: 3013+305=3018=0.6=Var(X). ✓
Covariance and correlation.E[XY] only gets contributions from the column Y=1:E[XY]=1⋅1⋅0.2+2⋅1⋅0.3=0.8. With E[Y]=0.6:Cov(X,Y)=0.8−1⋅0.6=0.2.Y is Bernoulli(0.6), so Var(Y)=0.6⋅0.4=0.24, and
ρ=0.2/0.6⋅0.24≈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 T has variance 4, and thermometer i reads
Xi=T+ei, where the errors e1,e2 have variance 1 and are independent of each other and of T.
Var(Xi)=Var(T)+Var(ei)=4+1=5.
Cov(X1,X2)=Cov(T+e1,T+e2)=Var(T)=4, by bilinearity (all other terms are 0).
So Σ=(5445) and ρ=4/5=0.8.
Average and difference at once. Take A=(1/211/2−1). Then
AΣ=(4.514.5−1) and
AΣA⊤=(4.5002). The average has variance 4.5, the difference 2,
and they are uncorrelated. The average removes half of the error variance (4+21), but none of the
variance of T: that part is signal, not noise.
PCA. The eigenvalues of Σ are 9 (eigenvector (1,1)/2) and 1 (eigenvector (1,−1)/2).
Check: Σ(1,1)⊤=(9,9)⊤ and Σ(1,−1)⊤=(1,−1)⊤. The first component, the "common
temperature" direction, explains 9/10=90% of the total variance trΣ=10.
(c) Bessel's correction by hand. Data 2,4,9. The mean is xˉ=15/3=5. The squared deviations are
9,1,16, with sum 26. So S2=26/2=13. Dividing by n=3 would give 8.67.
NumPy checks the hand computations. The notebook for this lesson has these cells and the exercises.
importnumpyasnp# (a) joint PMF: rows X = 0, 1, 2; columns Y = 0, 1P=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 sumsprint(pX,pY,np.linalg.matrix_rank(P))# [0.3 0.4 0.3] [0.4 0.6] 2 -> rank 2: dependentcond=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@pXprint(round(x@P@y-EX*(pY@y),4))# 0.2 = Cov(X, Y)# (b) Cov(AX + b) = A Σ Aᵀ, exactly and on samplesSigma=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 samplesprint(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^2xs=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 - 1d=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−1 by default.
Deep ensembles and mixtures. An ensemble of M Gaussian heads with means μm and variances σm2
predicts with variance M1∑mσm2+M1∑m(μm−μˉ)2. This is the law of total
variance with Y = 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] (the best predictor
above). The irreducible error is E[Var(Y∣X)].
Mini-batch gradients. The mean of B uncorrelated per-example gradients has 1/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⊤, which gives the identity covariance by Cov(AX)=AΣA⊤. 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) (ML for science 06).
With few members it has rank at most N−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.
Factorising the formula but not the support.p(x,y)=2 on 0<x<y<1 looks like a product, but X and
Y are dependent. The set where p>0 must be a rectangle.
Treating E[X∣Y] as a number. It is a random variable, a function of Y. Only
E[X∣Y=y], for a fixed y, is a number.
Averaging only the group variances. This misses the between part Var(E[X∣Y]).
Reading zero correlation as independence.X and X2 are uncorrelated (for symmetric X) and fully
dependent. A correlation near 0 means "no linear relation", not "no relation".
Forgetting the covariance terms in Var(X+Y). The variances add only for uncorrelated variables.
Dividing by n and calling the result unbiased, or claiming that S (not S2) is unbiased for σ.
The rowvar default of np.cov. A data matrix of shape (samples, features) needs rowvar=False or a transpose.
Otherwise you get an n×n matrix of covariances between samples.
(X,Y) has density p(x,y)=2 on 0<x<y<1. Find pX and E[Y∣X=x], and check the tower
rule for E[Y].AnswerpX(x)=∫x12dy=2(1−x) on (0,1). Given X=x,Y is uniform on (x,1), so E[Y∣X=x]=(1+x)/2.E[X]=∫01x⋅2(1−x)dx=1/3, so E[(1+X)/2]=(1+1/3)/2=2/3. Directly: pY(y)=2y and E[Y]=∫01y⋅2ydy=2/3. ✓
A deep ensemble has 5 members with predicted means 1.0,1.2,0.8,1.1,0.9, each with predicted variance 0.04.
What is the variance of the ensemble's predictive mixture?AnswerWithin part: E[Var]=0.04. The means average to 1.0; their variance (weights 1/5) is (0+0.04+0.04+0.01+0.01)/5=0.02. Total: 0.04+0.02=0.06.
Var(X)=1,Var(Y)=2 and Cov(X,Y)=0.5. Find 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. Or with a=(2,−3):a⊤Σa.
Var(X)=Var(Y)=1 and ρ=0.9. Find Var(X−Y).Answer1+1−2⋅0.9=0.2. The difference of two strongly correlated readings removes most of the shared variance.
Can (1221) be a covariance matrix? Show it with one vector a.AnswerNo. With a=(1,−1):a⊤Σa=1+1−2⋅2=−2. This would be Var(X1−X2)<0, which is impossible. (Its eigenvalues are 3 and −1; and "ρ" would be 2>1.)
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 n by default (`ddof=0`), so E=nn−1σ2=0.8σ2. Use `np.var(x, ddof=1)` for the unbiased S2.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
How do you get the marginal pX and the conditional p(x∣y) from a joint PMF table?
Marginal: sum each row, pX(x)=∑yp(x,y). Conditional: take column y and divide it by its sum, p(x∣y)=p(x,y)/pY(y).
What does independence look like in a joint PMF table?
Every column, rescaled, is the same distribution; the table is the outer product pXpY⊤, so it has rank 1.
Why is E[X∣Y] a random variable and not a number?
It is the function m(y)=E[X∣Y=y] evaluated at the random Y, so its value depends on the outcome of Y.
State the tower rule.
E[E[X∣Y]]=E[X]: the average of the conditional means is the overall mean.
State the law of total variance and name its two parts.
Var(X)=E[Var(X∣Y)]+Var(E[X∣Y]): the within-group part plus the between-group part.
Give a pair of variables that are uncorrelated but dependent.
X uniform on {−1,0,1} and Y=X2:Cov(X,Y)=E[X3]=0, but Y is a function of X.
Write Var(X+Y) and Var(X−Y).
Var(X)+Var(Y)±2Cov(X,Y).
What is Cov(AX+b) when Cov(X)=Σ?
AΣA⊤. The shift b drops out.
Why is every covariance matrix positive semidefinite?
For every a,a⊤Σa=Var(a⊤X)≥0.
Why does the sample variance divide by n−1?
Deviations from Xˉ are smaller than from μ:E∑i(Xi−Xˉ)2=(n−1)σ2, so dividing by n−1 makes it unbiased.
"Are uncorrelated variables independent?" — No: X and X2 for symmetric X. They are for jointly Gaussian
variables (lesson 05); Gaussian marginals alone are not enough.
"Why divide by n−1?" — The deviations from Xˉ are on average smaller than those from μ;E∑i(Xi−Xˉ)2=(n−1)σ2.
"n variables all have variance σ2 and the same pairwise correlation ρ. How negative can ρ be?" —
Var(∑iXi)=nσ2(1+(n−1)ρ)≥0, so ρ≥−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 is the largest variance of any unit-length projection u⊤X.
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).