Practice / Probability for ML

The multivariate Gaussian: gradients and identities

Ten problems on the multivariate Gaussian: the log-density and its normalising constant, the score −Σ⁻¹(x − μ), the maximum-likelihood mean and covariance through the gradients of log det Σ and tr(Σ⁻¹A), affine maps, the reparameterisation trick, conditionals by the Schur complement and the product of two Gaussian densities, with worked solutions and the mistakes that put Σ where Σ⁻¹ or its square root belongs.

Before you start

The multivariate Gaussian is the distribution behind least squares, the VAE's latent space, Gaussian processes, Kalman filters and the noise in diffusion models, and nearly every manipulation of it comes down to a handful of identities: the gradient of a log-determinant, the gradient of an inverse, the covariance of a linear map and a completed square. These ten problems derive each one, use them to find the maximum-likelihood mean and covariance, and then work through sampling with the reparameterisation trick, conditioning on part of the vector, and multiplying two densities together. The five mistakes at the end are the places where Σ\Sigma turns up in the wrong form: a normalising constant with det⁡Σ\det\Sigma where its square root belongs, a score with Σ\Sigma where its inverse belongs, a matrix inverse differentiated as if it were a number, samples scaled by the covariance instead of its square root, and a conditional variance read off the marginal block.

  • x,μ∈Rdx, \mu \in \mathbb{R}^d are column vectors, and Σ∈Rd×d\Sigma \in \mathbb{R}^{d \times d} is symmetric positive definite: Σ⊤=Σ\Sigma^\top = \Sigma and v⊤Σv>0v^\top\Sigma v > 0 for every v≠0v \neq 0. Then Σ\Sigma is invertible, det⁡Σ>0\det\Sigma > 0, and Σ−1\Sigma^{-1} is symmetric positive definite too. Λ=Σ−1\Lambda = \Sigma^{-1} is called the precision matrix.
  • The density is N(x;μ,Σ)=(2π)−d/2(det⁡Σ)−1/2exp⁡ ⁣(−12(x−μ)⊤Σ−1(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), and x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma) means xx has this density. Its mean is E[x]=μ\mathbb E[x] = \mu and its covariance is Cov⁡[x]=E[(x−μ)(x−μ)⊤]=Σ\operatorname{Cov}[x] = \mathbb E[(x - \mu)(x - \mu)^\top] = \Sigma. log⁡\log is the natural log.
  • q(x)=(x−μ)⊤Σ−1(x−μ)q(x) = (x - \mu)^\top\Sigma^{-1}(x - \mu) is the squared Mahalanobis distance from xx to μ\mu; the density is constant on the ellipsoids q(x)=constq(x) = \text{const}.
  • The conventions are those of the matrix-calculus page: a gradient has the shape of the variable, and for a scalar ff of a matrix XX, ∇Xf\nabla_X f is the matrix of the ∂f/∂Xij\partial f/\partial X_{ij}. If a small change dXdX changes ff by df=tr⁡(M dX)df = \operatorname{tr}(M\,dX) to first order, then ∇Xf=M⊤\nabla_X f = M^\top, because tr⁡(M dX)=∑i,jMji dXij\operatorname{tr}(M\,dX) = \sum_{i,j} M_{ji}\,dX_{ij}.
  • Gradients with respect to Σ\Sigma treat all d2d^2 entries as free variables and are then evaluated at a symmetric Σ\Sigma. The other convention, which counts Σij=Σji\Sigma_{ij} = \Sigma_{ji} as one variable, turns a symmetric gradient GG into 2G−diag⁡(G)2G - \operatorname{diag}(G), where diag⁡(G)\operatorname{diag}(G) keeps only the diagonal of GG. The two vanish together, so they give the same maximum-likelihood estimates.
  • tr⁡\operatorname{tr} is the trace. It is cyclic, tr⁡(ABC)=tr⁡(CAB)\operatorname{tr}(ABC) = \operatorname{tr}(CAB), and for a vector vv, v⊤Mv=tr⁡(Mvv⊤)v^\top Mv = \operatorname{tr}(Mvv^\top).
  • Data: NN independent examples x(1),…,x(N)x^{(1)}, \dots, x^{(N)} from N(μ,Σ)\mathcal N(\mu, \Sigma), the log-likelihood ℓ(μ,Σ)=∑nlog⁡N(x(n);μ,Σ)\ell(\mu, \Sigma) = \sum_n \log\mathcal N(x^{(n)}; \mu, \Sigma), and the sample mean xˉ=1N∑nx(n)\bar x = \tfrac1N\sum_n x^{(n)}. The maximum-likelihood page does the one-dimensional version.
  • Every symmetric positive definite Σ\Sigma has a Cholesky factor: Σ=LL⊤\Sigma = LL^\top with LL lower-triangular and its diagonal positive.
  • For Problem 9, xx is split into blocks xax_a and xbx_b, with μ=(μa;μb)\mu = (\mu_a; \mu_b) and Σ=(ΣaaΣabΣbaΣbb)\Sigma = \begin{pmatrix}\Sigma_{aa} & \Sigma_{ab} \\ \Sigma_{ba} & \Sigma_{bb}\end{pmatrix}, so that Σba=Σab⊤\Sigma_{ba} = \Sigma_{ab}^\top.

Builds on: Matrix calculus conventions, Maximum likelihood estimation: derivations by hand

