Before this lesson:lesson 04 (joint densities,
the factorisation criterion, Cov(AX+b)=AΣA⊤) and
lesson 03 (the normal and exponential distributions, moment
generating functions). From other tracks: the change of variables in integrals
(calculus 10), determinants
(linear algebra 03) and the Cholesky factorisation
(linear algebra 07).
Time: about 85 minutes, plus the notebook.
It is the default model for noise and uncertainty. Affine maps, marginals and conditionals of a Gaussian
are again Gaussian, with closed-form parameters.
Gaussian conditioning under other names. The update step of the Kalman filter (weather forecasting, tracking),
the prediction of a Gaussian process, and Bayesian linear regression are all one formula: the conditional of a
joint Gaussian.
Generative models. The VAE reparametrisation trick is the sampler x=μ+σ⊙z. A diffusion model
adds Gaussian noise with an affine map. A normalising flow trains with the change-of-variables formula.
Numerics. In high dimension, detΣ underflows and Σ−1 is inaccurate. Every serious
implementation uses the Cholesky factor instead.
Mass moves, density changes. Let X be a random point in the plane with density pX, and let Y=g(X)
for a smooth one-to-one map g. Take a tiny square of area dA around a point x. It holds probability about
pX(x)dA. The map sends it to a small, slightly curved parallelogram around y=g(x). Near x, the map
looks like its Jacobian matrix Jg(x) (the matrix of partial derivatives), so the image has area about
∣detJg(x)∣dA (calculus 10). The same mass now sits on that area:
Drag the yellow square (left) and watch its image (right). The blue points are samples
in the square; they keep their colour and their number after the map, so no mass is created or lost. The readout
compares the area ratio of the image with ∣detJg∣ at the centre. Switch to polar or tanh and make the square
smaller with the slider: for a curved map the two numbers agree only for small squares.
From circles to ellipses. Let z=(z1,z2) have independent N(0,1) entries. Its density
2π1e−(z12+z22)/2 depends only on the length ∥z∥, so its contour lines are circles and
the cloud of samples is round. Now apply a matrix and a shift: x=μ+Lz. Circles become ellipses, and by
lesson 04
Cov(x)=LCov(z)L⊤=LIL⊤=LL⊤=:Σ.
The contours of the density of x are the ellipses (x−μ)⊤Σ−1(x−μ)=c2. The number
(x−μ)⊤Σ−1(x−μ) is the Mahalanobis distance of x from μ: the distance measured
in standard deviations of the cloud, in each direction. On the ellipse with c=1, every point is "one standard
deviation" away, even when the Euclidean distances are very different.
Interactive figure: loading…
Press Play: z → x to watch the round cloud of z become the ellipse of x=μ+Lz. Then change
σ1,σ2,ρ. The blue points are samples; the orange
ellipses are Mahalanobis distance 1 and 2; the violet arrows are the columns of L.Drag the yellow point: its Euclidean distance and its Mahalanobis distance can be very different. In 2-D, about
39% of the samples fall inside distance 1 and about 86% inside distance 2.
A marginal is a shadow; a conditional is a slice. Project the cloud onto the x1 axis. The shadow is a 1-D
Gaussian with mean μ1 and variance Σ11: nothing to compute. Now fix x2 and look at the density
along that horizontal line, rescaled to integrate to 1. This slice is the conditional density of x1 given x2.
It is again a 1-D Gaussian. With standard deviations σ1,σ2 and correlation ρ:
x1∣x2∼N(μ1+ρσ2σ1(x2−μ2),σ12(1−ρ2)).
As x2 moves, the centre of the slice moves on a straight line (the regression line of x1 on x2). The width
of the slice stays the same. A strong correlation gives a narrow slice: knowing x2 tells you a lot about x1.
Section 3 proves the general version.
Interactive figure: loading…
Drag the yellow handle up and down to move the line x2=c. The aqua curve is the
slice (the conditional of x1 given x2=c); its centre moves on the dashed orange line,
but its width does not change. The violet curve at the bottom is the shadow (the marginal of
x1). Move ρ towards ±1: the slice gets narrower, the shadow does not.
Quick check
The map g(x)=2x doubles every length in the plane. X has density pX. What is the density of Y=2X at the point y=2x?
Why.
Jg=2I, so ∣detJg∣=4: every small square gets 4 times its area. The same mass on 4 times the area means a density 4 times lower. Dividing by 2 would be right in one dimension; in the plane both directions are stretched.
Theorem. Let X have density pX on an open set U⊆Rd. Let g:U→V be a bijection
onto an open set V, with g and g−1 continuously differentiable. Then Y=g(X) has the density
where Jg−1(y) is the d×d matrix of partial derivatives of g−1 at y.
Proof.
For any set B⊆V:P(Y∈B)=P(X∈g−1(B))=∫g−1(B)pX(x)dx.
Substitute x=g−1(y) in this integral. The multivariable substitution rule says
dx=∣detJg−1(y)∣dy, so the integral equals ∫BpX(g−1(y))∣detJg−1(y)∣dy.
This holds for every B, so the integrand is the density of Y.
For the second form: by the inverse function theorem Jg−1(y)=Jg(x)−1, so
∣detJg−1(y)∣=1/∣detJg(x)∣.■
It is enough that these conditions hold outside a set of probability 0. Two special cases:
One dimension:pY(y)=pX(x)/∣g′(x)∣, the rule of lesson 02.
A linear mapY=AX+b with invertible A:pY(y)=pX(A−1(y−b))/∣detA∣.
In log form, logpY(y)=logpX(x)−log∣detJg(x)∣. A normalising flow is a chain of invertible maps
with cheap Jacobian determinants, trained by maximising exactly this log-density
(generative models 04).
Theorem. Let Z1,Z2 be i.i.d. N(0,1), and write (Z1,Z2)=(RcosΘ,RsinΘ) with
R>0 and Θ∈[0,2π). Then R and Θ are independent, Θ is uniform on [0,2π), and
R2∼Exp(1/2) (rate 1/2, mean 2).
Proof.
The joint density of Z=(Z1,Z2) is pZ(z)=2π1e−(z12+z22)/2.
The polar map (r,θ)↦(rcosθ,rsinθ) is a smooth bijection from (0,∞)×(0,2π)
onto the plane without the half-line {(z1,0):z1≥0}, which has probability 0. It is g−1 in the
theorem above, and its Jacobian determinant is
det(cosθsinθ−rsinθrcosθ)=rcos2θ+rsin2θ=r.
So pR,Θ(r,θ)=pZ(rcosθ,rsinθ)⋅r=2π1⋅re−r2/2 for r>0,θ∈(0,2π).
This is a function of θ times a function of r, on a product set. By the factorisation criterion of
lesson 04, R and Θ are independent, Θ has the constant density 1/(2π), and R has density
re−r2/2 (the Rayleigh distribution).
Let S=R2. Then P(S>s)=P(R>s)=∫s∞re−r2/2dr=e−s/2: the tail of
Exp(1/2).■
The sampler runs the proof backwards. Take independent U1,U2 uniform on (0,1). Inverse-CDF sampling (lesson 02) gives
S=−2log(1−U1)∼Exp(1/2), and Θ=2πU2 is uniform. Then
Z1=ScosΘ,Z2=SsinΘ
are two independent N(0,1) numbers. (Many texts write −2logU1; it has the same distribution, because
1−U1 is also uniform.) Libraries use faster methods today (NumPy uses the ziggurat algorithm), but the Jacobian
argument is the same.
μ is the mean and Σ the covariance. The inverse Λ=Σ−1 is the precision matrix, and
(x−μ)⊤Σ−1(x−μ) is the squared Mahalanobis distance.
Theorem (from noise to density). Let z∼N(0,Id)(d independent standard normals). Let
Σ=LL⊤, where L is lower triangular with a positive diagonal (the Cholesky factor), and let
x=μ+Lz. Then x has the density above.
Proof.
The density of z is a product of d standard normal densities: pz(z)=(2π)−d/2e−z⊤z/2.
The map z↦μ+Lz is invertible. Its inverse is x↦L−1(x−μ), a linear map with
Jacobian matrix L−1.
By the change-of-variables theorem, px(x)=pz(L−1(x−μ))∣detL−1∣.
detΣ=detL⋅detL⊤=(detL)2, so ∣detL−1∣=1/∣detL∣=(detΣ)−1/2.
Put z=L−1(x−μ) into the exponent, and write L−⊤=(L−1)⊤:z⊤z=(x−μ)⊤L−⊤L−1(x−μ)=(x−μ)⊤Σ−1(x−μ),
because Σ−1=(LL⊤)−1=L−⊤L−1.■
The shape of the contours. With the eigendecomposition Σ=Qdiag(λ1,…,λd)Q⊤,
the contour (x−μ)⊤Σ−1(x−μ)=c2 is an ellipsoid with axes along the eigenvectors qk and
half-lengths cλk. In 2-D the probability inside the ellipse of Mahalanobis radius c is 1−e−c2/2:
about 39.3% for c=1 and 86.5% for c=2 (much less than the 68% and 95% of one dimension).
Jointly Gaussian, in general. A random vector x is jointly Gaussian if every linear combination a⊤x
is a 1-D Gaussian (a constant counts, as N(c,0)). Then its moment generating function is
Mx(t)=E[et⊤x]=exp(t⊤μ+21t⊤Σt),
because t⊤x∼N(t⊤μ,t⊤Σt) and E[eW]=em+s2/2 for W∼N(m,s2)
(lesson 03). The MGF depends only on μ and Σ, and it fixes the distribution, so a Gaussian vector is fully
described by its mean and covariance.Σ may be singular (only PSD); then there is no density on Rd,
and the mass lies on a lower-dimensional affine subspace. Below, Σ is PD unless we say otherwise.
Theorem (affine maps). If x∼N(μ,Σ),A∈Rm×d and b∈Rm, then
y=Ax+b∼N(Aμ+b,AΣA⊤).
Proof.
For t∈Rm:t⊤y=(A⊤t)⊤x+t⊤b.
So My(t)=et⊤bMx(A⊤t)=exp(t⊤(Aμ+b)+21t⊤AΣA⊤t).
This is the MGF of N(Aμ+b,AΣA⊤), and the MGF fixes the distribution. ■
Corollary (marginals). Split x=(xa,xb) into two blocks, and split μ and Σ in the same way:
μ=(μaμb),Σ=(ΣaaΣbaΣabΣbb),Σba=Σab⊤.
Then xa=(I0)x∼N(μa,Σaa):read off the blocks. The same
works for any subset of coordinates: keep their entries of μ and their rows and columns of Σ.
Quick check
x=(x1,x2,x3)∼N(μ,Σ) with μ=(1,2,3) and Σ=412130205. What is the distribution of (x1,x3)?
Why.
A marginal of a Gaussian keeps the matching entries of μ and the matching rows and columns of Σ. Rows and columns 1 and 3 give Σ11=4,Σ13=2,Σ33=5. Integrating out x2 would give the same answer; the affine-map theorem does it for you. The covariance of x1 and x3 stays.
Uncorrelated jointly Gaussian variables are independent #
Theorem. If (xa,xb) is jointly Gaussian and Σab=0, then xa and xb are independent.
Proof (PD Σ).
Σ=(Σaa00Σbb) is block diagonal. So
Σ−1=(Σaa−100Σbb−1) and detΣ=detΣaadetΣbb.
The quadratic form splits into a part with xa and a part with xb:(x−μ)⊤Σ−1(x−μ)=(xa−μa)⊤Σaa−1(xa−μa)+(xb−μb)⊤Σbb−1(xb−μb).
So p(x)=N(xa;μa,Σaa)⋅N(xb;μb,Σbb), a function of xa times a
function of xb, on all of Rd. By the factorisation criterion, xa and xb are independent. ■
The word "jointly" is essential. Gaussian marginals are not enough. Let X∼N(0,1), let W=±1
with probability 1/2 each, independent of X, and let Y=WX.
Y∼N(0,1), because the density of X is symmetric: flipping the sign does not change it.
Cov(X,Y)=E[WX2]=E[W]E[X2]=0⋅1=0: uncorrelated.
But X+Y=0 whenever W=−1, so P(X+Y=0)=1/2. A Gaussian has no point with positive probability
(except the constant case), so X+Y is not Gaussian, and (X,Y) is not jointly Gaussian.
X and Y are dependent: ∣Y∣=∣X∣ always.
Interactive figure: loading…
Press Draw 1000 more a few times and compare the two cases. In "Y=WX", both histograms of X and Y
(blue) match the orange N(0,1) curve, and the correlation is
about 0. But the scatter is an X shape, and the histogram of X+Y has a spike at 0 that holds
half of the samples, and the correlation of ∣X∣ and ∣Y∣ is exactly 1. In "independent Y", X+Y follows the
N(0,2) curve. In both cases Var(X+Y)≈2: the variance cannot see the difference.
Quick check
X∼N(0,1) and Y∼N(0,1). Is X+Y always Gaussian?
Why.
X+Y=(1,1)(X,Y)⊤ is a linear combination, so it is Gaussian when the pair is jointly Gaussian. With Y=WX (random sign), X and Y are N(0,1) and uncorrelated, but X+Y is 0 with probability 1/2. Zero covariance does not help here.
Cholesky in practice: sampling and a stable log-density #
Let Σ=LL⊤ be the Cholesky factorisation (linear algebra 07).
It costs about d3/3 floating-point operations, once.
Sampling. Draw z∼N(0,Id) and set x=μ+Lz. By the affine-map theorem,
x∼N(μ,LL⊤)=N(μ,Σ). Each sample costs about d2 operations. With n samples as
the rows of an n×d matrix Z, the samples are the rows of 1μ⊤+ZL⊤. Any B with
BB⊤=Σ works (for example Qdiag(λk)); Cholesky is the cheapest.
Log-density.logp(x)=−21[dlog(2π)+logdetΣ+(x−μ)⊤Σ−1(x−μ)]. Two
identities make it stable:
The determinant of a triangular matrix is the product of its diagonal. So detΣ=(detL)2=∏iLii2
and logdetΣ=2∑ilogLii.
Solve Lu=x−μ by forward substitution (about d2 operations). Then
(x−μ)⊤Σ−1(x−μ)=(x−μ)⊤L−⊤L−1(x−μ)=∥u∥2.
Why not det and inv.detΣ is a product of d numbers, so it under- or overflows in float64 while
logdetΣ is an ordinary number. An explicit Σ−1 costs more and loses accuracy when Σ is
ill-conditioned; triangular solves are backward stable.
A free check.u=L−1(x−μ)∼N(0,Id), so for real samples the squared Mahalanobis distance
∥u∥2 follows a χd2 distribution (mean d).
When Cholesky fails: jitter. A covariance with eigenvalues close to 0 (for example a smooth Gaussian-process
kernel on a dense grid) is PD in exact arithmetic, but rounding errors make it numerically indefinite, and Cholesky
stops with an error. The standard fix is a small jitter: factor Σ+εI, with ε around
10−8 times the size of the variances.
Quick check
In d=1000 dimensions, what does np.linalg.det(0.1 * np.eye(1000)) return in float64?
Why.
The true value is 0.11000=10−1000, far below the smallest positive float64 (about 5×10−324), so it underflows to 0.0, and its log would be −∞. The number −2302.6=1000log0.1 is logdet, which np.linalg.slogdet or the Cholesky formula 2∑ilogLii return without trouble.
For the split Σ with Σbb invertible, the Schur complement of Σbb is
S=Σaa−ΣabΣbb−1Σba.
It is what remains of Σaa after one step of block Gaussian elimination removes the b block.
First, the 2-D case with numbers. Take a={1},b={2},Σ=(σ12ρσ1σ2ρσ1σ2σ22).
Then K=ΣabΣbb−1=ρσ1/σ2 and S=σ12−(ρσ1σ2)2/σ22=σ12(1−ρ2),
which is the formula of section 2. With σ1=σ2=1 and ρ=0.8:K=0.8 and S=1−0.64=0.36.
Theorem (Gaussian conditioning). Let x=(xa,xb)∼N(μ,Σ) with Σbb invertible, and let
K=ΣabΣbb−1. Then xa∣xb∼N(μa∣b,Σa∣b) with
μa∣b=μa+K(xb−μb),Σa∣b=Σaa−ΣabΣbb−1Σba=S.
In precision form, with Λ=Σ−1 split into the same blocks:
Σa∣b=Λaa−1,μa∣b=μa−Λaa−1Λab(xb−μb).
Proof: remove the part of xa that xb explains
Let z=xa−Kxb. In matrix form (z,xb)=Tx with T=(I0−KI), so by the
affine-map theorem (z,xb) is jointly Gaussian.
By bilinearity, Cov(z,xb)=Σab−KΣbb=Σab−Σab=0. By the previous
theorem, z and xb are independent.
E[z]=μa−Kμb, and
Cov(z)=Σaa−KΣba−ΣabK⊤+KΣbbK⊤. Each of the last three terms equals
ΣabΣbb−1Σba (use Σbb⊤=Σbb). So Cov(z)=S, and
TΣT⊤=(S00Σbb). In particular S is PD.
Write xa=z+Kxb. Given xb, the term Kxb is a constant, and z keeps its distribution because it is
independent of xb. So xa∣xb∼N(μa−Kμb+Kxb,S): the covariance form.
Precision form. detT=1, so T is invertible, and step 3 gives
Σ=T−1(S00Σbb)T−⊤. Invert both sides:
So Λaa=S−1 and Λab=−S−1K, that is, S=Λaa−1 and K=−Λaa−1Λab.
Put these into step 4. ■
Second route: completing the square
As a function of xa,logp(xa∣xb)=logp(xa,xb)−logp(xb)=−21(x−μ)⊤Λ(x−μ)+const.
Expand in blocks and keep only the terms with xa:−21xa⊤Λaaxa+xa⊤[Λaaμa−Λab(xb−μb)]+const.
N(m,P−1) has log-density −21xa⊤Pxa+xa⊤Pm+const. Matching the quadratic
terms gives P=Λaa; matching the linear terms gives Pm=Λaaμa−Λab(xb−μb). This is
the precision form.
This route is the one used for Bayesian posteriors (lesson 10) and the Laplace approximation.
Zeros of the precision matrix. For i=j:Λij=0 if and only if xi and xj are conditionally
independent given all other coordinates. Take a={i,j}: the conditional covariance of (xi,xj) is
Λaa−1, whose off-diagonal entry is −Λij/(ΛiiΛjj−Λij2); it is 0 exactly when
Λij=0. With a={i}:Var(xi∣rest)=1/Λii. A zero in Σ means
marginal independence; a zero in Λ means conditional independence. Gaussian graphical models (graphical lasso)
and Gaussian Markov random fields are built on the zeros of Λ.
Rule of thumb: marginals are easy in covariance form (read off Σaa);conditionals are easy in precision
form (read off Λaa).
What the theorem says.
The conditional mean is affine in xb, and K is the matrix of regression coefficients of xa on xb. For
Gaussians, the best predictor E[xa∣xb] (lesson 04) is linear.
The conditional covariance Sdoes not depend on the observed valuexb.
S⪯Σaa, because ΣabΣbb−1Σba is PSD: for a Gaussian, conditioning never
increases a variance, whatever you observe. (For other distributions, only the average
E[Var(X∣Y)] is at most Var(X), by the law of total variance.)
detΣ=detΣbb⋅detS, the determinant version of p(x)=p(xb)p(xa∣xb).
Interactive figure: loading…
Drag the yellow handle at the left edge up and down to set the observation y of x2, and move the noise
slider r. The sensor reads y=x2+e with noise e∼N(0,r2). The
violet ellipses are the prior of (x1,x2); the blue band is the
observation y±r; the aqua ellipses are the posterior (the conditional given the
observation). The curves at the bottom are the distribution of x1 before (violet) and after
(aqua). x1 is never observed, but its uncertainty shrinks because it is correlated with x2.
Drag the handle: only the centre moves, the size stays. Set the noise to 0: the posterior collapses onto the line
x2=y, and the readouts become the 2-D formulas of section 2.
Answers to section 1. (1) E[x1∣x2=2]=0.8⋅2=1.6, and
Var(x1∣x2)=1−0.82=0.36. For x2=−5 the mean is −4, but the variance is the same 0.36: the
conditional covariance does not depend on the observed value. (2) detΣ=0.11000=10−1000 underflows, so
det returns 0.0 (and its log would be −∞). The true value is logdetΣ=1000log0.1≈−2302.585;
Cholesky gives it as 2∑ilog0.1.
(a) Box–Muller by hand. Take U1=1−e−1≈0.632 and U2=0.125.
S=−2log(1−U1)=−2loge−1=2, so R=2.
Θ=2π⋅0.125=π/4.
Z1=2cos(π/4)=2⋅22=1 and Z2=2sin(π/4)=1.
(b) Fusing two thermometers. The true temperature θ at a site has the prior N(20,4) (in °C).
Thermometer 1 reads y1=θ+e1 with error variance 1, and thermometer 2 reads y2=θ+e2 with error
variance 4. The errors are independent of each other and of θ. So x=(θ,y1,y2) is an affine map of the
independent Gaussians (θ,e1,e2), hence jointly Gaussian, with
μ=202020,Σ=444454448.
For example, Cov(y1,y2)=Var(θ)=4 (lesson 04, the two-thermometer example), and
Var(y2)=4+4=8.
L is the model itself: θ=20+2z1,y1=θ+z2,y2=θ+2z3. And detΣ=(2⋅1⋅2)2=16.
One sample by hand.z=(0.5,−1,1) gives x=μ+Lz=(20+1,20+1−1,20+1+2)=(21,20,23).
Log-density at x=(21,23,17). Here x−μ=(1,3,−3). Forward substitution in Lu=x−μ:u1=1/2=0.5, then u2=(3−2⋅0.5)/1=2, then u3=(−3−2⋅0.5−0⋅2)/2=−2. So
∥u∥2=0.25+4+4=8.25 and
8.25 is above the 95% quantile of χ32 (7.815), so this point is unusual. The reason: the thermometers disagree by
6, while y1−y2 has variance 5+8−2⋅4=5. The gap is 6/5≈2.7 standard deviations.
Marginal.(y1,y2)∼N((2020),(5448)): the lower-right block.
Conditional, covariance form. Observe y1=23 and y2=17. Take a={θ} and b={y1,y2}:Σab=(4,4),Σbb=(5448),detΣbb=40−16=24, and
Σbb−1=241(8−4−45).
Σa∣b=4−KΣba=4−(32⋅4+61⋅4)=4−310=32: a standard deviation of
0.816, down from 2.
Check: detΣbb⋅Σa∣b=24⋅32=16=detΣ. ✓
Conditional, precision form.
Λ=Σ−1=1.5−1−0.25−110−0.2500.25.
Σa∣b=1/Λθθ=1/1.5=2/3. ✓ And 1.5=41+1+41: the prior precision plus the two
thermometer precisions. Precisions add.
μa∣b=20−1.51((−1)⋅3+(−0.25)⋅(−3))=20+1.52.25=21.5. ✓ Equivalently, a
precision-weighted average: (20/4+23/1+17/4)/1.5=21.5. The prior mean, y1 and y2 get the weights
1/6,2/3 and 1/6: their shares of the total precision.
Λy1y2=0: the two readings are conditionally independent given θ (their errors are independent),
although Cov(y1,y2)=4=0.
Quick check
A colleague samples with X = mu + Z @ L, where the rows of Z are i.i.d. N(0,I) and L is the lower Cholesky factor of Σ. What covariance do the rows of X have?
Why.
Row i is μ⊤+zi⊤L, that is, xi=μ+L⊤zi, with covariance L⊤IL=L⊤L. For the thermometer example this gives Var(θ)=12 instead of 4. The correct row form is mu + Z @ L.T.
Kalman filter and data assimilation. Forecast x∼N(m,P), observation y=Hx+e with
e∼N(0,R). Then Cov(x,y)=PH⊤ and Cov(y)=HPH⊤+R, and the conditioning
theorem gives the update: the Kalman gain K=PH⊤(HPH⊤+R)−1, the mean m+K(y−Hm) and the covariance
P−KHP, a Schur complement (ML for science 06).
Gaussian processes. A GP prediction conditions the joint Gaussian of the training targets and the new function
values. Implementations factor k(X,X)+σ2I=LL⊤ once, use triangular solves and jitter, and compute
logdet as 2∑ilogLii (Rasmussen and Williams, Algorithm 2.1).
VAEs and diffusion models. The VAE samples its latent code as μϕ(x)+σϕ(x)⊙ε, which is
μ+Lz with a diagonal L; gradients flow through μ and σ. The diffusion forward process
xt=αˉtx0+1−αˉtε is an affine map of Gaussian noise, and the DDPM
posterior q(xt−1∣xt,x0) is Gaussian conditioning.
Normalising flows maximise logpX(g−1(y))+log∣detJg−1(y)∣, the change-of-variables formula,
with maps whose Jacobian determinant is cheap (triangular Jacobians)
(generative models 04).
Graphical models and random fields. The zeros of the precision matrix encode conditional independence: the
graphical lasso estimates a sparse Λ, and Gaussian Markov random fields on large grids (weather, images) use a
sparse precision with a sparse Cholesky factor.
Anomaly detection. The Mahalanobis distance to the mean of a class, compared with a χd2 quantile, is a simple
and strong out-of-distribution score, also on the features of a neural network.
Multiplying by ∣detJg∣ instead of dividing. Where the map stretches, the density goes down:
pY(y)=pX(x)/∣detJg(x)∣.
"Gaussian marginals, so jointly Gaussian." False (the random-sign example). So "Gaussian and uncorrelated, so
independent" is also false, unless the vector is jointly Gaussian.
The wrong transpose in the sampler.μ+L⊤z has covariance L⊤L=Σ. With samples as rows, write
mu + Z @ L.T.
Mixing up the blocks of Σ and Λ.Σaa is the marginal covariance; Λaa−1 is the
conditional covariance. In general (Σ−1)aa=(Σaa)−1.
Computing det(Sigma) and inv(Sigma). They under- or overflow and lose accuracy. Use Cholesky; if it fails, add
jitter rather than switching to a pseudo-inverse.
Thinking the conditional variance depends on the observed value, or that conditioning can increase a Gaussian
variance.
(x1,x2) is Gaussian with means 0, σ1=2,σ2=3 and ρ=0.5. Find E[x1∣x2=3] and
Var(x1∣x2).AnswerΣ12=ρσ1σ2=3, so K=3/9=1/3 and E[x1∣x2=3]=1.Var(x1∣x2)=4−32/9=3, which equals σ12(1−ρ2)=4⋅0.75.
Find the Cholesky factor and the log-determinant of (42210).AnswerL11=2,L21=2/2=1,L22=10−1=3.logdet=2(log2+log3)=2log6≈3.584. Check: det=40−4=36=62.
x∼N(0,(2112)). Give the joint distribution of (x1+x2,x1−x2). Are the
two independent?AnswerWith A=(111−1):AΣA⊤=diag(6,2), so N(0,diag(6,2)). They are jointly Gaussian (an affine map of a Gaussian) and uncorrelated, hence independent.
X is uniform on the unit square and Y=AX with A=(2013). What is the density of Y?AnswerpY(y)=pX(A−1y)/∣detA∣=1/6 on the parallelogram A[0,1]2 (area 6), and 0 elsewhere.
A Gaussian has the precision Λ=100.50210.512. Which pair is conditionally
independent given the third variable? Is that pair marginally independent? What is Var(x3∣x1,x2)?Answerx1 and x2 are conditionally independent given x3, because Λ12=0. They are not marginally independent: Σ=Λ−1 has Σ12=0.2=0.Var(x3∣x1,x2)=1/Λ33=0.5, against the marginal Σ33=0.8.
You have the Cholesky factor R of the precision, Λ=RR⊤ (common for sparse models). How do you sample
N(μ,Λ−1) without forming Σ?AnswerDraw z∼N(0,I), solve the triangular system R⊤v=z by back substitution, and set x=μ+v. Then Cov(v)=R−⊤R−1=(RR⊤)−1=Λ−1=Σ.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
State the change-of-variables formula for an invertible smooth map Y=g(X) in d dimensions.
pY(y)=pX(g−1(y))∣detJg−1(y)∣=pX(x)/∣detJg(x)∣ with x=g−1(y).
Why is the density divided by ∣detJg∣ and not multiplied?
A small area dA becomes an area ∣detJg∣dA but keeps its probability mass, so mass per area goes down by that factor.
In Box–Muller, what are the distributions of Θ and R2 for two i.i.d. standard normals?
Θ is uniform on [0,2π),R2∼Exp(1/2) (mean 2), and they are independent; the polar Jacobian is r.
Write the density of N(μ,Σ) in d dimensions.
(2π)−d/2(detΣ)−1/2exp(−21(x−μ)⊤Σ−1(x−μ)).
How do you sample N(μ,Σ), and why does it work?
x=μ+Lz with z∼N(0,I) and Σ=LL⊤ (Cholesky), because Cov(Lz)=LL⊤=Σ.
How do you compute logdetΣ from the Cholesky factor L?
logdetΣ=2∑ilogLii, because detΣ=(detL)2 and detL=∏iLii.
What is the marginal of xa when (xa,xb)∼N(μ,Σ)?
N(μa,Σaa): read off the matching blocks.
Give the conditional mean and covariance of xa given xb for a joint Gaussian.
Mean μa+ΣabΣbb−1(xb−μb); covariance Σaa−ΣabΣbb−1Σba (the Schur complement), which equals Λaa−1.
For a 2-D Gaussian with ρ=0.8 and unit variances, what is Var(x1∣x2)?
1−0.82=0.36, whatever value of x2 you observe.
When do zero covariance and independence mean the same thing?
When the variables are jointly Gaussian; Gaussian marginals alone are not enough.
"How do you sample from N(μ,Σ)?" — Cholesky Σ=LL⊤ and x=μ+Lz, since
Cov(Lz)=LL⊤. Cost: d3/3 once, d2 per sample. If Σ is only PSD: the eigendecomposition
Qdiag(λk), a pivoted Cholesky, or jitter.
"Derive the conditional of a bivariate Gaussian." — Mean μ1+ρσ2σ1(x2−μ2), variance
σ12(1−ρ2); the proof uses z=x1−Kx2, which is uncorrelated with x2 and therefore independent of it.
"Are uncorrelated Gaussian variables independent?" — Yes if they are jointly Gaussian; the random-sign example shows
that Gaussian marginals are not enough.
"Why compute a Gaussian log-likelihood with Cholesky?" — det under- or overflows and an inverse is slow and
inaccurate; 2∑ilogLii and one triangular solve are stable.
"What does a zero in the precision matrix mean? Where does the Kalman gain come from?" — Conditional independence given
all other variables; the gain is K=ΣxyΣyy−1 from Gaussian conditioning.
"How does a normalising flow compute logp(x)?" — Map x to the base variable z, then
logp(x)=logpZ(z)+log∣detJg−1(x)∣, with a Jacobian that is triangular so the determinant is cheap.
The change-of-variables formula with the Jacobian, in one and in two dimensions.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The second half defines the multivariate normal through linear combinations and shows why uncorrelated implies independent for it.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The rotational symmetry of the 2-D standard Gaussian, the same fact that makes Box–Muller work.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
A short visual summary of the density, the role of the covariance matrix and the ellipse contours.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Christopher Bishop, Pattern Recognition and Machine Learning
(free PDF from Microsoft Research): §2.3 on the geometry of the Gaussian, §2.3.1 conditional Gaussians (completing
the square and the Schur complement), §2.3.2 marginals, §2.3.3 linear Gaussian models.
Carl Rasmussen and Christopher Williams, Gaussian Processes for Machine Learning,
free chapters: Appendix A.2 (Gaussian identities and sampling with Cholesky), A.3 (inverse of a partitioned matrix),
A.4 (Cholesky and logdet).