Blog · updated 26 Oct 2026 · 27 min read

The Gaussian toolkit: identities, linear-algebra moves and tricks

The identities and moves that experts use without thinking and beginners struggle with: whitening, low-rank updates, Schur complements and spectra; moments, conditioning in both forms, KL, Stein, Bonnet and Price; total variance, the ELBO and Fisher information; and how all of it runs reinforcement learning. Each with a step-by-step proof.

Contents
  1. Notation
  2. 1. Where the constant comes from
  3. 2. Five linear-algebra moves
  4. Move 1: a scalar is its own trace
  5. Move 2: never invert; factor and whiten
  6. Move 3: update, don’t recompute
  7. Move 4: blocks, Schur complements, and the chain rule
  8. Move 5: think in eigenvalues
  9. 3. Moments
  10. 4. The closure toolkit
  11. 5. Conditioning and marginalising, in both coordinates
  12. 6. Entropy and KL divergence
  13. 7. Stein, Bonnet and Price: differentiating through a Gaussian
  14. 8. The matrix normal
  15. 9. Derivatives of the log-density
  16. 10. Probabilistic moves
  17. Splitting uncertainty: the law of total variance
  18. The evidence lower bound in three lines
  19. Fisher information: the geometry inside the KL
  20. 11. Gaussians in reinforcement learning
  21. Questions
  22. References

The Gaussian is the one distribution that stays closed under almost everything we want to do: linear maps, sums, conditioning, marginalising, multiplying densities. That closure is why Gaussian processes, Kalman filters, variational inference, diffusion models and most continuous-control reinforcement learning are tractable. It is also why derivations in these areas feel either effortless or hopeless: with the right handful of moves they collapse to a few lines, and without them they sprawl.

This page collects those moves. Section 2 is the linear algebra underneath (whitening, low-rank updates, Schur complements, spectra). Sections 3 to 9 are the Gaussian identities proper. Section 10 covers the probabilistic moves that recur everywhere (total variance, moment matching, the ELBO, Fisher information), and Section 11 shows the same tools running reinforcement learning. The core results are proved step by step in Gaussian processes, from first principles and cited as “GP Lemma 5” and so on; everything else is proved here. Proofs, notes, remarks and examples are folded; the notes marked why experts reach for it are the point of the page.

Two pictures carry most of the intuition. Conditioning is a slice: fix x1=ax_1 = a and read off the Gaussian along that line. Marginalising is a shadow: project the whole cloud onto the x2x_2 axis. Move ρ\rho and aa: the slice is always narrower than the shadow, and its centre moves along the dashed regression line.

Notation

Plain italic letters are scalars, bold italic lower-case letters are vectors (x,μ∈RD\boldsymbol{x}, \boldsymbol{\mu} \in \mathbb{R}^D), bold upright capitals are matrices (Σ\boldsymbol{\Sigma}, P=Σ−1\mathbf{P} = \boldsymbol{\Sigma}^{-1}, A\mathbf{A}). N(x∣μ,Σ)\mathcal{N}(\boldsymbol{x} \mid \boldsymbol{\mu}, \boldsymbol{\Sigma}) is the density

N(x∣μ,Σ)=(2π)−D/2∣Σ∣−1/2exp⁡ ⁣(−12(x−μ)⊤Σ−1(x−μ)).\mathcal{N}(\boldsymbol{x} \mid \boldsymbol{\mu}, \boldsymbol{\Sigma}) = (2\pi)^{-D/2}\lvert\boldsymbol{\Sigma}\rvert^{-1/2}\exp\!\Big(-\tfrac12(\boldsymbol{x} - \boldsymbol{\mu})^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{x} - \boldsymbol{\mu})\Big).

1. Where the constant comes from

Proposition 1 (The Gaussian integral)

For symmetric positive definite P∈RD×D\mathbf{P} \in \mathbb{R}^{D\times D},   ∫RDexp⁡ ⁣(−12x⊤Px) dx=(2π)D/2 ∣P∣−1/2\;\displaystyle\int_{\mathbb{R}^D} \exp\!\big(-\tfrac12\boldsymbol{x}^\top\mathbf{P}\boldsymbol{x}\big)\,d\boldsymbol{x} = (2\pi)^{D/2}\,\lvert\mathbf{P}\rvert^{-1/2}.

Proof

Step 1. Rotate to the eigenbasis. Write P=QΛQ⊤\mathbf{P} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top with Q\mathbf{Q} orthogonal and Λ=diag⁡(λ1,…,λD)\boldsymbol{\Lambda} = \operatorname{diag}(\lambda_1, \dots, \lambda_D), λi>0\lambda_i > 0. Substitute y=Q⊤x\boldsymbol{y} = \mathbf{Q}^\top\boldsymbol{x}; since ∣det⁡Q∣=1\lvert\det\mathbf{Q}\rvert = 1, dx=dyd\boldsymbol{x} = d\boldsymbol{y}.

Step 2. The exponent separates. x⊤Px=y⊤Λy=∑iλiyi2\boldsymbol{x}^\top\mathbf{P}\boldsymbol{x} = \boldsymbol{y}^\top\boldsymbol{\Lambda}\boldsymbol{y} = \sum_i\lambda_iy_i^2, so the integrand is a product of one-dimensional factors:

∫exp⁡ ⁣(−12∑iλiyi2)dy=∏i=1D∫−∞∞e−λiyi2/2 dyi.\int\exp\!\Big(-\tfrac12\sum_i\lambda_iy_i^2\Big)d\boldsymbol{y} = \prod_{i=1}^D\int_{-\infty}^{\infty}e^{-\lambda_iy_i^2/2}\,dy_i .

Step 3. Each factor. Substituting yi=s/λiy_i = s/\sqrt{\lambda_i} and using ∫e−s2/2ds=2π\int e^{-s^2/2}ds = \sqrt{2\pi}, each factor is 2π/λi\sqrt{2\pi/\lambda_i}.

Step 4. Multiply. ∏i2π/λi=(2π)D/2(∏iλi)−1/2=(2π)D/2∣P∣−1/2\prod_i\sqrt{2\pi/\lambda_i} = (2\pi)^{D/2}\big(\prod_i\lambda_i\big)^{-1/2} = (2\pi)^{D/2}\lvert\mathbf{P}\rvert^{-1/2}, since the determinant is the product of the eigenvalues.

With P=Σ−1\mathbf{P} = \boldsymbol{\Sigma}^{-1} (so ∣P∣−1/2=∣Σ∣1/2\lvert\mathbf{P}\rvert^{-1/2} = \lvert\boldsymbol{\Sigma}\rvert^{1/2}) and a shift by μ\boldsymbol{\mu}, this is exactly the normaliser in the density.

2. Five linear-algebra moves

Most “Gaussian algebra” is linear algebra in disguise, and most of the difficulty beginners meet is choosing which way to write a matrix expression. Five moves cover nearly every derivation in probabilistic machine learning. Beginners expand and compute; experts rewrite with one of these first.

Move 1: a scalar is its own trace

a⊤Mb=tr⁡(a⊤Mb)=tr⁡(Mba⊤)\boldsymbol{a}^\top\mathbf{M}\boldsymbol{b} = \operatorname{tr}(\boldsymbol{a}^\top\mathbf{M}\boldsymbol{b}) = \operatorname{tr}(\mathbf{M}\boldsymbol{b}\boldsymbol{a}^\top), because a 1×11\times1 matrix equals its trace and tr⁡(UV)=tr⁡(VU)\operatorname{tr}(\mathbf{U}\mathbf{V}) = \operatorname{tr}(\mathbf{V}\mathbf{U}). It turns a quadratic form into a linear function of an outer product, which can then be averaged or summed.

Note (Why experts reach for it: sufficient statistics appear by themselves)

Step 1. The data-dependent part of a Gaussian log-likelihood is −12∑n=1N(xn−μ)⊤Σ−1(xn−μ)-\tfrac12\sum_{n=1}^N(\boldsymbol{x}_n - \boldsymbol{\mu})^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{x}_n - \boldsymbol{\mu}).

Step 2. Each term is tr⁡(Σ−1(xn−μ)(xn−μ)⊤)\operatorname{tr}\big(\boldsymbol{\Sigma}^{-1}(\boldsymbol{x}_n - \boldsymbol{\mu})(\boldsymbol{x}_n - \boldsymbol{\mu})^\top\big) by Move 1; the trace is linear, so the sum is tr⁡(Σ−1Sμ)\operatorname{tr}(\boldsymbol{\Sigma}^{-1}\mathbf{S}_{\boldsymbol{\mu}}) with Sμ=∑n(xn−μ)(xn−μ)⊤\mathbf{S}_{\boldsymbol{\mu}} = \sum_n(\boldsymbol{x}_n - \boldsymbol{\mu})(\boldsymbol{x}_n - \boldsymbol{\mu})^\top.

Step 3. The data now enter only through one D×DD\times D matrix. Expanding Sμ\mathbf{S}_{\boldsymbol{\mu}} shows that it depends on the data only through ∑nxn\sum_n\boldsymbol{x}_n and ∑nxnxn⊤\sum_n\boldsymbol{x}_n\boldsymbol{x}_n^\top: those two sums are all a Gaussian model ever needs to remember, however large NN is.

Move 2: never invert; factor and whiten

Lemma 1 (Whitening with a Cholesky factor)

Let Σ=LL⊤\boldsymbol{\Sigma} = \mathbf{L}\mathbf{L}^\top with L\mathbf{L} lower triangular (Cholesky). Then

  1. x=μ+Lz\boldsymbol{x} = \boldsymbol{\mu} + \mathbf{L}\boldsymbol{z} with z∼N(0,I)\boldsymbol{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) has x∼N(μ,Σ)\boldsymbol{x} \sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma});
  2. (x−μ)⊤Σ−1(x−μ)=∥w∥2(\boldsymbol{x} - \boldsymbol{\mu})^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{x} - \boldsymbol{\mu}) = \lVert\boldsymbol{w}\rVert^2, where w\boldsymbol{w} solves the triangular system Lw=x−μ\mathbf{L}\boldsymbol{w} = \boldsymbol{x} - \boldsymbol{\mu};
  3. log⁡∣Σ∣=2∑ilog⁡Lii\log\lvert\boldsymbol{\Sigma}\rvert = 2\sum_i\log L_{ii}.
Proof

Step 1. Sampling. By GP Lemma 2, μ+Lz\boldsymbol{\mu} + \mathbf{L}\boldsymbol{z} is Gaussian with mean μ\boldsymbol{\mu} and covariance LIL⊤=Σ\mathbf{L}\mathbf{I}\mathbf{L}^\top = \boldsymbol{\Sigma}.

Step 2. The inverse factorises. Σ−1=(LL⊤)−1=L−⊤L−1\boldsymbol{\Sigma}^{-1} = (\mathbf{L}\mathbf{L}^\top)^{-1} = \mathbf{L}^{-\top}\mathbf{L}^{-1}.

Step 3. Mahalanobis distance. With w=L−1(x−μ)\boldsymbol{w} = \mathbf{L}^{-1}(\boldsymbol{x} - \boldsymbol{\mu}), the quadratic form is (x−μ)⊤L−⊤L−1(x−μ)=w⊤w(\boldsymbol{x} - \boldsymbol{\mu})^\top\mathbf{L}^{-\top}\mathbf{L}^{-1}(\boldsymbol{x} - \boldsymbol{\mu}) = \boldsymbol{w}^\top\boldsymbol{w}.

Step 4. Determinant. ∣Σ∣=∣L∣ ∣L⊤∣=∣L∣2\lvert\boldsymbol{\Sigma}\rvert = \lvert\mathbf{L}\rvert\,\lvert\mathbf{L}^\top\rvert = \lvert\mathbf{L}\rvert^2, and the determinant of a triangular matrix is the product of its diagonal. Take logs.