Problems

  1. ·

    Write log⁡N(x;μ,Σ)\log\mathcal N(x; \mu, \Sigma) term by term. Then evaluate it for d=2d = 2, μ=(1,−1)⊤\mu = (1, -1)^\top, Σ=(2112)\Sigma = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} and x=(2,0)⊤x = (2, 0)^\top. These numbers return in Problems 2, 7 and 9.

  2. ·

    Compute the score ∇xlog⁡N(x;μ,Σ)\nabla_x\log\mathcal N(x; \mu, \Sigma). Evaluate it at Problem 1's numbers, and write out entry ii when Σ=diag⁡(σ12,…,σd2)\Sigma = \operatorname{diag}(\sigma_1^2, \dots, \sigma_d^2).

  3. ·

    Compute ∇μℓ(μ,Σ)\nabla_\mu\ell(\mu, \Sigma) for NN examples and show that the maximum-likelihood mean is xˉ\bar x, whatever Σ\Sigma is.

  4. ··

    Show that ∇Σlog⁡det⁡Σ=Σ−1\nabla_\Sigma\log\det\Sigma = \Sigma^{-1}. Work first with a general invertible XX with det⁡X>0\det X > 0, then specialise to a symmetric Σ\Sigma.

  5. ··

    Let A∈Rd×dA \in \mathbb{R}^{d \times d} be symmetric. Show that ∇Σtr⁡(Σ−1A)=−Σ−1AΣ−1\nabla_\Sigma\operatorname{tr}(\Sigma^{-1}A) = -\Sigma^{-1}A\Sigma^{-1}, and deduce ∇Σ(v⊤Σ−1v)\nabla_\Sigma(v^\top\Sigma^{-1}v) for a fixed vector vv.

  6. ···

    Put μ=xˉ\mu = \bar x (Problem 3) and let S=∑n(x(n)−xˉ)(x(n)−xˉ)⊤S = \sum_n(x^{(n)} - \bar x)(x^{(n)} - \bar x)^\top, the scatter matrix, which is invertible when there are enough examples. Compute ∇Σℓ\nabla_\Sigma\ell and solve ∇Σℓ=0\nabla_\Sigma\ell = 0 for the maximum-likelihood covariance.

  7. ·

    Let x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma), A∈Rm×dA \in \mathbb{R}^{m \times d}, b∈Rmb \in \mathbb{R}^m and y=Ax+by = Ax + b. Take as known that an affine image of a Gaussian vector is Gaussian. Find E[y]\mathbb E[y] and Cov⁡[y]\operatorname{Cov}[y], then give the distribution of x1+x2x_1 + x_2 for Problem 1's μ\mu and Σ\Sigma.

  8. ··

    The reparameterisation trick. Let Σ=LL⊤\Sigma = LL^\top be the Cholesky factorisation, ε∼N(0,I)\varepsilon \sim \mathcal N(0, I) and x=μ+Lεx = \mu + L\varepsilon. Show that x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma). Then, for a symmetric BB and f(x)=x⊤Bxf(x) = x^\top Bx, compute F(μ,L)=E[f(x)]F(\mu, L) = \mathbb E[f(x)] in closed form, find ∇μF\nabla_\mu F and ∇LF\nabla_L F, and show that they equal E[∇f(x)]\mathbb E[\nabla f(x)] and E[∇f(x) ε⊤]\mathbb E[\nabla f(x)\,\varepsilon^\top].

  9. ···

    Split x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma) into blocks xax_a and xbx_b, and let K=ΣabΣbb−1K = \Sigma_{ab}\Sigma_{bb}^{-1}. Using z=xa−Kxbz = x_a - Kx_b, find the distribution of xax_a given xbx_b. Then find the distribution of x1x_1 given x2=0x_2 = 0 for Problem 1's μ\mu and Σ\Sigma.

  10. ··

    Show that, as functions of xx, N(x;a,A) N(x;b,B)\mathcal N(x; a, A)\,\mathcal N(x; b, B) is proportional to N(x;c,C)\mathcal N(x; c, C) with C=(A−1+B−1)−1C = (A^{-1} + B^{-1})^{-1} and c=C(A−1a+B−1b)c = C(A^{-1}a + B^{-1}b). In one dimension, find cc and CC for a=0a = 0, A=4A = 4, b=3b = 3 and B=2B = 2.

Worked solutions

Problem 1

Write log⁡N(x;μ,Σ)\log\mathcal N(x; \mu, \Sigma) term by term. Then evaluate it for d=2d = 2, μ=(1,−1)⊤\mu = (1, -1)^\top, Σ=(2112)\Sigma = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} and x=(2,0)⊤x = (2, 0)^\top. These numbers return in Problems 2, 7 and 9.

  1. log⁡N(x;μ,Σ)=−d2log⁡(2π)−12log⁡det⁡Σ−12(x−μ)⊤Σ−1(x−μ)\log\mathcal N(x; \mu, \Sigma) = -\tfrac d2\log(2\pi) - \tfrac12\log\det\Sigma - \tfrac12(x - \mu)^\top\Sigma^{-1}(x - \mu)The log of a product is the sum of the logs, log⁡c−1/2=−12log⁡c\log c^{-1/2} = -\tfrac12\log c, and log⁡\log undoes exp⁡\exp. The first two terms do not depend on xx: they are what make the density integrate to 11.
  2. −d2log⁡(2π)=−log⁡(2π)-\tfrac d2\log(2\pi) = -\log(2\pi)d=2d = 2.
  3. det⁡Σ=2⋅2−1⋅1=3\det\Sigma = 2 \cdot 2 - 1 \cdot 1 = 3The determinant of a 2×22 \times 2 matrix is ad−bcad - bc.
  4. Σ−1=13(2−1−12)\Sigma^{-1} = \tfrac13\begin{pmatrix} 2 & -1 \\ -1 & 2 \end{pmatrix}For a 2×22 \times 2 matrix, swap the diagonal entries, negate the off-diagonal ones and divide by the determinant; multiplying by Σ\Sigma gives II.
  5. x−μ=(1,1)⊤x - \mu = (1, 1)^\top, Σ−1(x−μ)=13(1,1)⊤\Sigma^{-1}(x - \mu) = \tfrac13(1, 1)^\top and q(x)=13(1+1)=23q(x) = \tfrac13(1 + 1) = \tfrac23Each row of the matrix in step 4 sums to 2−1=12 - 1 = 1, and qq is the dot product of x−μx - \mu with that vector.
  6. log⁡N(x;μ,Σ)=−log⁡(2π)−12log⁡3−13≈−2.721\log\mathcal N(x; \mu, \Sigma) = -\log(2\pi) - \tfrac12\log 3 - \tfrac13 \approx -2.721Steps 2 to 5 in step 1: −1.838−0.549−0.333-1.838 - 0.549 - 0.333. Only the last term depends on xx, and it depends on xx only through the Mahalanobis distance.

