Lecture notes · Bayesian and Probabilistic Machine Learning · Toolkit

Mathematical Toolkit

The same dozen identities carry most derivations in the course notes on Bishop's book (Chapters 1 to 14): Jensen's inequality, the Kullback–Leibler divergence, Lagrange multipliers, matrix derivatives, the Woodbury identity, completing the square, and the expectations of the Gamma, Dirichlet and Wishart distributions. Each is stated and proved once here, and the chapter notes link to it.

Ch. 1 Introduction Ch. 2 Probability distributions Ch. 3 Linear regression Ch. 4 Linear classification Ch. 5 Neural networks Ch. 6 Kernel methods Ch. 7 Sparse kernel machines Ch. 8 Graphical models Ch. 9 Mixture models and EM Ch. 10 Approximate inference Ch. 11 Sampling methods Ch. 12 Continuous latent variables Ch. 13 Sequential data Ch. 14 Combining models

How to use this page

Each entry gives a statement (with the book's equation number where there is one) and a proof that opens on click. Matrices are real; \(\mathbf{A}^{-\T}\) means \((\mathbf{A}^{-1})^\T\); \(|\mathbf{A}|\) is a determinant; \(\psi(x)\)\({}=\frac{\d}{\d x}\ln\Gamma(x)\) is the digamma function. Vectors are columns.

Proofs start hidden: click any bar to open one.

1. Inequalities and divergences

1.1 Jensen's inequality

Result T.1 (Jensen) (1.115)

If \(f\) is convex and \(X\) is a random vector with finite mean, then \(f(\E[X])\le \E[f(X)]\). If \(f\) is strictly convex, equality holds only when \(X\) is constant with probability one. For a concave \(f\) the inequality reverses; in particular \(\E[\ln X]\le \ln \E[X]\) for a positive random variable.

Proof

A convex function lies above each of its supporting hyperplanes: at the point \(\bm{\mu}=\E[X]\) there is a vector \(\mathbf{c}\) (the gradient if \(f\) is differentiable, a subgradient otherwise) with \(f(\mathbf{x})\)\({}\ge f(\bm{\mu})\)\({}+\mathbf{c}^\T(\mathbf{x}-\bm{\mu})\) for all \(\mathbf{x}\). Replace \(\mathbf{x}\) by \(X\) and take expectations: the linear term has mean zero, so \(\E[f(X)]\ge f(\bm{\mu})\).

If \(f\) is strictly convex the supporting inequality is strict for \(\mathbf{x}\ne\bm{\mu}\), so \(f(X)-f(\bm{\mu})-\mathbf{c}^\T(X-\bm{\mu})\) is a non-negative random variable that is positive whenever \(X\ne\bm{\mu}\). Its mean is zero only if \(P(X\ne\bm\mu)=0\). For concave \(f\), apply the result to \(-f\). ∎

How it is used. For any distribution \(q(\mathbf{Z})\) that is positive wherever \(p(\mathbf{X},\mathbf{Z})\) is,

\[ \ln p(\mathbf{X}) = \ln \sum_{\mathbf{Z}} q(\mathbf{Z})\,\frac{p(\mathbf{X},\mathbf{Z})}{q(\mathbf{Z})} \;\ge\; \sum_{\mathbf{Z}} q(\mathbf{Z})\ln \frac{p(\mathbf{X},\mathbf{Z})}{q(\mathbf{Z})} = \LB(q), \]

the lower bound behind EM (Section 9.4) and variational inference (Section 10.1). The gap is exactly a KL divergence, as the next result shows.

1.2 The Kullback–Leibler divergence

Definition T.1 (1.113)

For densities (or mass functions) \(q\) and \(p\), \(\displaystyle \KL(q\|p)\)\({}=\int q(\mathbf{z})\ln\frac{q(\mathbf{z})}{p(\mathbf{z})}\,\d\mathbf{z}\), with the conventions \(0\ln 0=0\) and \(\KL(q\|p)=+\infty\) if \(q\) puts mass where \(p\) is zero.

Result T.2 (Gibbs' inequality)

\(\KL(q\|p)\ge 0\), with equality if and only if \(q=p\) almost everywhere. In general \(\KL(q\|p)\ne\KL(p\|q)\).

Proof

Use \(\ln t\le t-1\) for \(t>0\), with equality only at \(t=1\). On the set \(S=\{q>0\}\),

\[ -\KL(q\|p)=\int_S q\ln\frac{p}{q}\le \int_S q\Big(\frac{p}{q}-1\Big)=\int_S p-1\le 0 . \]

Equality needs \(p=q\) almost everywhere on \(S\) (equality in \(\ln t\le t-1\)) and \(\int_S p=1\), so \(p\) has no mass outside \(S\); together, \(p=q\) almost everywhere. Asymmetry: for \(q=\N(0,1)\) and \(p=\N(0,4)\), the formula of Result T.16 gives \(\KL(q\|p)\)\({}=\tfrac12(\tfrac14-1+\ln 4)\)\({}\approx0.318\) and \(\KL(p\|q)\)\({}=\tfrac12(4-1-\ln 4)\)\({}\approx0.807\). ∎

Result T.3 (the bound and its gap)

For any \(q(\mathbf{Z})\), \(\ln p(\mathbf{X}) \)\({}= \LB(q) \)\({}+ \KL\big(q(\mathbf{Z})\,\|\,p(\mathbf{Z}\given\mathbf{X})\big)\) with \(\LB(q)\)\({}=\int q\ln\{p(\mathbf{X},\mathbf{Z})/q(\mathbf{Z})\}\,\d\mathbf{Z}\). Hence \(\LB(q)\le\ln p(\mathbf{X})\), with equality exactly when \(q\) is the posterior. This is Bishop's (9.70) and (10.2).

Proof

Write \(p(\mathbf{X},\mathbf{Z})\)\({}=p(\mathbf{Z}\given\mathbf{X})p(\mathbf{X})\) inside the logarithm: \(\LB(q)\)\({}=\int q\ln\frac{p(\mathbf{Z}\given\mathbf{X})}{q(\mathbf{Z})}\)\({}+\int q\ln p(\mathbf{X})\)\({}=-\KL(q\|p(\cdot\given\mathbf{X}))\)\({}+\ln p(\mathbf{X})\), since \(\int q=1\). Rearrange, and apply Result T.2. ∎

2. Optimisation

2.1 Lagrange multipliers

Result T.4 (Lagrange; Appendix E)

Let \(f\) and \(g_1,\dots,g_m\) be continuously differentiable and let \(\mathbf{x}^\star\) maximise or minimise \(f\) subject to \(g_j(\mathbf{x})=0\) for all \(j\). If the gradients \(\nabla g_j(\mathbf{x}^\star)\) are linearly independent, there are numbers \(\lambda_j\) with \(\nabla f(\mathbf{x}^\star)\)\({}+\sum_j\lambda_j\nabla g_j(\mathbf{x}^\star)\)\({}=\mathbf{0}\): \(\mathbf{x}^\star\) is a stationary point of the Lagrangian \(f+\sum_j\lambda_j g_j\).

Proof sketch

Moving from \(\mathbf{x}^\star\) along any direction \(\mathbf{v}\) tangent to the constraint surface (\(\nabla g_j^\T\mathbf{v}=0\) for all \(j\)) is possible to first order by the implicit function theorem, and cannot change \(f\) to first order at an extremum, so \(\nabla f^\T\mathbf{v}=0\). Thus \(\nabla f\) is orthogonal to the tangent space, which is the orthogonal complement of \(\mathrm{span}\{\nabla g_j\}\); hence \(\nabla f\) lies in that span. ∎

Result T.5 (the simplex example used for every mixing proportion)

For \(N_1,\dots,N_K\ge0\) with \(N=\sum_kN_k>0\), the maximum of \(F(\bm\pi)=\sum_k N_k\ln\pi_k\) over the simplex \(\{\pi_k\ge0,\ \sum_k\pi_k=1\}\) is attained at \(\pi_k=N_k/N\), and nowhere else.

Proof

Lagrange route (how the book finds it). Stationarity of \(\sum_kN_k\ln\pi_k+\lambda(\sum_k\pi_k-1)\) gives \(N_k/\pi_k+\lambda=0\), so \(\pi_k=-N_k/\lambda\). Summing over \(k\) and using the constraint gives \(\lambda=-N\), hence \(\pi_k=N_k/N\). The positivity constraints are inactive because \(\ln\pi_k\to-\infty\) as \(\pi_k\to0\) whenever \(N_k>0\).

Why it is the global maximum. With \(\hat\pi_k=N_k/N\), for any \(\bm\pi\) in the simplex, \(F(\bm\pi)-F(\hat{\bm\pi})\)\({}=N\sum_k\hat\pi_k\ln(\pi_k/\hat\pi_k)\)\({}=-N\,\KL(\hat{\bm\pi}\|\bm\pi)\)\({}\le0\) by Result T.2, with equality only at \(\bm\pi=\hat{\bm\pi}\). ∎

2.2 Optimising over distributions

Result T.6 (the variational principle behind mean field)

Let \(\tilde p(\mathbf{z})\ge0\) with \(Z=\int\tilde p<\infty\). Over all densities \(q\), the functional \(F[q]\)\({}=\int q(\mathbf{z})\ln\{\tilde p(\mathbf{z})/q(\mathbf{z})\}\,\d\mathbf{z}\) is maximised by \(q=\tilde p/Z\), and its maximum is \(\ln Z\).

Proof

Without calculus of variations. \(F[q]\)\({}=\int q\ln\frac{\tilde p/Z}{q}+\ln Z\)\({}=\ln Z-\KL(q\|\tilde p/Z)\)\({}\le\ln Z\), with equality if and only if \(q=\tilde p/Z\) (Result T.2).

With a functional derivative (Appendix D). Add a multiplier for normalisation: \(G[q]\)\({}=\int q\ln\tilde p-\int q\ln q\)\({}+\lambda(\int q-1)\). Perturbing \(q\to q+\epsilon\eta\) and differentiating at \(\epsilon=0\) gives \(\int\eta(\mathbf{z})\{\ln\tilde p(\mathbf{z})\)\({}-\ln q(\mathbf{z})\)\({}-1+\lambda\}\d\mathbf{z}\)\({}=0\) for every \(\eta\), so the bracket vanishes: \(q\propto\tilde p\). The first route also proves that this stationary point is the maximum. ∎

In Section 10.1.1 the role of \(\ln\tilde p\) is played by \(\E_{i\ne j}[\ln p(\mathbf{X},\mathbf{Z})]\), which gives the mean-field update (10.9).

2.3 Convex duality

Result T.7 (conjugate functions) (10.127)–(10.133)

For a convex function \(f\) define the conjugate \(g(\eta)=\sup_x\{\eta x-f(x)\}\). Then (a) \(f(x)\ge\eta x-g(\eta)\) for every \(x\) and \(\eta\): each \(\eta\) gives a linear lower bound; (b) if \(f\) is differentiable, the bound is tight at \(x\) for \(\eta=f'(x)\), so \(f(x)=\max_\eta\{\eta x-g(\eta)\}\). For a concave \(f\), \(g(\eta)=\inf_x\{\eta x-f(x)\}\) and \(f(x)=\min_\eta\{\eta x-g(\eta)\}\), giving upper bounds.

Proof

(a) By definition \(g(\eta)\ge\eta x-f(x)\) for each \(x\). (b) If \(f\) is convex and differentiable, \(f(y)\ge f(x)+f'(x)(y-x)\) for all \(y\), that is \(f'(x)y-f(y)\le f'(x)x-f(x)\), so the supremum defining \(g(f'(x))\) is attained at \(y=x\): \(g(f'(x))=f'(x)x-f(x)\), and the bound in (a) is an equality. The concave case follows by applying this to \(-f\).

Example (Figure 10.10). For \(f(x)=e^{-x}\) and \(\eta\lt0\), the supremum of \(\eta x-e^{-x}\) is where \(\eta+e^{-x}=0\), i.e. \(x=-\ln(-\eta)\), which gives \(g(\eta)=\eta-\eta\ln(-\eta)\) (10.130). The tangent at \(x=\xi\) has \(\eta=-e^{-\xi}\) and is the line \(e^{-\xi}(1+\xi-x)\) (10.125). ∎

3. Linear algebra and matrix calculus

3.1 Trace identities and expected quadratic forms

Result T.8 (C.8), (C.9)

(a) \(\Tr(\mathbf{AB})=\Tr(\mathbf{BA})\), so traces are invariant under cyclic permutations: \(\Tr(\mathbf{ABC})\)\({}=\Tr(\mathbf{CAB})\). (b) \(\mathbf{x}^\T\mathbf{A}\mathbf{x}\)\({}=\Tr(\mathbf{A}\mathbf{x}\mathbf{x}^\T)\). (c) If \(\E[\mathbf{x}]=\bm\mu\) and \(\cov[\mathbf{x}]=\bm\Sigma\), then \(\E[\mathbf{x}^\T\mathbf{A}\mathbf{x}]\)\({}=\Tr(\mathbf{A}\bm\Sigma)\)\({}+\bm\mu^\T\mathbf{A}\bm\mu\). (d) For data \(\mathbf{x}_1,\dots,\mathbf{x}_N\) with mean \(\bar{\mathbf{x}}\) and \(\mathbf{S}\)\({}=\frac1N\sum_n(\mathbf{x}_n-\bar{\mathbf{x}})(\mathbf{x}_n-\bar{\mathbf{x}})^\T\),

\[ \sum_{n=1}^N(\mathbf{x}_n-\bm\mu)^\T\bm\Lambda(\mathbf{x}_n-\bm\mu)=N\Tr(\bm\Lambda\mathbf{S})+N(\bar{\mathbf{x}}-\bm\mu)^\T\bm\Lambda(\bar{\mathbf{x}}-\bm\mu). \]
Proof

(a) \(\Tr(\mathbf{AB})\)\({}=\sum_{i,j}A_{ij}B_{ji}\)\({}=\sum_{j,i}B_{ji}A_{ij}\)\({}=\Tr(\mathbf{BA})\). (b) A scalar equals its trace; then use (a) with \(\mathbf{x}^\T\) and \(\mathbf{A}\mathbf{x}\). (c) By (b) and linearity, \(\E[\mathbf{x}^\T\mathbf{A}\mathbf{x}]\)\({}=\Tr(\mathbf{A}\E[\mathbf{x}\mathbf{x}^\T])\) and \(\E[\mathbf{x}\mathbf{x}^\T]\)\({}=\bm\Sigma+\bm\mu\bm\mu^\T\). (d) Write \(\mathbf{x}_n-\bm\mu\)\({}=(\mathbf{x}_n-\bar{\mathbf{x}})\)\({}+(\bar{\mathbf{x}}-\bm\mu)\) and expand; the cross terms sum to zero because \(\sum_n(\mathbf{x}_n-\bar{\mathbf{x}})\)\({}=\mathbf{0}\); the first sum is \(\Tr(\bm\Lambda\sum_n(\mathbf{x}_n-\bar{\mathbf{x}})(\mathbf{x}_n-\bar{\mathbf{x}})^\T)\) by (b). ∎

3.2 Matrix derivatives

For a scalar function \(f(\mathbf{A})\) of a matrix, \(\partial f/\partial\mathbf{A}\) is the matrix with entries \(\partial f/\partial A_{ij}\). The cleanest way to compute it is through differentials: if \(\d f\)\({}=\Tr(\mathbf{G}^\T\d\mathbf{A})\) for all \(\d\mathbf{A}\), then \(\partial f/\partial\mathbf{A}\)\({}=\mathbf{G}\).

Result T.9 (the derivatives used in Chapters 9, 10 and 12) (C.19)–(C.28)

  1. \(\dfrac{\partial}{\partial\mathbf{x}}(\mathbf{a}^\T\mathbf{x})\)\({}=\mathbf{a}\) and \(\dfrac{\partial}{\partial\mathbf{x}}(\mathbf{x}^\T\mathbf{A}\mathbf{x})\)\({}=(\mathbf{A}+\mathbf{A}^\T)\mathbf{x}\).
  2. \(\dfrac{\partial}{\partial\mathbf{A}}\Tr(\mathbf{AB})\)\({}=\mathbf{B}^\T\) and \(\dfrac{\partial}{\partial\mathbf{A}}\Tr(\mathbf{A}\mathbf{B}\mathbf{A}^\T)\)\({}=\mathbf{A}(\mathbf{B}+\mathbf{B}^\T)\).
  3. \(\d(\mathbf{A}^{-1})\)\({}=-\mathbf{A}^{-1}(\d\mathbf{A})\mathbf{A}^{-1}\), hence \(\dfrac{\partial}{\partial\mathbf{A}}\Tr(\mathbf{A}^{-1}\mathbf{B})\)\({}=-(\mathbf{A}^{-1}\mathbf{B}\mathbf{A}^{-1})^\T\) and \(\dfrac{\partial}{\partial\mathbf{A}}\,\mathbf{x}^\T\mathbf{A}^{-1}\mathbf{x}\)\({}=-\mathbf{A}^{-\T}\mathbf{x}\mathbf{x}^\T\mathbf{A}^{-\T}\).
  4. (Jacobi) \(\d|\mathbf{A}|\)\({}=|\mathbf{A}|\Tr(\mathbf{A}^{-1}\d\mathbf{A})\), hence \(\dfrac{\partial}{\partial\mathbf{A}}\ln|\mathbf{A}|\)\({}=\mathbf{A}^{-\T}\).
Proof

1. \(\d(\mathbf{x}^\T\mathbf{A}\mathbf{x})\)\({}=\d\mathbf{x}^\T\mathbf{A}\mathbf{x}\)\({}+\mathbf{x}^\T\mathbf{A}\d\mathbf{x}\)\({}=\mathbf{x}^\T(\mathbf{A}^\T+\mathbf{A})\d\mathbf{x}\).

2. \(\d\Tr(\mathbf{AB})\)\({}=\Tr(\d\mathbf{A}\,\mathbf{B})\)\({}=\Tr(\mathbf{B}\,\d\mathbf{A})\), so \(\mathbf{G}^\T=\mathbf{B}\). For the second, \(\d\Tr(\mathbf{ABA}^\T)\)\({}=\Tr(\d\mathbf{A}\,\mathbf{BA}^\T)\)\({}+\Tr(\mathbf{AB}\,\d\mathbf{A}^\T)\)\({}=\Tr(\mathbf{BA}^\T\d\mathbf{A})\)\({}+\Tr(\mathbf{B}^\T\mathbf{A}^\T\d\mathbf{A})\), using \(\Tr(\mathbf{M}^\T)=\Tr(\mathbf{M})\) on the second term; so \(\mathbf{G}^\T\)\({}=(\mathbf{B}+\mathbf{B}^\T)\mathbf{A}^\T\).

3. Differentiate \(\mathbf{A}\mathbf{A}^{-1}\)\({}=\mathbf{I}\): \(\d\mathbf{A}\,\mathbf{A}^{-1}\)\({}+\mathbf{A}\,\d(\mathbf{A}^{-1})\)\({}=\mathbf{0}\). Then \(\d\Tr(\mathbf{A}^{-1}\mathbf{B})\)\({}=-\Tr(\mathbf{A}^{-1}\d\mathbf{A}\,\mathbf{A}^{-1}\mathbf{B})\)\({}=-\Tr(\mathbf{A}^{-1}\mathbf{B}\mathbf{A}^{-1}\d\mathbf{A})\); the quadratic form is the case \(\mathbf{B}=\mathbf{x}\mathbf{x}^\T\) (Result T.8b).

4. For a small \(\epsilon\) and any \(\mathbf{E}\), \(|\mathbf{A}+\epsilon\mathbf{E}|\)\({}=|\mathbf{A}|\,|\mathbf{I}\)\({}+\epsilon\mathbf{A}^{-1}\mathbf{E}|\), and \(|\mathbf{I}+\epsilon\mathbf{M}|\)\({}=\prod_i(1+\epsilon\mu_i)\)\({}=1+\epsilon\Tr\mathbf{M}+O(\epsilon^2)\) where \(\mu_i\) are the (possibly complex) eigenvalues of \(\mathbf{M}\). So \(\d\ln|\mathbf{A}|\)\({}=\Tr(\mathbf{A}^{-1}\d\mathbf{A})\) and \(\mathbf{G}=\mathbf{A}^{-\T}\). ∎

Symmetric matrices

When the argument is a covariance or precision matrix, only symmetric perturbations are allowed. For a symmetric argument the derivative restricted to symmetric directions is the symmetric part \(\tfrac12(\mathbf{G}+\mathbf{G}^\T)\) of the unconstrained one. In every log likelihood of the course, such as \(-\tfrac N2\ln|\bm\Sigma|\)\({}-\tfrac12\sum_n(\mathbf{x}_n-\bm\mu)^\T\bm\Sigma^{-1}(\mathbf{x}_n-\bm\mu)\), the unconstrained gradient is already symmetric, so setting it to zero gives the correct stationarity condition. That is why the book can treat the entries as free.

Example (the Gaussian covariance, used for (9.19)). With \(\bm\Lambda=\bm\Sigma^{-1}\) the log likelihood is \(\tfrac N2\ln|\bm\Lambda|\)\({}-\tfrac N2\Tr(\bm\Lambda\mathbf{S}_{\bm\mu})\) plus a constant, where \(\mathbf{S}_{\bm\mu}\)\({}=\frac1N\sum_n(\mathbf{x}_n-\bm\mu)(\mathbf{x}_n-\bm\mu)^\T\). By items 2 and 4 its gradient is \(\tfrac N2(\bm\Lambda^{-1}-\mathbf{S}_{\bm\mu})\), which vanishes at \(\bm\Sigma=\mathbf{S}_{\bm\mu}\). The function is concave in \(\bm\Lambda\) (the log-determinant is concave on positive definite matrices and the trace term is linear), so this is the maximum whenever \(\mathbf{S}_{\bm\mu}\) is positive definite.

3.3 The Woodbury identity and the determinant lemma

Result T.10 (C.7), (C.14)

(a) If \(\mathbf{A}\) (\(D\times D\)) and \(\mathbf{D}\) (\(M\times M\)) are invertible, \((\mathbf{A}+\mathbf{BDC})^{-1}\)\({}=\mathbf{A}^{-1}\)\({}-\mathbf{A}^{-1}\mathbf{B}(\mathbf{D}^{-1}+\mathbf{C}\mathbf{A}^{-1}\mathbf{B})^{-1}\mathbf{C}\mathbf{A}^{-1}\). (b) \(|\mathbf{A}+\mathbf{U}\mathbf{V}^\T|\)\({}=|\mathbf{A}|\,|\mathbf{I}_M\)\({}+\mathbf{V}^\T\mathbf{A}^{-1}\mathbf{U}|\) for \(D\times M\) matrices \(\mathbf{U},\mathbf{V}\).

(c) For probabilistic PCA, \(\mathbf{C}\)\({}=\mathbf{W}\mathbf{W}^\T+\sigma^2\mathbf{I}\) and \(\mathbf{M}\)\({}=\mathbf{W}^\T\mathbf{W}+\sigma^2\mathbf{I}\) give \(\mathbf{C}^{-1}\)\({}=\sigma^{-2}(\mathbf{I}-\mathbf{W}\mathbf{M}^{-1}\mathbf{W}^\T)\) (12.41) and \(|\mathbf{C}|\)\({}=\sigma^{2(D-M)}|\mathbf{M}|\): a \(D\times D\) problem becomes an \(M\times M\) one.

Proof

(a) Multiply \(\mathbf{A}+\mathbf{BDC}\) by the right-hand side and write \(\mathbf{K}\)\({}=(\mathbf{D}^{-1}+\mathbf{CA}^{-1}\mathbf{B})^{-1}\):

\[ \begin{aligned} &\mathbf{I}+\mathbf{BDCA}^{-1}-\mathbf{B}\mathbf{K}\mathbf{CA}^{-1}-\mathbf{BDCA}^{-1}\mathbf{B}\mathbf{K}\mathbf{CA}^{-1}\\ &\quad=\mathbf{I}+\mathbf{BDCA}^{-1}-\mathbf{BD}\,(\mathbf{D}^{-1}+\mathbf{CA}^{-1}\mathbf{B})\,\mathbf{K}\mathbf{CA}^{-1}=\mathbf{I}. \end{aligned} \]

(b) Compute the determinant of \(\begin{pmatrix}\mathbf{A}&-\mathbf{U}\\ \mathbf{V}^\T&\mathbf{I}\end{pmatrix}\) in two ways. Eliminating the lower-left block with the first block row gives \(|\mathbf{A}|\,|\mathbf{I}\)\({}+\mathbf{V}^\T\mathbf{A}^{-1}\mathbf{U}|\); eliminating the upper-right block with the second block row gives \(|\mathbf{A}\)\({}+\mathbf{U}\mathbf{V}^\T|\,|\mathbf{I}|\).

(c) Take \(\mathbf{A}=\sigma^2\mathbf{I}\), \(\mathbf{B}=\mathbf{W}\), \(\mathbf{D}=\mathbf{I}\), \(\mathbf{C}=\mathbf{W}^\T\) in (a): \(\mathbf{C}^{-1}\)\({}=\sigma^{-2}\mathbf{I}\)\({}-\sigma^{-4}\mathbf{W}(\mathbf{I}+\sigma^{-2}\mathbf{W}^\T\mathbf{W})^{-1}\mathbf{W}^\T\)\({}=\sigma^{-2}\mathbf{I}\)\({}-\sigma^{-2}\mathbf{W}\mathbf{M}^{-1}\mathbf{W}^\T\). For the determinant use (b) with \(\mathbf{U}=\mathbf{V}=\mathbf{W}\): \(|\mathbf{C}|\)\({}=\sigma^{2D}|\mathbf{I}\)\({}+\sigma^{-2}\mathbf{W}^\T\mathbf{W}|\)\({}=\sigma^{2D}\sigma^{-2M}|\mathbf{M}|\). ∎

3.4 The spectral theorem, Rayleigh quotients and Ky Fan's principle

Result T.11 (used for PCA, Section 12.1)

A real symmetric \(D\times D\) matrix \(\mathbf{S}\) has an orthonormal basis of eigenvectors: \(\mathbf{S}\)\({}=\mathbf{U}\bm\Lambda\mathbf{U}^\T\) with \(\mathbf{U}^\T\mathbf{U}=\mathbf{I}\) and \(\lambda_1\ge\dots\ge\lambda_D\). Then

(a) \(\max_{\|\mathbf{u}\|=1}\mathbf{u}^\T\mathbf{S}\mathbf{u}\)\({}=\lambda_1\), attained at \(\mathbf{u}=\mathbf{u}_1\);

(b) (Ky Fan) for \(1\le M\le D\), \(\max\{\Tr(\mathbf{V}^\T\mathbf{S}\mathbf{V}):\ \mathbf{V}\in\R^{D\times M},\ \mathbf{V}^\T\mathbf{V}\)\({}=\mathbf{I}_M\}\)\({}=\lambda_1+\dots+\lambda_M\), attained by \(\mathbf{V}\)\({}=(\mathbf{u}_1,\dots,\mathbf{u}_M)\mathbf{R}\) for any orthogonal \(\mathbf{R}\); if \(\lambda_M>\lambda_{M+1}\) the column space of a maximiser is unique. Likewise the minimum is the sum of the \(M\) smallest eigenvalues.

Proof

The spectral theorem is standard linear algebra (Appendix C). For (b), write \(p_i\)\({}=\|\mathbf{V}^\T\mathbf{u}_i\|^2\), the squared length of the projection of \(\mathbf{u}_i\) onto the column space of \(\mathbf{V}\). Then

\[ \Tr(\mathbf{V}^\T\mathbf{S}\mathbf{V})=\sum_{i=1}^D\lambda_i\,\Tr(\mathbf{V}^\T\mathbf{u}_i\mathbf{u}_i^\T\mathbf{V})=\sum_{i=1}^D\lambda_i p_i , \] \[ 0\le p_i\le 1,\qquad \sum_{i=1}^Dp_i=\Tr(\mathbf{V}^\T\mathbf{U}\mathbf{U}^\T\mathbf{V})=M . \]

(\(p_i\le1\) because projection does not lengthen a unit vector.) Hence, using \(\lambda_i\ge\lambda_M\) and \(p_i-1\le0\) for \(i\le M\), and \(\lambda_i\le\lambda_M\) for \(i>M\),

\[ \begin{aligned} \sum_i\lambda_ip_i-\sum_{i\le M}\lambda_i&=\sum_{i\le M}\lambda_i(p_i-1)+\sum_{i>M}\lambda_ip_i\\ &\le\lambda_M\sum_{i\le M}(p_i-1)+\lambda_M\sum_{i>M}p_i=\lambda_M\Big(\sum_ip_i-M\Big)=0 . \end{aligned} \]

The value \(\sum_{i\le M}\lambda_i\) is attained when \(p_1=\dots=p_M=1\), i.e. when \(\mathbf{u}_1,\dots,\mathbf{u}_M\) lie in the column space. If \(\lambda_M>\lambda_{M+1}\), equality forces \(p_i=0\) for every \(i>M\), hence \(p_1=\dots=p_M=1\), so the column space must be \(\mathrm{span}\{\mathbf{u}_1,\dots,\mathbf{u}_M\}\). Part (a) is the case \(M=1\). ∎

3.5 The singular value decomposition

Result T.12

Every \(N\times D\) matrix can be written \(\mathbf{X}\)\({}=\mathbf{P}\bm\Sigma\mathbf{Q}^\T\) with orthonormal columns in \(\mathbf{P}\) (\(N\times r\)) and \(\mathbf{Q}\) (\(D\times r\)) and positive singular values \(\sigma_1\ge\dots\ge\sigma_r\), \(r=\mathrm{rank}\,\mathbf{X}\). If the rows of \(\mathbf{X}\) are centred data, the sample covariance is \(\mathbf{S}\)\({}=\frac1N\mathbf{X}^\T\mathbf{X}\)\({}=\mathbf{Q}\frac{\bm\Sigma^2}{N}\mathbf{Q}^\T\): the principal directions are the right singular vectors and \(\lambda_i=\sigma_i^2/N\). Since \(\frac1N\mathbf{X}\mathbf{X}^\T\)\({}=\mathbf{P}\frac{\bm\Sigma^2}{N}\mathbf{P}^\T\) has the same non-zero eigenvalues, an eigenvector \(\mathbf{v}_i\) of the \(N\times N\) matrix gives \(\mathbf{u}_i\)\({}=\mathbf{X}^\T\mathbf{v}_i/(N\lambda_i)^{1/2}\) (12.30), which is how PCA is done when \(N\lt D\).

Proof

\(\mathbf{X}^\T\mathbf{X}\) is symmetric and positive semi-definite; let \(\mathbf{q}_i\) be orthonormal eigenvectors with eigenvalues \(\sigma_i^2>0\) (\(i\le r\)), and put \(\mathbf{p}_i\)\({}=\mathbf{X}\mathbf{q}_i/\sigma_i\). These are orthonormal: \(\mathbf{p}_i^\T\mathbf{p}_j\)\({}=\mathbf{q}_i^\T\mathbf{X}^\T\mathbf{X}\mathbf{q}_j/(\sigma_i\sigma_j)\)\({}=\delta_{ij}\). Then \(\mathbf{X}\mathbf{q}_i\)\({}=\sigma_i\mathbf{p}_i\) and \(\mathbf{X}\) vanishes on the orthogonal complement of the \(\mathbf{q}_i\) (there \(\|\mathbf{X}\mathbf{q}\|^2\)\({}=\mathbf{q}^\T\mathbf{X}^\T\mathbf{X}\mathbf{q}\)\({}=0\)), which gives \(\mathbf{X}\)\({}=\sum_i\sigma_i\mathbf{p}_i\mathbf{q}_i^\T\). The covariance statements follow by substitution. For the last formula, \(\mathbf{X}^\T\mathbf{v}_i\) is an eigenvector of \(\mathbf{X}^\T\mathbf{X}\) with eigenvalue \(N\lambda_i\), and its squared length is \(\mathbf{v}_i^\T\mathbf{X}\mathbf{X}^\T\mathbf{v}_i\)\({}=N\lambda_i\) for a unit \(\mathbf{v}_i\). ∎

4. Gaussian identities

4.1 Completing the square

Result T.13 (reading off a Gaussian) (2.71)

If \(\mathbf{A}\) is symmetric positive definite, then

\[ -\tfrac12\mathbf{x}^\T\mathbf{A}\mathbf{x}+\mathbf{b}^\T\mathbf{x}=-\tfrac12(\mathbf{x}-\mathbf{A}^{-1}\mathbf{b})^\T\mathbf{A}(\mathbf{x}-\mathbf{A}^{-1}\mathbf{b})+\tfrac12\mathbf{b}^\T\mathbf{A}^{-1}\mathbf{b}. \]

Hence a density proportional to \(\exp(-\tfrac12\mathbf{x}^\T\mathbf{A}\mathbf{x}+\mathbf{b}^\T\mathbf{x})\) is \(\N(\mathbf{x}\given\mathbf{A}^{-1}\mathbf{b},\mathbf{A}^{-1})\): the quadratic term gives the precision and the linear term gives precision times mean.

Proof

Expand the right-hand side: \(-\tfrac12\mathbf{x}^\T\mathbf{A}\mathbf{x}\)\({}+\tfrac12\mathbf{x}^\T\mathbf{b}\)\({}+\tfrac12\mathbf{b}^\T\mathbf{x}\)\({}-\tfrac12\mathbf{b}^\T\mathbf{A}^{-1}\mathbf{b}\)\({}+\tfrac12\mathbf{b}^\T\mathbf{A}^{-1}\mathbf{b}\), using \(\mathbf{A}^{-1}\) symmetric; the middle terms are both \(\mathbf{b}^\T\mathbf{x}\)/2. The constant does not depend on \(\mathbf{x}\), so it is absorbed by normalisation. ∎

This one rule derives almost every Gaussian posterior in the course: collect the terms in the variable of interest, read off \(\mathbf{A}\) and \(\mathbf{b}\), and write down \(\N(\mathbf{A}^{-1}\mathbf{b},\mathbf{A}^{-1})\).

4.2 Conditionals, marginals and the linear-Gaussian model

Result T.14 (partitioned Gaussians) (2.73)–(2.98)

Let \(\mathbf{x}\)\({}=(\mathbf{x}_a,\mathbf{x}_b)\)\({}\sim\N(\bm\mu,\bm\Sigma)\) with precision \(\bm\Lambda=\bm\Sigma^{-1}\), all partitioned conformably. Then \(\mathbf{x}_b\sim\N(\bm\mu_b,\bm\Sigma_{bb})\) and

\[ p(\mathbf{x}_a\given\mathbf{x}_b)=\N\big(\mathbf{x}_a\given\bm\mu_a-\bm\Lambda_{aa}^{-1}\bm\Lambda_{ab}(\mathbf{x}_b-\bm\mu_b),\ \bm\Lambda_{aa}^{-1}\big), \] \[ \bm\Lambda_{aa}^{-1}=\bm\Sigma_{aa}-\bm\Sigma_{ab}\bm\Sigma_{bb}^{-1}\bm\Sigma_{ba},\qquad -\bm\Lambda_{aa}^{-1}\bm\Lambda_{ab}=\bm\Sigma_{ab}\bm\Sigma_{bb}^{-1}. \]
Proof

Conditional. As a function of \(\mathbf{x}_a\), \(-\tfrac12(\mathbf{x}-\bm\mu)^\T\bm\Lambda(\mathbf{x}-\bm\mu)\)\({}=-\tfrac12\mathbf{x}_a^\T\bm\Lambda_{aa}\mathbf{x}_a\)\({}+\mathbf{x}_a^\T\{\bm\Lambda_{aa}\bm\mu_a\)\({}-\bm\Lambda_{ab}(\mathbf{x}_b-\bm\mu_b)\}\)\({}+\const\), using \(\bm\Lambda_{ba}=\bm\Lambda_{ab}^\T\). Result T.13 gives the stated mean and covariance.

Block inverse. Multiplying out shows that \(\bm\Lambda\bm\Sigma=\mathbf{I}\) holds blockwise iff \(\bm\Lambda_{aa}\)\({}=(\bm\Sigma_{aa}-\bm\Sigma_{ab}\bm\Sigma_{bb}^{-1}\bm\Sigma_{ba})^{-1}\) and \(\bm\Lambda_{ab}\)\({}=-\bm\Lambda_{aa}\bm\Sigma_{ab}\bm\Sigma_{bb}^{-1}\) (the Schur-complement formula (2.76), (2.77)); substituting gives the covariance form.

Marginal. \(\mathbf{x}_b\) is a linear map of a Gaussian vector, and a linear map of a Gaussian vector is Gaussian (its characteristic function \(\E e^{i\mathbf{t}^\T\mathbf{B}\mathbf{x}}\)\({}=e^{i\mathbf{t}^\T\mathbf{B}\bm\mu-\frac12\mathbf{t}^\T\mathbf{B}\bm\Sigma\mathbf{B}^\T\mathbf{t}}\) is Gaussian), with mean \(\bm\mu_b\) and covariance \(\bm\Sigma_{bb}\). ∎

Result T.15 (the linear-Gaussian model) (2.113)–(2.117), (B.42)–(B.46)

If \(p(\mathbf{x})\)\({}=\N(\mathbf{x}\given\bm\mu,\bm\Lambda^{-1})\) and \(p(\mathbf{y}\given\mathbf{x})\)\({}=\N(\mathbf{y}\given\mathbf{A}\mathbf{x}+\mathbf{b},\mathbf{L}^{-1})\), then

\[ p(\mathbf{y})=\N\big(\mathbf{y}\given\mathbf{A}\bm\mu+\mathbf{b},\ \mathbf{L}^{-1}+\mathbf{A}\bm\Lambda^{-1}\mathbf{A}^\T\big), \] \[ p(\mathbf{x}\given\mathbf{y})=\N\big(\mathbf{x}\given\bm\Sigma\{\mathbf{A}^\T\mathbf{L}(\mathbf{y}-\mathbf{b})+\bm\Lambda\bm\mu\},\ \bm\Sigma\big),\qquad \bm\Sigma=(\bm\Lambda+\mathbf{A}^\T\mathbf{L}\mathbf{A})^{-1}. \]
Proof

Posterior. As a function of \(\mathbf{x}\), \(\ln p(\mathbf{x})\)\({}+\ln p(\mathbf{y}\given\mathbf{x})\)\({}=-\tfrac12\mathbf{x}^\T(\bm\Lambda+\mathbf{A}^\T\mathbf{L}\mathbf{A})\mathbf{x}\)\({}+\mathbf{x}^\T\{\bm\Lambda\bm\mu\)\({}+\mathbf{A}^\T\mathbf{L}(\mathbf{y}-\mathbf{b})\}\)\({}+\const\); apply Result T.13.

Marginal. Write \(\mathbf{y}\)\({}=\mathbf{A}\mathbf{x}+\mathbf{b}+\bm\epsilon\) with \(\bm\epsilon\sim\N(\mathbf{0},\mathbf{L}^{-1})\) independent of \(\mathbf{x}\). The pair \((\mathbf{x},\bm\epsilon)\) is jointly Gaussian, so \(\mathbf{y}\) is Gaussian (a linear map, as above) with mean \(\mathbf{A}\bm\mu+\mathbf{b}\) and covariance \(\mathbf{A}\bm\Lambda^{-1}\mathbf{A}^\T\)\({}+\mathbf{L}^{-1}\) (independence kills the cross-covariance). ∎

Probabilistic PCA (Section 12.2) is the case \(\mathbf{x}\)\({}=\mathbf{z}\sim\N(\mathbf{0},\mathbf{I})\), \(\mathbf{A}=\mathbf{W}\), \(\mathbf{b}=\bm\mu\), \(\mathbf{L}=\sigma^{-2}\mathbf{I}\); Bayesian linear regression (Section 9.3.4) is the case \(\mathbf{x}=\mathbf{w}\), \(\mathbf{A}=\bm\Phi\).

4.3 Moments, entropy, products and KL of Gaussians

Result T.16 (2.59), (2.62), (B.41)

For \(\mathbf{x}\sim\N(\bm\mu,\bm\Sigma)\) in \(D\) dimensions: (a) \(\E[\mathbf{x}]=\bm\mu\), \(\E[\mathbf{x}\mathbf{x}^\T]\)\({}=\bm\Sigma+\bm\mu\bm\mu^\T\); (b) entropy \(\mathrm{H}\)\({}=\tfrac12\ln|\bm\Sigma|+\tfrac D2(1+\ln2\pi)\); (c) \(\KL\big(\N(\bm\mu_0,\bm\Sigma_0)\|\N(\bm\mu_1,\bm\Sigma_1)\big)\)\({}=\tfrac12\big\{\Tr(\bm\Sigma_1^{-1}\bm\Sigma_0)\)\({}+(\bm\mu_1-\bm\mu_0)^\T\bm\Sigma_1^{-1}(\bm\mu_1-\bm\mu_0)\)\({}-D\)\({}+\ln\frac{|\bm\Sigma_1|}{|\bm\Sigma_0|}\big\}\); (d) a product of Gaussian densities in \(\mathbf{x}\) is an unnormalised Gaussian whose precision is the sum of the precisions and whose precision-times-mean is the sum of the precision-times-means; a ratio subtracts them (used for the EP cavity, Section 10.7), and the result is a proper density only if the difference of precisions is positive definite.

Proof

(a) Write \(\mathbf{x}\)\({}=\bm\mu+\mathbf{L}\mathbf{u}\) with \(\mathbf{L}\mathbf{L}^\T=\bm\Sigma\) and \(\mathbf{u}\) standard normal: \(\E\mathbf{u}=\mathbf{0}\) and \(\E\mathbf{u}\mathbf{u}^\T\)\({}=\mathbf{I}\). (b) \(-\E\ln\N\)\({}=\tfrac D2\ln2\pi+\tfrac12\ln|\bm\Sigma|\)\({}+\tfrac12\E[(\mathbf{x}-\bm\mu)^\T\bm\Sigma^{-1}(\mathbf{x}-\bm\mu)]\) and the last expectation is \(\Tr(\bm\Sigma^{-1}\bm\Sigma)=D\) (Result T.8c). (c) \(\KL=\E_0[\ln\N_0-\ln\N_1]\); the \(\ln\N_0\) part is minus (b), and \(\E_0[(\mathbf{x}-\bm\mu_1)^\T\bm\Sigma_1^{-1}(\mathbf{x}-\bm\mu_1)]\)\({}=\Tr(\bm\Sigma_1^{-1}\bm\Sigma_0)\)\({}+(\bm\mu_0-\bm\mu_1)^\T\bm\Sigma_1^{-1}(\bm\mu_0-\bm\mu_1)\) by T.8c; collect terms. (d) Add (or subtract) the exponents \(-\tfrac12\mathbf{x}^\T\bm\Lambda_i\mathbf{x}\)\({}+\mathbf{x}^\T\bm\Lambda_i\bm\mu_i\) and apply Result T.13. ∎

5. Exponential families and conjugate priors

5.1 Exponential families and moment matching

Result T.17 (2.194), (2.226), (10.184)–(10.187)

Let \(q(\mathbf{x}\given\bm\eta)\)\({}=h(\mathbf{x})g(\bm\eta)\exp\{\bm\eta^\T\mathbf{u}(\mathbf{x})\}\). Then (a) \(-\nabla\ln g(\bm\eta)\)\({}=\E_{\bm\eta}[\mathbf{u}(\mathbf{x})]\) and \(-\nabla\nabla\ln g(\bm\eta)\)\({}=\cov_{\bm\eta}[\mathbf{u}(\mathbf{x})]\); (b) for any density \(p\) for which the expectations exist, \(\KL(p\|q_{\bm\eta})\) is a convex function of \(\bm\eta\), minimised where \(\E_{q_{\bm\eta}}[\mathbf{u}]\)\({}=\E_p[\mathbf{u}]\). For Gaussians, \(\mathbf{u}\)\({}=(\mathbf{x},\mathbf{x}\mathbf{x}^\T)\): the best Gaussian in the sense of \(\KL(p\|q)\) has the mean and covariance of \(p\).

Proof

(a) Differentiate \(g(\bm\eta)\int h\,e^{\bm\eta^\T\mathbf{u}}\d\mathbf{x}\)\({}=1\) (differentiation under the integral sign is allowed in the interior of the natural parameter space): \(\nabla g\int h e^{\bm\eta^\T\mathbf{u}}\)\({}+g\int h e^{\bm\eta^\T\mathbf{u}}\mathbf{u}\)\({}=\mathbf{0}\); since \(\int he^{\bm\eta^\T\mathbf{u}}\d\mathbf{x}\)\({}=1/g\), the first term is \(\nabla g/g=\nabla\ln g\) and the second is \(\E[\mathbf{u}]\), so \(\nabla\ln g+\E[\mathbf{u}]\)\({}=\mathbf{0}\). Differentiating \(\E_{\bm\eta}[\mathbf{u}]\)\({}=\int\mathbf{u}\,q\) once more gives \(\E[\mathbf{u}\mathbf{u}^\T]\)\({}-\E[\mathbf{u}]\E[\mathbf{u}]^\T\).

(b) \(\KL(p\|q_{\bm\eta})\)\({}=\int p\ln p-\int p\ln h-\ln g(\bm\eta)\)\({}-\bm\eta^\T\E_p[\mathbf{u}]\). Only the last two terms depend on \(\bm\eta\). By (a) the gradient is \(\E_{q_{\bm\eta}}[\mathbf{u}]-\E_p[\mathbf{u}]\) and the Hessian is \(\cov_{q_{\bm\eta}}[\mathbf{u}]\succeq0\), so the function is convex and any stationary point is a global minimum. ∎

5.2 The Gamma distribution

Result T.18 (B.26)–(B.31)

\(\Gam(\tau\given a,b)\)\({}=\frac{b^a}{\Gamma(a)}\tau^{a-1}e^{-b\tau}\) for \(\tau>0\), with shape \(a>0\) and rate \(b>0\). Then \(\E[\tau]=a/b\), \(\var[\tau]=a/b^2\), \(\E[\ln\tau]=\psi(a)-\ln b\), and \(\mathrm{H}[\tau]\)\({}=\ln\Gamma(a)-(a-1)\psi(a)-\ln b+a\).

Proof

\(\E[\tau^k]\)\({}=\frac{b^a}{\Gamma(a)}\int\tau^{a+k-1}e^{-b\tau}\d\tau\)\({}=\frac{\Gamma(a+k)}{\Gamma(a)b^k}\), giving the mean and \(\E\tau^2=a(a+1)/b^2\). For the log moment, differentiate \(\int\tau^{a-1}e^{-b\tau}\d\tau\)\({}=\Gamma(a)b^{-a}\) with respect to \(a\): \(\int\ln\tau\,\tau^{a-1}e^{-b\tau}\d\tau\)\({}=\Gamma(a)b^{-a}\{\psi(a)-\ln b\}\); divide by \(\Gamma(a)b^{-a}\). Finally \(\mathrm{H}\)\({}=-\E\ln\Gam\)\({}=-a\ln b+\ln\Gamma(a)-(a-1)\E[\ln\tau]\)\({}+b\E[\tau]\); substitute. ∎

5.3 The Dirichlet distribution

Result T.19 (B.16)–(B.24), (10.66)

\(\Dir(\bm\mu\given\bm\alpha)\)\({}=C(\bm\alpha)\prod_{k=1}^K\mu_k^{\alpha_k-1}\) on the simplex, with \(C(\bm\alpha)\)\({}=\Gamma(\hat\alpha)/\prod_k\Gamma(\alpha_k)\) and \(\hat\alpha=\sum_k\alpha_k\). Then \(\E[\mu_k]=\alpha_k/\hat\alpha\) and \(\E[\ln\mu_k]\)\({}=\psi(\alpha_k)-\psi(\hat\alpha)\). The entropy is \(-\ln C(\bm\alpha)\)\({}-\sum_k(\alpha_k-1)\{\psi(\alpha_k)\)\({}-\psi(\hat\alpha)\}\). With a Dirichlet prior and multinomial counts \(N_k\), the posterior is \(\Dir(\bm\alpha+\mathbf{N})\).

Proof

The normaliser means \(\int\prod_k\mu_k^{\alpha_k-1}\d\bm\mu\)\({}=1/C(\bm\alpha)\) for every \(\bm\alpha\). Mean: \(\E[\mu_k]\)\({}=C(\bm\alpha)/C(\bm\alpha+\mathbf{e}_k)\)\({}=\frac{\Gamma(\hat\alpha)\Gamma(\alpha_k+1)}{\Gamma(\hat\alpha+1)\Gamma(\alpha_k)}\)\({}=\alpha_k/\hat\alpha\). Log moment: differentiate \(\ln\int\prod_j\mu_j^{\alpha_j-1}\d\bm\mu\)\({}=-\ln C(\bm\alpha)\) with respect to \(\alpha_k\): the left side gives \(\E[\ln\mu_k]\) and the right side gives \(\psi(\alpha_k)-\psi(\hat\alpha)\). Entropy: \(-\E\ln\Dir\)\({}=-\ln C-\sum_k(\alpha_k-1)\E\ln\mu_k\). Conjugacy: \(\prod_k\mu_k^{N_k}\prod_k\mu_k^{\alpha_k-1}\)\({}=\prod_k\mu_k^{\alpha_k+N_k-1}\). ∎

Because \(\psi(x)\approx\ln x-\tfrac1{2x}\) for large \(x\), \(\exp\E[\ln\pi_k]\)\({}\approx\alpha_k/\hat\alpha\) when the counts are large, but it is much smaller than \(\alpha_k/\hat\alpha\) when \(\alpha_k\) is small: this is the mechanism by which variational Bayes switches off components with little data (Section 10.2).

5.4 The Wishart and Gaussian–Wishart distributions

Result T.20 (B.78)–(B.82), (10.64), (10.65)

For a \(D\times D\) symmetric positive definite \(\bm\Lambda\), scale matrix \(\mathbf{W}\) and \(\nu>D-1\) degrees of freedom,

\[ \Wish(\bm\Lambda\given\mathbf{W},\nu)=B(\mathbf{W},\nu)\,|\bm\Lambda|^{(\nu-D-1)/2}\exp\big\{-\tfrac12\Tr(\mathbf{W}^{-1}\bm\Lambda)\big\}, \] \[ B(\mathbf{W},\nu)=|\mathbf{W}|^{-\nu/2}\Big(2^{\nu D/2}\pi^{D(D-1)/4}\prod_{i=1}^D\Gamma\big(\tfrac{\nu+1-i}{2}\big)\Big)^{-1}. \]

Then (a) \(\E[\bm\Lambda]=\nu\mathbf{W}\); (b) \(\E[\ln|\bm\Lambda|]\)\({}=\sum_{i=1}^D\psi\big(\frac{\nu+1-i}{2}\big)\)\({}+D\ln2\)\({}+\ln|\mathbf{W}|\); (c) \(\mathrm{H}[\bm\Lambda]\)\({}=-\ln B(\mathbf{W},\nu)\)\({}-\frac{\nu-D-1}{2}\E[\ln|\bm\Lambda|]\)\({}+\frac{\nu D}{2}\); (d) under the Gaussian–Wishart \(p(\bm\mu,\bm\Lambda)\)\({}=\N(\bm\mu\given\mathbf{m},(\beta\bm\Lambda)^{-1})\Wish(\bm\Lambda\given\mathbf{W},\nu)\), \(\E_{\bm\mu,\bm\Lambda}[(\mathbf{x}-\bm\mu)^\T\bm\Lambda(\mathbf{x}-\bm\mu)]\)\({}=D\beta^{-1}\)\({}+\nu(\mathbf{x}-\mathbf{m})^\T\mathbf{W}(\mathbf{x}-\mathbf{m})\).

Proof

The density integrates to one for every \((\mathbf{W},\nu)\): \(\int|\bm\Lambda|^{(\nu-D-1)/2}e^{-\frac12\Tr(\mathbf{V}\bm\Lambda)}\d\bm\Lambda\)\({}=1/B\), where \(\mathbf{V}=\mathbf{W}^{-1}\), and \(\ln B\)\({}=\tfrac\nu2\ln|\mathbf{V}|-\tfrac{\nu D}2\ln2\)\({}-\tfrac{D(D-1)}4\ln\pi\)\({}-\sum_i\ln\Gamma(\tfrac{\nu+1-i}2)\).

(a) Differentiate \(\ln\int(\cdot)=-\ln B\) with respect to \(\mathbf{V}\) (Result T.9, items 2 and 4): the left side gives \(-\tfrac12\E[\bm\Lambda]\) and the right side gives \(-\tfrac\nu2\mathbf{V}^{-1}\), so \(\E[\bm\Lambda]=\nu\mathbf{W}\).

(b) Differentiate with respect to \(\nu\): the left side gives \(\tfrac12\E[\ln|\bm\Lambda|]\), the right side gives \(-\partial\ln B/\partial\nu\)\({}=-\tfrac12\ln|\mathbf{V}|+\tfrac D2\ln2\)\({}+\tfrac12\sum_i\psi(\tfrac{\nu+1-i}2)\). Multiply by 2 and use \(-\ln|\mathbf{V}|=\ln|\mathbf{W}|\).

(c) \(\mathrm{H}\)\({}=-\E\ln\Wish\)\({}=-\ln B-\tfrac{\nu-D-1}2\E\ln|\bm\Lambda|\)\({}+\tfrac12\Tr(\mathbf{W}^{-1}\E\bm\Lambda)\), and the last term is \(\tfrac12\Tr(\nu\mathbf{I})=\nu D/2\) by (a).

(d) Condition on \(\bm\Lambda\): \(\bm\mu\) has mean \(\mathbf{m}\) and covariance \((\beta\bm\Lambda)^{-1}\), so by Result T.8c \(\E_{\bm\mu}[(\mathbf{x}-\bm\mu)^\T\bm\Lambda(\mathbf{x}-\bm\mu)]\)\({}=(\mathbf{x}-\mathbf{m})^\T\bm\Lambda(\mathbf{x}-\mathbf{m})\)\({}+\Tr(\bm\Lambda(\beta\bm\Lambda)^{-1})\)\({}=(\mathbf{x}-\mathbf{m})^\T\bm\Lambda(\mathbf{x}-\mathbf{m})\)\({}+D/\beta\). Now average over \(\bm\Lambda\) using (a). ∎

In Chapter 10, \(\ln\tilde\Lambda_k\)\({}=\E[\ln|\bm\Lambda_k|]\) is item (b) with \(\nu=\nu_k\), \(\mathbf{W}=\mathbf{W}_k\) (10.65), and item (d) is (10.64).

5.5 Student's t as a scale mixture of Gaussians

Result T.21 (2.158)–(2.162), (10.81), (10.82)

(a) If \(\mathbf{x}\given\eta\)\({}\sim\N(\bm\mu,(\eta\bm\Lambda)^{-1})\) and \(\eta\sim\Gam(\nu/2,\nu/2)\), then \(\mathbf{x}\) has the multivariate Student density

\[ \St(\mathbf{x}\given\bm\mu,\bm\Lambda,\nu)=\frac{\Gamma(\frac{\nu+D}2)}{\Gamma(\frac\nu2)}\frac{|\bm\Lambda|^{1/2}}{(\nu\pi)^{D/2}}\Big(1+\frac{\Delta^2}{\nu}\Big)^{-(\nu+D)/2},\qquad \Delta^2=(\mathbf{x}-\bm\mu)^\T\bm\Lambda(\mathbf{x}-\bm\mu), \]

with mean \(\bm\mu\) (\(\nu>1\)) and covariance \(\frac{\nu}{\nu-2}\bm\Lambda^{-1}\) (\(\nu>2\)); it tends to \(\N(\bm\mu,\bm\Lambda^{-1})\) as \(\nu\to\infty\).

(b) (Predictive density of the variational mixture.) If \(p(\mathbf{x}\given\bm\mu,\bm\Lambda)\)\({}=\N(\mathbf{x}\given\bm\mu,\bm\Lambda^{-1})\) and \((\bm\mu,\bm\Lambda)\) is Gaussian–Wishart with parameters \(\mathbf{m},\beta,\mathbf{W},\nu\), then \(p(\mathbf{x})\)\({}=\St\big(\mathbf{x}\given\mathbf{m},\mathbf{L},\nu+1-D\big)\) with \(\mathbf{L}\)\({}=\frac{(\nu+1-D)\beta}{1+\beta}\mathbf{W}\).

Proof

(a) \(\int_0^\infty\frac{\eta^{D/2}|\bm\Lambda|^{1/2}}{(2\pi)^{D/2}}e^{-\eta\Delta^2/2}\,\frac{(\nu/2)^{\nu/2}}{\Gamma(\nu/2)}\eta^{\nu/2-1}e^{-\nu\eta/2}\d\eta\) is a Gamma integral with shape \((\nu+D)/2\) and rate \((\nu+\Delta^2)/2\); it equals \(\frac{|\bm\Lambda|^{1/2}(\nu/2)^{\nu/2}}{(2\pi)^{D/2}\Gamma(\nu/2)}\Gamma(\tfrac{\nu+D}2)\big(\tfrac{\nu+\Delta^2}2\big)^{-(\nu+D)/2}\), which simplifies to the stated density. The mean is \(\bm\mu\) by symmetry; the covariance is \(\E[\eta^{-1}]\bm\Lambda^{-1}\) with \(\E[\eta^{-1}]\)\({}=\frac{\nu/2}{\nu/2-1}\). As \(\nu\to\infty\), \(\eta\to1\) in probability.

(b) Integrating out \(\bm\mu\) with Result T.15 gives \(\N(\mathbf{x}\given\mathbf{m},(1+\beta^{-1})\bm\Lambda^{-1})\)\({}=\N(\mathbf{x}\given\mathbf{m},(c\bm\Lambda)^{-1})\) with \(c=\beta/(1+\beta)\). With \(\mathbf{d}=\mathbf{x}-\mathbf{m}\),

\[ \begin{aligned} \int\N(\mathbf{x}\given\mathbf{m},(c\bm\Lambda)^{-1})\Wish(\bm\Lambda\given\mathbf{W},\nu)\d\bm\Lambda&=\frac{c^{D/2}B(\mathbf{W},\nu)}{(2\pi)^{D/2}}\int|\bm\Lambda|^{\frac{(\nu+1)-D-1}{2}}e^{-\frac12\Tr\{(\mathbf{W}^{-1}+c\mathbf{d}\mathbf{d}^\T)\bm\Lambda\}}\d\bm\Lambda\\ &=\frac{c^{D/2}B(\mathbf{W},\nu)}{(2\pi)^{D/2}B(\mathbf{W}',\nu+1)}, \end{aligned} \]

where \(\mathbf{W}'^{-1}\)\({}=\mathbf{W}^{-1}+c\,\mathbf{d}\mathbf{d}^\T\). As a function of \(\mathbf{x}\), \(1/B(\mathbf{W}',\nu+1)\)\({}\propto|\mathbf{W}'|^{(\nu+1)/2}\)\({}=|\mathbf{W}^{-1}\)\({}+c\mathbf{d}\mathbf{d}^\T|^{-(\nu+1)/2}\)\({}\propto(1+c\,\mathbf{d}^\T\mathbf{W}\mathbf{d})^{-(\nu+1)/2}\) by Result T.10b. Matching with \((1+\mathbf{d}^\T\mathbf{L}\mathbf{d}/\nu')^{-(\nu'+D)/2}\) gives \(\nu'=\nu+1-D\) and \(\mathbf{L}=\nu'c\,\mathbf{W}\). ∎

6. Computing safely

6.1 Log-sum-exp and responsibilities

Result T.22

For real \(a_1,\dots,a_K\) and \(m=\max_ka_k\), \(\ln\sum_ke^{a_k}\)\({}=m+\ln\sum_ke^{a_k-m}\), and the softmax \(\gamma_k\)\({}=e^{a_k}/\sum_je^{a_j}\)\({}=\exp\{a_k-\ln\sum_je^{a_j}\}\). The function \(\mathbf{a}\mapsto\ln\sum_ke^{a_k}\) is convex, with gradient \(\bm\gamma\) and Hessian \(\diag(\bm\gamma)-\bm\gamma\bm\gamma^\T\).

Proof

Factor \(e^m\) out of the sum. On the right every exponent is at most 0 and one equals 0, so the sum lies in \([1,K]\): nothing overflows, and nothing underflows to \(\ln0\). Differentiating gives \(\partial/\partial a_k=\gamma_k\) and \(\partial^2/\partial a_k\partial a_j\)\({}=\gamma_k\delta_{kj}-\gamma_k\gamma_j\), the covariance matrix of a one-hot vector with probabilities \(\bm\gamma\), which is positive semi-definite. ∎

In practice. Compute \(a_{nk}\)\({}=\ln\pi_k\)\({}+\ln\N(\mathbf{x}_n\given\bm\mu_k,\bm\Sigma_k)\) on the log scale, then \(\gamma(z_{nk})\)\({}=\exp\{a_{nk}-\mathrm{LSE}_k(a_{nk})\}\) and \(\ln p(\mathbf{X})\)\({}=\sum_n\mathrm{LSE}_k(a_{nk})\). Evaluating \(\N\) directly underflows to zero for points far from every component, which produces \(0/0\) responsibilities; the same device is used for the variational responsibilities (10.46)–(10.49). All the course code does this.

References

  1. Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer, New York. Appendices B (probability distributions), C (properties of matrices), D (calculus of variations) and E (Lagrange multipliers).
  2. Petersen, K. B. and Pedersen, M. S. (2012). The Matrix Cookbook. Technical University of Denmark (version of 15 November 2012).
  3. Magnus, J. R. and Neudecker, H. (2019). Matrix Differential Calculus with Applications in Statistics and Econometrics, 3rd edition. Wiley, Chichester. doi:10.1002/9781119541219.
  4. Fan, K. (1949). On a theorem of Weyl concerning eigenvalues of linear transformations I. Proceedings of the National Academy of Sciences, 35(11), 652–655. doi:10.1073/pnas.35.11.652.
  5. Boyd, S. and Vandenberghe, L. (2004). Convex Optimization. Cambridge University Press. doi:10.1017/CBO9780511804441.