Every Gaussian is a standard normal seen through a linear map. Expert practice is to move to z\boldsymbol{z}-space, where every coordinate is independent with unit variance, do the work there, and map back at the end. Numerically, a triangular solve costs O(D2)O(D^2) and is stable, while forming Σ−1\boldsymbol{\Sigma}^{-1} costs O(D3)O(D^3) and amplifies rounding error by the condition number. In code, inv(S) is almost always a bug.

Move 3: update, don’t recompute

Lemma 2 (Sherman–Morrison and the matrix determinant lemma)

For invertible A\mathbf{A} and vectors u,v\boldsymbol{u}, \boldsymbol{v} with 1+v⊤A−1u≠01 + \boldsymbol{v}^\top\mathbf{A}^{-1}\boldsymbol{u} \ne 0:

(A+uv⊤)−1=A−1−A−1uv⊤A−11+v⊤A−1u,∣A+uv⊤∣=∣A∣ (1+v⊤A−1u).(\mathbf{A} + \boldsymbol{u}\boldsymbol{v}^\top)^{-1} = \mathbf{A}^{-1} - \frac{\mathbf{A}^{-1}\boldsymbol{u}\boldsymbol{v}^\top\mathbf{A}^{-1}}{1 + \boldsymbol{v}^\top\mathbf{A}^{-1}\boldsymbol{u}}, \qquad \lvert\mathbf{A} + \boldsymbol{u}\boldsymbol{v}^\top\rvert = \lvert\mathbf{A}\rvert\,\big(1 + \boldsymbol{v}^\top\mathbf{A}^{-1}\boldsymbol{u}\big).
Proof

Step 1. Multiply the candidate inverse by A+uv⊤\mathbf{A} + \boldsymbol{u}\boldsymbol{v}^\top. Write γ=v⊤A−1u\gamma = \boldsymbol{v}^\top\mathbf{A}^{-1}\boldsymbol{u}, a scalar. Then

(A+uv⊤)(A−1−A−1uv⊤A−11+γ)=I+uv⊤A−1−uv⊤A−1+u γ v⊤A−11+γ,(\mathbf{A} + \boldsymbol{u}\boldsymbol{v}^\top)\Big(\mathbf{A}^{-1} - \frac{\mathbf{A}^{-1}\boldsymbol{u}\boldsymbol{v}^\top\mathbf{A}^{-1}}{1 + \gamma}\Big) = \mathbf{I} + \boldsymbol{u}\boldsymbol{v}^\top\mathbf{A}^{-1} - \frac{\boldsymbol{u}\boldsymbol{v}^\top\mathbf{A}^{-1} + \boldsymbol{u}\,\gamma\,\boldsymbol{v}^\top\mathbf{A}^{-1}}{1 + \gamma},

using v⊤A−1u=γ\boldsymbol{v}^\top\mathbf{A}^{-1}\boldsymbol{u} = \gamma in the last product.

Step 2. Simplify. The fraction is u(1+γ)v⊤A−1/(1+γ)=uv⊤A−1\boldsymbol{u}(1 + \gamma)\boldsymbol{v}^\top\mathbf{A}^{-1}/(1 + \gamma) = \boldsymbol{u}\boldsymbol{v}^\top\mathbf{A}^{-1}, which cancels the middle term, leaving I\mathbf{I}.

Step 3. Determinant: factor out A\mathbf{A}. ∣A+uv⊤∣=∣A∣ ∣I+bv⊤∣\lvert\mathbf{A} + \boldsymbol{u}\boldsymbol{v}^\top\rvert = \lvert\mathbf{A}\rvert\,\lvert\mathbf{I} + \boldsymbol{b}\boldsymbol{v}^\top\rvert with b=A−1u\boldsymbol{b} = \mathbf{A}^{-1}\boldsymbol{u}.

Step 4. Eigenvalues of I+bv⊤\mathbf{I} + \boldsymbol{b}\boldsymbol{v}^\top. The rank-one matrix bv⊤\boldsymbol{b}\boldsymbol{v}^\top maps every vector to a multiple of b\boldsymbol{b}; it has eigenvalue v⊤b\boldsymbol{v}^\top\boldsymbol{b} (eigenvector b\boldsymbol{b}) and eigenvalue 00 on the (D−1)(D-1)-dimensional subspace orthogonal to v\boldsymbol{v}. Adding I\mathbf{I} adds 11 to each, so the determinant is 1+v⊤b1 + \boldsymbol{v}^\top\boldsymbol{b}.

Note (Why experts reach for it: one new data point costs O(D²), not O(D³))

In Bayesian linear regression the posterior precision gains one rank-one term per observation, A←A+σn−2ϕϕ⊤\mathbf{A} \leftarrow \mathbf{A} + \sigma_n^{-2}\boldsymbol{\phi}\boldsymbol{\phi}^\top (GP Section 2). Sherman–Morrison updates A−1\mathbf{A}^{-1}, and the determinant lemma updates log⁡∣A∣\log\lvert\mathbf{A}\rvert, in O(M2)O(M^2) per point. This is recursive least squares, the Kalman filter’s update step, and the reason a GP can absorb one new observation by extending its Cholesky factor by one row in O(N2)O(N^2). The general rank-kk version is the Woodbury identity (GP Lemma 6).

Move 4: blocks, Schur complements, and the chain rule

Lemma 3 (Block factorisation and determinant)

For M=[ABCD]\mathbf{M} = \begin{bmatrix}\mathbf{A} & \mathbf{B}\\\mathbf{C} & \mathbf{D}\end{bmatrix} with A\mathbf{A} invertible, and the Schur complement S=D−CA−1B\mathbf{S} = \mathbf{D} - \mathbf{C}\mathbf{A}^{-1}\mathbf{B},

M=[I0CA−1I][A00S][IA−1B0I],∣M∣=∣A∣ ∣S∣.\mathbf{M} = \begin{bmatrix}\mathbf{I} & \mathbf{0}\\\mathbf{C}\mathbf{A}^{-1} & \mathbf{I}\end{bmatrix}\begin{bmatrix}\mathbf{A} & \mathbf{0}\\\mathbf{0} & \mathbf{S}\end{bmatrix}\begin{bmatrix}\mathbf{I} & \mathbf{A}^{-1}\mathbf{B}\\\mathbf{0} & \mathbf{I}\end{bmatrix}, \qquad \lvert\mathbf{M}\rvert = \lvert\mathbf{A}\rvert\,\lvert\mathbf{S}\rvert .
Proof

Step 1. Multiply the last two factors. [A00S][IA−1B0I]=[AB0S]\begin{bmatrix}\mathbf{A} & \mathbf{0}\\\mathbf{0} & \mathbf{S}\end{bmatrix}\begin{bmatrix}\mathbf{I} & \mathbf{A}^{-1}\mathbf{B}\\\mathbf{0} & \mathbf{I}\end{bmatrix} = \begin{bmatrix}\mathbf{A} & \mathbf{B}\\\mathbf{0} & \mathbf{S}\end{bmatrix}.

Step 2. Multiply by the first factor. The top block row is unchanged; the bottom block row becomes [CA−1A,    CA−1B+S]=[C,    D]\big[\mathbf{C}\mathbf{A}^{-1}\mathbf{A},\;\; \mathbf{C}\mathbf{A}^{-1}\mathbf{B} + \mathbf{S}\big] = [\mathbf{C},\;\; \mathbf{D}]. That is M\mathbf{M}.

Step 3. Determinants. The outer factors are block triangular with identity diagonal blocks, so their determinants are 11. The middle factor is block diagonal, with determinant ∣A∣ ∣S∣\lvert\mathbf{A}\rvert\,\lvert\mathbf{S}\rvert.

Note (Why experts reach for it: the determinant obeys the chain rule of probability)

For a Gaussian joint covariance, A=Σaa\mathbf{A} = \boldsymbol{\Sigma}_{aa} and S=Σbb−ΣbaΣaa−1Σab\mathbf{S} = \boldsymbol{\Sigma}_{bb} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab} is the conditional covariance of b\boldsymbol{b} given a\boldsymbol{a} (GP Lemma 5). So log⁡∣Σ∣=log⁡∣Σaa∣+log⁡∣Σb∣a∣\log\lvert\boldsymbol{\Sigma}\rvert = \log\lvert\boldsymbol{\Sigma}_{aa}\rvert + \log\lvert\boldsymbol{\Sigma}_{b\mid a}\rvert: the log-determinant splits exactly as log⁡p(a,b)=log⁡p(a)+log⁡p(b∣a)\log p(\boldsymbol{a}, \boldsymbol{b}) = \log p(\boldsymbol{a}) + \log p(\boldsymbol{b} \mid \boldsymbol{a}). Applied one variable at a time, it is why a GP’s marginal likelihood is a sum of one-step-ahead predictive terms (GP Section 7), and why a Cholesky factor’s diagonal entries are conditional standard deviations.

Move 5: think in eigenvalues

A function of a symmetric matrix acts on its eigenvalues and leaves its eigenvectors alone: if K=UΛU⊤\mathbf{K} = \mathbf{U}\boldsymbol{\Lambda}\mathbf{U}^\top, then (K+σ2I)−1=U(Λ+σ2I)−1U⊤(\mathbf{K} + \sigma^2\mathbf{I})^{-1} = \mathbf{U}(\boldsymbol{\Lambda} + \sigma^2\mathbf{I})^{-1}\mathbf{U}^\top. Many formulas that look opaque become one-line statements about numbers.

Note (Why experts reach for it: a GP is a spectral filter)

Step 1. The GP posterior mean at the training inputs is K(K+σn2I)−1y\mathbf{K}(\mathbf{K} + \sigma_n^2\mathbf{I})^{-1}\boldsymbol{y} (GP Proposition 5).

Step 2. In the eigenbasis this is U Λ(Λ+σn2I)−1 U⊤y\mathbf{U}\,\boldsymbol{\Lambda}(\boldsymbol{\Lambda} + \sigma_n^2\mathbf{I})^{-1}\,\mathbf{U}^\top\boldsymbol{y}, that is,

∑iλiλi+σn2 (ui⊤y) ui.\sum_i\frac{\lambda_i}{\lambda_i + \sigma_n^2}\,(\boldsymbol{u}_i^\top\boldsymbol{y})\,\boldsymbol{u}_i .

Step 3. Each direction of the data is multiplied by λi/(λi+σn2)∈(0,1)\lambda_i/(\lambda_i + \sigma_n^2) \in (0, 1): kept where the prior’s signal variance λi\lambda_i dominates the noise, suppressed where it does not. Smooth kernels have rapidly decaying λi\lambda_i, so the GP keeps a few smooth directions and filters the rest. The sum ∑iλi/(λi+σn2)\sum_i\lambda_i/(\lambda_i + \sigma_n^2) is the model’s effective number of parameters.

3. Moments

Proposition 2 (First and second moments, and quadratic forms)

For x∼N(μ,Σ)\boldsymbol{x} \sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}) and any matrix A\mathbf{A}:

E[xx⊤]=Σ+μμ⊤,E[x⊤Ax]=tr⁡(AΣ)+μ⊤Aμ.\mathbb{E}[\boldsymbol{x}\boldsymbol{x}^\top] = \boldsymbol{\Sigma} + \boldsymbol{\mu}\boldsymbol{\mu}^\top, \qquad \mathbb{E}[\boldsymbol{x}^\top\mathbf{A}\boldsymbol{x}] = \operatorname{tr}(\mathbf{A}\boldsymbol{\Sigma}) + \boldsymbol{\mu}^\top\mathbf{A}\boldsymbol{\mu}.

(These hold for any distribution with mean μ\boldsymbol{\mu} and covariance Σ\boldsymbol{\Sigma}.)

Proof