Problem 2

Compute the score ∇xlog⁡N(x;μ,Σ)\nabla_x\log\mathcal N(x; \mu, \Sigma). Evaluate it at Problem 1's numbers, and write out entry ii when Σ=diag⁡(σ12,…,σd2)\Sigma = \operatorname{diag}(\sigma_1^2, \dots, \sigma_d^2).

  1. Only −12q(x)-\tfrac12 q(x) depends on xx.The first two terms of Problem 1, step 1 are constants, and a constant has zero gradient.
  2. With v=x−μv = x - \mu, ∇v(v⊤Σ−1v)=2Σ−1v\nabla_v(v^\top\Sigma^{-1}v) = 2\Sigma^{-1}v.∇v(v⊤Av)=(A+A⊤)v\nabla_v(v^\top Av) = (A + A^\top)v (the matrix-calculus page, Problem 4), and Σ−1\Sigma^{-1} is symmetric because (Σ−1)⊤=(Σ⊤)−1=Σ−1(\Sigma^{-1})^\top = (\Sigma^\top)^{-1} = \Sigma^{-1}.
  3. ∇xq=2Σ−1(x−μ)\nabla_x q = 2\Sigma^{-1}(x - \mu)∂v/∂x=I\partial v/\partial x = I: subtracting a constant does not change a derivative.
  4. With Σ=diag⁡(σi2)\Sigma = \operatorname{diag}(\sigma_i^2), Σ−1=diag⁡(1/σi2)\Sigma^{-1} = \operatorname{diag}(1/\sigma_i^2) and entry ii of the score is −(xi−μi)/σi2-(x_i - \mu_i)/\sigma_i^2.The inverse of a diagonal matrix inverts each diagonal entry. Each coordinate gets the one-dimensional score, because with a diagonal Σ\Sigma the density is a product of dd one-dimensional densities and its log a sum.
  5. ∇xlog⁡N(x;μ,Σ)=−Σ−1(x−μ)\nabla_x\log\mathcal N(x; \mu, \Sigma) = -\Sigma^{-1}(x - \mu), which is (−13,−13)⊤(-\tfrac13, -\tfrac13)^\top at Problem 1's numbersStep 3 times −12-\tfrac12; Problem 1, step 5 gives Σ−1(x−μ)\Sigma^{-1}(x - \mu). The score points back towards μ\mu, weighting the directions of small variance most. Here x−μ=(1,1)⊤x - \mu = (1, 1)^\top is an eigenvector of Σ\Sigma with eigenvalue 33, so the score points straight back at μ\mu, shrunk by a factor 33. Score-based diffusion models train a network to approximate this vector field.

Problem 3

Compute ∇μℓ(μ,Σ)\nabla_\mu\ell(\mu, \Sigma) for NN examples and show that the maximum-likelihood mean is xˉ\bar x, whatever Σ\Sigma is.

  1. ∇μlog⁡N(x(n);μ,Σ)=Σ−1(x(n)−μ)\nabla_\mu\log\mathcal N(x^{(n)}; \mu, \Sigma) = \Sigma^{-1}(x^{(n)} - \mu)Problem 2's computation with v=x(n)−μv = x^{(n)} - \mu, whose Jacobian with respect to μ\mu is −I-I; that flips the sign.
  2. ∇μℓ=Σ−1∑n(x(n)−μ)=NΣ−1(xˉ−μ)\nabla_\mu\ell = \Sigma^{-1}\sum_n(x^{(n)} - \mu) = N\Sigma^{-1}(\bar x - \mu)The gradient of a sum is the sum of the gradients, Σ−1\Sigma^{-1} factors out of the sum, and ∑nx(n)=Nxˉ\sum_n x^{(n)} = N\bar x.
  3. ∇μℓ=0  ⟺  μ=xˉ\nabla_\mu\ell = 0 \iff \mu = \bar xΣ−1\Sigma^{-1} is invertible, so it sends only the zero vector to zero.
  4. ∇μ2ℓ=−NΣ−1\nabla_\mu^2\ell = -N\Sigma^{-1}, which is negative definite.Step 2 is affine in μ\mu, so its Jacobian is the matrix that multiplies μ\mu. Σ−1\Sigma^{-1} is positive definite, so ℓ\ell is strictly concave in μ\mu and its stationary point is the global maximum.
  5. ∇μℓ=NΣ−1(xˉ−μ)\nabla_\mu\ell = N\Sigma^{-1}(\bar x - \mu), so μ^=xˉ\hat\mu = \bar x for every Σ\SigmaΣ\Sigma dropped out in step 3, so the mean can be estimated before the covariance is known. That is what lets Problem 6 put xˉ\bar x in place of μ\mu.

Problem 4

