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

    Probability and Statistics · Lesson 5 · Many random variables

    Change of variables and the multivariate Gaussian

    Intermediate 85 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 04 (joint densities, the factorisation criterion, Cov⁡(AX+b)=AΣA⊤\operatorname{Cov}(AX + b) = A\Sigma A^\top) 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.


    Why this matters #

    • 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=μ+σ⊙zx = \mu + \sigma \odot 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⁡Σ\det\Sigma underflows and Σ−1\Sigma^{-1} is inaccurate. Every serious implementation uses the Cholesky factor instead.

    1. Predict first #

    2. The picture #

    Mass moves, density changes. Let XX be a random point in the plane with density pXp_X, and let Y=g(X)Y = g(X) for a smooth one-to-one map gg. Take a tiny square of area dAdA around a point xx. It holds probability about pX(x) dAp_X(x)\,dA. The map sends it to a small, slightly curved parallelogram around y=g(x)y = g(x). Near xx, the map looks like its Jacobian matrix Jg(x)J_g(x) (the matrix of partial derivatives), so the image has area about ∣det⁡Jg(x)∣ dA|\det J_g(x)|\,dA (calculus 10). The same mass now sits on that area:

    pY(y) ∣det⁡Jg(x)∣ dA≈pX(x) dA⟹pY(y)=pX(x)∣det⁡Jg(x)∣,x=g−1(y).p_Y(y)\,|\det J_g(x)|\,dA \approx p_X(x)\,dA \quad\Longrightarrow\quad p_Y(y) = \frac{p_X(x)}{|\det J_g(x)|}, \qquad x = g^{-1}(y).

    Stretch the area, lower the density.

    Interactive figure: loading…

    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 ∣det⁡Jg∣|\det J_g| 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)z = (z_1, z_2) have independent N(0,1)\mathcal N(0, 1) entries. Its density 12πe−(z12+z22)/2\frac{1}{2\pi} e^{-(z_1^2 + z_2^2)/2} depends only on the length ∥z∥\|z\|, so its contour lines are circles and the cloud of samples is round. Now apply a matrix and a shift: x=μ+Lzx = \mu + Lz. Circles become ellipses, and by lesson 04

    Cov⁡(x)=L Cov⁡(z) L⊤=L I L⊤=LL⊤=:Σ.\operatorname{Cov}(x) = L\,\operatorname{Cov}(z)\,L^\top = L\,I\,L^\top = LL^\top =: \Sigma .

    The contours of the density of xx are the ellipses (x−μ)⊤Σ−1(x−μ)=c2(x - \mu)^\top \Sigma^{-1} (x - \mu) = c^2. The number (x−μ)⊤Σ−1(x−μ)\sqrt{(x - \mu)^\top \Sigma^{-1}(x - \mu)} is the Mahalanobis distance of xx from μ\mu: the distance measured in standard deviations of the cloud, in each direction. On the ellipse with c=1c = 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 zz become the ellipse of x=μ+Lzx = \mu + Lz. Then change σ1,σ2,ρ\sigma_1, \sigma_2, \rho. The blue points are samples; the orange ellipses are Mahalanobis distance 1 and 2; the violet arrows are the columns of LL. 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 x1x_1 axis. The shadow is a 1-D Gaussian with mean μ1\mu_1 and variance Σ11\Sigma_{11}: nothing to compute. Now fix x2x_2 and look at the density along that horizontal line, rescaled to integrate to 1. This slice is the conditional density of x1x_1 given x2x_2. It is again a 1-D Gaussian. With standard deviations σ1,σ2\sigma_1, \sigma_2 and correlation ρ\rho:

    x1∣x2∼N(μ1+ρ σ1σ2 (x2−μ2),  σ12 (1−ρ2)).x_1 \mid x_2 \sim \mathcal N\Big(\mu_1 + \rho\,\frac{\sigma_1}{\sigma_2}\,(x_2 - \mu_2),\ \ \sigma_1^2\,(1 - \rho^2)\Big).

    As x2x_2 moves, the centre of the slice moves on a straight line (the regression line of x1x_1 on x2x_2). The width of the slice stays the same. A strong correlation gives a narrow slice: knowing x2x_2 tells you a lot about x1x_1. Section 3 proves the general version.

    Interactive figure: loading…

    Drag the yellow handle up and down to move the line x2=cx_2 = c. The aqua curve is the slice (the conditional of x1x_1 given x2=cx_2 = 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 x1x_1). Move ρ\rho towards ±1\pm 1: the slice gets narrower, the shadow does not.

    Quick check

    The map g(x)=2xg(x) = 2x doubles every length in the plane. XX has density pXp_X. What is the density of Y=2XY = 2X at the point y=2xy = 2x?

    3. Definitions and main results #

    Change of variables for a joint density #

    Theorem. Let XX have density pXp_X on an open set U⊆RdU \subseteq \mathbb{R}^d. Let g:U→Vg: U \to V be a bijection onto an open set VV, with gg and g−1g^{-1} continuously differentiable. Then Y=g(X)Y = g(X) has the density

    pY(y)=pX(g−1(y)) ∣det⁡Jg−1(y)∣=pX(x)∣det⁡Jg(x)∣∣x=g−1(y),p_Y(y) = p_X\big(g^{-1}(y)\big)\,\big|\det J_{g^{-1}}(y)\big| = \frac{p_X(x)}{|\det J_g(x)|}\bigg|_{x = g^{-1}(y)} ,

    where Jg−1(y)J_{g^{-1}}(y) is the d×dd \times d matrix of partial derivatives of g−1g^{-1} at yy.

    Proof.

    1. For any set B⊆VB \subseteq V: P(Y∈B)=P(X∈g−1(B))=∫g−1(B)pX(x) dxP(Y \in B) = P(X \in g^{-1}(B)) = \int_{g^{-1}(B)} p_X(x)\,dx.
    2. Substitute x=g−1(y)x = g^{-1}(y) in this integral. The multivariable substitution rule says dx=∣det⁡Jg−1(y)∣ dydx = |\det J_{g^{-1}}(y)|\,dy, so the integral equals ∫BpX(g−1(y)) ∣det⁡Jg−1(y)∣ dy\int_B p_X(g^{-1}(y))\,|\det J_{g^{-1}}(y)|\,dy.
    3. This holds for every BB, so the integrand is the density of YY.
    4. For the second form: by the inverse function theorem Jg−1(y)=Jg(x)−1J_{g^{-1}}(y) = J_g(x)^{-1}, so ∣det⁡Jg−1(y)∣=1/∣det⁡Jg(x)∣|\det J_{g^{-1}}(y)| = 1/|\det J_g(x)|. ■\blacksquare

    It is enough that these conditions hold outside a set of probability 0. Two special cases:

    • One dimension: pY(y)=pX(x)/∣g′(x)∣p_Y(y) = p_X(x)/|g'(x)|, the rule of lesson 02.
    • A linear map Y=AX+bY = AX + b with invertible AA: pY(y)=pX(A−1(y−b))/∣det⁡A∣p_Y(y) = p_X\big(A^{-1}(y - b)\big)/|\det A|.

    In log form, log⁡pY(y)=log⁡pX(x)−log⁡∣det⁡Jg(x)∣\log p_Y(y) = \log p_X(x) - \log|\det J_g(x)|. A normalising flow is a chain of invertible maps with cheap Jacobian determinants, trained by maximising exactly this log-density (generative models 04).

    Box–Muller: two normals from two uniforms #

    Theorem. Let Z1,Z2Z_1, Z_2 be i.i.d. N(0,1)\mathcal N(0, 1), and write (Z1,Z2)=(Rcos⁡Θ,Rsin⁡Θ)(Z_1, Z_2) = (R\cos\Theta, R\sin\Theta) with R>0R > 0 and Θ∈[0,2π)\Theta \in [0, 2\pi). Then RR and Θ\Theta are independent, Θ\Theta is uniform on [0,2π)[0, 2\pi), and R2∼Exp(1/2)R^2 \sim \text{Exp}(1/2) (rate 1/21/2, mean 2).

    Proof.

    1. The joint density of Z=(Z1,Z2)Z = (Z_1, Z_2) is pZ(z)=12πe−(z12+z22)/2p_Z(z) = \frac{1}{2\pi} e^{-(z_1^2 + z_2^2)/2}.
    2. The polar map (r,θ)↦(rcos⁡θ,rsin⁡θ)(r, \theta) \mapsto (r\cos\theta, r\sin\theta) is a smooth bijection from (0,∞)×(0,2π)(0, \infty) \times (0, 2\pi) onto the plane without the half-line {(z1,0):z1≥0}\{(z_1, 0) : z_1 \ge 0\}, which has probability 0. It is g−1g^{-1} in the theorem above, and its Jacobian determinant is
    det⁡(cos⁡θ−rsin⁡θsin⁡θrcos⁡θ)=rcos⁡2θ+rsin⁡2θ=r.\det \begin{pmatrix} \cos\theta & -r\sin\theta \\ \sin\theta & r\cos\theta \end{pmatrix} = r\cos^2\theta + r\sin^2\theta = r .
    1. So pR,Θ(r,θ)=pZ(rcos⁡θ,rsin⁡θ)⋅r=12π⋅re−r2/2p_{R,\Theta}(r, \theta) = p_Z(r\cos\theta, r\sin\theta) \cdot r = \dfrac{1}{2\pi} \cdot r e^{-r^2/2} for r>0r > 0, θ∈(0,2π)\theta \in (0, 2\pi).
    2. This is a function of θ\theta times a function of rr, on a product set. By the factorisation criterion of lesson 04, RR and Θ\Theta are independent, Θ\Theta has the constant density 1/(2π)1/(2\pi), and RR has density re−r2/2re^{-r^2/2} (the Rayleigh distribution).
    3. Let S=R2S = R^2. Then P(S>s)=P(R>s)=∫s∞re−r2/2 dr=e−s/2P(S > s) = P(R > \sqrt s) = \int_{\sqrt s}^\infty r e^{-r^2/2}\,dr = e^{-s/2}: the tail of Exp(1/2)\text{Exp}(1/2). ■\blacksquare

    The sampler runs the proof backwards. Take independent U1,U2U_1, U_2 uniform on (0,1)(0, 1). Inverse-CDF sampling (lesson 02) gives S=−2log⁡(1−U1)∼Exp(1/2)S = -2\log(1 - U_1) \sim \text{Exp}(1/2), and Θ=2πU2\Theta = 2\pi U_2 is uniform. Then

    Z1=Scos⁡Θ,Z2=Ssin⁡ΘZ_1 = \sqrt S \cos\Theta, \qquad Z_2 = \sqrt S \sin\Theta

    are two independent N(0,1)\mathcal N(0, 1) numbers. (Many texts write −2log⁡U1-2\log U_1; it has the same distribution, because 1−U11 - U_1 is also uniform.) Libraries use faster methods today (NumPy uses the ziggurat algorithm), but the Jacobian argument is the same.

    The multivariate Gaussian density #

    Definition. For μ∈Rd\mu \in \mathbb{R}^d and a symmetric positive definite (PD) d×dd \times d matrix Σ\Sigma, the Gaussian (normal) distribution N(μ,Σ)\mathcal N(\mu, \Sigma) has the density

    p(x)=N(x;μ,Σ)=(2π)−d/2 (det⁡Σ)−1/2exp⁡ ⁣(−12(x−μ)⊤Σ−1(x−μ)).p(x) = \mathcal N(x; \mu, \Sigma) = (2\pi)^{-d/2}\,(\det\Sigma)^{-1/2} \exp\!\Big(-\tfrac12 (x - \mu)^\top \Sigma^{-1} (x - \mu)\Big).

    μ\mu is the mean and Σ\Sigma the covariance. The inverse Λ=Σ−1\Lambda = \Sigma^{-1} is the precision matrix, and (x−μ)⊤Σ−1(x−μ)(x - \mu)^\top \Sigma^{-1} (x - \mu) is the squared Mahalanobis distance.

    Theorem (from noise to density). Let z∼N(0,Id)z \sim \mathcal N(0, I_d) (dd independent standard normals). Let Σ=LL⊤\Sigma = LL^\top, where LL is lower triangular with a positive diagonal (the Cholesky factor), and let x=μ+Lzx = \mu + Lz. Then xx has the density above.

    Proof.

    1. The density of zz is a product of dd standard normal densities: pz(z)=(2π)−d/2e−z⊤z/2p_z(z) = (2\pi)^{-d/2} e^{-z^\top z/2}.
    2. The map z↦μ+Lzz \mapsto \mu + Lz is invertible. Its inverse is x↦L−1(x−μ)x \mapsto L^{-1}(x - \mu), a linear map with Jacobian matrix L−1L^{-1}.
    3. By the change-of-variables theorem, px(x)=pz(L−1(x−μ)) ∣det⁡L−1∣p_x(x) = p_z\big(L^{-1}(x - \mu)\big)\,|\det L^{-1}|.
    4. det⁡Σ=det⁡L⋅det⁡L⊤=(det⁡L)2\det\Sigma = \det L \cdot \det L^\top = (\det L)^2, so ∣det⁡L−1∣=1/∣det⁡L∣=(det⁡Σ)−1/2|\det L^{-1}| = 1/|\det L| = (\det\Sigma)^{-1/2}.
    5. Put z=L−1(x−μ)z = L^{-1}(x - \mu) into the exponent, and write L−⊤=(L−1)⊤L^{-\top} = (L^{-1})^\top: z⊤z=(x−μ)⊤L−⊤L−1(x−μ)=(x−μ)⊤Σ−1(x−μ)z^\top z = (x - \mu)^\top L^{-\top} L^{-1} (x - \mu) = (x - \mu)^\top \Sigma^{-1} (x - \mu), because Σ−1=(LL⊤)−1=L−⊤L−1\Sigma^{-1} = (LL^\top)^{-1} = L^{-\top} L^{-1}. ■\blacksquare

    The shape of the contours. With the eigendecomposition Σ=Q diag⁡(λ1,…,λd) Q⊤\Sigma = Q\,\operatorname{diag}(\lambda_1, \dots, \lambda_d)\,Q^\top, the contour (x−μ)⊤Σ−1(x−μ)=c2(x - \mu)^\top \Sigma^{-1} (x - \mu) = c^2 is an ellipsoid with axes along the eigenvectors qkq_k and half-lengths cλkc\sqrt{\lambda_k}. In 2-D the probability inside the ellipse of Mahalanobis radius cc is 1−e−c2/21 - e^{-c^2/2}: about 39.3%39.3\% for c=1c = 1 and 86.5%86.5\% for c=2c = 2 (much less than the 68% and 95% of one dimension).

    Jointly Gaussian, in general. A random vector xx is jointly Gaussian if every linear combination a⊤xa^\top x is a 1-D Gaussian (a constant counts, as N(c,0)\mathcal N(c, 0)). Then its moment generating function is

    Mx(t)=E[et⊤x]=exp⁡ ⁣(t⊤μ+12 t⊤Σ t),M_x(t) = \mathbb{E}\big[e^{t^\top x}\big] = \exp\!\big(t^\top \mu + \tfrac12\,t^\top \Sigma\, t\big),

    because t⊤x∼N(t⊤μ,t⊤Σt)t^\top x \sim \mathcal N(t^\top\mu, t^\top\Sigma t) and E[eW]=em+s2/2\mathbb{E}[e^W] = e^{m + s^2/2} for W∼N(m,s2)W \sim \mathcal N(m, s^2) (lesson 03). The MGF depends only on μ\mu and Σ\Sigma, and it fixes the distribution, so a Gaussian vector is fully described by its mean and covariance. Σ\Sigma may be singular (only PSD); then there is no density on Rd\mathbb{R}^d, and the mass lies on a lower-dimensional affine subspace. Below, Σ\Sigma is PD unless we say otherwise.

    Affine maps and marginals are Gaussian #

    Theorem (affine maps). If x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma), A∈Rm×dA \in \mathbb{R}^{m \times d} and b∈Rmb \in \mathbb{R}^m, then

    y=Ax+b∼N(Aμ+b, AΣA⊤).y = Ax + b \sim \mathcal N\big(A\mu + b,\ A\Sigma A^\top\big).

    Proof.

    1. For t∈Rmt \in \mathbb{R}^m: t⊤y=(A⊤t)⊤x+t⊤bt^\top y = (A^\top t)^\top x + t^\top b.
    2. So My(t)=et⊤b Mx(A⊤t)=exp⁡ ⁣(t⊤(Aμ+b)+12 t⊤AΣA⊤t)M_y(t) = e^{t^\top b}\,M_x(A^\top t) = \exp\!\big(t^\top (A\mu + b) + \tfrac12\,t^\top A\Sigma A^\top t\big).
    3. This is the MGF of N(Aμ+b,AΣA⊤)\mathcal N(A\mu + b, A\Sigma A^\top), and the MGF fixes the distribution. ■\blacksquare

    Corollary (marginals). Split x=(xa,xb)x = (x_a, x_b) into two blocks, and split μ\mu and Σ\Sigma in the same way:

    μ=(μaμb),Σ=(ΣaaΣabΣbaΣbb),Σba=Σab⊤.\mu = \begin{pmatrix} \mu_a \\ \mu_b \end{pmatrix}, \qquad \Sigma = \begin{pmatrix} \Sigma_{aa} & \Sigma_{ab} \\ \Sigma_{ba} & \Sigma_{bb} \end{pmatrix}, \qquad \Sigma_{ba} = \Sigma_{ab}^\top .

    Then xa=(I0)x∼N(μa,Σaa)x_a = \begin{pmatrix} I & 0 \end{pmatrix} x \sim \mathcal N(\mu_a, \Sigma_{aa}): read off the blocks. The same works for any subset of coordinates: keep their entries of μ\mu and their rows and columns of Σ\Sigma.

    Quick check

    x=(x1,x2,x3)∼N(μ,Σ)x = (x_1, x_2, x_3) \sim \mathcal N(\mu, \Sigma) with μ=(1,2,3)\mu = (1, 2, 3) and Σ=(412130205)\Sigma = \begin{pmatrix} 4 & 1 & 2 \\ 1 & 3 & 0 \\ 2 & 0 & 5 \end{pmatrix}. What is the distribution of (x1,x3)(x_1, x_3)?

    Uncorrelated jointly Gaussian variables are independent #

    Theorem. If (xa,xb)(x_a, x_b) is jointly Gaussian and Σab=0\Sigma_{ab} = 0, then xax_a and xbx_b are independent.

    Proof (PD Σ\Sigma).

    1. Σ=(Σaa00Σbb)\Sigma = \begin{pmatrix} \Sigma_{aa} & 0 \\ 0 & \Sigma_{bb} \end{pmatrix} is block diagonal. So Σ−1=(Σaa−100Σbb−1)\Sigma^{-1} = \begin{pmatrix} \Sigma_{aa}^{-1} & 0 \\ 0 & \Sigma_{bb}^{-1} \end{pmatrix} and det⁡Σ=det⁡Σaadet⁡Σbb\det\Sigma = \det\Sigma_{aa} \det\Sigma_{bb}.
    2. The quadratic form splits into a part with xax_a and a part with xbx_b: (x−μ)⊤Σ−1(x−μ)=(xa−μa)⊤Σaa−1(xa−μa)+(xb−μb)⊤Σbb−1(xb−μb)(x - \mu)^\top \Sigma^{-1} (x - \mu) = (x_a - \mu_a)^\top \Sigma_{aa}^{-1} (x_a - \mu_a) + (x_b - \mu_b)^\top \Sigma_{bb}^{-1} (x_b - \mu_b).
    3. So p(x)=N(xa;μa,Σaa)⋅N(xb;μb,Σbb)p(x) = \mathcal N(x_a; \mu_a, \Sigma_{aa}) \cdot \mathcal N(x_b; \mu_b, \Sigma_{bb}), a function of xax_a times a function of xbx_b, on all of Rd\mathbb{R}^d. By the factorisation criterion, xax_a and xbx_b are independent. ■\blacksquare

    The word "jointly" is essential. Gaussian marginals are not enough. Let X∼N(0,1)X \sim \mathcal N(0, 1), let W=±1W = \pm 1 with probability 1/21/2 each, independent of XX, and let Y=WXY = WX.

    1. Y∼N(0,1)Y \sim \mathcal N(0, 1), because the density of XX is symmetric: flipping the sign does not change it.
    2. Cov⁡(X,Y)=E[WX2]=E[W] E[X2]=0⋅1=0\operatorname{Cov}(X, Y) = \mathbb{E}[WX^2] = \mathbb{E}[W]\,\mathbb{E}[X^2] = 0 \cdot 1 = 0: uncorrelated.
    3. But X+Y=0X + Y = 0 whenever W=−1W = -1, so P(X+Y=0)=1/2P(X + Y = 0) = 1/2. A Gaussian has no point with positive probability (except the constant case), so X+YX + Y is not Gaussian, and (X,Y)(X, Y) is not jointly Gaussian.
    4. XX and YY are dependent: ∣Y∣=∣X∣|Y| = |X| always.
    Interactive figure: loading…

    Press Draw 1000 more a few times and compare the two cases. In "Y=WXY = WX", both histograms of XX and YY (blue) match the orange N(0,1)\mathcal N(0, 1) curve, and the correlation is about 0. But the scatter is an X shape, and the histogram of X+YX + Y has a spike at 0 that holds half of the samples, and the correlation of ∣X∣|X| and ∣Y∣|Y| is exactly 1. In "independent YY", X+YX + Y follows the N(0,2)\mathcal N(0, 2) curve. In both cases Var⁡(X+Y)≈2\operatorname{Var}(X + Y) \approx 2: the variance cannot see the difference.

    Quick check

    X∼N(0,1)X \sim \mathcal N(0, 1) and Y∼N(0,1)Y \sim \mathcal N(0, 1). Is X+YX + Y always Gaussian?

    Cholesky in practice: sampling and a stable log-density #

    Let Σ=LL⊤\Sigma = LL^\top be the Cholesky factorisation (linear algebra 07). It costs about d3/3d^3/3 floating-point operations, once.

    • Sampling. Draw z∼N(0,Id)z \sim \mathcal N(0, I_d) and set x=μ+Lzx = \mu + Lz. By the affine-map theorem, x∼N(μ,LL⊤)=N(μ,Σ)x \sim \mathcal N(\mu, LL^\top) = \mathcal N(\mu, \Sigma). Each sample costs about d2d^2 operations. With nn samples as the rows of an n×dn \times d matrix ZZ, the samples are the rows of 1μ⊤+ZL⊤\mathbf 1 \mu^\top + Z L^\top. Any BB with BB⊤=ΣBB^\top = \Sigma works (for example Q diag⁡(λk)Q\,\operatorname{diag}(\sqrt{\lambda_k})); Cholesky is the cheapest.
    • Log-density. log⁡p(x)=−12[dlog⁡(2π)+log⁡det⁡Σ+(x−μ)⊤Σ−1(x−μ)]\log p(x) = -\tfrac12\big[d\log(2\pi) + \log\det\Sigma + (x - \mu)^\top \Sigma^{-1}(x - \mu)\big]. Two identities make it stable:
      1. The determinant of a triangular matrix is the product of its diagonal. So det⁡Σ=(det⁡L)2=∏iLii2\det\Sigma = (\det L)^2 = \prod_i L_{ii}^2 and log⁡det⁡Σ=2∑ilog⁡Lii\log\det\Sigma = 2\sum_i \log L_{ii}.
      2. Solve Lu=x−μLu = x - \mu by forward substitution (about d2d^2 operations). Then (x−μ)⊤Σ−1(x−μ)=(x−μ)⊤L−⊤L−1(x−μ)=∥u∥2(x - \mu)^\top \Sigma^{-1} (x - \mu) = (x - \mu)^\top L^{-\top} L^{-1} (x - \mu) = \|u\|^2.
    • Why not det and inv. det⁡Σ\det\Sigma is a product of dd numbers, so it under- or overflows in float64 while log⁡det⁡Σ\log\det\Sigma is an ordinary number. An explicit Σ−1\Sigma^{-1} costs more and loses accuracy when Σ\Sigma is ill-conditioned; triangular solves are backward stable.
    • A free check. u=L−1(x−μ)∼N(0,Id)u = L^{-1}(x - \mu) \sim \mathcal N(0, I_d), so for real samples the squared Mahalanobis distance ∥u∥2\|u\|^2 follows a χd2\chi^2_d distribution (mean dd).
    • 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\Sigma + \varepsilon I, with ε\varepsilon around 10−810^{-8} times the size of the variances.
    Quick check

    In d=1000d = 1000 dimensions, what does np.linalg.det(0.1 * np.eye(1000)) return in float64?

    Conditioning: the Schur complement #

    For the split Σ\Sigma with Σbb\Sigma_{bb} invertible, the Schur complement of Σbb\Sigma_{bb} is

    S=Σaa−Σab Σbb−1 Σba.S = \Sigma_{aa} - \Sigma_{ab}\,\Sigma_{bb}^{-1}\,\Sigma_{ba} .

    It is what remains of Σaa\Sigma_{aa} after one step of block Gaussian elimination removes the bb block.

    First, the 2-D case with numbers. Take a={1}a = \{1\}, b={2}b = \{2\}, Σ=(σ12ρσ1σ2ρσ1σ2σ22)\Sigma = \begin{pmatrix} \sigma_1^2 & \rho\sigma_1\sigma_2 \\ \rho\sigma_1\sigma_2 & \sigma_2^2 \end{pmatrix}. Then K=ΣabΣbb−1=ρσ1/σ2K = \Sigma_{ab}\Sigma_{bb}^{-1} = \rho\sigma_1/\sigma_2 and S=σ12−(ρσ1σ2)2/σ22=σ12(1−ρ2)S = \sigma_1^2 - (\rho\sigma_1\sigma_2)^2/\sigma_2^2 = \sigma_1^2(1 - \rho^2), which is the formula of section 2. With σ1=σ2=1\sigma_1 = \sigma_2 = 1 and ρ=0.8\rho = 0.8: K=0.8K = 0.8 and S=1−0.64=0.36S = 1 - 0.64 = 0.36.

    Theorem (Gaussian conditioning). Let x=(xa,xb)∼N(μ,Σ)x = (x_a, x_b) \sim \mathcal N(\mu, \Sigma) with Σbb\Sigma_{bb} invertible, and let K=ΣabΣbb−1K = \Sigma_{ab}\Sigma_{bb}^{-1}. Then xa∣xb∼N(μa∣b,Σa∣b)x_a \mid x_b \sim \mathcal N(\mu_{a \mid b}, \Sigma_{a \mid b}) with

    μa∣b=μa+K (xb−μb),Σa∣b=Σaa−Σab Σbb−1 Σba=S.\mu_{a \mid b} = \mu_a + K\,(x_b - \mu_b), \qquad \Sigma_{a \mid b} = \Sigma_{aa} - \Sigma_{ab}\,\Sigma_{bb}^{-1}\,\Sigma_{ba} = S .

    In precision form, with Λ=Σ−1\Lambda = \Sigma^{-1} split into the same blocks:

    Σa∣b=Λaa−1,μa∣b=μa−Λaa−1Λab (xb−μb).\Sigma_{a \mid b} = \Lambda_{aa}^{-1}, \qquad \mu_{a \mid b} = \mu_a - \Lambda_{aa}^{-1}\Lambda_{ab}\,(x_b - \mu_b).
    Proof: remove the part of xax_a that xbx_b explains
    1. Let z=xa−Kxbz = x_a - Kx_b. In matrix form (z,xb)=Tx(z, x_b) = Tx with T=(I−K0I)T = \begin{pmatrix} I & -K \\ 0 & I \end{pmatrix}, so by the affine-map theorem (z,xb)(z, x_b) is jointly Gaussian.
    2. By bilinearity, Cov⁡(z,xb)=Σab−KΣbb=Σab−Σab=0\operatorname{Cov}(z, x_b) = \Sigma_{ab} - K\Sigma_{bb} = \Sigma_{ab} - \Sigma_{ab} = 0. By the previous theorem, zz and xbx_b are independent.
    3. E[z]=μa−Kμb\mathbb{E}[z] = \mu_a - K\mu_b, and Cov⁡(z)=Σaa−KΣba−ΣabK⊤+KΣbbK⊤\operatorname{Cov}(z) = \Sigma_{aa} - K\Sigma_{ba} - \Sigma_{ab}K^\top + K\Sigma_{bb}K^\top. Each of the last three terms equals ΣabΣbb−1Σba\Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{ba} (use Σbb⊤=Σbb\Sigma_{bb}^\top = \Sigma_{bb}). So Cov⁡(z)=S\operatorname{Cov}(z) = S, and TΣT⊤=(S00Σbb)T\Sigma T^\top = \begin{pmatrix} S & 0 \\ 0 & \Sigma_{bb} \end{pmatrix}. In particular SS is PD.
    4. Write xa=z+Kxbx_a = z + Kx_b. Given xbx_b, the term KxbKx_b is a constant, and zz keeps its distribution because it is independent of xbx_b. So xa∣xb∼N(μa−Kμb+Kxb, S)x_a \mid x_b \sim \mathcal N(\mu_a - K\mu_b + Kx_b,\ S): the covariance form.
    5. Precision form. det⁡T=1\det T = 1, so TT is invertible, and step 3 gives Σ=T−1(S00Σbb)T−⊤\Sigma = T^{-1}\begin{pmatrix} S & 0 \\ 0 & \Sigma_{bb} \end{pmatrix}T^{-\top}. Invert both sides:
    Λ=T⊤(S−100Σbb−1)T=(S−1−S−1K−K⊤S−1Σbb−1+K⊤S−1K).\Lambda = T^\top \begin{pmatrix} S^{-1} & 0 \\ 0 & \Sigma_{bb}^{-1} \end{pmatrix} T = \begin{pmatrix} S^{-1} & -S^{-1}K \\ -K^\top S^{-1} & \Sigma_{bb}^{-1} + K^\top S^{-1} K \end{pmatrix}.

    So Λaa=S−1\Lambda_{aa} = S^{-1} and Λab=−S−1K\Lambda_{ab} = -S^{-1}K, that is, S=Λaa−1S = \Lambda_{aa}^{-1} and K=−Λaa−1ΛabK = -\Lambda_{aa}^{-1}\Lambda_{ab}. Put these into step 4. ■\blacksquare

    Second route: completing the square
    1. As a function of xax_a, log⁡p(xa∣xb)=log⁡p(xa,xb)−log⁡p(xb)=−12(x−μ)⊤Λ(x−μ)+const\log p(x_a \mid x_b) = \log p(x_a, x_b) - \log p(x_b) = -\tfrac12 (x - \mu)^\top \Lambda (x - \mu) + \text{const}.
    2. Expand in blocks and keep only the terms with xax_a: −12 xa⊤Λaaxa+xa⊤[Λaaμa−Λab(xb−μb)]+const-\tfrac12\,x_a^\top \Lambda_{aa} x_a + x_a^\top \big[\Lambda_{aa}\mu_a - \Lambda_{ab}(x_b - \mu_b)\big] + \text{const}.
    3. N(m,P−1)\mathcal N(m, P^{-1}) has log-density −12 xa⊤Pxa+xa⊤Pm+const-\tfrac12\,x_a^\top P x_a + x_a^\top P m + \text{const}. Matching the quadratic terms gives P=ΛaaP = \Lambda_{aa}; matching the linear terms gives Pm=Λaaμa−Λab(xb−μb)Pm = \Lambda_{aa}\mu_a - \Lambda_{ab}(x_b - \mu_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≠ji \ne j: Λij=0\Lambda_{ij} = 0 if and only if xix_i and xjx_j are conditionally independent given all other coordinates. Take a={i,j}a = \{i, j\}: the conditional covariance of (xi,xj)(x_i, x_j) is Λaa−1\Lambda_{aa}^{-1}, whose off-diagonal entry is −Λij/(ΛiiΛjj−Λij2)-\Lambda_{ij}/(\Lambda_{ii}\Lambda_{jj} - \Lambda_{ij}^2); it is 0 exactly when Λij=0\Lambda_{ij} = 0. With a={i}a = \{i\}: Var⁡(xi∣rest)=1/Λii\operatorname{Var}(x_i \mid \text{rest}) = 1/\Lambda_{ii}. A zero in Σ\Sigma means marginal independence; a zero in Λ\Lambda means conditional independence. Gaussian graphical models (graphical lasso) and Gaussian Markov random fields are built on the zeros of Λ\Lambda.

    Rule of thumb: marginals are easy in covariance form (read off Σaa\Sigma_{aa}); conditionals are easy in precision form (read off Λaa\Lambda_{aa}).

    What the theorem says.

    • The conditional mean is affine in xbx_b, and KK is the matrix of regression coefficients of xax_a on xbx_b. For Gaussians, the best predictor E[xa∣xb]\mathbb{E}[x_a \mid x_b] (lesson 04) is linear.
    • The conditional covariance SS does not depend on the observed value xbx_b.
    • S⪯ΣaaS \preceq \Sigma_{aa}, because ΣabΣbb−1Σba\Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{ba} is PSD: for a Gaussian, conditioning never increases a variance, whatever you observe. (For other distributions, only the average E[Var⁡(X∣Y)]\mathbb{E}[\operatorname{Var}(X \mid Y)] is at most Var⁡(X)\operatorname{Var}(X), by the law of total variance.)
    • det⁡Σ=det⁡Σbb⋅det⁡S\det\Sigma = \det\Sigma_{bb} \cdot \det S, the determinant version of p(x)=p(xb) p(xa∣xb)p(x) = p(x_b)\,p(x_a \mid x_b).
    Interactive figure: loading…

    Drag the yellow handle at the left edge up and down to set the observation yy of x2x_2, and move the noise slider rr. The sensor reads y=x2+ey = x_2 + e with noise e∼N(0,r2)e \sim \mathcal N(0, r^2). The violet ellipses are the prior of (x1,x2)(x_1, x_2); the blue band is the observation y±ry \pm r; the aqua ellipses are the posterior (the conditional given the observation). The curves at the bottom are the distribution of x1x_1 before (violet) and after (aqua). x1x_1 is never observed, but its uncertainty shrinks because it is correlated with x2x_2. Drag the handle: only the centre moves, the size stays. Set the noise to 0: the posterior collapses onto the line x2=yx_2 = 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\mathbb{E}[x_1 \mid x_2 = 2] = 0.8 \cdot 2 = 1.6, and Var⁡(x1∣x2)=1−0.82=0.36\operatorname{Var}(x_1 \mid x_2) = 1 - 0.8^2 = 0.36. For x2=−5x_2 = -5 the mean is −4-4, but the variance is the same 0.360.36: the conditional covariance does not depend on the observed value. (2) det⁡Σ=0.11000=10−1000\det\Sigma = 0.1^{1000} = 10^{-1000} underflows, so det returns 0.0 (and its log would be −∞-\infty). The true value is log⁡det⁡Σ=1000log⁡0.1≈−2302.585\log\det\Sigma = 1000\log 0.1 \approx -2302.585; Cholesky gives it as 2∑ilog⁡0.12\sum_i \log\sqrt{0.1}.

    4. Worked example #

    (a) Box–Muller by hand. Take U1=1−e−1≈0.632U_1 = 1 - e^{-1} \approx 0.632 and U2=0.125U_2 = 0.125.

    1. S=−2log⁡(1−U1)=−2log⁡e−1=2S = -2\log(1 - U_1) = -2\log e^{-1} = 2, so R=2R = \sqrt 2.
    2. Θ=2π⋅0.125=π/4\Theta = 2\pi \cdot 0.125 = \pi/4.
    3. Z1=2cos⁡(π/4)=2⋅22=1Z_1 = \sqrt 2 \cos(\pi/4) = \sqrt 2 \cdot \tfrac{\sqrt 2}{2} = 1 and Z2=2sin⁡(π/4)=1Z_2 = \sqrt 2 \sin(\pi/4) = 1.

    (b) Fusing two thermometers. The true temperature θ\theta at a site has the prior N(20,4)\mathcal N(20, 4) (in °C). Thermometer 1 reads y1=θ+e1y_1 = \theta + e_1 with error variance 1, and thermometer 2 reads y2=θ+e2y_2 = \theta + e_2 with error variance 4. The errors are independent of each other and of θ\theta. So x=(θ,y1,y2)x = (\theta, y_1, y_2) is an affine map of the independent Gaussians (θ,e1,e2)(\theta, e_1, e_2), hence jointly Gaussian, with

    μ=(202020),Σ=(444454448).\mu = \begin{pmatrix} 20 \\ 20 \\ 20 \end{pmatrix}, \qquad \Sigma = \begin{pmatrix} 4 & 4 & 4 \\ 4 & 5 & 4 \\ 4 & 4 & 8 \end{pmatrix}.

    For example, Cov⁡(y1,y2)=Var⁡(θ)=4\operatorname{Cov}(y_1, y_2) = \operatorname{Var}(\theta) = 4 (lesson 04, the two-thermometer example), and Var⁡(y2)=4+4=8\operatorname{Var}(y_2) = 4 + 4 = 8.

    Cholesky, column by column.

    • Column 1: L11=4=2L_{11} = \sqrt 4 = 2, L21=4/2=2L_{21} = 4/2 = 2, L31=4/2=2L_{31} = 4/2 = 2.
    • Column 2: L22=5−22=1L_{22} = \sqrt{5 - 2^2} = 1, L32=(4−2⋅2)/1=0L_{32} = (4 - 2 \cdot 2)/1 = 0.
    • Column 3: L33=8−22−02=2L_{33} = \sqrt{8 - 2^2 - 0^2} = 2.
    L=(200210202),log⁡det⁡Σ=2(log⁡2+log⁡1+log⁡2)=4log⁡2≈2.7726.L = \begin{pmatrix} 2 & 0 & 0 \\ 2 & 1 & 0 \\ 2 & 0 & 2 \end{pmatrix}, \qquad \log\det\Sigma = 2(\log 2 + \log 1 + \log 2) = 4\log 2 \approx 2.7726 .

    LL is the model itself: θ=20+2z1\theta = 20 + 2z_1, y1=θ+z2y_1 = \theta + z_2, y2=θ+2z3y_2 = \theta + 2z_3. And det⁡Σ=(2⋅1⋅2)2=16\det\Sigma = (2 \cdot 1 \cdot 2)^2 = 16.

    One sample by hand. z=(0.5,−1,1)z = (0.5, -1, 1) gives x=μ+Lz=(20+1, 20+1−1, 20+1+2)=(21,20,23)x = \mu + Lz = (20 + 1,\ 20 + 1 - 1,\ 20 + 1 + 2) = (21, 20, 23).

    Log-density at x=(21,23,17)x = (21, 23, 17). Here x−μ=(1,3,−3)x - \mu = (1, 3, -3). Forward substitution in Lu=x−μLu = x - \mu: u1=1/2=0.5u_1 = 1/2 = 0.5, then u2=(3−2⋅0.5)/1=2u_2 = (3 - 2 \cdot 0.5)/1 = 2, then u3=(−3−2⋅0.5−0⋅2)/2=−2u_3 = (-3 - 2 \cdot 0.5 - 0 \cdot 2)/2 = -2. So ∥u∥2=0.25+4+4=8.25\|u\|^2 = 0.25 + 4 + 4 = 8.25 and

    log⁡p(x)=−12[3log⁡(2π)+4log⁡2+8.25]=−12 [5.5136+2.7726+8.25]≈−8.268.\log p(x) = -\tfrac12\big[3\log(2\pi) + 4\log 2 + 8.25\big] = -\tfrac12\,[5.5136 + 2.7726 + 8.25] \approx -8.268 .

    8.258.25 is above the 95% quantile of χ32\chi^2_3 (7.815), so this point is unusual. The reason: the thermometers disagree by 6, while y1−y2y_1 - y_2 has variance 5+8−2⋅4=55 + 8 - 2 \cdot 4 = 5. The gap is 6/5≈2.76/\sqrt 5 \approx 2.7 standard deviations.

    Marginal. (y1,y2)∼N((2020),(5448))(y_1, y_2) \sim \mathcal N\Big(\begin{pmatrix}20\\20\end{pmatrix}, \begin{pmatrix} 5 & 4 \\ 4 & 8 \end{pmatrix}\Big): the lower-right block.

    Conditional, covariance form. Observe y1=23y_1 = 23 and y2=17y_2 = 17. Take a={θ}a = \{\theta\} and b={y1,y2}b = \{y_1, y_2\}: Σab=(4,4)\Sigma_{ab} = (4, 4), Σbb=(5448)\Sigma_{bb} = \begin{pmatrix} 5 & 4 \\ 4 & 8 \end{pmatrix}, det⁡Σbb=40−16=24\det\Sigma_{bb} = 40 - 16 = 24, and Σbb−1=124(8−4−45)\Sigma_{bb}^{-1} = \frac{1}{24}\begin{pmatrix} 8 & -4 \\ -4 & 5 \end{pmatrix}.

    1. K=ΣabΣbb−1=124 (4⋅8−4⋅4, −4⋅4+4⋅5)=124(16,4)=(2/3, 1/6)K = \Sigma_{ab}\Sigma_{bb}^{-1} = \frac{1}{24}\,(4 \cdot 8 - 4 \cdot 4,\ -4 \cdot 4 + 4 \cdot 5) = \frac{1}{24}(16, 4) = (2/3,\ 1/6).
    2. μa∣b=20+23⋅(23−20)+16⋅(17−20)=20+2−0.5=21.5\mu_{a \mid b} = 20 + \tfrac23 \cdot (23 - 20) + \tfrac16 \cdot (17 - 20) = 20 + 2 - 0.5 = 21.5.
    3. Σa∣b=4−KΣba=4−(23⋅4+16⋅4)=4−103=23\Sigma_{a \mid b} = 4 - K\Sigma_{ba} = 4 - \big(\tfrac23 \cdot 4 + \tfrac16 \cdot 4\big) = 4 - \tfrac{10}{3} = \tfrac23: a standard deviation of 0.8160.816, down from 22.
    4. Check: det⁡Σbb⋅Σa∣b=24⋅23=16=det⁡Σ\det\Sigma_{bb} \cdot \Sigma_{a \mid b} = 24 \cdot \tfrac23 = 16 = \det\Sigma. ✓

    Conditional, precision form.

    Λ=Σ−1=(1.5−1−0.25−110−0.2500.25).\Lambda = \Sigma^{-1} = \begin{pmatrix} 1.5 & -1 & -0.25 \\ -1 & 1 & 0 \\ -0.25 & 0 & 0.25 \end{pmatrix}.
    1. Σa∣b=1/Λθθ=1/1.5=2/3\Sigma_{a \mid b} = 1/\Lambda_{\theta\theta} = 1/1.5 = 2/3. ✓ And 1.5=14+1+141.5 = \tfrac14 + 1 + \tfrac14: the prior precision plus the two thermometer precisions. Precisions add.
    2. μa∣b=20−11.5((−1)⋅3+(−0.25)⋅(−3))=20+2.251.5=21.5\mu_{a \mid b} = 20 - \tfrac{1}{1.5}\big((-1) \cdot 3 + (-0.25) \cdot (-3)\big) = 20 + \tfrac{2.25}{1.5} = 21.5. ✓ Equivalently, a precision-weighted average: (20/4+23/1+17/4)/1.5=21.5(20/4 + 23/1 + 17/4)/1.5 = 21.5. The prior mean, y1y_1 and y2y_2 get the weights 1/61/6, 2/32/3 and 1/61/6: their shares of the total precision.
    3. Λy1y2=0\Lambda_{y_1 y_2} = 0: the two readings are conditionally independent given θ\theta (their errors are independent), although Cov⁡(y1,y2)=4≠0\operatorname{Cov}(y_1, y_2) = 4 \ne 0.
    Quick check

    A colleague samples with X = mu + Z @ L, where the rows of Z are i.i.d. N(0,I)\mathcal N(0, I) and L is the lower Cholesky factor of Σ\Sigma. What covariance do the rows of X have?

    5. In code #

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

    import numpy as np
    from scipy import stats
    
    mu = np.array([20.0, 20.0, 20.0])                 # (θ, y1, y2)
    Sigma = np.array([[4.0, 4.0, 4.0],
                      [4.0, 5.0, 4.0],
                      [4.0, 4.0, 8.0]])
    L = np.linalg.cholesky(Sigma)
    print(L)                                          # [[2. 0. 0.] [2. 1. 0.] [2. 0. 2.]]
    print(round(2 * np.log(np.diag(L)).sum(), 4))     # 2.7726 = log det Σ = 4 log 2
    
    x = np.array([21.0, 23.0, 17.0])
    u = np.linalg.solve(L, x - mu)                    # forward substitution (use scipy.linalg.solve_triangular for speed)
    logp = -0.5 * (3 * np.log(2 * np.pi) + 4 * np.log(2) + u @ u)
    print(u, round(logp, 4))                          # [ 0.5  2.  -2. ] -8.2681
    print(round(stats.multivariate_normal(mu, Sigma).logpdf(x), 4))   # -8.2681
    
    # conditioning θ on (y1, y2) = (23, 17), covariance form
    Sab, Sbb = Sigma[0, 1:], Sigma[1:, 1:]
    K = np.linalg.solve(Sbb, Sab)                     # = Σ_ab Σ_bb^{-1} (Σ_bb is symmetric)
    print(K.round(4), 20 + K @ (x[1:] - 20), round(4 - K @ Sab, 4))   # [0.6667 0.1667] 21.5 0.6667
    
    # det underflows, log det does not
    C = 0.1 * np.eye(1000)
    print(np.linalg.det(C), round(np.linalg.slogdet(C)[1], 3))       # 0.0 -2302.585
    
    # Gaussian marginals, uncorrelated, but not jointly Gaussian: Y = W X
    rng = np.random.default_rng(1)
    X1 = rng.standard_normal(200_000)
    Y1 = rng.choice([-1.0, 1.0], size=X1.size) * X1
    print(round(np.corrcoef(X1, Y1)[0, 1], 2), round(np.mean(X1 + Y1 == 0), 2))  # about 0.0 and 0.5
    

    A least-squares fit on samples from the model finds KK and the Schur complement, because the conditional mean is affine:

    rng = np.random.default_rng(0)
    n = 1_000_000
    theta = 20 + 2 * rng.standard_normal(n)           # θ ~ N(20, 4)
    y1 = theta + rng.standard_normal(n)               # error variance 1
    y2 = theta + 2 * rng.standard_normal(n)           # error variance 4
    D = np.column_stack([np.ones(n), y1, y2])
    coef, *_ = np.linalg.lstsq(D, theta, rcond=None)
    print(coef[1:].round(3), (theta - D @ coef).var().round(3))      # about [0.667 0.167] 0.667
    

    6. Where it shows up #

    • Kalman filter and data assimilation. Forecast x∼N(m,P)x \sim \mathcal N(m, P), observation y=Hx+ey = Hx + e with e∼N(0,R)e \sim \mathcal N(0, R). Then Cov⁡(x,y)=PH⊤\operatorname{Cov}(x, y) = PH^\top and Cov⁡(y)=HPH⊤+R\operatorname{Cov}(y) = HPH^\top + R, and the conditioning theorem gives the update: the Kalman gain K=PH⊤(HPH⊤+R)−1K = PH^\top(HPH^\top + R)^{-1}, the mean m+K(y−Hm)m + K(y - Hm) and the covariance P−KHPP - 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⊤k(X, X) + \sigma^2 I = LL^\top once, use triangular solves and jitter, and compute log⁡det⁡\log\det as 2∑ilog⁡Lii2\sum_i \log L_{ii} (Rasmussen and Williams, Algorithm 2.1).
    • VAEs and diffusion models. The VAE samples its latent code as μϕ(x)+σϕ(x)⊙ε\mu_\phi(x) + \sigma_\phi(x) \odot \varepsilon, which is μ+Lz\mu + Lz with a diagonal LL; gradients flow through μ\mu and σ\sigma. The diffusion forward process xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1 - \bar\alpha_t}\,\varepsilon is an affine map of Gaussian noise, and the DDPM posterior q(xt−1∣xt,x0)q(x_{t-1} \mid x_t, x_0) is Gaussian conditioning.
    • Normalising flows maximise log⁡pX(g−1(y))+log⁡∣det⁡Jg−1(y)∣\log p_X(g^{-1}(y)) + \log|\det J_{g^{-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 Λ\Lambda, 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\chi^2_d quantile, is a simple and strong out-of-distribution score, also on the features of a neural network.

    7. Common mistakes #

    • Multiplying by ∣det⁡Jg∣|\det J_g| instead of dividing. Where the map stretches, the density goes down: pY(y)=pX(x)/∣det⁡Jg(x)∣p_Y(y) = p_X(x)/|\det J_g(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\mu + L^\top z has covariance L⊤L≠ΣL^\top L \ne \Sigma. With samples as rows, write mu + Z @ L.T.
    • Mixing up the blocks of Σ\Sigma and Λ\Lambda. Σaa\Sigma_{aa} is the marginal covariance; Λaa−1\Lambda_{aa}^{-1} is the conditional covariance. In general (Σ−1)aa≠(Σaa)−1(\Sigma^{-1})_{aa} \ne (\Sigma_{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.

    8. Check yourself #

    1. (x1,x2)(x_1, x_2) is Gaussian with means 0, σ1=2\sigma_1 = 2, σ2=3\sigma_2 = 3 and ρ=0.5\rho = 0.5. Find E[x1∣x2=3]\mathbb{E}[x_1 \mid x_2 = 3] and Var⁡(x1∣x2)\operatorname{Var}(x_1 \mid x_2).
      AnswerΣ12=ρσ1σ2=3\Sigma_{12} = \rho\sigma_1\sigma_2 = 3, so K=3/9=1/3K = 3/9 = 1/3 and E[x1∣x2=3]=1\mathbb{E}[x_1 \mid x_2 = 3] = 1. Var⁡(x1∣x2)=4−32/9=3\operatorname{Var}(x_1 \mid x_2) = 4 - 3^2/9 = 3, which equals σ12(1−ρ2)=4⋅0.75\sigma_1^2(1 - \rho^2) = 4 \cdot 0.75.
    2. Find the Cholesky factor and the log-determinant of (42210)\begin{pmatrix} 4 & 2 \\ 2 & 10 \end{pmatrix}.
      AnswerL11=2L_{11} = 2, L21=2/2=1L_{21} = 2/2 = 1, L22=10−1=3L_{22} = \sqrt{10 - 1} = 3. log⁡det⁡=2(log⁡2+log⁡3)=2log⁡6≈3.584\log\det = 2(\log 2 + \log 3) = 2\log 6 \approx 3.584. Check: det⁡=40−4=36=62\det = 40 - 4 = 36 = 6^2.
    3. x∼N(0,(2112))x \sim \mathcal N\Big(0, \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}\Big). Give the joint distribution of (x1+x2, x1−x2)(x_1 + x_2,\ x_1 - x_2). Are the two independent?
      AnswerWith A=(111−1)A = \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}: AΣA⊤=diag⁡(6,2)A\Sigma A^\top = \operatorname{diag}(6, 2), so N(0,diag⁡(6,2))\mathcal N(0, \operatorname{diag}(6, 2)). They are jointly Gaussian (an affine map of a Gaussian) and uncorrelated, hence independent.
    4. XX is uniform on the unit square and Y=AXY = AX with A=(2103)A = \begin{pmatrix} 2 & 1 \\ 0 & 3 \end{pmatrix}. What is the density of YY?
      AnswerpY(y)=pX(A−1y)/∣det⁡A∣=1/6p_Y(y) = p_X(A^{-1}y)/|\det A| = 1/6 on the parallelogram A[0,1]2A[0, 1]^2 (area 6), and 0 elsewhere.
    5. A Gaussian has the precision Λ=(100.50210.512)\Lambda = \begin{pmatrix} 1 & 0 & 0.5 \\ 0 & 2 & 1 \\ 0.5 & 1 & 2 \end{pmatrix}. Which pair is conditionally independent given the third variable? Is that pair marginally independent? What is Var⁡(x3∣x1,x2)\operatorname{Var}(x_3 \mid x_1, x_2)?
      Answerx1x_1 and x2x_2 are conditionally independent given x3x_3, because Λ12=0\Lambda_{12} = 0. They are not marginally independent: Σ=Λ−1\Sigma = \Lambda^{-1} has Σ12=0.2≠0\Sigma_{12} = 0.2 \ne 0. Var⁡(x3∣x1,x2)=1/Λ33=0.5\operatorname{Var}(x_3 \mid x_1, x_2) = 1/\Lambda_{33} = 0.5, against the marginal Σ33=0.8\Sigma_{33} = 0.8.
    6. You have the Cholesky factor RR of the precision, Λ=RR⊤\Lambda = RR^\top (common for sparse models). How do you sample N(μ,Λ−1)\mathcal N(\mu, \Lambda^{-1}) without forming Σ\Sigma?
      AnswerDraw z∼N(0,I)z \sim \mathcal N(0, I), solve the triangular system R⊤v=zR^\top v = z by back substitution, and set x=μ+vx = \mu + v. Then Cov⁡(v)=R−⊤R−1=(RR⊤)−1=Λ−1=Σ\operatorname{Cov}(v) = R^{-\top}R^{-1} = (RR^\top)^{-1} = \Lambda^{-1} = \Sigma.
    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. State the change-of-variables formula for an invertible smooth map Y=g(X)Y = g(X) in dd dimensions.

    2. Why is the density divided by ∣det⁡Jg∣|\det J_g| and not multiplied?

    3. In Box–Muller, what are the distributions of Θ\Theta and R2R^2 for two i.i.d. standard normals?

    4. Write the density of N(μ,Σ)\mathcal N(\mu, \Sigma) in dd dimensions.

    5. How do you sample N(μ,Σ)\mathcal N(\mu, \Sigma), and why does it work?

    6. How do you compute log⁡det⁡Σ\log\det\Sigma from the Cholesky factor LL?

    7. What is the marginal of xax_a when (xa,xb)∼N(μ,Σ)(x_a, x_b) \sim \mathcal N(\mu, \Sigma)?

    8. Give the conditional mean and covariance of xax_a given xbx_b for a joint Gaussian.

    9. For a 2-D Gaussian with ρ=0.8\rho = 0.8 and unit variances, what is Var⁡(x1∣x2)\operatorname{Var}(x_1 \mid x_2)?

    10. When do zero covariance and independence mean the same thing?

    Open the review →

    9. Interview angle #

    • "How do you sample from N(μ,Σ)\mathcal N(\mu, \Sigma)?" — Cholesky Σ=LL⊤\Sigma = LL^\top and x=μ+Lzx = \mu + Lz, since Cov⁡(Lz)=LL⊤\operatorname{Cov}(Lz) = LL^\top. Cost: d3/3d^3/3 once, d2d^2 per sample. If Σ\Sigma is only PSD: the eigendecomposition Q diag⁡(λk)Q\,\operatorname{diag}(\sqrt{\lambda_k}), a pivoted Cholesky, or jitter.
    • "Derive the conditional of a bivariate Gaussian." — Mean μ1+ρσ1σ2(x2−μ2)\mu_1 + \rho\frac{\sigma_1}{\sigma_2}(x_2 - \mu_2), variance σ12(1−ρ2)\sigma_1^2(1 - \rho^2); the proof uses z=x1−Kx2z = x_1 - Kx_2, which is uncorrelated with x2x_2 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⁡\det under- or overflows and an inverse is slow and inaccurate; 2∑ilog⁡Lii2\sum_i \log L_{ii} 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−1K = \Sigma_{xy}\Sigma_{yy}^{-1} from Gaussian conditioning.
    • "How does a normalising flow compute log⁡p(x)\log p(x)?" — Map xx to the base variable zz, then log⁡p(x)=log⁡pZ(z)+log⁡∣det⁡Jg−1(x)∣\log p(x) = \log p_Z(z) + \log|\det J_{g^{-1}}(x)|, with a Jacobian that is triangular so the determinant is cheap.

    10. Practice #

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

    1. A Box–Muller sampler, tested with a Kolmogorov–Smirnov test against N(0,1)\mathcal N(0, 1).
    2. mvn_sample with the Cholesky factor; check the sample mean and covariance.
    3. mvn_logpdf with Cholesky, compared with scipy.stats.multivariate_normal, also for d=500d = 500.
    4. mvn_condition with the Schur complement, compared with a least-squares regression on samples.
    5. Jitter: the smallest jitter that lets Cholesky factor a smooth kernel matrix on a dense grid.

    11. Watch and read #

    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 log⁡det⁡\log\det).
    • Kevin Murphy, Probabilistic Machine Learning: An Introduction, free PDF: §3.2 (multivariate Gaussian, Mahalanobis distance, marginals and conditionals) and §3.3 (linear Gaussian systems).
    • Joseph Blitzstein and Jessica Hwang, Introduction to Probability, 2nd edition: §8.1 (change of variables, with Box–Muller as an example).