Step 1. Second moment. Expand the definition of covariance: Σ=E[(x−μ)(x−μ)⊤]=E[xx⊤]−μμ⊤\boldsymbol{\Sigma} = \mathbb{E}[(\boldsymbol{x} - \boldsymbol{\mu})(\boldsymbol{x} - \boldsymbol{\mu})^\top] = \mathbb{E}[\boldsymbol{x}\boldsymbol{x}^\top] - \boldsymbol{\mu}\boldsymbol{\mu}^\top (the two cross terms each equal −μμ⊤-\boldsymbol{\mu}\boldsymbol{\mu}^\top, and +μμ⊤+\boldsymbol{\mu}\boldsymbol{\mu}^\top remains). Rearrange.

Step 2. The trace trick. A scalar is its own trace, and tr⁡(UV)=tr⁡(VU)\operatorname{tr}(\mathbf{U}\mathbf{V}) = \operatorname{tr}(\mathbf{V}\mathbf{U}): x⊤Ax=tr⁡(x⊤Ax)=tr⁡(Axx⊤)\boldsymbol{x}^\top\mathbf{A}\boldsymbol{x} = \operatorname{tr}(\boldsymbol{x}^\top\mathbf{A}\boldsymbol{x}) = \operatorname{tr}(\mathbf{A}\boldsymbol{x}\boldsymbol{x}^\top).

Step 3. Expectation passes through the trace (both are linear): E[x⊤Ax]=tr⁡(A E[xx⊤])=tr⁡(AΣ)+tr⁡(Aμμ⊤)\mathbb{E}[\boldsymbol{x}^\top\mathbf{A}\boldsymbol{x}] = \operatorname{tr}(\mathbf{A}\,\mathbb{E}[\boldsymbol{x}\boldsymbol{x}^\top]) = \operatorname{tr}(\mathbf{A}\boldsymbol{\Sigma}) + \operatorname{tr}(\mathbf{A}\boldsymbol{\mu}\boldsymbol{\mu}^\top) by Step 1, and tr⁡(Aμμ⊤)=μ⊤Aμ\operatorname{tr}(\mathbf{A}\boldsymbol{\mu}\boldsymbol{\mu}^\top) = \boldsymbol{\mu}^\top\mathbf{A}\boldsymbol{\mu} by Step 2 read backwards.

Fourth moments are where the Gaussian is special: they are fixed entirely by the covariance.

Proposition 3 (Isserlis (Wick) theorem)

For x∼N(0,Σ)\boldsymbol{x} \sim \mathcal{N}(\mathbf{0}, \boldsymbol{\Sigma}),   E[xixjxkxl]=ΣijΣkl+ΣikΣjl+ΣilΣjk\;\mathbb{E}[x_ix_jx_kx_l] = \Sigma_{ij}\Sigma_{kl} + \Sigma_{ik}\Sigma_{jl} + \Sigma_{il}\Sigma_{jk}: a sum over the three ways to pair four indices.

Proof

Step 1. The moment generating function. M(t)=E[et⊤x]=exp⁡(q)M(\boldsymbol{t}) = \mathbb{E}[e^{\boldsymbol{t}^\top\boldsymbol{x}}] = \exp(q) with q=12t⊤Σtq = \tfrac12\boldsymbol{t}^\top\boldsymbol{\Sigma}\boldsymbol{t} (complete the square in the integral, as in GP Lemma 1, then use Proposition 1). Moments are derivatives at zero: E[xixjxkxl]=∂i∂j∂k∂lM ∣t=0\mathbb{E}[x_ix_jx_kx_l] = \partial_i\partial_j\partial_k\partial_l M\,\big|_{\boldsymbol{t} = \mathbf{0}}.

Step 2. Derivatives of qq. Write si=∂iq=(Σt)is_i = \partial_iq = (\boldsymbol{\Sigma}\boldsymbol{t})_i. Then ∂jsi=Σij\partial_js_i = \Sigma_{ij}, all higher derivatives of qq vanish, and si=0s_i = 0 at t=0\boldsymbol{t} = \mathbf{0}.

Step 3. Differentiate four times, using ∂iM=siM\partial_iM = s_iM and the product rule:

∂j∂iM=(Σij+sisj) M,∂k∂j∂iM=(Σijsk+Σiksj+Σjksi+sisjsk) M.\begin{aligned} \partial_j\partial_iM &= (\Sigma_{ij} + s_is_j)\,M, \\ \partial_k\partial_j\partial_iM &= (\Sigma_{ij}s_k + \Sigma_{ik}s_j + \Sigma_{jk}s_i + s_is_js_k)\,M . \end{aligned}

Step 4. Differentiate once more and set t=0\boldsymbol{t} = \mathbf{0}. Every term still containing an ss vanishes. What survives is each ss in Step 3 differentiated by ∂l\partial_l, with M(0)=1M(\mathbf{0}) = 1: ΣijΣkl+ΣikΣjl+ΣjkΣil\Sigma_{ij}\Sigma_{kl} + \Sigma_{ik}\Sigma_{jl} + \Sigma_{jk}\Sigma_{il}.

Example 1 (Where Isserlis is used: the Hutchinson trace estimator)

Expensive GP computations need tr⁡(A)\operatorname{tr}(\mathbf{A}) for a matrix A\mathbf{A} we can only multiply by (for example A=Ky−1∂Ky\mathbf{A} = \mathbf{K}_y^{-1}\partial\mathbf{K}_y in the gradient of the marginal likelihood, GP Proposition 8). Draw z∼N(0,I)\boldsymbol{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}).

Step 1. Unbiased. By Proposition 2 with Σ=I\boldsymbol{\Sigma} = \mathbf{I}, μ=0\boldsymbol{\mu} = \mathbf{0}: E[z⊤Az]=tr⁡A\mathbb{E}[\boldsymbol{z}^\top\mathbf{A}\boldsymbol{z}] = \operatorname{tr}\mathbf{A}.

Step 2. Second moment by Isserlis (symmetric A\mathbf{A}, Σij=δij\Sigma_{ij} = \delta_{ij}):

E[(z⊤Az)2]=∑ijklAijAkl(δijδkl+δikδjl+δilδjk)=(tr⁡A)2+2tr⁡(A2).\mathbb{E}[(\boldsymbol{z}^\top\mathbf{A}\boldsymbol{z})^2] = \sum_{ijkl}A_{ij}A_{kl}(\delta_{ij}\delta_{kl} + \delta_{ik}\delta_{jl} + \delta_{il}\delta_{jk}) = (\operatorname{tr}\mathbf{A})^2 + 2\operatorname{tr}(\mathbf{A}^2).

Step 3. Variance. Subtract the squared mean: Var⁡[z⊤Az]=2tr⁡(A2)=2∥A∥F2\operatorname{Var}[\boldsymbol{z}^\top\mathbf{A}\boldsymbol{z}] = 2\operatorname{tr}(\mathbf{A}^2) = 2\lVert\mathbf{A}\rVert_F^2. Averaging SS independent probes divides it by SS.

4. The closure toolkit

OperationResultProof
Linear map Cx+d\mathbf{C}\boldsymbol{x} + \boldsymbol{d}N(Cμ+d, CΣC⊤)\mathcal{N}(\mathbf{C}\boldsymbol{\mu} + \boldsymbol{d},\, \mathbf{C}\boldsymbol{\Sigma}\mathbf{C}^\top)GP Lemma 2
Sum of independent Gaussians (convolution)means add, covariances addGP Lemma 3
Product of densities in the same variableprecisions add; normaliser N(μ1∣μ2,Σ1+Σ2)\mathcal{N}(\boldsymbol{\mu}_1 \mid \boldsymbol{\mu}_2, \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2)GP Lemma 4
Conditioning, moment formμb+ΣbaΣaa−1(a−μa)\boldsymbol{\mu}_b + \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}(\boldsymbol{a} - \boldsymbol{\mu}_a), Schur complementGP Lemma 5
Likelihood N(y∣Cx+d,S)\mathcal{N}(\boldsymbol{y} \mid \mathbf{C}\boldsymbol{x} + \boldsymbol{d}, \mathbf{S}) times prior on x\boldsymbol{x}evidence times posterior, two formsGP Lemma 8
Push-through and WoodburyA−1=Σw−ΣwΦ⊤G−1ΦΣw\mathbf{A}^{-1} = \boldsymbol{\Sigma}_w - \boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1}\boldsymbol{\Phi}\boldsymbol{\Sigma}_wGP Lemma 6
Sampling a conditional by correcting a prior sampleMatheron’s ruleGP Proposition 6
Mixture of Gaussiansnot GaussianGP Note on Lemma 4

5. Conditioning and marginalising, in both coordinates

The moment form makes marginalising trivial (read off a block) and conditioning costly (a Schur complement). The information form swaps the roles. Here is the information-form half, which the GP post only sketched.

Proposition 4 (Information form: slice and shadow)

Let [ab]\begin{bmatrix}\boldsymbol{a}\\\boldsymbol{b}\end{bmatrix} be Gaussian with mean [μaμb]\begin{bmatrix}\boldsymbol{\mu}_a\\\boldsymbol{\mu}_b\end{bmatrix} and precision P=[PaaPabPbaPbb]\mathbf{P} = \begin{bmatrix}\mathbf{P}_{aa} & \mathbf{P}_{ab}\\\mathbf{P}_{ba} & \mathbf{P}_{bb}\end{bmatrix}. Then

(slice)b∣a∼N(μb−Pbb−1Pba(a−μa),    Pbb−1),(shadow)a∼N(μa,    (Paa−PabPbb−1Pba)−1).\begin{aligned} \text{(slice)}\quad & \boldsymbol{b} \mid \boldsymbol{a} \sim \mathcal{N}\big(\boldsymbol{\mu}_b - \mathbf{P}_{bb}^{-1}\mathbf{P}_{ba}(\boldsymbol{a} - \boldsymbol{\mu}_a),\;\; \mathbf{P}_{bb}^{-1}\big), \\ \text{(shadow)}\quad & \boldsymbol{a} \sim \mathcal{N}\big(\boldsymbol{\mu}_a,\;\; (\mathbf{P}_{aa} - \mathbf{P}_{ab}\mathbf{P}_{bb}^{-1}\mathbf{P}_{ba})^{-1}\big). \end{aligned}
Proof

Write α=a−μa\boldsymbol{\alpha} = \boldsymbol{a} - \boldsymbol{\mu}_a and β=b−μb\boldsymbol{\beta} = \boldsymbol{b} - \boldsymbol{\mu}_b.

Step 1. Expand the joint exponent in blocks. Using Pab⊤=Pba\mathbf{P}_{ab}^\top = \mathbf{P}_{ba},

−12[αβ]⊤P[αβ]=−12α⊤Paaα−β⊤Pbaα−12β⊤Pbbβ.-\tfrac12\begin{bmatrix}\boldsymbol{\alpha}\\\boldsymbol{\beta}\end{bmatrix}^\top\mathbf{P}\begin{bmatrix}\boldsymbol{\alpha}\\\boldsymbol{\beta}\end{bmatrix} = -\tfrac12\boldsymbol{\alpha}^\top\mathbf{P}_{aa}\boldsymbol{\alpha} - \boldsymbol{\beta}^\top\mathbf{P}_{ba}\boldsymbol{\alpha} - \tfrac12\boldsymbol{\beta}^\top\mathbf{P}_{bb}\boldsymbol{\beta}.

Step 2. Slice: fix a\boldsymbol{a}. As a function of β\boldsymbol{\beta} this is −12β⊤Pbbβ+β⊤h+const-\tfrac12\boldsymbol{\beta}^\top\mathbf{P}_{bb}\boldsymbol{\beta} + \boldsymbol{\beta}^\top\boldsymbol{h} + \text{const} with h=−Pbaα\boldsymbol{h} = -\mathbf{P}_{ba}\boldsymbol{\alpha}. GP Lemma 1 gives β∣a∼N(Pbb−1h,Pbb−1)\boldsymbol{\beta} \mid \boldsymbol{a} \sim \mathcal{N}(\mathbf{P}_{bb}^{-1}\boldsymbol{h}, \mathbf{P}_{bb}^{-1}). Add back μb\boldsymbol{\mu}_b.