Show that ∇Σlog⁡det⁡Σ=Σ−1\nabla_\Sigma\log\det\Sigma = \Sigma^{-1}. Work first with a general invertible XX with det⁡X>0\det X > 0, then specialise to a symmetric Σ\Sigma.

  1. det⁡X=∑jXijCij\det X = \sum_j X_{ij}C_{ij} for any fixed row ii, where CijC_{ij} is (−1)i+j(-1)^{i+j} times the determinant of XX with row ii and column jj deleted.The cofactor (Laplace) expansion along row ii.
  2. ∂det⁡X/∂Xij=Cij\partial\det X/\partial X_{ij} = C_{ij}Every cofactor in the row-ii expansion deletes row ii, so XijX_{ij} appears only as the explicit factor of the jj-th term.
  3. X−1=C⊤/det⁡XX^{-1} = C^\top/\det XThe inverse is the adjugate divided by the determinant, and the adjugate is the transpose of the matrix of cofactors.
  4. ∂log⁡det⁡X/∂Xij=Cij/det⁡X=(X−1)ji\partial\log\det X/\partial X_{ij} = C_{ij}/\det X = (X^{-1})_{ji}Chain rule through the log, whose derivative is 1/det⁡X1/\det X; then step 3 read at entry (j,i)(j, i).
  5. ∇Xlog⁡det⁡X=X−⊤\nabla_X\log\det X = X^{-\top}, so ∇Σlog⁡det⁡Σ=Σ−1\nabla_\Sigma\log\det\Sigma = \Sigma^{-1}Step 4 puts entry (j,i)(j, i) of X−1X^{-1} at position (i,j)(i, j), which is the transpose. For a symmetric Σ\Sigma, Σ−⊤=Σ−1\Sigma^{-\top} = \Sigma^{-1}. In differential form, dlog⁡det⁡Σ=tr⁡(Σ−1 dΣ)d\log\det\Sigma = \operatorname{tr}(\Sigma^{-1}\,d\Sigma), which Problem 6 uses.

Problem 5

Let A∈Rd×dA \in \mathbb{R}^{d \times d} be symmetric. Show that ∇Σtr⁡(Σ−1A)=−Σ−1AΣ−1\nabla_\Sigma\operatorname{tr}(\Sigma^{-1}A) = -\Sigma^{-1}A\Sigma^{-1}, and deduce ∇Σ(v⊤Σ−1v)\nabla_\Sigma(v^\top\Sigma^{-1}v) for a fixed vector vv.

  1. d(Σ−1)=−Σ−1 dΣ Σ−1d(\Sigma^{-1}) = -\Sigma^{-1}\,d\Sigma\,\Sigma^{-1}Differentiate ΣΣ−1=I\Sigma\Sigma^{-1} = I with the product rule, dΣ Σ−1+Σ d(Σ−1)=0d\Sigma\,\Sigma^{-1} + \Sigma\,d(\Sigma^{-1}) = 0, and multiply on the left by Σ−1\Sigma^{-1}. The factors keep their order because matrices do not commute.
  2. dtr⁡(Σ−1A)=−tr⁡(Σ−1 dΣ Σ−1A)=tr⁡(−Σ−1AΣ−1 dΣ)d\operatorname{tr}(\Sigma^{-1}A) = -\operatorname{tr}(\Sigma^{-1}\,d\Sigma\,\Sigma^{-1}A) = \operatorname{tr}\big(-\Sigma^{-1}A\Sigma^{-1}\,d\Sigma\big)The trace is linear, so the differential passes inside; cycling moves Σ−1A\Sigma^{-1}A from the end to the front.
  3. ∇Σtr⁡(Σ−1A)=(−Σ−1AΣ−1)⊤=−Σ−1AΣ−1\nabla_\Sigma\operatorname{tr}(\Sigma^{-1}A) = \big(-\Sigma^{-1}A\Sigma^{-1}\big)^\top = -\Sigma^{-1}A\Sigma^{-1}df=tr⁡(M dΣ)df = \operatorname{tr}(M\,d\Sigma) means the gradient is M⊤M^\top (Before you start). Transposing reverses the order of a product, and Σ−1\Sigma^{-1} and AA are symmetric.
  4. v⊤Σ−1v=tr⁡(Σ−1vv⊤)v^\top\Sigma^{-1}v = \operatorname{tr}(\Sigma^{-1}vv^\top)A scalar is its own trace, and the trace is cyclic; vv⊤vv^\top is symmetric.
  5. ∇Σtr⁡(Σ−1A)=−Σ−1AΣ−1\nabla_\Sigma\operatorname{tr}(\Sigma^{-1}A) = -\Sigma^{-1}A\Sigma^{-1} and ∇Σ(v⊤Σ−1v)=−Σ−1vv⊤Σ−1\nabla_\Sigma(v^\top\Sigma^{-1}v) = -\Sigma^{-1}vv^\top\Sigma^{-1}Step 3, then step 4 with A=vv⊤A = vv^\top. With d=1d = 1 it is the scalar rule dds(a/s)=−a/s2\tfrac{d}{ds}(a/s) = -a/s^2. The minus sign says that a larger covariance makes every point closer in Mahalanobis distance.

Problem 6

