sampling.tex (53643B)
1 \setchapterpreamble[u]{\margintoc} 2 \chapter{Sampling} 3 \labch{sampling} 4 5 We will now discuss how to perform inference by sampling from the Laplace posterior distribution. 6 We first give an overview of how we typically sample from the Laplace posterior and discuss the memory constraints of this sampling method when using the full posterior precision matrix. 7 In \cref{sec:ciq}, we propose an algorithm for sampling from the Laplace posterior that does not require storing the full precision matrix, allowing us to scale sampling to larger models. 8 Finally, we discuss how to mitigate some of the issues of our method (\cref{sec:preconditioning}) and propose a practical technique for determining whether sampling was successful (\cref{sec:sampling-evaluation}). 9 10 \section{Sampling from the Laplace Posterior}[Posterior Sampling] 11 12 In many methods, such as the Laplace approximation (\cref{sec:laplace}), we sample from a multivariate normal distribution parametrised by a mean vector \(\bm \mu\) and a covariance matrix \(\bm \Sigma\). 13 To sample from a Gaussian distribution, you can apply the reparametrisation trick to compute a vector of samples as 14 \begin{align}\label{eq:sample-reparam} 15 \bm \varepsilon ={} & \bm \mu + \bm \Sigma^{1/2} \bm \varepsilon_0, 16 \end{align} 17 where \(\bm \varepsilon_0\) are samples drawn from a standard normal distribution, i.e., \(\bm \varepsilon_0 \sim \normal(\bm 0, \identity)\). 18 19 Using the Laplace approximation, we estimate the precision matrix as the Hessian matrix of the loss with respect to the parameters, as discussed in \cref{sec:laplace}. 20 Because of this, the covariance matrix is given by the inverse of this Hessian, and we thus sample from the Laplace-approximate posterior distribution as 21 \begin{align}\label{eq:sample-laplace} 22 \bm \varepsilon = \bm \theta_{\textsc{map}} + \bm \Lambda^{-1/2} \bm \varepsilon_0, 23 \end{align} 24 where \(\bm \Lambda = \nabla^2_{\bm\theta} \log p(\bm \theta \given \bm y)\). 25 In \cref{eq:sample-reparam}, the square root of a matrix yields another matrix which, when multiplied by itself, is equal to the original matrix. 26 However, since an isotropic Gaussian distribution is invariant to rotation, in this special case, we just need to obtain a matrix which returns the original matrix \emph{up to a rotation}.\sidenote{That is, if we have a vector of standard normal samples \(\bm \varepsilon \sim \normal(\bm 0, \identity)\), then, for an orthogonal matrix \(\bm R\) (i.e., a rotation matrix), \(\bm R\T \bm \varepsilon \sim \normal(\bm 0, \identity)\). We then have that we can sample \emph{non}-standard normal samples as \(\bm \Lambda^{-1/2} \bm R\T \bm \varepsilon \sim \normal(\bm 0, \bm \Lambda^{-1})\).} 27 We thus need to find a method which computes the inverse square root of the full \(D \times D\) posterior precision matrix up to a rotation, where \(D\) is the number of model parameters. 28 29 Some methods exist which compute the inverse square root of a matrix up to a rotation, such as the Cholesky decomposition. 30 However, these methods are not suitable for large matrices, as they require the instantiation of the full precision matrix, which is not feasible for large matrices. 31 There are also methods which can compute the inverse square root of a low-rank matrix that is constructed as the inner product of two rectangular matrices by performing the inverse square root on the outer product (which is low-dimensional for low-rank problems), such as the Woodbury matrix identity. 32 However, the precision matrix we are interested in is not itself low-rank but a sum of low-rank matrices, and thus this method is not suitable for our problem. 33 This is discussed in more detail in \cref{sec:laplace,sec:preconditioning}. 34 % As such, these are not adequate methods for computing the inverse square root using only matrix-vector products. 35 36 Furthermore, methods which do not require computation of the square root of the covariance matrix typically attempt to exploit properties such as a block structure or sparsity of the covariance matrix~\cite{vono2022high}. 37 However, we do not have a good understanding of the structure of the Laplace covariance matrix, and thus these methods are not suitable for our problem. 38 % Talk about what we do know about the structure? Wrt the sum of low-rank GGNs 39 When sampling from high-dimensional Gaussian distributions, it is common to use Markov chain Monte Carlo (MCMC) sampling methods such as Gibbs sampling~\cite{vono2022high}. 40 However, these methods are tricky to tune and tend to converge very slowly for high-dimensional problems. 41 As such, we do not use these methods for sampling from the Laplace approximation, and instead sample using the reparametrisation trick. 42 For this, we need a method to compute the inverse square root using only matrix-vector products. 43 44 \section{Contour Integral Quadrature}[Contour Integral Quad] 45 \label{sec:ciq} 46 47 \textcite{pleiss2020fast} propose one such method. 48 Contour integral quadrature (CIQ) attempts to solve the problem of computing the inverse square root of a matrix, up to a rotation, by approximating the expression \(f(\bm K) \bm \varepsilon_0 = \bm K^{-1/2} \bm \varepsilon_0\) where \(\bm K\) is a positive semi-definite matrix using Cauchy's integral formula. 49 To approximate this integral, it exploits the Lanczos algorithm, Gaussian quadrature, and the msMINRES algorithm, methods which only require the computation of matrix-vector products \(\bm K \bm v\). 50 There are then essentially three steps to CIQ, namely: 51 \begin{enumerate} 52 \item Perform the Lanczos algorithm to compute a lower bound on the maximum eigenvalue and an upper bound on the minimum eigenvalue of \(\bm K\). 53 \item Sample \(Q\) quadrature points from a complex-plane circle to approximate the contour integral from Cauchy's integral formula, using these quadrature points to create \(Q\) shifted linear systems. 54 \item Solve these \(Q\) shifted systems using the msMINRES algorithm. 55 \end{enumerate} 56 57 \begin{algorithm} 58 \caption{Contour Integral Quad (CIQ)}\label{alg:ciq} 59 \Input{\(\bm K \succ 0\), \(\bm b\), \(\bm P \succ 0\), \(J > 0\), \(Q > 0\)} 60 \Output{\(\bm s = \bm K^{-1/2} b\)} 61 \(\_, \bm\alpha, \bm\beta \gets \mathrm{CG}(\bm K, \bm b, \bm P, J)\)\; 62 \(\bm M_{\mathrm{Lanczos}} \gets \mathrm{TriDiag}(\bm\alpha, \bm\beta)\)\; 63 \(\lambda_{\bm M} \gets \lambda(\bm M_{\mathrm{Lanczos}})\)\; 64 \(\lambda_{\min} \gets \min{\lambda_{\bm M}}\)\; 65 \(\lambda_{\max} \gets \max{\lambda_{\bm M}}\)\; 66 \(w_1, \ldots, w_Q, t_1, \ldots, t_Q \gets \mathrm{Quadrature}(\lambda_{\min}, \lambda_{\max}, Q)\)\; 67 \(\bm c_1, \ldots, \bm c_Q \gets \mathrm{msMINRES}(\bm K, \bm b, t_q, \bm P, J)\) \Comment*[r]{\(\left( t_q \identity + \bm K \right)^{-1} \bm b\).} 68 % \(s_q \gets \left( t_q \identity + H \right)^{-1} b\) \Comment*[r]{Computed via msMINRES(K, t, J).} 69 \(\bm s \gets \sum^Q_{q=1} w_q \bm c_q\)\; 70 % \(\mathrm{samples} \gets \sum^Q_{q=1} w_q \left( t_q \identity + H \right)^{-1} b\) 71 \end{algorithm} 72 73 Since this procedure (see \cref{alg:ciq}) computes the approximate inverse square root product \(\bm K\) by \(\bm \varepsilon_0\) up to a rotation, i.e., 74 \begin{align}\label{eq:ciq-asdf} 75 \bm K^{-1/2} \bm \varepsilon_0, 76 \end{align} 77 it is then also trivial to compute the square root product by multiplying \(\bm K\) onto this vector, as per 78 \begin{align} 79 \bm K^{1/2} \bm \varepsilon_0 = \bm K \cdot \underbrace{\bm K^{-1/2} \bm \varepsilon_0}_{\text{\cref{eq:ciq-asdf}}}. 80 \end{align} 81 \marginnote{The inverse square root function is holomorphic for all positive definite real matrices.} 82 83 \def\ymax{2.0} 84 \def\ymin{-2.0} 85 \def\xmax{3.0} 86 \def\xmin{-0.5} 87 \def\R{1.3} 88 \begin{marginfigure}%[-7cm] 89 \centering 90 \input{figures/cauchy_integral.tex} 91 \caption[Integration circle for the Cauchy integral formula.]{Integration circle \(\Gamma\) contained within the domain of \(f\) as defined according to the Cauchy integral formula.} 92 \label{fig:cauchy-integral-formula} 93 \end{marginfigure} 94 95 % ------------------------------------------ 96 % CAUCHY INTEGRAL FORMULA 97 % ------------------------------------------ 98 99 The first step of CIQ invokes Cauchy's integral formula. 100 Cauchy's integral formula is a central theorem in complex analysis which states that, for a holomorphic function \(f\), there exists a closed contour \(\Gamma\) in the complex plane (see \cref{fig:cauchy-integral-formula}) which encloses the eigenvalues of \(\bm K\) such that \(f(\bm K)\) can be approximated as 101 \begin{align}\label{eq:cauchy-integral-formula} 102 f(\bm K) ={} & \frac{1}{2 \pi i} \oint_\Gamma f(\tau) \left(\tau \identity - \bm K\right)^{-1}\,d\tau, 103 \end{align} 104 where \(\oint_\Gamma\) is the contour integral along the contour \(\Gamma\) parametrised in the complex variable \(\tau\). 105 Since, in our case, the eigenvalues of \(\bm K\) will all be real-valued, a circle centred on the real axis which encloses the minimum and maximum eigenvalues of \(\bm K\) will suffice.\marginnote{If a real-valued matrix is symmetric, its eigenvalues are real. In our case, \(\bm K\) will be the precision matrix obtained from the Laplace approximation.} 106 To estimate these values, we can use the Lanczos algorithm, a Krylov subspace method which adapts the power iteration method, to compute a lower bound on the maximum eigenvalue and an upper bound on the minimum eigenvalue of \(\bm K\).\sidenote{Krylov subspace methods are a class of iterative methods which estimate certain properties (e.g., the rank, eigenvalues) of a matrix \(\bm K\) with a Krylov subspace \(\mathcal{K}_j(\bm K, \bm v)\) for some vector \(\bm v\). This subspace is spanned by the set of images of \(\bm v\) under the first \(j\) powers of \(\bm K\), \(\mathcal{K}_j( \bm K, \bm v) = \mathrm{span}\{ \bm v, \bm K \bm v, \ldots, \bm K^{j-1} \bm v \}\).} 107 108 % ------------------------------------------ 109 % QUADRATURE 110 % ------------------------------------------ 111 112 \def\ymax{2.0} 113 \def\ymin{-2.0} 114 \def\xmax{3.0} 115 \def\xmin{-0.5} 116 \def\R{1.3} 117 \begin{marginfigure} 118 \centering 119 \input{figures/cauchy_quadrature.tex} 120 \caption[Sampling of quadrature points for CIQ.]{The shifts \(\tau_1, \ldots, \tau_Q\) are sampled from a circle with its centre on the real axis which intersects the real axis at the points \(\lambda_{\mathrm{min}}\) and \(\lambda_{\mathrm{max}}\). Since we use Jacobi elliptic functions, we over-sample quadrature points that are closer to the minimum eigenvalue \(\lambda_{\mathrm{min}}\).} 121 \label{fig:ciq} 122 \end{marginfigure} 123 124 We then apply Cauchy's integral formula to \(f(\bm K) = \bm K^{-1/2}\), apply a change of variable, and then approximate this contour integral using the quadrature rule with \(Q\) quadrature points (see \cref{fig:ciq}), as per 125 \begin{align} 126 \begin{split} 127 \bm K^{-1/2} ={} & \frac{1}{2 \pi i} \oint_\Gamma \tau^{-1/2} \left(\tau \identity - \bm K\right)^{-1}\,d\tau \label{eq:ciq-integral-tau} 128 \\ \approx{}& \frac{1}{2 \pi i} \sum_{q=1}^Q \Tilde{w}_q \tau_q^{-1/2} \left( \tau_q \identity - \bm K \right)^{-1}, %\label{eq:ciq-quadrature-tau} 129 \end{split} 130 \end{align} 131 where we have the sampled quadrature points \(\tau_1, \ldots, \tau_Q \in \Gamma\) and the quadrature weights \(\Tilde{w}_1, \ldots, \Tilde{w}_Q \in \reals\). 132 % Since we will be sampling a fixed number of quadrature points from the circle, to improve the closeness of the approximation of the integral, it would be beneficial to choose a closed circle which intersects the real axis \emph{at} the minimum and maximum eigenvalues, since this will avoid sampling quadrature points which lie outside the spectrum of \(\bm K\). 133 Since we will be sampling a fixed number of quadrature points from the circle, the closer the contour is to the singular points (i.e., the eigenvalues), the better the approximation to the integral will be. 134 135 % ------------------------------------------ 136 % JACOBI ELLIPTICAL FUNCTIONS 137 % ------------------------------------------ 138 139 We could sample these quadrature points from the circle uniformly, which corresponds to the regular trapezoid quadrature rule. 140 However, the convergence for this quadrature is linear with regard to the condition number of \(\bm K\)~\cite{pleiss2020fast,hale2008computing}. 141 Since the precision matrix (which will be computed from the Laplace approximation as the GGN matrix) can often have a low rank and poor conditioning, uniform sampling would then require a very large number of quadrature points \(Q\) and would therefore be inadequate~\cite{pleiss2020fast}. 142 143 Since our GGN precision matrix \(\bm \Lambda = \sum^N_{i=1} \bm J_i\T \bm H_i \bm J_i\) is often low-rank, we would instead prefer to oversample quadrature points which lie close to the minimum eigenvalue. 144 This is accomplished by applying a change of variable from \(\tau\) to \(\sigma = \tau^{1/2}\) and sampling the quadrature points from \(\sigma\). 145 To account for this change of variable, we reformulate \cref{eq:ciq-integral-tau} as % eq:ciq-quadrature-tau 146 \begin{align}\label{eq:ciq-integral-sigma} 147 \begin{split} 148 \bm K^{-1/2} ={} & \frac{1}{2 \pi i} \oint_\Gamma \tau^{-1/2} \left(\tau \identity - \bm K\right)^{-1}\,d\tau 149 \\ ={}& \frac{1}{\pi i} \oint_{\Gamma_\sigma} \left( \sigma^2 \identity - \bm K \right)^{-1}\,d\sigma 150 \\ \approx{}& \frac{1}{\pi i} \sum_{q=1}^Q \Tilde{w}_q \left( \sigma^2_q \identity - \bm K \right)^{-1}. 151 \end{split} 152 \end{align} 153 We can now adapt this equation to allow for computation using only matrix-vector products. 154 From here, the inverse square root vector product is given by 155 \begin{align}\label{eq:ciq-sigma-product} 156 \bm K^{-1/2} \bm v \approx{} & \frac{1}{\pi i} \sum_{q=1}^Q \Tilde{w}_q \underbrace{\left( \sigma^2_q \identity - \bm K \right)^{-1} \bm v}_{Q\text{ system solves}}. 157 \end{align} 158 We can then solve the \(Q\) systems of equations to obtain the solutions \(\bm c_q = \left(\tau_q \identity - \bm K \right)^{-1} \bm v\) for \(q \in 1, \ldots, Q\). 159 160 Since the integrand is symmetric with respect to the real axis, only the imaginary component of \(\Gamma_\sigma\) is required.\sidenote{This is because the circle with its centre on the \(x\)-axis is symmetric with respect to said \(x\)-axis.} 161 Since \(\sigma\) is then imaginary (its real part is zero), \(\tau \coloneqq \sigma^2\) is real such that \(\tau = - \Im(\sigma)^2\). 162 As such, since \(\Im(\sigma)^2\) must be positive, \(\tau\) must then be real-valued and negative. 163 In practice, we incorporate the \(1 / (\pi i)\) term into the quadrature weights and, since \(\Tilde{w}_q\) also happens to be real-valued and negative, we can redefine the quadrature weights \(w_q = -\Tilde{w}_q\) and the quadrature points \(t_q = -\sigma^2_q\) for simplicity.\marginnote{With this redefinition, both \(w_q > 0\) and \(t_q > 0\). This then means that \(\left(t_q \identity + \bm K\right)\) is positive definite.} 164 This yields the quadrature rule result \(\bm K^{-1/2} \bm v \approx \sum^Q_{q=1} w_q \bm c_q\), where \(\bm c_q = (t_q \identity + \bm K)^{-1}\). 165 166 \begin{marginfigure} 167 \includegraphics[scale=1]{ellipk.pdf} 168 \caption[Comparison of SciPy and JAX \texttt{ellipk} implementations.]{Comparison of SciPy and JAX \texttt{ellipk} implementations. For values for which the complete elliptic integral \(\mathcal{K}(p)\) is well-defined, both implementations yield the same results. Furthermore, due to its static nature, JAX implementation is compatible with \emph{just-in-time} compilation.} 169 \label{fig:ellipk} 170 \end{marginfigure} 171 172 This change of variables from \(\tau\) to \(\sigma\) yields a set of quadrature weights \(w_1, \ldots, w_Q\) given by 173 \begin{align}\label{eq:quadrature-weights} 174 w_q = \frac{2 \sqrt{\lambda_\mathrm{min}}}{\pi Q} \mathcal{K}^\prime (k) \cdot \cn(i u_q \mathcal{K}^\prime (k) \given k) \cdot \dn (i u_q \mathcal{K}^\prime (k) \given k), 175 \end{align} 176 and a set of quadrature points (shifts) given by 177 \begin{align}\label{eq:quadrature-shifts} 178 \sigma^2_q = \lambda_{\mathrm{min}} \cdot \sn(i u_q \mathcal{K}^\prime (k) \given k)^2, 179 \end{align} 180 where \(\sn\), \(\cn\), and \(\dn\) are the Jacobi elliptic functions and \(\mathcal{K}^\prime(k)\) is the complete elliptic integral, such that \(\mathcal{K}^\prime(k) = \mathcal{K}(k^\prime) = \mathcal{K}(\sqrt{1 - k^2})\). 181 The complete elliptic integral of the first kind is given by 182 \begin{align} 183 \mathcal{K}(p) = \int_0^{\pi/2} \left( 1 - m \sin(t)^2 \right)^{-1/2}\,dt, 184 \end{align} 185 where \(p = 1 - m\), and is defined in the domain \(0 < p \leq 1\).\sidenote{If \(p > 1\), then what we compute is instead the identity \(\mathcal{K}(p) = \mathcal{K}(1/p) / \sqrt{p}\).} 186 This integral is approximated by 187 \begin{align} 188 \mathcal{K}(p) \approx P(p) - \log(p) Q(p), 189 \end{align} 190 where \(P\) and \(Q\) are tenth-order polynomials. 191 Additionally, we need to compute the Jacobian elliptic functions \(\sn(u \given m)\), \(\cn(u \given m)\), and \(\dn(u \given m)\). 192 To compute these values, we first calculate the elliptic modulus \(k = \kappa(\bm K)^{-1/2}\) (where \(\kappa(\bm K)\) denotes the condition number of \(\bm K\)) and \(u_q = (q - 1/2) / Q\). 193 These functions are not implemented in JAX, and SciPy uses the Cephes numerical library in C to compute them. 194 Because of this, we ported the Cephes Jacobi elliptical functions to JAX, and \cref{fig:ellipk,fig:ellipj} show the functions evaluated for the two implementations. 195 Our static JAX implementation allows for efficient \emph{just-in-time} compilation, which is not possible with the SciPy implementation. 196 We can thereby now compute the quadrature weights and shifts as described in \cref{alg:quad}. 197 198 \begin{figure} 199 \centering 200 \includegraphics[scale=1]{ellipj.pdf} 201 \caption[Comparison of SciPy and JAX \texttt{ellipj} implementations.]{Comparison of SciPy and JAX \texttt{ellipj} implementations. For values for which the functions are well-defined, both implementations yield the same results. Plotted are $\sn(u \given m = 0.7)$, $\cn(u \given m = 0.7)$, and $\dn(u \given m = 0.7)$. Furthermore, due to its static nature, JAX implementation is compatible with \emph{just-in-time} compilation.} 202 \label{fig:ellipj} 203 \end{figure} 204 205 \begin{marginfigure} 206 \begin{algorithm}[H] 207 \caption{Quadrature}\label{alg:quad} 208 \Input{\(\lambda_{\min}\), \(\lambda_{\max}\), \(Q > 0\)} 209 \Output{\(w_q, t_q \given q \in 1, \ldots, Q\)} 210 \(k^2 \gets \lambda_{\min} / \lambda_{\max}\)\; 211 \({k^{\prime}}^2 \gets \sqrt{1 - k^2}\)\; 212 \(K^\prime \gets \mathcal{K}({k^{\prime}}^2)\)\; 213 \For{\(q \gets 1\) \KwTo \(Q\)}{ 214 \(u_q \gets (q - 1/2) / Q\)\; 215 \(\overline{\sn}_q \gets \sn(u_q K^\prime \given {k^{\prime}}^2)\)\; 216 \(\overline{\cn}_q \gets \cn(u_q K^\prime \given {k^{\prime}}^2)\)\; 217 \(\overline{\dn}_q \gets \dn(u_q K^\prime \given {k^{\prime}}^2)\)\; 218 \(\sn_q \gets i ( \overline{\sn}_q / \overline{\cn}_q )\)\; 219 \(\dn_q \gets ( \overline{\dn}_q / \overline{\cn}_q )\)\; 220 \(\cn_q \gets ( 1 / \overline{\cn}_q )\)\; 221 \(w_q \gets \lambda_{\min}^{1/2} K^\prime \cn_q \dn_q\)\; 222 \(w_q \gets 2 w_q / \left(\pi Q\right) \)\; 223 \(t_q \gets -\lambda_{\min} \sn_q^2\)\; 224 } 225 \end{algorithm} 226 \end{marginfigure} 227 228 The last step involves solving the \(Q\) linear systems defined in \cref{eq:ciq-sigma-product} to determine \(\bm c_q\). % Actually we solve the other linear system. 229 As mentioned above, this is done by using msMINRES, an algorithm for solving linear systems of the form \((\bm K + t_q \identity) \bm c_q = \bm b\) where \(\bm K\) is symmetric. 230 Like other Krylov methods, msMINRES computes the solutions to the linear systems by constructing a Krylov subspace \(\mathcal{K}_q\) of \(\bm K + t_q \identity\) and then iteratively minimises the norm of the residual in \(\mathcal{K}_q\). 231 While msMINRES only requires that the matrices \(\bm K + t_q \identity\) be symmetric, and not necessarily also positive definite (since the shifts \(t_q\) are positive and \(\bm K\) is positive semidefinite), the sum is also positive definite. 232 However, the CIQ implementation from \textcite{pleiss2020fast} uses msMINRES, and we follow their implementation. 233 There is also evidence suggesting that msMINRES converges faster than, for example, the conjugate gradient method (CG)~\cite{fong2012cg}, a Krylov method that requires the matrices to be positive definite. 234 235 Since the Lanczos algorithm only requires the matrix-vector product \(\bm K \bm v\) with a random vector \(\bm v\) to estimate a bound on the eigenvalues, we can use the same bound for all \(Q\) linear systems. 236 As such, the computational bottleneck of the contour integral quadrature is performing the msMINRES for the shifted systems. 237 Additionally, since msMINRES is an iterative Krylov method, its convergence bound is sensitive to the conditioning of the linear systems, i.e., the conditioning of \(\bm K\). 238 To mitigate the effect of ill conditioning, we use a preconditioner \(\bm P\) such that \(\bm P^{-1} \bm K\) is well-conditioned. 239 240 \section{Preconditioning} 241 \label{sec:preconditioning} 242 243 One drawback of CIQ is that the accuracy its of approximation of \(\bm \Lambda^{-1/2} \bm \varepsilon_0\) suffers when the precision matrix \(\bm \Lambda\) is poorly conditioned. 244 In the case of the Laplace approximation, the precision matrix is given by 245 \begin{align}\label{eq:ggn-laplace} 246 \bm \Lambda = \sum_i^N \bm J_i\T \bm H_i \bm J_i + \alpha \identity, 247 \\ \bm \theta \sim \normal(\bm \theta_{\textsc{map}}, \bm \Lambda^{-1}), 248 \end{align} 249 where \(N\) is the number of observations in your dataset. 250 % Since each \(J_i\T H_i J_i\) is, for \(i \in 1 \ldots N\), a low-rank positive semi-definite matrix of rank \(O\), it has \(O\) non-zero eigenvalues (see \cref{sec:spectrum-ggn}). 251 % Let us define \(\lambda_1, \ldots, \lambda_o\) as the eigenvalues of a matrix sorted in decreasing order. 252 % Assume that the non-zero spectrum of \(\bm J_i\T \bm H_i \bm J_i\) is relatively flat, i.e., \(\lambda_1 \not\gg \lambda_o\). 253 254 % In a hand-wavy way, we have that the ``more similar'' the eigenvectors (and assuming that the eigenvectors have the same ordering between the two matrices) are, the more the largest eigenvalue will be to the sum of the eigenvalues. 255 256 For preconditioning, we need to find a preconditioner matrix \(\bm P\) and its inverse \(\bm P^{-1}\) such that the matrix \(\bm P^{-1} \bm \Lambda\) is well-conditioned. 257 This is done by choosing \(\bm P\) such that the two matrices are ``similar'', i.e., they have close eigenvalues and eigenvectors. 258 In this way, the preconditioner acts as an initial best guess of the value to be computed. 259 We are thus seeking a preconditioner \(\bm P\) such that 260 \begin{align} 261 \bm P^{-1} \bm A \approx{} & \identity \Rightarrow \bm P \approx \bm A, 262 \\ \bm P^{-1} \bm \Lambda ={}& \bm P^{-1} \left( \textstyle{\sum^N_i} \bm J_i\T \bm H \bm J_i + \alpha \identity \right), 263 \end{align} 264 where \(\bm \Lambda\) is the posterior precision matrix given by \cref{eq:ggn-laplace} to improve convergence of the msMINRES and Lanczos algorithms. 265 The eigenvalues of our GGN approximation are thus given by 266 \begin{align} 267 \lambda\left( \textstyle{\sum^N_i} \bm J_i\T \bm H \bm J_i + \alpha \identity \right) = \big\{ \underbrace{s_1, \ldots, s_K}_{K \leq N \cdot O}, \underbrace{\alpha, \ldots, \alpha}_{D - K} \big\}, 268 \end{align} 269 where \(\{s_1, \ldots, s_K\}, K \leq N \cdot O\) are the non-zero eigenvalues of the rank-\(K\) matrix \(\sum^N_i \bm J_i\T \bm H_i \bm J_i\). 270 If a preconditioner approximates all eigenvalues and eigenvectors of a matrix, then the preconditioner and the matrix are equal and the preconditioner is exact. 271 However, we often do not have a preconditioner which is exact, but rather an approximation of the matrix. 272 In this case, there is a tradeoff between the accuracy of the preconditioner and its computational cost. 273 Furthermore, the inverse of the preconditioner is required for preconditioning, which is often not available. 274 To approximate the inverse of the preconditioner, we can use the conjugate gradient method, though this method is then sensitive to the condition number of the preconditioner; and if the original matrix \(\bm \Lambda\) is ill-conditioned, then a good preconditioner will also tend to be ill-conditioned. 275 276 As such, the condition number of our preconditioner also becomes an issue, since we have two conflicting objectives: to have a preconditioner which is as similar to \(\bm \Lambda\) as possible and to have a preconditioner which we can invert easily. 277 The first point lends itself toward the use of preconditioners with a similar condition number as \(\bm \Lambda\). 278 However, to invert an arbitrary positive semi-definite matrix, we will often resort to using the conjugate gradient method which is sentitive to the condition number of the preconditioner. 279 Thus, there is an intrinsic balance to be struck between a preconditioner which is very similar to \(\bm \Lambda\) (but whose condition number is problematic) and a preconditioner which is less similar to \(\bm \Lambda\) (and will therefore not improve the conditioning of the problem). 280 281 Let us consider the toy example of a preconditioner which we can guarantee to have the same eigenvectors as \(\bm \Lambda\) and whose eigenvalues we can freely choose. 282 To do so, one could hypothetically attempt to compute the eigenvectors of \(\bm \Lambda\) and then use these eigenvectors as the columns of the preconditioner. 283 % Depending on the rank of the preconditioner, we can then eliminate as many of the largest / smallest eigenvalues of \(\bm \Lambda\) as the rank of the preconditioner. 284 If we have a rank-one preconditioner whose only eigenvector is the largest eigenvector of \(\bm \Lambda\), then the eigenvalue corresponding to this eigenvector becomes one and the second-largest eigenvalue becomes the largest (assuming that the second-largest eigenvalue is greater than one). 285 Thus, in the best-case scenario, you can then eliminate the largest eigenvalue of \(\bm \Lambda\) by using a rank-one preconditioner. 286 Thus, if we have a rank-\(K\) preconditioner, then we can eliminate at most the \(K\) challenging (largest or smallest) eigenvalues of \(\bm \Lambda\) by using a rank-\(K\) preconditioner. 287 The steeper the spectrum of \(\bm \Lambda\), the more effective this preconditioning will be (however, in this case, \(\bm \Lambda\) will probably also tend to be worse conditioned). 288 % This analogy can be extended in a hand-wavy way to an imperfect preconditioner which does not exactly approximate the eigenvalue 289 For a preconditioner with a condition number of \(\kappa^\prime\), we can at best obtain a preconditioned system with conditioning \(\kappa / \kappa^\prime\), where \(\kappa\) is the condition number of \(\bm \Lambda\). 290 This is because we want the highest eigenvalue of the preconditioner to neutralise (i.e., align with) the highest eigenvalue of \(\bm \Lambda\), and the same for the lowest eigenvalues. 291 292 In CIQ we will, in each iteration, perform one matrix-vector product \(\bm \Lambda \bm v\) and one preconditioner-vector product, as seen in \cref{alg:ciq}.%\sidenote{Because of this, to avoid the preconditioner becoming a performance bottleneck, the speed of the preconditioner-vector products should be significantly greater than that of the matrix-vector products.} 293 As such, if the preconditioner-vector product is similarly or more expensive to compute compared to the matrix-vector product, then preconditioning will significantly slow down the algorithm. 294 However, we usually choose preconditioners which are relatively easy to compute relative to the posterior precision itself. 295 Additionally, an effective preconditioner can significantly improve the convergence speed of CIQ, and this effect can outweigh the cost of computing the preconditioner. 296 Thus, we will often choose to use a preconditioner which is not exact, but which is still close to \(\bm \Lambda\) and is cheap to compute. 297 We will now discuss some potential choices for preconditioners to sample using CIQ. 298 299 \subsection{Fully Linear GGN} 300 \label{sec:adam-preconditioner} 301 302 One of the simplest preconditioners is similar to the second moment estimation used in Adam (\cref{sec:adam}). 303 If we calculate the GGN approximation of the Hessian by linearizing over the whole likelihood (as described in \cref{sec:practical-ggn}), we get 304 \begin{align} 305 \bm P ={} & \nabla \nabla\T + \alpha \identity \label{eq:adam-preconditioner} 306 \\ \bm P^{-1} ={}& \beta \cdot \nabla \nabla\T + \alpha^{-1} \identity \label{eq:adam-preconditioner-inverse} 307 \\ \beta ={}& \left(\alpha \norm{\nabla}^2 + \norm{\nabla}^4\right)^{-1} - \left(\alpha \norm{\nabla}^2\right)^{-1}, \label{eq:adam-preconditioner-beta} 308 \end{align} 309 where \(\nabla\) is the gradient of the loss with regard to the model parameters. 310 Since \cref{eq:adam-preconditioner} is in the form of a diagonal perturbation to a vector outer product, we have the closed-form inverse given by \cref{eq:adam-preconditioner-inverse,eq:adam-preconditioner-beta}. 311 The problem with using this approximation is that the rank of the outer product is one and this term determines the largest eigenvalue of the preconditioner. 312 This means that, best-case scenario, we can only reduce the highest eigenvalue of the Hessian. 313 This occurs when the gradient corresponds to the direction of the highest precision (this may not be the case). 314 This would then leave the second-highest eigenvalue unaffected. 315 % If the rank of our precision matrix is greater than one, then this value seems likely to be similarly large to originally the largest eigenvalue. 316 % Therefore, the condition number of our problem will not be significantly reduced. 317 We therefore need to find a higher-rank approximation of the posterior precision. 318 319 \subsection{Sub-Sampling the Data} 320 \label{sec:sub-sampling-preconditioner} 321 322 We could just choose to calculate the true posterior precision over fewer observations and invert it using the conjugate gradient method to use it as a preconditioner. 323 This would have the advantage of being guaranteed to converge to the true posterior precision as the number of preconditioner observations increases. 324 The spectrum of this preconditioner would thus be similar to that of the true posterior precision. 325 Since the lowest eigenvalue of the posterior precision is the prior precision \(\alpha\), we can (even with few preconditioner observations) increase the lowest eigenvalue of the preconditioned system to be equal to one. 326 327 Alternatively, we can estimate the eigenvectors and eigenvalues of \(\textstyle{N / B \sum^B_i} J_i\T J_i\), where \(B\) is the number of preconditioner observations. 328 This would \emph{hopefully} have an eigenbasis that is close to that of \(\textstyle{\sum^N_i} J_i\T J_i\) without needing to calculate the precision matrix over the whole dataset (we may need to guarantee all classes are represented in \(x_i, i \sim \mathcal{B}\)).\sidenote{Empirically, it seems likely that the eigenvectors of the posterior precision are relatively similar across different observations---see \cref{fig:spectrum-mnist} and \cref{sec:spectrum-ggn}.} 329 However, scaling the precondititioner up to the size of the entire dataset will lead to a preconditioner which is ill-conditioned, making inversion via conjugate gradient difficult. 330 331 As mentioned before, each msMINRES iteration performs one matrix-vector product and one preconditioner-vector product. 332 However, even though the preconditioner is constructed in nearly the same way as the sum of GGN matrices in the posterior precision, the preconditioner is significantly cheaper to compute. 333 This is because the preconditioner-vector product will cost \(B / N \cdot t_{\mathrm{GGN}}\), where \(t_{\mathrm{GGN}}\) is the computational cost of performing the GGN-vector product.\sidenote{This analysis assumes that the computation of the GGN-vector product scales linearly in the number of observations used in the GGN matrix. Mathematically speaking, this is a valid assumption, due to the number of elementary operations that are performed in the algorithm. However, the efficient JAX XLA-compiled implementation of Jacobian-vector products can scale differently to na\"ive implementations, so the validity of the assumption is less clear in our case.} 334 Since we will have \(N \gg B\), then the cost of computing the preconditioner will be negligible compared to that of the GGN-vector product. 335 336 % This preconditioner can also be adjusted to cases where the smallest eigenvalue \(\alpha\) is not very small, but for which the largest eigenvalue is very large. 337 % In this case, we can compute this preconditioner with a larger value of \(\alpha\) and a large number of preconditioner observations. 338 % This will lead to a preconditioner which has a larger smallest eigenvalue than the true posterior precision, but whose largest eigenvalue is closer to that of the true posterior precision. 339 % This will lead to a preconditioner which can reduce the largest eigenvalues of the posterior precision. 340 % This may be useful, for instance, in problems for which the eigenvectors of the posterior precision are not constant across the dataset, but are instead highly variable. 341 % However, this preconditioner will be very expensive to compute, since it effectively requires computing the entire posterior precision.\sidenote{MEH Empirically, I found that this is useful for the sine problem, though, which seems to have a very constant posterior precision.} 342 343 \subsection{Using the Woodbury Matrix Identity}[Woodbury] 344 \label{sec:woodbury-preconditioner} 345 346 The Woodbury matrix identity~\cite{woodbury1950inverting} is a useful identity for computing the inverse of a matrix which is the sum of two matrices of which one is low-rank and the other can easily be inverted. 347 The identity is given by 348 \begin{align} 349 \left( \bm A + \bm B \bm C \bm D \right)^{-1} = \bm A^{-1} - \bm A^{-1} \bm B \left( \bm C^{-1} + \bm D \bm A^{-1} \bm B \right)^{-1} \bm D \bm A^{-1}, 350 \end{align} 351 where \(\bm A\) is a \(D \times D\) matrix and can easily be inverted, \(\bm B\) is a \(D \times O\) matrix, \(\bm C\) is a \(O \times O\) matrix, and \(\bm D\) is a \(O \times D\) matrix. 352 This identity can be used to compute the inverse of the GGN matrix with the prior precision as 353 \begin{align} 354 \left( \alpha \identity + \bm J\T \bm H \bm J \right)^{-1} = \alpha^{-1} \identity - \alpha^{-2} \bm J\T \left( \bm H^{-1} + \alpha^{-1} \bm J \bm J\T \right)^{-1} \bm J, 355 \end{align} 356 where \(\alpha\) is the prior precision and \(\bm H \in \reals^{O \times O}\) and \(\bm J \in \reals^{O \times D}\) are the Hessian and Jacobian from \cref{eq:negative-log-likelihood-ggn} evaluated at a single observation. 357 358 Here, to exploit the low-rank structure of the preconditioner, we only compute the GGN matrix over one observation. 359 For this preconditioner to be effective, we assume that the eigenvectors of the GGN matrix are approximately constant across the dataset. 360 This may be a reasonable assumption for many problems, but is not guaranteed to be true. 361 362 However, even if the eigenvectors of the GGN matrix are constant across the dataset, the eigenvalues of this preconditioner should be large enough to reduce the condition number of the posterior precision matrix. 363 To do so, we can scale the outer product \(\bm J \bm J\T\) term by some factor, e.g., the number of observations, \(N\). 364 Since, by scaling up the outer product term we are effectively scaling the eigenvalues of \(\bm J \bm J\T\) by a constant, the condition number of this outer product will remain the same. 365 In this way, we can increase the maximum eigenvalues of the preconditioner without increasing the condition number of the outer product \(\bm J \bm J\T\) which we want to invert using the conjugate gradient method. 366 367 Before the Woodbury matrix identity can be used, the Hessian of the loss with respect to the model outputs \(\bm H\) must first be inverted. 368 To use the conjugate gradient method to invert this Hessian, we would have to perform nested conjugate gradient to also compute the inverse of the outer product term \(\bm H^{-1} + \alpha^{-1} \bm J \bm J\T\), which could be very expensive, depending on the number of outputs. 369 For regression tasks where the Hessian is diagonal (\cref{eq:mse-ggn}), this is not a problem. 370 For classification tasks using the cross-entropy loss, we can compute the inverse of the Hessian as a diagonal and outer product matrix, so this is also not a problem. 371 For models with a very large number of outputs, such as autoencoders, the matrix \(\bm H\) will be fairly large, and so computing its inverse manually can become expensive. 372 However, in the case of autoencoders, the loss is usually the MSE loss, and so has a diagonal Hessian which can be inverted easily. 373 374 Thus, if \(\bm H\) is positive definite (which is necessary for the Laplace approximation to be valid) and easy to invert, then it should be possible to use the Woodbury matrix identity to compute the inverse of this preconditioner. 375 This preconditioner is fast to compute, but may not be effective when the eigenvectors of the GGN matrix are not constant across the dataset. 376 However, due to the effectiveness of the sub-sampled GGN preconditioner and the assumption of stationary eigenvectors, we did not end up using this preconditioner. 377 Additionally, this preconditioner cannot assume a higher rank than the number of outputs \(O\), since adding more observations to the preconditioner will increase its rank to some unknown value and prevent the application of the Woodbury matrix identity. 378 Ideally, we would like to be able to find a preconditioner for which we can choose the rank of the low-rank approximation \(\bm A \approx \sum^N_{i=1} \bm J_i\T \bm H_i \bm J_i\) without simply computing the GGN on a single observation (\(\bm J_i\T \bm H_i \bm J_i \approx \sum^N_{i=1} \bm J_i\T \bm H_i \bm J_i\)), while still allowing us to apply the Woodbury matrix identity, unlike in the case of the sub-sampling preconditioner in \cref{sec:sub-sampling-preconditioner} (\(\sum^B_{i=1} \bm J_i\T \bm H_i \bm J_i \approx \sum^N_{i=1} \bm J_i\T \bm H_i \bm J_i\)). 379 380 \subsection{Pivoted Cholesky} 381 \label{sec:pivoted-cholesky} 382 383 The pivoted Cholesky factorisation is a method for computing a low-rank approximation to the Cholesky decomposition of a matrix~\cite{harbrecht2012low,bach2013sharp}. 384 This decomposition has been successfully applied to preconditioning for kernel matrices in Gaussian processes~\cite{gardner2018gpytorch}, but not yet to preconditioning for the posterior precision of neural networks using the Laplace approximation. 385 Given the posterior precision matrix \(\bm \Lambda\) from the Laplace approximation, we can compute the pivoted Cholesky factorisation as 386 \begin{align} 387 \begin{split} 388 \bm \Lambda ={} & \sum_i^N \bm J_i\T \bm H_i \bm J_i + \alpha \identity 389 \\ \approx{}& \bm L\T \bm L + \alpha \identity, 390 \end{split} 391 \end{align} 392 where \(\bm L \in \reals^{K \times D}\), with \(K\) being the chosen rank of the pivoted Cholesky factorisation and \(D\) being the number of parameters. 393 We can then use this approximation as a preconditioner by computing its inverse via the Woodbury matrix identity, similarly to \cref{sec:woodbury-preconditioner}, as 394 \begin{align} 395 \bm P ={} & \bm L\T \bm L + \alpha \identity, 396 \\ \bm P^{-1} ={}& \alpha^{-1} \identity - \alpha^{-2} \bm L\T \left( \identity + \alpha^{-1} \bm L \bm L\T \right)^{-1} \bm L. 397 \end{align} 398 We then obtain the preconditioner-vector and inverse preconditioner-vector products as in Equation~\ref{eq:pivoted-cholesky-preconditioner}. 399 The most expensive computation in the inverse preconditioner is inverting a \(K \times K\) matrix, where \(K\) is the rank of our pivoted Cholesky factorisation (which we choose). 400 \begin{align}\label{eq:pivoted-cholesky-preconditioner} 401 \bm P \bm v ={} & \bm L\T ( \bm L \bm v ) + \alpha \bm v, 402 \\ \bm P^{-1} \bm v ={}& \alpha^{-1} \bm v - \alpha^{-2} \bm L\T ( \underbrace{\identity + \alpha^{-1} \bm L \bm L\T}_{K \times K} )^{-1} ( \bm L \bm v ). 403 \end{align} 404 Here it is possible to precompute the inverse factorised product given by \(\bm L\T ( \identity + \alpha^{-1} \bm L \bm L\T )^{-1} \in \reals^{D \times K}\), meaning that we only need to perform two matrix multiplications for both the preconditioner- and inverse preconditioner-vector products. 405 This would then require storing the factorisation \(\bm L \in \reals^{K \times D}\) and the inverse factorised product \(\reals^{D \times K}\) matrix.\sidenote{We can also precompute the inverse \(( \identity + \alpha^{-1} \bm L \bm L\T )^{-1} \in \reals^{K \times K}\), which means we would only need to store the factorisation \(\bm L \in \reals^{K \times D}\) and this inverse \(K \times K\) matrix, in which case we would just need to perform one more matrix multiplication (multiplying \(\bm L\T\) with this \(K \times K\) matrix) in every preconditioner call.} 406 407 As explained in \cref{sec:spectrum-ggn}, as we sum over multiple low-rank GGN matrices, we will likely find that the minimum non-zero eigenvalue gets progressively smaller compared to the largest. 408 The condition number will therefore get progressively worse. 409 The preconditioning of \(\sum^N_{i=1} \bm J_i\T \bm H_i \bm J_i\) (which should be low-rank) will progressively become \emph{less} low-rank and its condition number will progressively become higher. 410 411 Regarding the non-zero eigenvalues and eigenvectors, we consider a best-case scenario. 412 Assume that the pivoted Cholesky factorisation finds the \(K\) largest eigenvectors of the precision matrix. 413 If the eigenspectrum of this factorisation is steep enough, then the largest eigenvalue \(s_1\) will be significantly larger than the smallest eigenvalue \(s_K\) (i.e., there will be a steep drop-off from the largest to the smallest non-zero eigenvalue). 414 Since the eigenvectors of each per-observation term \(\bm J_i\T \bm H_i \bm J_i\) will not be the same, when we sum over them, the eigenvalues of the sum will not be the sum of the eigenvalues of the individual Hessians. 415 However, some of the eigenvectors may be close to each other, and so the spectrum of the GGN sum may be approximately the sum of the GGN eigenvalues. 416 Nevertheless, the rank of the total Hessian will increase with the number of observations and so the number of non-zero eigenvalues consequently increase as well. 417 These eigenvalues will be smaller than the summed eigenvalues (because the eigenvectors may not be contained in the same subspace), but they will be larger than the smallest eigenvalue of the per-observation Hessian. 418 This will then lead to a steeper spectrum and a larger condition number of the total Hessian. 419 Thus, when the batch size \(B\) is large, the condition number of the sum of GGN Hessians will increase and the preconditioner will become less effective. 420 How much the condition number increases with the batch size depends on the eigenspectrum of the per-observation Hessians (i.e., how close the eigenvectors are to each other). 421 422 If we want to use the pivoted Cholesky decomposition to approximate \(\sum^B_i \bm J_i\T \bm H_i \bm J_i\), we are largely trying to approximate the higher eigenvectors of the sum. 423 In order for this to be the case, we will want to oversample vectors from this matrix which correspond to the largest eigenvalues. 424 A common approach to sampling the largest eigenvalues is to sample vectors with probability proportional to the diagonal value~\cite{harbrecht2012low}. 425 However, it is not possible to generally and efficiently determine the diagonal of a matrix via matrix-vector products. 426 Often, it is necessary to multiply a vector of zeros with a value of one at the index at which to find the diagonal. 427 This operation needs to be repeated as many times as there are elements in the diagonal with no shared computation between each element, which is very expensive. 428 Alternatively, we can approximate the diagonal of the GGN matrix as the element-wise product of the gradient with itself, similarly to the preconditioner described in \cref{sec:adam-preconditioner} and the GGN approximation computed in \cref{eq:fully-linear-ggn}, though this may not be the most accurate approximation. 429 430 In conclusion, to find the optimal parameters for the pivoted Cholesky preconditioner (the number of observations \(B\) and the rank of the pivoted Cholesky factorisation \(K\)), there are some rules of thumb to consider. 431 Notably, the quality of the preconditioner depends largely on the ease of inverting the \(\identity + \alpha^{-1} \bm L \bm L\T\) matrix, which depends on its condition number. 432 In practice, increasing the rank of the pivoted Cholesky factorisation will increase the condition number of this matrix, since we are approximating more of the largest eigenvectors and so we will capture progressively smaller eigenvalues. 433 434 We have found that it is most effective to keep \(K\) low, such that the \(K \times K\) outer product of the pivoted Cholesky factorisation matrix \(L\) approximates few enough of the largest eigenvectors that the eigenvalue of the largest is close to that of the smallest (meaning this outer product is well-conditioned). 435 We thus need to choose \(K\) such that it is large enough to approximate enough large eigenvectors and small enough to ensure the condition number of the outer product is low. 436 Furthermore, you want enough observations \(B\) to be included in the preconditioner such that it is possible to accurately approximate the eigenvectors of the problem---it may be optimal to use the whole dataset, though this may worsen the conditioning of the outer product. 437 438 Furthermore, the pivoted Cholesky decomposition is not implemented in JAX. 439 Since the pivoted Cholesky decomposition involves incrementally adding columns to the factorisation, and JAX is not friendly towards mutable variables, this preconditioner is not straightforward to implement. 440 Additionally, the pivoted Cholesky decomposition traditionally uses the diagonal of the matrix to determine which columns to add to the factorisation, which is difficult without instantiating the matrix, so we would need to approximate the diagonal of the GGN matrix. 441 442 \subsection{Other Preconditioners} 443 444 The randomly pivoted Cholesky~\cite{chen2022randomly} preconditioner is simple and inexpensive variant of the partial Cholesky decomposition family of algorithms. 445 Similarly to the pivoted Cholesky preconditioner, the randomly pivoted Cholesky preconditioner requires the diagonal of the GGN matrix in order for the decomposition to be close to the GGN. 446 We can thus approximate this term as we do in the pivoted Cholesky preconditioner (see \cref{sec:pivoted-cholesky}). 447 Early tests of the randomly pivoted Cholesky preconditioner were not successful, and so it was not pursued further. 448 449 Additionally, we considered potential preconditioners to be computed as traditional approximations of the posterior precision, like the diagonal approximation or the Kronecker factorisation. 450 These methods have been shown to be relatively effective approximations of the posterior precision in the context of Bayesian neural networks~\cite{daxberger2021laplace}, and so they may contain enough information about the full posterior precision to be effective preconditioners. 451 452 However, due to the speed, effectiveness, and theoretical properties of other preconditioners proposed in this section, these methods were not used. 453 Overall, both the sub-sampled preconditioner and the pivoted Cholesky preconditioners were found to be effective under the correct settings. 454 However, the pivoted Cholesky preconditioner is not easy to implement in JAX and was more difficult to tune than the sub-sampled preconditioner. 455 The sub-sampled preconditioner is thus the preferred preconditioner in this project. 456 We provide a high-level comparison of the various preconditioners we have presented in this chapter in \cref{tab:preconditioner-comparison}. 457 458 \begin{table*} 459 \caption[Comparison of various preconditioners.]{Comparison of various approximations of the likelihood contribution \(\sum_i^N \bm J_i\T \bm H_i \bm J_i\) to the Laplace posterior precision for use in preconditioning. We compare the rank of the approximation, the speed of computing preconditioner-vector products, the closeness of the approximation, and the method used for inverting the preconditioner.} 460 \label{tab:preconditioner-comparison} 461 \include{tables/preconditioner-comparison.tex} 462 \end{table*} 463 464 \section{Sampling Evaluation} 465 \label{sec:sampling-evaluation} 466 467 As we have explained, sampling through CIQ is sensitive to the condition number of the posterior precision. 468 As such, we need a technique for evaluating the quality of the samples drawn from the approximate posterior to determine if sampling occurred correctly. 469 This means evaluating whether the samples are drawn from a normal distribution with the correct mean and precision. 470 471 What we would like to fundamentally test is whether the CIQ algorithm converged correctly and thus whether the samples were generated successfully. 472 However, for cases when sampling fails to converge, this is typically caused by ill-conditioning of the posterior precision matrix. 473 Since we only have access to the precision matrix and computing the covariance involves inverting the precision matrix using, for example, the conjugate gradient method (which is itself sensitive to ill-conditioning), we cannot directly evaluate the covariance of the samples. 474 Instead, we require a method to evaluate the quality of the samples without access to the covariance matrix, which is what we will discuss in the next section. 475 476 \subsection{The Chi-Squared Distribution}[Chi-Squared Distribution] 477 478 As per the definition of the chi-squared distribution, for \(D\)-dimensional standard normal samples \(\bm \varepsilon_0\), the sum of square deviations from the mean is chi-squared distributed with \(D\) degrees of freedom, as per 479 \begin{align} 480 {\bm \varepsilon_0}\T \bm \varepsilon_0 \sim \chi^2(D). 481 \end{align} 482 Samples \(\bm \varepsilon\) which are normally distributed but do not have a mean of zero and a standard deviation of one can be standardised and their sum of square deviations be calculated by computing their squared Mahalanobis distances, which will be chi-squared distributed with \(D\) degrees of freedom, as per 483 % 484 \begin{align} 485 \begin{split} 486 \bm d ={} & \sqrt{(\bm \varepsilon - \bm \mu)\T \bm \Lambda (\bm \varepsilon - \bm \mu)}\label{eq:mahalanobis-distance} 487 \\ \Rightarrow{}& \bm d\T \bm d \sim \chi^2(D). 488 \end{split} 489 \end{align} 490 % 491 This has the advantage of only requiring the computation of the product of the precision matrix \(\bm \Lambda\) with a vector, which we can compute efficiently, as it does not require instantiation of the matrix. 492 For the Laplace approximation, this can easily be computed as $\bm \Lambda = - \nabla^2_{\bm \theta} \log p(\bm \theta \given \bm y)$. 493 Since this precision matrix can be computed exactly, this calculation can be performed to evaluate the whether a set of samples \(\bm \varepsilon\) are drawn from a distribution \(\normal(\bm \mu, \bm \Lambda^{-1})\) while only implicitly accessing this precision matrix in the evaluation via matrix-vector products. 494 To do so, we can compute the squared Mahalanobis distance of each sample \(\bm \varepsilon_i\) from the mean \(\bm \mu\) and compare it to the chi-squared distribution with \(D\) degrees of freedom. 495 We do this by comparing the histogram of these squared Mahalanobis distances to the appropriate PDF and by visualising the sample empirical percentiles against the theoretical percentiles in a quantile-quantile plot. 496 As such, this method can be used to evaluate whether the approximate normal samples obtained from CIQ are correctly distributed. 497 However, this method does not provide a quantitative measure of the quality of the samples. 498 In order to run experiments using the Laplace approximation without manually checking the plots of every set of samples we generate, we also need a method to evaluate the quality of the samples quantitatively. 499 Our current methods do not provide this, and so we need to evaluate the quality of the samples in a different way. 500 501 The Kolmogorov-Smirnov test is a non-parametric test for comparing two continuous distributions. 502 It is a test of whether two samples are drawn from the same distribution. 503 Since the chi-squared distribution is continuous, we can use the Kolmogorov-Smirnov test to compare the chi-squared distribution with \(D\) degrees of freedom to the squared Mahalanobis distances of the samples. 504 This test is performed by computing the Kolmogorov-Smirnov statistic, which is the maximum difference between the empirical and theoretical CDFs. 505 The null hypothesis of the test is that the samples are drawn from the same distribution, and so the test is rejected if the Kolmogorov-Smirnov statistic is greater than the critical value for the test. 506 This means that sample normality is rejected if the p-value of the test is less than the significance level of the test. 507 However, this test is very sensitive to the number of samples used to compute the statistic, and so a large number of samples will lead to a very low p-value even if the samples are drawn from the same distribution. 508 Since we can sample an arbitrarily large number of samples from the approximate posterior, this may lead to low p-values even when sampling succeeds. 509 510 % \section{Limitations} 511 512 % The CIQ algorithm 513 514 % Scaling with data? 515 % Any comments on the model size and how it scales?