Step 3. Shadow: complete the square in β\boldsymbol{\beta}. By GP Lemma 1, the two β\boldsymbol{\beta} terms equal −12(β−Pbb−1h)⊤Pbb(β−Pbb−1h)+12h⊤Pbb−1h-\tfrac12(\boldsymbol{\beta} - \mathbf{P}_{bb}^{-1}\boldsymbol{h})^\top\mathbf{P}_{bb}(\boldsymbol{\beta} - \mathbf{P}_{bb}^{-1}\boldsymbol{h}) + \tfrac12\boldsymbol{h}^\top\mathbf{P}_{bb}^{-1}\boldsymbol{h}.

Step 4. Integrate b\boldsymbol{b} out. By Proposition 1 the integral of the first term over β\boldsymbol{\beta} is (2π)dim⁡b/2∣Pbb∣−1/2(2\pi)^{\dim\boldsymbol{b}/2}\lvert\mathbf{P}_{bb}\rvert^{-1/2}, which does not depend on a\boldsymbol{a}. What remains in α\boldsymbol{\alpha} is

−12α⊤Paaα+12α⊤PabPbb−1Pbaα=−12α⊤(Paa−PabPbb−1Pba)α,-\tfrac12\boldsymbol{\alpha}^\top\mathbf{P}_{aa}\boldsymbol{\alpha} + \tfrac12\boldsymbol{\alpha}^\top\mathbf{P}_{ab}\mathbf{P}_{bb}^{-1}\mathbf{P}_{ba}\boldsymbol{\alpha} = -\tfrac12\boldsymbol{\alpha}^\top\big(\mathbf{P}_{aa} - \mathbf{P}_{ab}\mathbf{P}_{bb}^{-1}\mathbf{P}_{ba}\big)\boldsymbol{\alpha},

using h⊤Pbb−1h=α⊤PabPbb−1Pbaα\boldsymbol{h}^\top\mathbf{P}_{bb}^{-1}\boldsymbol{h} = \boldsymbol{\alpha}^\top\mathbf{P}_{ab}\mathbf{P}_{bb}^{-1}\mathbf{P}_{ba}\boldsymbol{\alpha}. Read off the precision.

Remark (A free corollary: the block inverse)

The marginal of a\boldsymbol{a} also has covariance Σaa\boldsymbol{\Sigma}_{aa} (moment form). Comparing with the shadow, Σaa=(Paa−PabPbb−1Pba)−1\boldsymbol{\Sigma}_{aa} = (\mathbf{P}_{aa} - \mathbf{P}_{ab}\mathbf{P}_{bb}^{-1}\mathbf{P}_{ba})^{-1}. Comparing the slice with GP Lemma 5, Pbb−1=Σbb−ΣbaΣaa−1Σab\mathbf{P}_{bb}^{-1} = \boldsymbol{\Sigma}_{bb} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab}. The block-inverse formulas fall out of probability with no matrix algebra. In the figure, with unit variances, P22=1/(1−ρ2)\mathbf{P}_{22} = 1/(1 - \rho^2): the slice’s variance 1−ρ21 - \rho^2 is 1/P221/\mathbf{P}_{22}, while the shadow’s is Σ22=1\Sigma_{22} = 1.

6. Entropy and KL divergence

Proposition 5 (Entropy)

  H[N(μ,Σ)]=12log⁡((2πe)D∣Σ∣)\;H\big[\mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma})\big] = \tfrac12\log\big((2\pi e)^D\lvert\boldsymbol{\Sigma}\rvert\big).

Proof

Step 1. H=−E[log⁡p(x)]=D2log⁡2π+12log⁡∣Σ∣+12E[(x−μ)⊤Σ−1(x−μ)]H = -\mathbb{E}[\log p(\boldsymbol{x})] = \tfrac D2\log 2\pi + \tfrac12\log\lvert\boldsymbol{\Sigma}\rvert + \tfrac12\mathbb{E}\big[(\boldsymbol{x} - \boldsymbol{\mu})^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{x} - \boldsymbol{\mu})\big].

Step 2. By Proposition 2 applied to x−μ\boldsymbol{x} - \boldsymbol{\mu} (mean 0\mathbf{0}), the expectation is tr⁡(Σ−1Σ)=D\operatorname{tr}(\boldsymbol{\Sigma}^{-1}\boldsymbol{\Sigma}) = D.

Step 3. D2log⁡2π+D2+12log⁡∣Σ∣=12log⁡((2πe)D∣Σ∣)\tfrac D2\log 2\pi + \tfrac D2 + \tfrac12\log\lvert\boldsymbol{\Sigma}\rvert = \tfrac12\log\big((2\pi e)^D\lvert\boldsymbol{\Sigma}\rvert\big).

Entropy depends only on ∣Σ∣\lvert\boldsymbol{\Sigma}\rvert, the volume of the uncertainty ellipsoid. Among all distributions with a given covariance, the Gaussian has the largest entropy, which is one reason it is the default “least committed” choice.

Proposition 6 (KL divergence between Gaussians)

For p0=N(μ0,Σ0)p_0 = \mathcal{N}(\boldsymbol{\mu}_0, \boldsymbol{\Sigma}_0) and p1=N(μ1,Σ1)p_1 = \mathcal{N}(\boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1) in RD\mathbb{R}^D,

KL⁡(p0 ∥ p1)=12[tr⁡(Σ1−1Σ0)+(μ1−μ0)⊤Σ1−1(μ1−μ0)−D+log⁡∣Σ1∣∣Σ0∣].\operatorname{KL}(p_0 \,\|\, p_1) = \tfrac12\Big[\operatorname{tr}(\boldsymbol{\Sigma}_1^{-1}\boldsymbol{\Sigma}_0) + (\boldsymbol{\mu}_1 - \boldsymbol{\mu}_0)^\top\boldsymbol{\Sigma}_1^{-1}(\boldsymbol{\mu}_1 - \boldsymbol{\mu}_0) - D + \log\frac{\lvert\boldsymbol{\Sigma}_1\rvert}{\lvert\boldsymbol{\Sigma}_0\rvert}\Big].
Proof

Step 1. Definition. KL⁡(p0∥p1)=E0[log⁡p0(x)]−E0[log⁡p1(x)]\operatorname{KL}(p_0 \| p_1) = \mathbb{E}_0[\log p_0(\boldsymbol{x})] - \mathbb{E}_0[\log p_1(\boldsymbol{x})], expectations under p0p_0.

Step 2. The first term is minus the entropy of p0p_0: −D2log⁡2π−12log⁡∣Σ0∣−D2-\tfrac D2\log 2\pi - \tfrac12\log\lvert\boldsymbol{\Sigma}_0\rvert - \tfrac D2 (Proposition 5, Steps 1–2).

Step 3. The second term. E0[log⁡p1]=−D2log⁡2π−12log⁡∣Σ1∣−12E0[(x−μ1)⊤Σ1−1(x−μ1)]\mathbb{E}_0[\log p_1] = -\tfrac D2\log 2\pi - \tfrac12\log\lvert\boldsymbol{\Sigma}_1\rvert - \tfrac12\mathbb{E}_0\big[(\boldsymbol{x} - \boldsymbol{\mu}_1)^\top\boldsymbol{\Sigma}_1^{-1}(\boldsymbol{x} - \boldsymbol{\mu}_1)\big]. Under p0p_0, x−μ1\boldsymbol{x} - \boldsymbol{\mu}_1 has mean μ0−μ1\boldsymbol{\mu}_0 - \boldsymbol{\mu}_1 and covariance Σ0\boldsymbol{\Sigma}_0, so by Proposition 2 the expectation is tr⁡(Σ1−1Σ0)+(μ0−μ1)⊤Σ1−1(μ0−μ1)\operatorname{tr}(\boldsymbol{\Sigma}_1^{-1}\boldsymbol{\Sigma}_0) + (\boldsymbol{\mu}_0 - \boldsymbol{\mu}_1)^\top\boldsymbol{\Sigma}_1^{-1}(\boldsymbol{\mu}_0 - \boldsymbol{\mu}_1).

Step 4. Subtract. The log⁡2π\log 2\pi terms cancel; collecting the rest gives the formula.

Note (Where the KL appears)

It is the regulariser in every variational autoencoder (with p1=N(0,I)p_1 = \mathcal{N}(\mathbf{0}, \mathbf{I}) it becomes 12∑i(σi2+μi2−1−log⁡σi2)\tfrac12\sum_i(\sigma_i^2 + \mu_i^2 - 1 - \log\sigma_i^2) for a diagonal p0p_0), and the term that keeps inducing-point posteriors close to the prior in sparse and deep GPs. Its three parts read cleanly: a shape mismatch (trace), a location mismatch (Mahalanobis distance of the means), and a volume mismatch (log-determinants).

7. Stein, Bonnet and Price: differentiating through a Gaussian

Variational inference and reinforcement learning need gradients of Ex∼N(μ,Σ)[f(x)]\mathbb{E}_{\boldsymbol{x}\sim\mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma})}[f(\boldsymbol{x})] with respect to μ\boldsymbol{\mu} and Σ\boldsymbol{\Sigma}. Three identities turn them into expectations of derivatives of ff.

Lemma 4 (Stein’s lemma)

For x∼N(μ,Σ)\boldsymbol{x} \sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}) and differentiable gg with E∣∇g∣<∞\mathbb{E}\lvert\nabla g\rvert < \infty:   E[g(x)(x−μ)]=Σ E[∇g(x)]\;\mathbb{E}\big[g(\boldsymbol{x})(\boldsymbol{x} - \boldsymbol{\mu})\big] = \boldsymbol{\Sigma}\,\mathbb{E}\big[\nabla g(\boldsymbol{x})\big].

Proof

Step 1. One dimension, the key fact. For φ(z)=N(z∣0,1)\varphi(z) = \mathcal{N}(z \mid 0, 1), differentiating gives φ′(z)=−z φ(z)\varphi'(z) = -z\,\varphi(z).

Step 2. Integrate by parts. For a scalar function hh of z∼N(0,1)z \sim \mathcal{N}(0, 1),