Put μ=xˉ\mu = \bar x (Problem 3) and let S=∑n(x(n)−xˉ)(x(n)−xˉ)⊤S = \sum_n(x^{(n)} - \bar x)(x^{(n)} - \bar x)^\top, the scatter matrix, which is invertible when there are enough examples. Compute ∇Σℓ\nabla_\Sigma\ell and solve ∇Σℓ=0\nabla_\Sigma\ell = 0 for the maximum-likelihood covariance.

  1. ℓ(Σ)=−Nd2log⁡(2π)−N2log⁡det⁡Σ−12tr⁡(Σ−1S)\ell(\Sigma) = -\tfrac{Nd}{2}\log(2\pi) - \tfrac N2\log\det\Sigma - \tfrac12\operatorname{tr}(\Sigma^{-1}S)Sum Problem 1, step 1 over the NN examples. Each quadratic term is tr⁡(Σ−1vv⊤)\operatorname{tr}(\Sigma^{-1}vv^\top) (Problem 5, step 4), and a sum of traces is the trace of the sum.
  2. ∇Σℓ=−N2Σ−1+12Σ−1SΣ−1\nabla_\Sigma\ell = -\tfrac N2\Sigma^{-1} + \tfrac12\Sigma^{-1}S\Sigma^{-1}Problem 4 for the log-determinant and Problem 5 with A=SA = S, which is symmetric as a sum of outer products vv⊤vv^\top.
  3. ∇Σℓ=0  ⟺  −NΣ+S=0\nabla_\Sigma\ell = 0 \iff -N\Sigma + S = 0Multiply on the left and on the right by Σ\Sigma, then by 22. Σ\Sigma is invertible, so this can be undone and the two equations have the same solutions.
  4. With Λ=Σ−1\Lambda = \Sigma^{-1}, ℓ=N2log⁡det⁡Λ−12tr⁡(ΛS)+const\ell = \tfrac N2\log\det\Lambda - \tfrac12\operatorname{tr}(\Lambda S) + \text{const}, which is concave in Λ\Lambda.log⁡det⁡Λ=−log⁡det⁡Σ\log\det\Lambda = -\log\det\Sigma. log⁡det⁡\log\det is concave on positive definite matrices (a standard fact) and tr⁡(ΛS)\operatorname{tr}(\Lambda S) is linear, so the one stationary point is the global maximum. S/NS/N is positive definite because SS is a positive semidefinite sum of outer products and invertible.
  5. ∇Σℓ=−N2Σ−1+12Σ−1SΣ−1\nabla_\Sigma\ell = -\tfrac N2\Sigma^{-1} + \tfrac12\Sigma^{-1}S\Sigma^{-1}, so Σ^=1N∑n(x(n)−xˉ)(x(n)−xˉ)⊤\hat\Sigma = \tfrac1N\sum_n(x^{(n)} - \bar x)(x^{(n)} - \bar x)^\topIt divides by NN, not N−1N - 1: it is NumPy's np.cov(X.T, bias=True), while the default np.cov divides by N−1N - 1. The centred examples span at most N−1N - 1 dimensions, so SS is invertible only if N>dN > d; otherwise ℓ\ell has no maximum, because Σ\Sigma can collapse onto the data's subspace and log⁡det⁡Σ→−∞\log\det\Sigma \to -\infty.

Problem 7

Let x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma), A∈Rm×dA \in \mathbb{R}^{m \times d}, b∈Rmb \in \mathbb{R}^m and y=Ax+by = Ax + b. Take as known that an affine image of a Gaussian vector is Gaussian. Find E[y]\mathbb E[y] and Cov⁡[y]\operatorname{Cov}[y], then give the distribution of x1+x2x_1 + x_2 for Problem 1's μ\mu and Σ\Sigma.

  1. E[y]=A E[x]+b=Aμ+b\mathbb E[y] = A\,\mathbb E[x] + b = A\mu + bExpectation is linear, and AA and bb are constants.
  2. y−E[y]=A(x−μ)y - \mathbb E[y] = A(x - \mu)bb cancels.
  3. Cov⁡[y]=E[A(x−μ)(x−μ)⊤A⊤]=AΣA⊤\operatorname{Cov}[y] = \mathbb E\big[A(x - \mu)(x - \mu)^\top A^\top\big] = A\Sigma A^\top(Av)⊤=v⊤A⊤(Av)^\top = v^\top A^\top, and the constants AA and A⊤A^\top come out of the expectation on either side.
  4. For x1+x2x_1 + x_2: A=(1,1)A = (1, 1) and b=0b = 0, so Aμ=1−1=0A\mu = 1 - 1 = 0 and AΣA⊤=2+1+1+2=6A\Sigma A^\top = 2 + 1 + 1 + 2 = 6.(1,1) Σ (1,1)⊤(1, 1)\,\Sigma\,(1, 1)^\top adds up every entry of Σ\Sigma.
  5. y∼N(Aμ+b,AΣA⊤)y \sim \mathcal N(A\mu + b, A\Sigma A^\top); for Problem 1, x1+x2∼N(0,6)x_1 + x_2 \sim \mathcal N(0, 6)A Gaussian is fixed by its mean and covariance, so steps 1 and 3 give the whole distribution. The variance 66 exceeds 2+22 + 2 by twice the covariance Σ12=1\Sigma_{12} = 1. With A=(I  0)A = (I\ \ 0) the same result says that a block of a Gaussian vector is Gaussian, with the matching blocks of μ\mu and Σ\Sigma; Problem 9 uses that.

Problem 8

The reparameterisation trick. Let Σ=LL⊤\Sigma = LL^\top be the Cholesky factorisation, ε∼N(0,I)\varepsilon \sim \mathcal N(0, I) and x=μ+Lεx = \mu + L\varepsilon. Show that x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma). Then, for a symmetric BB and f(x)=x⊤Bxf(x) = x^\top Bx, compute F(μ,L)=E[f(x)]F(\mu, L) = \mathbb E[f(x)] in closed form, find ∇μF\nabla_\mu F and ∇LF\nabla_L F, and show that they equal E[∇f(x)]\mathbb E[\nabla f(x)] and E[∇f(x) ε⊤]\mathbb E[\nabla f(x)\,\varepsilon^\top].

  1. x∼N(μ,LIL⊤)=N(μ,Σ)x \sim \mathcal N(\mu, LIL^\top) = \mathcal N(\mu, \Sigma)Problem 7 with A=LA = L, b=μb = \mu, and ε\varepsilon of mean 00 and covariance II.
  2. f(x)=μ⊤Bμ+2μ⊤BLε+ε⊤L⊤BLεf(x) = \mu^\top B\mu + 2\mu^\top BL\varepsilon + \varepsilon^\top L^\top BL\varepsilonExpand (μ+Lε)⊤B(μ+Lε)(\mu + L\varepsilon)^\top B(\mu + L\varepsilon). The two cross terms are equal scalars because BB is symmetric: (Lε)⊤Bμ=μ⊤BLε(L\varepsilon)^\top B\mu = \mu^\top BL\varepsilon.
  3. F=μ⊤Bμ+tr⁡(L⊤BL)F = \mu^\top B\mu + \operatorname{tr}(L^\top BL)E[ε]=0\mathbb E[\varepsilon] = 0 removes the middle term, and E[ε⊤Mε]=E[tr⁡(Mεε⊤)]=tr⁡(M E[εε⊤])=tr⁡(M)\mathbb E[\varepsilon^\top M\varepsilon] = \mathbb E[\operatorname{tr}(M\varepsilon\varepsilon^\top)] = \operatorname{tr}(M\,\mathbb E[\varepsilon\varepsilon^\top]) = \operatorname{tr}(M).
  4. ∇μF=2Bμ\nabla_\mu F = 2B\mu and ∇LF=2BL\nabla_L F = 2BL∇x(x⊤Bx)=(B+B⊤)x\nabla_x(x^\top Bx) = (B + B^\top)x and ∇Xtr⁡(X⊤BX)=(B+B⊤)X\nabla_X\operatorname{tr}(X^\top BX) = (B + B^\top)X (the matrix-calculus page, Problems 4 and 8), with B=B⊤B = B^\top.
  5. ∇f(x)=2Bx\nabla f(x) = 2Bx, so E[∇f(x)]=2Bμ\mathbb E[\nabla f(x)] = 2B\mu and E[∇f(x) ε⊤]=2B(μ E[ε]⊤+L E[εε⊤])=2BL\mathbb E[\nabla f(x)\,\varepsilon^\top] = 2B\big(\mu\,\mathbb E[\varepsilon]^\top + L\,\mathbb E[\varepsilon\varepsilon^\top]\big) = 2BLSubstitute x=μ+Lεx = \mu + L\varepsilon and use linearity, E[ε]=0\mathbb E[\varepsilon] = 0 and E[εε⊤]=I\mathbb E[\varepsilon\varepsilon^\top] = I.
  6. ∇μF=2Bμ=E[∇f(x)]\nabla_\mu F = 2B\mu = \mathbb E[\nabla f(x)] and ∇LF=2BL=E[∇f(x) ε⊤]\nabla_L F = 2BL = \mathbb E[\nabla f(x)\,\varepsilon^\top]This holds for any smooth ff: F=Eε[f(μ+Lε)]F = \mathbb E_\varepsilon[f(\mu + L\varepsilon)], the distribution of ε\varepsilon does not involve the parameters, so the gradient passes inside the expectation, and ∂f(μ+Lε)/∂Lij=(∇f)i εj\partial f(\mu + L\varepsilon)/\partial L_{ij} = (\nabla f)_i\,\varepsilon_j. That is why a VAE samples ε\varepsilon rather than xx: the average of ∇f(x) ε⊤\nabla f(x)\,\varepsilon^\top over samples is an unbiased estimate of the gradient. Only the lower triangle of LL is free, so its gradient is the lower triangle of 2BL2BL.

Problem 9

Split x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma) into blocks xax_a and xbx_b, and let K=ΣabΣbb−1K = \Sigma_{ab}\Sigma_{bb}^{-1}. Using z=xa−Kxbz = x_a - Kx_b, find the distribution of xax_a given xbx_b. Then find the distribution of x1x_1 given x2=0x_2 = 0 for Problem 1's μ\mu and Σ\Sigma.

  1. (z,xb)(z, x_b) is jointly Gaussian.It is the affine image of xx under (I−K0I)\begin{pmatrix} I & -K \\ 0 & I \end{pmatrix}, so Problem 7 applies.
  2. Cov⁡[z,xb]=Σab−KΣbb=Σab−ΣabΣbb−1Σbb=0\operatorname{Cov}[z, x_b] = \Sigma_{ab} - K\Sigma_{bb} = \Sigma_{ab} - \Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{bb} = 0The cross-covariance E[(z−Ez)(xb−μb)⊤]\mathbb E[(z - \mathbb E z)(x_b - \mu_b)^\top] is linear in zz, and Cov⁡[xa,xb]=Σab\operatorname{Cov}[x_a, x_b] = \Sigma_{ab}, Cov⁡[xb,xb]=Σbb\operatorname{Cov}[x_b, x_b] = \Sigma_{bb}.
  3. zz is independent of xbx_b.For jointly Gaussian vectors, zero cross-covariance makes the joint covariance block-diagonal, so the quadratic in the exponent and the determinant both split, and the density factorises.
  4. E[z]=μa−Kμb\mathbb E[z] = \mu_a - K\mu_b and Cov⁡[z]=Σaa−KΣba−ΣabK⊤+KΣbbK⊤=Σaa−ΣabΣbb−1Σba\operatorname{Cov}[z] = \Sigma_{aa} - K\Sigma_{ba} - \Sigma_{ab}K^\top + K\Sigma_{bb}K^\top = \Sigma_{aa} - \Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{ba}Problem 7 with A=(I  −K)A = (I\ \ {-K}). Each of the last three terms equals ΣabΣbb−1Σba\Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{ba}, because Σbb−1\Sigma_{bb}^{-1} is symmetric and Σab⊤=Σba\Sigma_{ab}^\top = \Sigma_{ba}; two carry a minus sign and one a plus.
  5. Given xbx_b, xa=z+Kxbx_a = z + Kx_b with zz still distributed as in step 4.By step 3, conditioning on xbx_b does not change the distribution of zz, and KxbKx_b becomes a constant shift: the mean moves by KxbKx_b and the covariance stays.
  6. For Problem 1, K=Σ12/Σ22=12K = \Sigma_{12}/\Sigma_{22} = \tfrac12, the mean is 1+12(0−(−1))=321 + \tfrac12\big(0 - (-1)\big) = \tfrac32 and the variance is 2−12⋅1=322 - \tfrac12 \cdot 1 = \tfrac32.Every block is 1×11 \times 1: μa=1\mu_a = 1, μb=−1\mu_b = -1, Σaa=Σbb=2\Sigma_{aa} = \Sigma_{bb} = 2 and Σab=1\Sigma_{ab} = 1.
  7. xa∣xb∼N(μa+ΣabΣbb−1(xb−μb), Σaa−ΣabΣbb−1Σba)x_a \mid x_b \sim \mathcal N\big(\mu_a + \Sigma_{ab}\Sigma_{bb}^{-1}(x_b - \mu_b),\ \Sigma_{aa} - \Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{ba}\big); for Problem 1, x1∣x2=0∼N(32,32)x_1 \mid x_2 = 0 \sim \mathcal N(\tfrac32, \tfrac32)The covariance is the Schur complement of Σbb\Sigma_{bb} in Σ\Sigma, which by the block-inverse formula is (Λaa)−1(\Lambda_{aa})^{-1}, the inverse of the precision's top-left block. It does not depend on the observed xbx_b and is never larger than Σaa\Sigma_{aa}: observing a correlated variable can only narrow the distribution. Gaussian-process prediction is this formula.