E[h(z) z]=∫h(z) z φ(z) dz=−∫h(z) φ′(z) dz=∫h′(z) φ(z) dz=E[h′(z)],\mathbb{E}[h(z)\,z] = \int h(z)\,z\,\varphi(z)\,dz = -\int h(z)\,\varphi'(z)\,dz = \int h'(z)\,\varphi(z)\,dz = \mathbb{E}[h'(z)],

since the boundary term hφh\varphi vanishes at ±∞\pm\infty.

Step 3. Whiten. Write x=μ+Lz\boldsymbol{x} = \boldsymbol{\mu} + \mathbf{L}\boldsymbol{z} with LL⊤=Σ\mathbf{L}\mathbf{L}^\top = \boldsymbol{\Sigma} and z∼N(0,I)\boldsymbol{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) (GP Lemma 2). Let h(z)=g(μ+Lz)h(\boldsymbol{z}) = g(\boldsymbol{\mu} + \mathbf{L}\boldsymbol{z}); by the chain rule ∇zh=L⊤∇g\nabla_{\boldsymbol{z}}h = \mathbf{L}^\top\nabla g.

Step 4. Coordinate by coordinate. The zjz_j are independent, so Step 2 applies to each with the others held fixed: E[h(z) z]=E[∇zh]=L⊤E[∇g]\mathbb{E}[h(\boldsymbol{z})\,\boldsymbol{z}] = \mathbb{E}[\nabla_{\boldsymbol{z}}h] = \mathbf{L}^\top\mathbb{E}[\nabla g].

Step 5. Undo the whitening. E[g(x)(x−μ)]=L E[h(z) z]=LL⊤E[∇g]=Σ E[∇g]\mathbb{E}[g(\boldsymbol{x})(\boldsymbol{x} - \boldsymbol{\mu})] = \mathbf{L}\,\mathbb{E}[h(\boldsymbol{z})\,\boldsymbol{z}] = \mathbf{L}\mathbf{L}^\top\mathbb{E}[\nabla g] = \boldsymbol{\Sigma}\,\mathbb{E}[\nabla g].

Proposition 7 (Bonnet and Price)

For smooth ff with integrable derivatives,

∇μ E[f(x)]=E[∇f(x)]    (Bonnet),∇Σ E[f(x)]=12 E[∇2f(x)]    (Price).\nabla_{\boldsymbol{\mu}}\,\mathbb{E}[f(\boldsymbol{x})] = \mathbb{E}[\nabla f(\boldsymbol{x})] \;\;\text{(Bonnet)}, \qquad \nabla_{\boldsymbol{\Sigma}}\,\mathbb{E}[f(\boldsymbol{x})] = \tfrac12\,\mathbb{E}[\nabla^2 f(\boldsymbol{x})] \;\;\text{(Price)}.
Proof

Bonnet.

Step 1. Reparameterise. E[f(x)]=Ez[f(μ+Lz)]\mathbb{E}[f(\boldsymbol{x})] = \mathbb{E}_{\boldsymbol{z}}[f(\boldsymbol{\mu} + \mathbf{L}\boldsymbol{z})], and now μ\boldsymbol{\mu} appears only inside ff.

Step 2. Differentiate under the integral. ∇μf(μ+Lz)=∇f(x)\nabla_{\boldsymbol{\mu}}f(\boldsymbol{\mu} + \mathbf{L}\boldsymbol{z}) = \nabla f(\boldsymbol{x}), so ∇μE[f]=E[∇f]\nabla_{\boldsymbol{\mu}}\mathbb{E}[f] = \mathbb{E}[\nabla f]. This is the reparameterisation trick.

Price, in one dimension (the multivariate case follows the same steps entrywise).

Step 3. The Gaussian density solves the heat equation in its variance. For pv(x)=N(x∣μ,v)p_v(x) = \mathcal{N}(x \mid \mu, v), direct differentiation gives

∂vpv=pv(−12v+(x−μ)22v2),∂x2pv=pv(−1v+(x−μ)2v2),\partial_vp_v = p_v\Big(-\frac{1}{2v} + \frac{(x - \mu)^2}{2v^2}\Big), \qquad \partial_x^2p_v = p_v\Big(-\frac1v + \frac{(x - \mu)^2}{v^2}\Big),

so ∂vpv=12∂x2pv\partial_vp_v = \tfrac12\partial_x^2p_v.

Step 4. Move the derivative onto ff. ∂vE[f]=∫f ∂vpv dx=12∫f ∂x2pv dx=12∫f′′ pv dx\partial_v\mathbb{E}[f] = \int f\,\partial_vp_v\,dx = \tfrac12\int f\,\partial_x^2p_v\,dx = \tfrac12\int f''\,p_v\,dx, integrating by parts twice (boundary terms vanish).

Remark (Two different gradient estimators)

Bonnet’s identity is the reparameterisation gradient behind VAEs and Gaussian variational inference: low variance, but it needs ∇f\nabla f. The score-function identity ∇μE[f]=E[f(x) Σ−1(x−μ)]\nabla_{\boldsymbol{\mu}}\mathbb{E}[f] = \mathbb{E}\big[f(\boldsymbol{x})\,\boldsymbol{\Sigma}^{-1}(\boldsymbol{x} - \boldsymbol{\mu})\big] needs only values of ff (it is Stein’s lemma read in reverse, combined with Bonnet), which is why it powers policy gradients in reinforcement learning, at the price of higher variance. Price’s Step 3 is the same heat equation that the operator post learns: adding Gaussian noise is running heat flow.

8. The matrix normal

A random matrix X∈Rn×p\mathbf{X} \in \mathbb{R}^{n\times p} whose rows are correlated by one covariance and whose columns are correlated by another appears whenever data have two axes: inputs and outputs in a multi-output GP, space and time in a weather field.

Definition 1 (Matrix normal distribution)

X∼MNn×p(M,Ω,Σ)\mathbf{X} \sim \mathcal{MN}_{n\times p}(\mathbf{M}, \boldsymbol{\Omega}, \boldsymbol{\Sigma}), with mean M∈Rn×p\mathbf{M} \in \mathbb{R}^{n\times p}, row covariance Ω∈Rn×n\boldsymbol{\Omega} \in \mathbb{R}^{n\times n} and column covariance Σ∈Rp×p\boldsymbol{\Sigma} \in \mathbb{R}^{p\times p} (both positive definite), if

vec⁡(X)∼N(vec⁡(M),  Σ⊗Ω),\operatorname{vec}(\mathbf{X}) \sim \mathcal{N}\big(\operatorname{vec}(\mathbf{M}),\; \boldsymbol{\Sigma} \otimes \boldsymbol{\Omega}\big),

where vec⁡\operatorname{vec} stacks the columns. Entrywise, Cov⁡[Xij,Xkl]=Ωik Σjl\operatorname{Cov}[X_{ij}, X_{kl}] = \Omega_{ik}\,\Sigma_{jl}.

Conventions differ between books (some stack rows, which swaps the order of the Kronecker factors); the entrywise covariance is the unambiguous statement.

Lemma 5 (Two vec identities)

  1. vec⁡(AXB)=(B⊤⊗A)vec⁡(X)\operatorname{vec}(\mathbf{A}\mathbf{X}\mathbf{B}) = (\mathbf{B}^\top \otimes \mathbf{A})\operatorname{vec}(\mathbf{X}).
  2. vec⁡(A)⊤vec⁡(B)=tr⁡(A⊤B)\operatorname{vec}(\mathbf{A})^\top\operatorname{vec}(\mathbf{B}) = \operatorname{tr}(\mathbf{A}^\top\mathbf{B}).
Proof

Step 1. One column of the product. Column jj of AXB\mathbf{A}\mathbf{X}\mathbf{B} is AXbj=∑kBkj Axk\mathbf{A}\mathbf{X}\boldsymbol{b}_j = \sum_kB_{kj}\,\mathbf{A}\boldsymbol{x}_k, where xk\boldsymbol{x}_k is column kk of X\mathbf{X}.

Step 2. Stack the columns. Block jj of vec⁡(AXB)\operatorname{vec}(\mathbf{A}\mathbf{X}\mathbf{B}) is ∑k(BkjA)xk\sum_k(B_{kj}\mathbf{A})\boldsymbol{x}_k, which is block row jj of the matrix with blocks BkjA=(B⊤)jkAB_{kj}\mathbf{A} = (\mathbf{B}^\top)_{jk}\mathbf{A} applied to vec⁡(X)\operatorname{vec}(\mathbf{X}). That matrix is B⊤⊗A\mathbf{B}^\top \otimes \mathbf{A}.

Step 3. Inner product of vecs. vec⁡(A)⊤vec⁡(B)=∑ijAijBij=∑j(A⊤B)jj=tr⁡(A⊤B)\operatorname{vec}(\mathbf{A})^\top\operatorname{vec}(\mathbf{B}) = \sum_{ij}A_{ij}B_{ij} = \sum_j(\mathbf{A}^\top\mathbf{B})_{jj} = \operatorname{tr}(\mathbf{A}^\top\mathbf{B}).

Proposition 8 (Matrix normal density and characteristic function)

With E=X−M\mathbf{E} = \mathbf{X} - \mathbf{M},

p(X)=(2π)−np/2 ∣Σ∣−n/2 ∣Ω∣−p/2exp⁡ ⁣(−12tr⁡(Σ−1E⊤Ω−1E)),p(\mathbf{X}) = (2\pi)^{-np/2}\,\lvert\boldsymbol{\Sigma}\rvert^{-n/2}\,\lvert\boldsymbol{\Omega}\rvert^{-p/2}\exp\!\Big(-\tfrac12\operatorname{tr}\big(\boldsymbol{\Sigma}^{-1}\mathbf{E}^\top\boldsymbol{\Omega}^{-1}\mathbf{E}\big)\Big),

and E[eitr⁡(Z⊤X)]=exp⁡ ⁣(itr⁡(Z⊤M)−12tr⁡(Z⊤ΩZΣ))\mathbb{E}\big[e^{i\operatorname{tr}(\mathbf{Z}^\top\mathbf{X})}\big] = \exp\!\big(i\operatorname{tr}(\mathbf{Z}^\top\mathbf{M}) - \tfrac12\operatorname{tr}(\mathbf{Z}^\top\boldsymbol{\Omega}\mathbf{Z}\boldsymbol{\Sigma})\big).

Proof

Step 1. Start from the vector density of vec⁡(X)\operatorname{vec}(\mathbf{X}) in Rnp\mathbb{R}^{np}, with covariance Σ⊗Ω\boldsymbol{\Sigma} \otimes \boldsymbol{\Omega}.

Step 2. The determinant. By the mixed-product property, the eigenvalues of Σ⊗Ω\boldsymbol{\Sigma} \otimes \boldsymbol{\Omega} are all products σiωj\sigma_i\omega_j of eigenvalues of Σ\boldsymbol{\Sigma} (pp of them) and Ω\boldsymbol{\Omega} (nn of them). So ∣Σ⊗Ω∣=∏i,jσiωj=(∏iσi)n(∏jωj)p=∣Σ∣n∣Ω∣p\lvert\boldsymbol{\Sigma} \otimes \boldsymbol{\Omega}\rvert = \prod_{i,j}\sigma_i\omega_j = \big(\prod_i\sigma_i\big)^n\big(\prod_j\omega_j\big)^p = \lvert\boldsymbol{\Sigma}\rvert^n\lvert\boldsymbol{\Omega}\rvert^p.

Step 3. The quadratic form. (Σ⊗Ω)−1=Σ−1⊗Ω−1(\boldsymbol{\Sigma} \otimes \boldsymbol{\Omega})^{-1} = \boldsymbol{\Sigma}^{-1} \otimes \boldsymbol{\Omega}^{-1}, and by Lemma 5, part 1 (with B=Σ−1\mathbf{B} = \boldsymbol{\Sigma}^{-1}, which is symmetric),

vec⁡(E)⊤(Σ−1⊗Ω−1)vec⁡(E)=vec⁡(E)⊤vec⁡(Ω−1EΣ−1)=tr⁡(E⊤Ω−1EΣ−1),\operatorname{vec}(\mathbf{E})^\top(\boldsymbol{\Sigma}^{-1} \otimes \boldsymbol{\Omega}^{-1})\operatorname{vec}(\mathbf{E}) = \operatorname{vec}(\mathbf{E})^\top\operatorname{vec}(\boldsymbol{\Omega}^{-1}\mathbf{E}\boldsymbol{\Sigma}^{-1}) = \operatorname{tr}(\mathbf{E}^\top\boldsymbol{\Omega}^{-1}\mathbf{E}\boldsymbol{\Sigma}^{-1}),

the last step by part 2. Cycle the trace to tr⁡(Σ−1E⊤Ω−1E)\operatorname{tr}(\boldsymbol{\Sigma}^{-1}\mathbf{E}^\top\boldsymbol{\Omega}^{-1}\mathbf{E}).

Step 4. The characteristic function. tr⁡(Z⊤X)=vec⁡(Z)⊤vec⁡(X)\operatorname{tr}(\mathbf{Z}^\top\mathbf{X}) = \operatorname{vec}(\mathbf{Z})^\top\operatorname{vec}(\mathbf{X}) (part 2), so this is the Gaussian characteristic function (GP Lemma 2, Step 4) at t=vec⁡(Z)\boldsymbol{t} = \operatorname{vec}(\mathbf{Z}). Its quadratic term is vec⁡(Z)⊤(Σ⊗Ω)vec⁡(Z)=tr⁡(Z⊤ΩZΣ)\operatorname{vec}(\mathbf{Z})^\top(\boldsymbol{\Sigma} \otimes \boldsymbol{\Omega})\operatorname{vec}(\mathbf{Z}) = \operatorname{tr}(\mathbf{Z}^\top\boldsymbol{\Omega}\mathbf{Z}\boldsymbol{\Sigma}), by the same two identities.

Remark (A multi-output GP is a matrix normal)

For a separable kernel k(x,x′)Bk(\boldsymbol{x}, \boldsymbol{x}')\mathbf{B}, the N×pN\times p matrix F\mathbf{F} of function values (rows: inputs, columns: outputs) has Cov⁡[Fia,Fjb]=k(xi,xj) Bab\operatorname{Cov}[F_{ia}, F_{jb}] = k(\boldsymbol{x}_i, \boldsymbol{x}_j)\,B_{ab}. That is F∼MN(0,K,B)\mathbf{F} \sim \mathcal{MN}(\mathbf{0}, \mathbf{K}, \mathbf{B}): the row covariance is the Gram matrix and the column covariance is the output covariance. The density’s determinant ∣B∣N∣K∣p\lvert\mathbf{B}\rvert^{N}\lvert\mathbf{K}\rvert^{p} and its trace form are why the marginal likelihood of such models costs O(N3+p3)O(N^3 + p^3) rather than O(N3p3)O(N^3p^3), the same saving as the Kronecker trick in the operator post.

9. Derivatives of the log-density

Proposition 9 (Gradients, and the maximum-likelihood estimates)

For ℓ(μ,Σ)=log⁡N(x∣μ,Σ)\ell(\boldsymbol{\mu}, \boldsymbol{\Sigma}) = \log\mathcal{N}(\boldsymbol{x} \mid \boldsymbol{\mu}, \boldsymbol{\Sigma}) and r=x−μ\boldsymbol{r} = \boldsymbol{x} - \boldsymbol{\mu} (treating the entries of Σ\boldsymbol{\Sigma} as free),

∇μℓ=Σ−1r,∇Σℓ=12(Σ−1rr⊤Σ−1−Σ−1).\nabla_{\boldsymbol{\mu}}\ell = \boldsymbol{\Sigma}^{-1}\boldsymbol{r}, \qquad \nabla_{\boldsymbol{\Sigma}}\ell = \tfrac12\big(\boldsymbol{\Sigma}^{-1}\boldsymbol{r}\boldsymbol{r}^\top\boldsymbol{\Sigma}^{-1} - \boldsymbol{\Sigma}^{-1}\big).

Summing over data x1,…,xN\boldsymbol{x}_1, \dots, \boldsymbol{x}_N and setting both to zero gives the sample mean and the (biased) sample covariance.

Proof

Step 1. Write the log-density. ℓ=−D2log⁡2π−12log⁡∣Σ∣−12r⊤Σ−1r\ell = -\tfrac D2\log 2\pi - \tfrac12\log\lvert\boldsymbol{\Sigma}\rvert - \tfrac12\boldsymbol{r}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{r}.

Step 2. Gradient in μ\boldsymbol{\mu}. ∇μ(−12r⊤Σ−1r)=Σ−1r\nabla_{\boldsymbol{\mu}}\big(-\tfrac12\boldsymbol{r}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{r}\big) = \boldsymbol{\Sigma}^{-1}\boldsymbol{r}, since ∇r(r⊤Ar)=2Ar\nabla_{\boldsymbol{r}}(\boldsymbol{r}^\top\mathbf{A}\boldsymbol{r}) = 2\mathbf{A}\boldsymbol{r} for symmetric A\mathbf{A} and ∂r/∂μ=−I\partial\boldsymbol{r}/\partial\boldsymbol{\mu} = -\mathbf{I}.

Step 3. Gradient in Σ\boldsymbol{\Sigma}, entry by entry. Let Eij\mathbf{E}_{ij} be the matrix with a single 11 in position (i,j)(i, j). By GP Lemma 7, ∂log⁡∣Σ∣/∂Σij=tr⁡(Σ−1Eij)=(Σ−1)ji\partial\log\lvert\boldsymbol{\Sigma}\rvert/\partial\Sigma_{ij} = \operatorname{tr}(\boldsymbol{\Sigma}^{-1}\mathbf{E}_{ij}) = (\boldsymbol{\Sigma}^{-1})_{ji} and

∂ r⊤Σ−1r∂Σij=−r⊤Σ−1EijΣ−1r=−(Σ−1r)i(Σ−1r)j.\frac{\partial\,\boldsymbol{r}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{r}}{\partial\Sigma_{ij}} = -\boldsymbol{r}^\top\boldsymbol{\Sigma}^{-1}\mathbf{E}_{ij}\boldsymbol{\Sigma}^{-1}\boldsymbol{r} = -(\boldsymbol{\Sigma}^{-1}\boldsymbol{r})_i(\boldsymbol{\Sigma}^{-1}\boldsymbol{r})_j .

Collecting entries (and Σ−1\boldsymbol{\Sigma}^{-1} symmetric) gives the stated matrix.

Step 4. Maximum likelihood. Summing Step 2 over data, Σ−1∑n(xn−μ)=0\boldsymbol{\Sigma}^{-1}\sum_n(\boldsymbol{x}_n - \boldsymbol{\mu}) = \mathbf{0} gives μ^=1N∑nxn\hat{\boldsymbol{\mu}} = \tfrac1N\sum_n\boldsymbol{x}_n. Summing Step 3 and multiplying by Σ\boldsymbol{\Sigma} on both sides, ∑nrnrn⊤=NΣ\sum_n\boldsymbol{r}_n\boldsymbol{r}_n^\top = N\boldsymbol{\Sigma}, so Σ^=1N∑n(xn−μ^)(xn−μ^)⊤\hat{\boldsymbol{\Sigma}} = \tfrac1N\sum_n(\boldsymbol{x}_n - \hat{\boldsymbol{\mu}})(\boldsymbol{x}_n - \hat{\boldsymbol{\mu}})^\top.

10. Probabilistic moves

Splitting uncertainty: the law of total variance

Proposition 10 (Law of total covariance)

For random vectors x\boldsymbol{x} and y\boldsymbol{y} with m(y)=E[x∣y]\boldsymbol{m}(\boldsymbol{y}) = \mathbb{E}[\boldsymbol{x} \mid \boldsymbol{y}],

Cov⁡[x]=E[Cov⁡[x∣y]]⏟average spread+Cov⁡[m(y)]⏟spread of the averages.\operatorname{Cov}[\boldsymbol{x}] = \underbrace{\mathbb{E}\big[\operatorname{Cov}[\boldsymbol{x} \mid \boldsymbol{y}]\big]}_{\text{average spread}} + \underbrace{\operatorname{Cov}\big[\boldsymbol{m}(\boldsymbol{y})\big]}_{\text{spread of the averages}} .
Proof

Step 1. Second moment by the tower rule. E[xx⊤]=E[E[xx⊤∣y]]=E[Cov⁡[x∣y]+m(y)m(y)⊤]\mathbb{E}[\boldsymbol{x}\boldsymbol{x}^\top] = \mathbb{E}\big[\mathbb{E}[\boldsymbol{x}\boldsymbol{x}^\top \mid \boldsymbol{y}]\big] = \mathbb{E}\big[\operatorname{Cov}[\boldsymbol{x} \mid \boldsymbol{y}] + \boldsymbol{m}(\boldsymbol{y})\boldsymbol{m}(\boldsymbol{y})^\top\big], using Proposition 2, Step 1 conditionally on y\boldsymbol{y}.

Step 2. Mean by the tower rule. E[x]=E[m(y)]\mathbb{E}[\boldsymbol{x}] = \mathbb{E}[\boldsymbol{m}(\boldsymbol{y})].

Step 3. Subtract. Cov⁡[x]=E[xx⊤]−E[x]E[x]⊤=E[Cov⁡[x∣y]]+(E[mm⊤]−E[m]E[m]⊤)\operatorname{Cov}[\boldsymbol{x}] = \mathbb{E}[\boldsymbol{x}\boldsymbol{x}^\top] - \mathbb{E}[\boldsymbol{x}]\mathbb{E}[\boldsymbol{x}]^\top = \mathbb{E}[\operatorname{Cov}[\boldsymbol{x} \mid \boldsymbol{y}]] + \big(\mathbb{E}[\boldsymbol{m}\boldsymbol{m}^\top] - \mathbb{E}[\boldsymbol{m}]\mathbb{E}[\boldsymbol{m}]^\top\big), and the bracket is Cov⁡[m(y)]\operatorname{Cov}[\boldsymbol{m}(\boldsymbol{y})].

Proposition 11 (Moment matching is the best Gaussian fit in KL(p ‖ q))

For any distribution pp with mean μp\boldsymbol{\mu}_p and covariance Σp\boldsymbol{\Sigma}_p, the Gaussian q=N(m,S)q = \mathcal{N}(\boldsymbol{m}, \mathbf{S}) minimising KL⁡(p ∥ q)\operatorname{KL}(p \,\|\, q) has m=μp\boldsymbol{m} = \boldsymbol{\mu}_p and S=Σp\mathbf{S} = \boldsymbol{\Sigma}_p.

Proof

Step 1. Drop what does not depend on qq. KL⁡(p∥q)=Ep[log⁡p]−Ep[log⁡q]\operatorname{KL}(p \| q) = \mathbb{E}_p[\log p] - \mathbb{E}_p[\log q], so minimise −Ep[log⁡q]=12log⁡∣S∣+12Ep[(x−m)⊤S−1(x−m)]+const-\mathbb{E}_p[\log q] = \tfrac12\log\lvert\mathbf{S}\rvert + \tfrac12\mathbb{E}_p\big[(\boldsymbol{x} - \boldsymbol{m})^\top\mathbf{S}^{-1}(\boldsymbol{x} - \boldsymbol{m})\big] + \text{const}.

Step 2. Expectation of the quadratic form (Proposition 2): tr⁡(S−1Σp)+(μp−m)⊤S−1(μp−m)\operatorname{tr}(\mathbf{S}^{-1}\boldsymbol{\Sigma}_p) + (\boldsymbol{\mu}_p - \boldsymbol{m})^\top\mathbf{S}^{-1}(\boldsymbol{\mu}_p - \boldsymbol{m}).

Step 3. The mean. Only the second term involves m\boldsymbol{m}; it is non-negative and zero at m=μp\boldsymbol{m} = \boldsymbol{\mu}_p.

Step 4. The covariance. With m=μp\boldsymbol{m} = \boldsymbol{\mu}_p, the objective is 12log⁡∣S∣+12tr⁡(S−1Σp)\tfrac12\log\lvert\mathbf{S}\rvert + \tfrac12\operatorname{tr}(\mathbf{S}^{-1}\boldsymbol{\Sigma}_p). Its gradient in S\mathbf{S} (Proposition 9, Step 3) is 12S−1−12S−1ΣpS−1\tfrac12\mathbf{S}^{-1} - \tfrac12\mathbf{S}^{-1}\boldsymbol{\Sigma}_p\mathbf{S}^{-1}, which vanishes at S=Σp\mathbf{S} = \boldsymbol{\Sigma}_p.

Example 2 (Ensembles: aleatoric plus epistemic)

A deep ensemble of MM networks predicts N(μj,σj2)\mathcal{N}(\mu_j, \sigma_j^2), j=1,…,Mj = 1, \dots, M, each with weight 1/M1/M.

Step 1. Let JJ be a uniformly random member. By Proposition 10 with y=J\boldsymbol{y} = J: Var⁡[y]=1M∑jσj2+1M∑j(μj−μˉ)2\operatorname{Var}[y] = \tfrac1M\sum_j\sigma_j^2 + \tfrac1M\sum_j(\mu_j - \bar\mu)^2, with μˉ=1M∑jμj\bar\mu = \tfrac1M\sum_j\mu_j.

Step 2. The first term is the noise each member believes is in the data (aleatoric). The second is the members’ disagreement (epistemic): it shrinks where data constrain the model and grows away from them.

Step 3. By Proposition 11, N(μˉ,Var⁡[y])\mathcal{N}(\bar\mu, \operatorname{Var}[y]) is the best single Gaussian summary of the ensemble in KL⁡(p∥q)\operatorname{KL}(p \| q). Moment matching is also the core step of expectation propagation, assumed-density filtering, and PILCO’s uncertainty propagation.

Note (The other direction of KL behaves differently)

Minimising KL⁡(q∥p)\operatorname{KL}(q \| p) instead, as variational inference does, penalises qq for putting mass where pp has none. Fitted to a two-mode pp, it locks onto one mode and underestimates the spread, while KL⁡(p∥q)\operatorname{KL}(p \| q) (Proposition 11) covers both modes and overestimates it. Knowing which direction an algorithm minimises predicts how its uncertainty will be wrong.

The evidence lower bound in three lines

Proposition 12 (ELBO)

For any density q(z)q(\boldsymbol{z}),   log⁡p(y)=Eq[log⁡p(y,z)−log⁡q(z)]⏟ELBO(q)+KL⁡(q(z) ∥ p(z∣y))  ≥  ELBO(q)\;\log p(\boldsymbol{y}) = \underbrace{\mathbb{E}_q\big[\log p(\boldsymbol{y}, \boldsymbol{z}) - \log q(\boldsymbol{z})\big]}_{\text{ELBO}(q)} + \operatorname{KL}\big(q(\boldsymbol{z}) \,\|\, p(\boldsymbol{z} \mid \boldsymbol{y})\big) \;\ge\; \text{ELBO}(q).

Proof

Step 1. log⁡p(y)\log p(\boldsymbol{y}) does not depend on z\boldsymbol{z}, so it equals its own average under qq: log⁡p(y)=Eq[log⁡p(y)]\log p(\boldsymbol{y}) = \mathbb{E}_q[\log p(\boldsymbol{y})].

Step 2. Bayes’ rule, p(y)=p(y,z)/p(z∣y)p(\boldsymbol{y}) = p(\boldsymbol{y}, \boldsymbol{z})/p(\boldsymbol{z} \mid \boldsymbol{y}), and multiplying and dividing by q(z)q(\boldsymbol{z}): log⁡p(y)=Eq[log⁡p(y,z)q(z)+log⁡q(z)p(z∣y)]\log p(\boldsymbol{y}) = \mathbb{E}_q\Big[\log\dfrac{p(\boldsymbol{y}, \boldsymbol{z})}{q(\boldsymbol{z})} + \log\dfrac{q(\boldsymbol{z})}{p(\boldsymbol{z} \mid \boldsymbol{y})}\Big].

Step 3. The second expectation is a KL divergence, which is non-negative.

So maximising the ELBO over qq is the same as minimising the KL to the true posterior, without ever computing that posterior. With Gaussian qq, the KL to a Gaussian prior is Proposition 6 in closed form and the expected log-likelihood has Bonnet and Price gradients (Proposition 7): this is how VAEs, sparse GPs and Bayesian neural networks are trained.

Note (log-sum-exp: never exponentiate first)

Mixtures, softmaxes and importance weights need log⁡∑ieai\log\sum_ie^{a_i} with aia_i of size ±1000\pm 1000, where eaie^{a_i} overflows or underflows. With m=max⁡iaim = \max_ia_i,   log⁡∑ieai=m+log⁡∑ieai−m\;\log\sum_ie^{a_i} = m + \log\sum_ie^{a_i - m}: every exponent is now at most 00 and the largest term is exactly 11. Work with log-densities throughout and exponentiate only at the very end, if at all.

Fisher information: the geometry inside the KL

Proposition 13 (For Gaussians, the KL is exactly the Fisher metric)

For fixed Σ\boldsymbol{\Sigma}:   KL⁡(N(μ+δ,Σ) ∥ N(μ,Σ))=12δ⊤Σ−1δ\;\operatorname{KL}\big(\mathcal{N}(\boldsymbol{\mu} + \boldsymbol{\delta}, \boldsymbol{\Sigma}) \,\|\, \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma})\big) = \tfrac12\boldsymbol{\delta}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{\delta}, and the Fisher information of the mean is F=E[∇μlog⁡p ∇μlog⁡p⊤]=Σ−1\mathbf{F} = \mathbb{E}\big[\nabla_{\boldsymbol{\mu}}\log p\,\nabla_{\boldsymbol{\mu}}\log p^\top\big] = \boldsymbol{\Sigma}^{-1}.

Proof

Step 1. KL. In Proposition 6 with Σ0=Σ1=Σ\boldsymbol{\Sigma}_0 = \boldsymbol{\Sigma}_1 = \boldsymbol{\Sigma}: the trace is tr⁡(I)=D\operatorname{tr}(\mathbf{I}) = D, cancelling −D-D, and the log-determinant ratio is log⁡1=0\log 1 = 0. Only the Mahalanobis term remains.

Step 2. Fisher. By Proposition 9, ∇μlog⁡p=Σ−1r\nabla_{\boldsymbol{\mu}}\log p = \boldsymbol{\Sigma}^{-1}\boldsymbol{r} with r=x−μ\boldsymbol{r} = \boldsymbol{x} - \boldsymbol{\mu}, so F=Σ−1E[rr⊤]Σ−1=Σ−1ΣΣ−1=Σ−1\mathbf{F} = \boldsymbol{\Sigma}^{-1}\mathbb{E}[\boldsymbol{r}\boldsymbol{r}^\top]\boldsymbol{\Sigma}^{-1} = \boldsymbol{\Sigma}^{-1}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^{-1} = \boldsymbol{\Sigma}^{-1}.

In general the KL between nearby distributions is 12δ⊤Fδ\tfrac12\boldsymbol{\delta}^\top\mathbf{F}\boldsymbol{\delta} to second order; for a Gaussian mean it is exact. The natural gradient F−1∇\mathbf{F}^{-1}\nabla (Amari, 1998) here is Σ∇\boldsymbol{\Sigma}\nabla: it measures steps in units of the distribution’s own spread, so it does not care how the parameters happen to be scaled.