Problem 10

Show that, as functions of xx, N(x;a,A) N(x;b,B)\mathcal N(x; a, A)\,\mathcal N(x; b, B) is proportional to N(x;c,C)\mathcal N(x; c, C) with C=(A−1+B−1)−1C = (A^{-1} + B^{-1})^{-1} and c=C(A−1a+B−1b)c = C(A^{-1}a + B^{-1}b). In one dimension, find cc and CC for a=0a = 0, A=4A = 4, b=3b = 3 and B=2B = 2.

  1. log⁡[N(x;a,A) N(x;b,B)]=−12(x−a)⊤A−1(x−a)−12(x−b)⊤B−1(x−b)+const\log\big[\mathcal N(x; a, A)\,\mathcal N(x; b, B)\big] = -\tfrac12(x - a)^\top A^{-1}(x - a) - \tfrac12(x - b)^\top B^{-1}(x - b) + \text{const}The log of a product is the sum of the logs, and the normalising constants do not depend on xx.
  2. =−12x⊤(A−1+B−1)x+x⊤(A−1a+B−1b)+const= -\tfrac12 x^\top(A^{-1} + B^{-1})x + x^\top(A^{-1}a + B^{-1}b) + \text{const}(x−a)⊤A−1(x−a)=x⊤A−1x−2x⊤A−1a+a⊤A−1a(x - a)^\top A^{-1}(x - a) = x^\top A^{-1}x - 2x^\top A^{-1}a + a^\top A^{-1}a, the two cross terms being equal because A−1A^{-1} is symmetric; the same for bb.
  3. −12(x−c)⊤C−1(x−c)=−12x⊤C−1x+x⊤C−1c+const-\tfrac12(x - c)^\top C^{-1}(x - c) = -\tfrac12 x^\top C^{-1}x + x^\top C^{-1}c + \text{const}The same expansion.
  4. C−1=A−1+B−1C^{-1} = A^{-1} + B^{-1} and C−1c=A−1a+B−1bC^{-1}c = A^{-1}a + B^{-1}bTwo quadratics in xx with the same quadratic and linear parts differ by a constant, so the densities differ by a constant factor. A−1+B−1A^{-1} + B^{-1} is positive definite as a sum of positive definite matrices, so CC exists.
  5. One dimension: C=(14+12)−1=43C = (\tfrac14 + \tfrac12)^{-1} = \tfrac43 and c=43(04+32)=2c = \tfrac43\big(\tfrac04 + \tfrac32\big) = 2Step 4 with 1×11 \times 1 matrices.
  6. N(x;a,A) N(x;b,B)∝N(x;c,C)\mathcal N(x; a, A)\,\mathcal N(x; b, B) \propto \mathcal N(x; c, C) with C=(A−1+B−1)−1C = (A^{-1} + B^{-1})^{-1}, c=C(A−1a+B−1b)c = C(A^{-1}a + B^{-1}b); for the 1-D numbers, c=2c = 2 and C=43C = \tfrac43Precisions add, and the new mean is a precision-weighted average: 22 lies nearer 33 than 00 because B=2B = 2 is the more certain of the two. The constant of proportionality is N(a;b,A+B)\mathcal N(a; b, A + B). This is Bayes' rule for a Gaussian prior on a mean and one Gaussian measurement of it, the update step of a Kalman filter.

Where this goes wrong

1. Normalising constant with det Σ where its square root belongs

In one dimension the constant is 1/2πσ21/\sqrt{2\pi\sigma^2}, and σ2\sigma^2 is the variance, so the matrix version seems to need only the covariance in its place.

  1. p(x)=12πσ2exp⁡ ⁣(−(x−μ)2/(2σ2))p(x) = \dfrac{1}{\sqrt{2\pi\sigma^2}}\exp\!\big(-(x - \mu)^2/(2\sigma^2)\big) for d=1d = 1Right so far: the scalar density.
  2. “Replace 2π2\pi by (2π)d(2\pi)^d under the root and σ2\sigma^2 by det⁡Σ\det\Sigma, the variance of the whole vector.”The shortcut that causes the mistake: the square root is carried onto 2π2\pi and left off the determinant.
  3. log⁡N(x;μ,Σ)=−d2log⁡(2π)−log⁡det⁡Σ−12(x−μ)⊤Σ−1(x−μ)\log\mathcal N(x; \mu, \Sigma) = -\tfrac d2\log(2\pi) - \log\det\Sigma - \tfrac12(x - \mu)^\top\Sigma^{-1}(x - \mu)The coefficient is −12-\tfrac12 (Problem 1); this density integrates to 1/det⁡Σ1/\sqrt{\det\Sigma}, and at Problem 1's point it is low by 12log⁡3≈0.549\tfrac12\log 3 \approx 0.549. The mean estimate is unaffected, which hides the slip, but setting the Σ\Sigma-gradient to zero now gives S/(2N)S/(2N), half the maximum-likelihood covariance of Problem 6.