11. Gaussians in reinforcement learning

Continuous-control policies are almost always Gaussian, πθ(a∣s)=N(a∣μθ(s),Σ)\pi_{\boldsymbol{\theta}}(\boldsymbol{a} \mid \boldsymbol{s}) = \mathcal{N}\big(\boldsymbol{a} \mid \boldsymbol{\mu}_{\boldsymbol{\theta}}(\boldsymbol{s}), \boldsymbol{\Sigma}\big), and the standard algorithms are the identities above wearing different names. When an RL derivation gets messy, it is usually one of the following.

Proposition 14 (The score of a Gaussian policy, and why baselines are free)

With Jθ(s)=∂μθ(s)/∂θ\mathbf{J}_{\boldsymbol{\theta}}(\boldsymbol{s}) = \partial\boldsymbol{\mu}_{\boldsymbol{\theta}}(\boldsymbol{s})/\partial\boldsymbol{\theta},

∇θlog⁡πθ(a∣s)=Jθ(s)⊤Σ−1(a−μθ(s)),Ea∼π[∇θlog⁡πθ(a∣s)]=0.\nabla_{\boldsymbol{\theta}}\log\pi_{\boldsymbol{\theta}}(\boldsymbol{a} \mid \boldsymbol{s}) = \mathbf{J}_{\boldsymbol{\theta}}(\boldsymbol{s})^\top\boldsymbol{\Sigma}^{-1}\big(\boldsymbol{a} - \boldsymbol{\mu}_{\boldsymbol{\theta}}(\boldsymbol{s})\big), \qquad \mathbb{E}_{\boldsymbol{a}\sim\pi}\big[\nabla_{\boldsymbol{\theta}}\log\pi_{\boldsymbol{\theta}}(\boldsymbol{a} \mid \boldsymbol{s})\big] = \mathbf{0} .

Hence subtracting any baseline b(s)b(\boldsymbol{s}) from the return leaves the policy gradient E[∇log⁡π  (Q(s,a)−b(s))]\mathbb{E}\big[\nabla\log\pi\;(Q(\boldsymbol{s}, \boldsymbol{a}) - b(\boldsymbol{s}))\big] unchanged.

Proof

Step 1. Log-density. log⁡π=−12(a−μ)⊤Σ−1(a−μ)+(terms without θ)\log\pi = -\tfrac12(\boldsymbol{a} - \boldsymbol{\mu})^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{a} - \boldsymbol{\mu}) + (\text{terms without } \boldsymbol{\theta}).

Step 2. Chain rule. By Proposition 9, ∇μlog⁡π=Σ−1(a−μ)\nabla_{\boldsymbol{\mu}}\log\pi = \boldsymbol{\Sigma}^{-1}(\boldsymbol{a} - \boldsymbol{\mu}), and ∇θ=J⊤∇μ\nabla_{\boldsymbol{\theta}} = \mathbf{J}^\top\nabla_{\boldsymbol{\mu}}.

Step 3. Zero mean. Under π\pi, E[a−μ]=0\mathbb{E}[\boldsymbol{a} - \boldsymbol{\mu}] = \mathbf{0}, and J⊤Σ−1\mathbf{J}^\top\boldsymbol{\Sigma}^{-1} does not depend on a\boldsymbol{a}.

Step 4. The baseline term vanishes. Ea[∇log⁡π  b(s)]=b(s) Ea[∇log⁡π]=0\mathbb{E}_{\boldsymbol{a}}\big[\nabla\log\pi\;b(\boldsymbol{s})\big] = b(\boldsymbol{s})\,\mathbb{E}_{\boldsymbol{a}}[\nabla\log\pi] = \mathbf{0} by Step 3.

Remark (Why REINFORCE gets noisier as the policy converges, and the fix)

Step 1. With Σ=σ2I\boldsymbol{\Sigma} = \sigma^2\mathbf{I} write a=μ+σε\boldsymbol{a} = \boldsymbol{\mu} + \sigma\boldsymbol{\varepsilon}, ε∼N(0,I)\boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}). The score of Proposition 14 is J⊤ε/σ\mathbf{J}^\top\boldsymbol{\varepsilon}/\sigma: its size grows like 1/σ1/\sigma, its variance like 1/σ21/\sigma^2.

Step 2. As training shrinks the exploration noise σ\sigma, the score-function gradient estimate becomes ever noisier.

Step 3. Reparameterise instead (Bonnet, Proposition 7): ∇θE[Q(s,μθ(s)+σε)]=E[J⊤∇aQ(s,a)]\nabla_{\boldsymbol{\theta}}\mathbb{E}\big[Q(\boldsymbol{s}, \boldsymbol{\mu}_{\boldsymbol{\theta}}(\boldsymbol{s}) + \sigma\boldsymbol{\varepsilon})\big] = \mathbb{E}\big[\mathbf{J}^\top\nabla_{\boldsymbol{a}}Q(\boldsymbol{s}, \boldsymbol{a})\big]. No 1/σ1/\sigma appears, and at σ→0\sigma \to 0 it becomes the deterministic policy gradient (Silver et al., 2014). This is why DDPG, TD3 and SAC differentiate through a learned QQ.

Proposition 15 (Trust regions for Gaussian policies are Mahalanobis balls)

For πold=N(μ0,Σ)\pi_{\text{old}} = \mathcal{N}(\boldsymbol{\mu}_0, \boldsymbol{\Sigma}) and πnew=N(μ1,Σ)\pi_{\text{new}} = \mathcal{N}(\boldsymbol{\mu}_1, \boldsymbol{\Sigma}),   KL⁡(πold∥πnew)=12(μ1−μ0)⊤Σ−1(μ1−μ0)\;\operatorname{KL}(\pi_{\text{old}} \| \pi_{\text{new}}) = \tfrac12(\boldsymbol{\mu}_1 - \boldsymbol{\mu}_0)^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{\mu}_1 - \boldsymbol{\mu}_0); for diagonal Σ\boldsymbol{\Sigma},   ∑i(Δμi)2/(2σi2)\;\sum_i(\Delta\mu_i)^2/(2\sigma_i^2).

Proof

Step 1. This is Proposition 13 with δ=μ1−μ0\boldsymbol{\delta} = \boldsymbol{\mu}_1 - \boldsymbol{\mu}_0.

Step 2. For diagonal Σ\boldsymbol{\Sigma}, Σ−1=diag⁡(1/σi2)\boldsymbol{\Sigma}^{-1} = \operatorname{diag}(1/\sigma_i^2) and the quadratic form is a sum over action dimensions.

A TRPO or PPO constraint KL⁡≤δ\operatorname{KL} \le \delta therefore lets the mean of action dimension ii move by about σi2δ\sigma_i\sqrt{2\delta}: dimensions the policy is still exploring may change a lot, settled ones only a little. Natural policy gradients are Proposition 13 applied to the whole policy.

Note (Entropy bonuses act only on the covariance; and the tanh correction)

Step 1. By Proposition 5, H[π(⋅∣s)]=12log⁡((2πe)D∣Σ∣)H[\pi(\cdot \mid \boldsymbol{s})] = \tfrac12\log\big((2\pi e)^D\lvert\boldsymbol{\Sigma}\rvert\big) does not involve the mean: a maximum-entropy bonus (SAC) pushes only on Σ\boldsymbol{\Sigma}.

Step 2. SAC bounds actions by squashing, a=tanh⁡(u)\boldsymbol{a} = \tanh(\boldsymbol{u}) elementwise with u∼N(μ,diag⁡σ2)\boldsymbol{u} \sim \mathcal{N}(\boldsymbol{\mu}, \operatorname{diag}\boldsymbol{\sigma}^2). The change-of-variables formula for an invertible map gives pa(a)=pu(u) ∣det⁡∂a/∂u∣−1p_{\boldsymbol{a}}(\boldsymbol{a}) = p_{\boldsymbol{u}}(\boldsymbol{u})\,\lvert\det\partial\boldsymbol{a}/\partial\boldsymbol{u}\rvert^{-1}.

Step 3. The Jacobian is diagonal with entries 1−tanh⁡2(ui)1 - \tanh^2(u_i), so log⁡π(a)=log⁡N(u∣μ,diag⁡σ2)−∑ilog⁡(1−tanh⁡2(ui))\log\pi(\boldsymbol{a}) = \log\mathcal{N}(\boldsymbol{u} \mid \boldsymbol{\mu}, \operatorname{diag}\boldsymbol{\sigma}^2) - \sum_i\log\big(1 - \tanh^2(u_i)\big). Forgetting the second term is a classic bug: the entropy of the squashed policy is then wrong, increasingly so as actions saturate.

Proposition 16 (Linear-quadratic control: noise does not change the best action)

For dynamics x′=Ax+Bu+w\boldsymbol{x}' = \mathbf{A}\boldsymbol{x} + \mathbf{B}\boldsymbol{u} + \boldsymbol{w} with w∼N(0,W)\boldsymbol{w} \sim \mathcal{N}(\mathbf{0}, \mathbf{W}) independent of x,u\boldsymbol{x}, \boldsymbol{u}, and a quadratic cost-to-go V(x′)=x′⊤Px′V(\boldsymbol{x}') = \boldsymbol{x}'^\top\mathbf{P}\boldsymbol{x}',

E[V(x′)]=(Ax+Bu)⊤P(Ax+Bu)+tr⁡(PW),\mathbb{E}[V(\boldsymbol{x}')] = (\mathbf{A}\boldsymbol{x} + \mathbf{B}\boldsymbol{u})^\top\mathbf{P}(\mathbf{A}\boldsymbol{x} + \mathbf{B}\boldsymbol{u}) + \operatorname{tr}(\mathbf{P}\mathbf{W}),

so the action minimising u⊤Ru+E[V(x′)]\boldsymbol{u}^\top\mathbf{R}\boldsymbol{u} + \mathbb{E}[V(\boldsymbol{x}')] is the same as for W=0\mathbf{W} = \mathbf{0}.

Proof

Step 1. Given x\boldsymbol{x} and u\boldsymbol{u}, x′\boldsymbol{x}' has mean Ax+Bu\mathbf{A}\boldsymbol{x} + \mathbf{B}\boldsymbol{u} and covariance W\mathbf{W}.

Step 2. Proposition 2 with A→P\mathbf{A} \to \mathbf{P} gives the formula.

Step 3. The noise enters only through tr⁡(PW)\operatorname{tr}(\mathbf{P}\mathbf{W}), which does not depend on u\boldsymbol{u}; its gradient in u\boldsymbol{u} is zero, so the minimiser is unchanged. This is certainty equivalence: plan as if there were no noise, and pay a constant for it.

Note (The Kalman filter is two identities)

Predict. xt+1=Axt+w\boldsymbol{x}_{t+1} = \mathbf{A}\boldsymbol{x}_t + \boldsymbol{w}: an affine map plus independent noise (GP Lemmas 2 and 3), so N(μ,Σ)↦N(Aμ,AΣA⊤+W)\mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}) \mapsto \mathcal{N}(\mathbf{A}\boldsymbol{\mu}, \mathbf{A}\boldsymbol{\Sigma}\mathbf{A}^\top + \mathbf{W}). Covariances add: uncertainty grows.

Update. Observe y=Cx+v\boldsymbol{y} = \mathbf{C}\boldsymbol{x} + \boldsymbol{v}: the linear-Gaussian model, GP Lemma 8. Its moment form is the Kalman gain ΣC⊤(CΣC⊤+V)−1\boldsymbol{\Sigma}\mathbf{C}^\top(\mathbf{C}\boldsymbol{\Sigma}\mathbf{C}^\top + \mathbf{V})^{-1}; its information form is the information filter, where precisions add and uncertainty shrinks. For a scalar observation, Sherman–Morrison (Lemma 2) makes the update O(D2)O(D^2).

Questions

Question 1

In the figure, why is the slice never wider than the shadow?

Answer

The slice’s variance is 1/P22=Σ22−Σ21Σ11−1Σ121/\mathbf{P}_{22} = \Sigma_{22} - \Sigma_{21}\Sigma_{11}^{-1}\Sigma_{12} (Remark after Proposition 4), which is Σ22\Sigma_{22} minus a non-negative quantity. Observing x1x_1 can only remove uncertainty about x2x_2; for Gaussians it never adds any, whatever value aa is observed.

Question 2

KL⁡(p0∥p1)≠KL⁡(p1∥p0)\operatorname{KL}(p_0 \| p_1) \ne \operatorname{KL}(p_1 \| p_0). Which term of Proposition 6 makes the asymmetry obvious?

Answer

The Mahalanobis term measures the distance between the means in units of Σ1\boldsymbol{\Sigma}_1. Swapping the arguments measures it in units of Σ0\boldsymbol{\Sigma}_0, which differs whenever the covariances do.

Question 3

How many Hutchinson probes give a trace estimate with relative standard deviation 1%1\% for A=I1000\mathbf{A} = \mathbf{I}_{1000}, and for a rank-one A=vv⊤\mathbf{A} = \boldsymbol{v}\boldsymbol{v}^\top?

Answer

With SS probes the variance is 2∥A∥F2/S2\lVert\mathbf{A}\rVert_F^2/S. For I1000\mathbf{I}_{1000}: tr⁡=1000\operatorname{tr} = 1000, ∥A∥F2=1000\lVert\mathbf{A}\rVert_F^2 = 1000, so the relative s.d. is 2/(1000S)\sqrt{2/(1000S)}, and S=20S = 20 suffices. For vv⊤\boldsymbol{v}\boldsymbol{v}^\top: tr⁡=∥v∥2\operatorname{tr} = \lVert\boldsymbol{v}\rVert^2 and ∥A∥F2=∥v∥4\lVert\mathbf{A}\rVert_F^2 = \lVert\boldsymbol{v}\rVert^4, so the relative s.d. is 2/S\sqrt{2/S} and S=20 000S = 20\,000. Hutchinson is good for “spread out” spectra and poor for low-rank ones.

Question 4

Your Gaussian-policy gradient estimates get noisier as training converges. Why, and what are two fixes?

Answer

The score is J⊤ε/σ\mathbf{J}^\top\boldsymbol{\varepsilon}/\sigma (Remark after Proposition 14), so its variance grows like 1/σ21/\sigma^2 as exploration shrinks. Fixes: use the reparameterised gradient through a learned QQ (Bonnet), subtract a baseline or use advantages (Proposition 14), and keep σ\sigma from collapsing (an entropy bonus).

Question 5

You add one data point to a GP with N=10 000N = 10\,000 observations. What does it cost to update the posterior, and why?

Answer

O(N2)O(N^2), not O(N3)O(N^3). The new Gram matrix is the old one bordered by one row and column, so its Cholesky factor gains one row, computed by a triangular solve (Lemma 3 with a 1×11\times1 block D\mathbf{D}: the new diagonal entry is the square root of the Schur complement, the predictive variance of the new point).

References

  • C. E. Rasmussen, C. K. I. Williams. Gaussian Processes for Machine Learning. MIT Press, 2006. Appendix A.
  • K. B. Petersen, M. S. Pedersen. The Matrix Cookbook. 2012.
  • C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006. Section 2.3.
  • A. K. Gupta, D. K. Nagar. Matrix Variate Distributions. Chapman & Hall/CRC, 1999.
  • L. Isserlis. On a formula for the product-moment coefficient of any order of a normal frequency distribution in any number of variables. Biometrika, 1918.
  • C. M. Stein. Estimation of the mean of a multivariate normal distribution. Annals of Statistics, 1981.
  • M. Opper, C. Archambeau. The variational Gaussian approximation revisited. Neural Computation, 2009. (Bonnet’s and Price’s theorems.)
  • M. F. Hutchinson. A stochastic estimator of the trace of the influence matrix for Laplacian smoothing splines. Communications in Statistics, 1989.
  • G. H. Golub, C. F. Van Loan. Matrix Computations. Johns Hopkins University Press, 4th ed., 2013.
  • S. Amari. Natural gradient works efficiently in learning. Neural Computation, 1998.
  • B. Lakshminarayanan, A. Pritzel, C. Blundell. Simple and scalable predictive uncertainty estimation using deep ensembles. NeurIPS 2017.
  • R. J. Williams. Simple statistical gradient-following algorithms for connectionist reinforcement learning. Machine Learning, 1992.
  • D. Silver, G. Lever, N. Heess, T. Degris, D. Wierstra, M. Riedmiller. Deterministic policy gradient algorithms. ICML 2014.
  • J. Schulman, S. Levine, P. Abbeel, M. Jordan, P. Moritz. Trust region policy optimization. ICML 2015.
  • T. Haarnoja, A. Zhou, P. Abbeel, S. Levine. Soft actor-critic: off-policy maximum entropy deep reinforcement learning with a stochastic actor. ICML 2018.
  • R. E. Kalman. A new approach to linear filtering and prediction problems. Journal of Basic Engineering, 1960.