2. Score with Σ in place of Σ⁻¹

In one dimension the score is −(x−μ)/σ2-(x - \mu)/\sigma^2, a division, and a division by a matrix is easy to write as a multiplication.

  1. log⁡N(x;μ,Σ)=const−12(x−μ)⊤Σ−1(x−μ)\log\mathcal N(x; \mu, \Sigma) = \text{const} - \tfrac12(x - \mu)^\top\Sigma^{-1}(x - \mu)Right so far: Problem 1, step 1.
  2. “The score pulls xx back towards μ\mu, scaled by the spread of the distribution.”The shortcut that causes the mistake: reading the covariance as the scale of the pull, when it is the inverse that sits in the exponent.
  3. ∇xlog⁡N(x;μ,Σ)=−Σ(x−μ)\nabla_x\log\mathcal N(x; \mu, \Sigma) = -\Sigma(x - \mu)The score is −Σ−1(x−μ)-\Sigma^{-1}(x - \mu) (Problem 2). At Problem 1's numbers this gives (−3,−3)⊤(-3, -3)^\top instead of (−13,−13)⊤(-\tfrac13, -\tfrac13)^\top. The directions of large variance should get the weakest pull and here get the strongest; the units are wrong too, since with xx in metres the score is per metre and Σ(x−μ)\Sigma(x - \mu) is in cubic metres.

3. Derivative of the inverse taken as −Σ⁻² dΣ

For a number, d(1/s)=−ds/s2d(1/s) = -ds/s^2, and Σ−2\Sigma^{-2} looks like the matrix version.

  1. dtr⁡(Σ−1A)=tr⁡(d(Σ−1) A)d\operatorname{tr}(\Sigma^{-1}A) = \operatorname{tr}\big(d(\Sigma^{-1})\,A\big)Right so far: the trace is linear.
  2. “d(Σ−1)=−Σ−2 dΣd(\Sigma^{-1}) = -\Sigma^{-2}\,d\Sigma, as for numbers.”The analogy that causes the mistake: the scalar rule assumes dΣd\Sigma commutes with Σ\Sigma, and a general perturbation does not.
  3. ∇Σtr⁡(Σ−1A)=−Σ−2A\nabla_\Sigma\operatorname{tr}(\Sigma^{-1}A) = -\Sigma^{-2}AThe gradient is −Σ−1AΣ−1-\Sigma^{-1}A\Sigma^{-1} (Problem 5), from d(Σ−1)=−Σ−1 dΣ Σ−1d(\Sigma^{-1}) = -\Sigma^{-1}\,d\Sigma\,\Sigma^{-1}. The wrong form is generally not even symmetric, while the true gradient is. The two agree when AA commutes with Σ\Sigma, for example A=IA = I, so a test with an identity matrix will not catch it.

4. Sampling with x = μ + Σε

A one-dimensional sample is μ+σε\mu + \sigma\varepsilon, and Σ\Sigma is the matrix that comes to hand.

  1. With ε∼N(0,I)\varepsilon \sim \mathcal N(0, I), x=μ+Mεx = \mu + M\varepsilon has covariance MM⊤MM^\top.Right so far: Problem 7 with A=MA = M.
  2. “The matrix that scales the noise is the covariance.”The analogy that causes the mistake: in one dimension the multiplier is the standard deviation σ\sigma, and Σ\Sigma plays the part of σ2\sigma^2, not σ\sigma.
  3. x=μ+Σεx = \mu + \Sigma\varepsilonIts covariance is ΣΣ⊤=Σ2\Sigma\Sigma^\top = \Sigma^2: for Problem 1, (5445)\begin{pmatrix} 5 & 4 \\ 4 & 5 \end{pmatrix} instead of (2112)\begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}. The multiplier must satisfy MM⊤=ΣMM^\top = \Sigma, which the Cholesky factor LL does (Problem 8).

5. Conditional covariance read off as the block Σ_aa

The conditional mean needs a formula, but the variance of xax_a seems to be sitting in Σ\Sigma already, in its top-left block.

  1. For Problem 1, Var⁡[x1]=Σ11=2\operatorname{Var}[x_1] = \Sigma_{11} = 2Right so far: that is the marginal variance (Problem 7 with A=(1,0)A = (1, 0)).
  2. “Knowing x2x_2 moves the centre of x1x_1 but not its spread, so the variance stays Σ11\Sigma_{11}.”The shortcut that causes the mistake: conditioning on a correlated variable removes the part of x1x_1 that x2x_2 predicts, and that part carries variance.
  3. x1∣x2∼N(1+12(x2+1), 2)x_1 \mid x_2 \sim \mathcal N\big(1 + \tfrac12(x_2 + 1),\ 2\big)The mean is right and the variance is Σ11−Σ122/Σ22=32\Sigma_{11} - \Sigma_{12}^2/\Sigma_{22} = \tfrac32 (Problem 9). Using the marginal block gives intervals that are too wide; it is correct only when Σab=0\Sigma_{ab} = 0, when there was nothing to learn from xbx_b.

Print this set: multivariate-gaussian.pdf (problems, answers, and worked solutions on separate pages).