Bayesian Optimization
Part I: Foundations
中文

The Linear Algebra of Uncertainty

Section 2.6.3 introduced a table: the covariances between every pair of uncertain quantities, the covariance matrix. A Bayesian optimizer keeps such a table for the objective's values at every input it has evaluated and every input it is considering, so after 50 evaluations the table has 50 rows at the least. Each update of its belief, each prediction, and each fit of the model's own settings is a computation with that table. This chapter is about those computations.

Linear algebra is a large subject, and this chapter takes only the part that the rest of the book uses. It treats a matrix as a map that moves the points of space, because that picture explains the properties that matter: which tables can be covariance matrices, what shape a covariance gives a cloud of uncertainty, how to solve the equations of a Gaussian process without ever inverting a matrix, and what a determinant measures. Two-dimensional pictures carry most of the intuition, but a few facts appear only from three dimensions on, so the chapter also turns a three-dimensional covariance in your hands.

The payoff comes in the next chapter, Chapter 4, where every result here becomes a statement about the Gaussian distribution, and in Chapter 8, where those statements become an algorithm.

3.1 Vectors #

A vector is an ordered list of numbers. Bold lowercase letters name vectors, and by convention a vector is a column:

x=[x1x2⋮xd]∈Rd.\vx = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_d \end{bmatrix} \in \R^d.

The symbol Rd\R^d is the set of all such lists of dd real numbers, and dd is the vector's dimension. Writing x⊤=(x1,…,xd)\vx^\T = (x_1, \dots, x_d), read "x\vx transpose", turns the column into a row.

Vectors appear in this book in two roles, and keeping them apart prevents a common confusion. In the first role a vector is a point in the space being searched: a configuration of hyperparameters, the settings of an exoskeleton, the parameters of a photo filter. Snoek et al. (2012), for instance, tuned nine settings of an image classifier's training procedure at once, so each configuration they tried was a vector in R9\R^9 (Section 9.2 returns to what they found). In the second role a vector holds the values of the objective at a list of inputs, f=(f(x1),…,f(xn))⊤\vf = (f(\vx_1), \dots, f(\vx_n))^\T. Its dimension is the number of inputs, not the number of hyperparameters, and it grows with every evaluation. The covariance matrices of a Gaussian process live in this second space.

Vectors add entry by entry, and multiplying a vector by a number multiplies every entry. Geometrically, x+y\vx + \vy places the arrow of y\vy at the tip of the arrow of x\vx, and 2x2\vx is the same arrow twice as long.

3.1.1 Inner products, lengths, and angles #

The inner product, or dot product, of two vectors of the same dimension multiplies them entry by entry and adds the results:

x⊤y=∑i=1dxiyi.\vx^\T\vy = \sum_{i=1}^d x_i y_i.
(3.1)

The notation reads as a row times a column, which is the rule for multiplying matrices introduced in Section 3.2. From the inner product come length and distance. The norm of a vector, its length, is ∥x∥=x⊤x\lVert\vx\rVert = \sqrt{\vx^\T\vx}, Pythagoras's theorem in dd dimensions, and the distance between two points is the norm of their difference, ∥x−y∥\lVert\vx - \vy\rVert.

The inner product also measures angle. For any two vectors,

x⊤y=∥x∥ ∥y∥cos⁡θ,\vx^\T\vy = \lVert\vx\rVert\,\lVert\vy\rVert \cos\theta,

where θ\theta is the angle between them. Two vectors are orthogonal, perpendicular, when their inner product is zero. Dividing by the two lengths gives cos⁡θ\cos\theta, which is 1 for vectors pointing the same way, 0 for perpendicular ones, and −1-1 for opposite ones. The correlation of Section 2.6.3 is this cosine, computed for centered random variables instead of lists of numbers.

Distance matters to Bayesian optimization because the most common kernels, the functions that decide how strongly the objective's values at two inputs are correlated (Chapter 9), depend on the inputs only through the distance between them. The RBF kernel, k(x,x′)=exp⁡ ⁣(−∥x−x′∥2/2ℓ2)k(\vx, \vx') = \exp\!\left(-\lVert\vx - \vx'\rVert^2 / 2\ell^2\right), says that nearby inputs have similar values and distant ones are unrelated, with the lengthscale ℓ\ell setting what "nearby" means.

3.1.2 Distances in many dimensions #

Our intuition about distance comes from two and three dimensions, and it misleads in more. Scatter points uniformly at random in the unit cube [0,1]d[0, 1]^d. Each coordinate contributes on average 1/61/6 to the squared distance between two points, so the typical distance grows like d/6\sqrt{d/6}: about 0.58 in two dimensions, 1.0 in six, and 2.9 in fifty. The spread of the distances does not grow with it. In a simulation with 200 random points, the standard deviation of the pairwise distances stayed near 0.24 from two dimensions to a hundred. As a result, the ratio of each point's nearest neighbor distance to its farthest neighbor distance, which was 0.04 for a typical point in two dimensions, rose to 0.22 in six dimensions, 0.65 in fifty, and 0.74 in a hundred.

In many dimensions, then, every point is far from every other, and all of them are about equally far. A kernel with a lengthscale suited to a two-dimensional problem would declare every pair of points in a fifty-dimensional one unrelated, and the model would learn nothing from one evaluation about any other. This is one root of the difficulty of Bayesian optimization in many dimensions, and the remedies, such as lengthscales that grow with the dimension and a lengthscale for each input (Section 9.2), start from it (Chapter 30).

Sources cited in Section 3.1 1
  1. Snoek et al. (2012) Practical Bayesian Optimization of Machine Learning Algorithms

3.2 Matrices as transformations #

A matrix is a rectangular table of numbers, written with a bold capital. An m×nm \times n matrix A\mA has mm rows and nn columns, and AijA_{ij} is the entry in row ii and column jj. The table is the matrix's storage format. Its meaning is the map it defines.

3.2.1 The product of a matrix and a vector #

Multiplying an m×nm \times n matrix by a vector of dimension nn gives a vector of dimension mm, whose ii-th entry is the inner product of row ii with the vector:

(Ax)i=∑j=1nAijxj.(\mA\vx)_i = \sum_{j=1}^n A_{ij} x_j.

The same product has a second reading, which is the one to picture. Write the columns of A\mA as a1,…,an\mathbf{a}_1, \dots, \mathbf{a}_n. Then

Ax=x1a1+x2a2+⋯+xnan.\mA\vx = x_1 \mathbf{a}_1 + x_2 \mathbf{a}_2 + \dots + x_n \mathbf{a}_n.
(3.2)

The vector e1=(1,0,…,0)⊤\mathbf{e}_1 = (1, 0, \dots, 0)^\T picks out the first column: Ae1=a1\mA\mathbf{e}_1 = \mathbf{a}_1. So the columns of a matrix record where it sends the coordinate directions, and Equation (3.2) says that every other vector goes where its coordinates say: as much of a1\mathbf{a}_1 as x\vx had of e1\mathbf{e}_1, and so on.

A map of this kind is linear: it sends sums to sums and multiples to multiples, A(x+y)=Ax+Ay\mA(\vx + \vy) = \mA\vx + \mA\vy and A(cx)=c Ax\mA(c\vx) = c\,\mA\vx. In the plane, a linear map sends straight lines to straight lines, keeps the origin in place, and turns a square grid into a grid of parallelograms. The unit circle, the set of vectors of length one, becomes an ellipse.

Ae₁Ae₂AxxA =1.200.600.200.80det A = ad − bc = 0.84The unit square's area is multiplied by 0.84.eigenvalues λ1 = 1.40, λ2 = 0.60angle between x and Ax: 15°unit circle → its imageunit square → parallelogram
Ae₁Ae₂AxxA =1.200.600.200.80det A = ad − bc = 0.84The unit square's area is multiplied by 0.84.eigenvalues λ1 = 1.40, λ2 = 0.60angle between x and Ax: 15°unit circle → its imageunit square → parallelogram
Figure 3.1 A 2×2 matrix as a map of the plane. The magenta and green arrows are the columns of A\mA, the images of the two coordinate directions; drag their tips to change the matrix. The dashed circle and square are the unit circle and unit square, the blue ellipse and yellow parallelogram are their images, and the faint blue lines are the image of the grid. The black arrows are a vector x\vx on the unit circle and its image Ax\mA\vx; drag x\vx around the circle. The readouts give the determinant and the eigenvalues, which Section 3.4 and Section 3.6 explain.

Some things to try:

  • Drag the tip of Ae1\mA\mathbf{e}_1. Only the first column changes, and the whole grid follows: every point moves by its first coordinate times the change.
  • Choose the Rotation, Shear, and Stretch presets. A rotation turns the circle without changing its shape; a stretch scales the two axes by different amounts; a shear slides one axis along the other and turns the square into a slanted parallelogram of the same area.
  • Choose Singular. The two columns point the same way, the ellipse collapses to a line segment, and the whole plane is flattened onto a line. Every point on that line came from infinitely many points, so the map cannot be undone.

3.2.2 Composition, identity, and inverse #

Applying B\mathbf{B} and then A\mA is again a linear map, and its matrix is the product AB\mA\mathbf{B}, whose columns are A\mA applied to the columns of B\mathbf{B}. Entry by entry, (AB)ij=∑kAikBkj(\mA\mathbf{B})_{ij} = \sum_k A_{ik} B_{kj}, the inner product of row ii of A\mA with column jj of B\mathbf{B}. Order matters: stretching and then rotating is not the same as rotating and then stretching, so in general AB≠BA\mA\mathbf{B} \ne \mathbf{B}\mA. Multiplying two n×nn \times n matrices this way takes n3n^3 multiplications, one for each combination of ii, jj, and kk.

The identity matrix I\mI, with ones on the diagonal and zeros elsewhere, leaves every vector where it is. A square matrix A\mA is invertible when another matrix A−1\mA^{-1} undoes it, A−1A=AA−1=I\mA^{-1}\mA = \mA\mA^{-1} = \mI. A matrix is invertible exactly when it does not flatten space: when no nonzero vector is sent to zero, so no two points land on the same image. The Singular preset of Figure 3.1 is the opposite case. Inverting a product reverses the order, (AB)−1=B−1A−1(\mA\mathbf{B})^{-1} = \mathbf{B}^{-1}\mA^{-1}, since the last map applied must be the first one undone.

3.2.3 Transpose #

The transpose A⊤\mA^\T flips a matrix across its diagonal, so that (A⊤)ij=Aji(\mA^\T)_{ij} = A_{ji} and an m×nm \times n matrix becomes n×mn \times m. A column vector is an n×1n \times 1 matrix, its transpose a 1×n1 \times n row, and the inner product x⊤y\vx^\T\vy is the product of a row with a column. The reverse product xy⊤\vx\vy^\T, a column times a row, is an n×nn \times n matrix called the outer product. Transposing a product also reverses the order, (AB)⊤=B⊤A⊤(\mA\mathbf{B})^\T = \mathbf{B}^\T\mA^\T.

The shapes in Chapter 8 follow these rules. With nn observed inputs and mm inputs to predict at, the kernel matrix K\mK among observed inputs is n×nn \times n, the matrix K∗\mK_* between observed and new inputs is n×mn \times m, and the posterior mean K∗⊤K−1y\mK_*^\T\mK^{-1}\vy multiplies an m×nm \times n matrix, an n×nn \times n matrix, and an nn-vector to give one prediction for each of the mm new inputs.

3.3 Symmetric and positive definite matrices #

Not every square table of numbers can be a covariance matrix. This section finds the condition, which turns out to be geometric: a covariance matrix must not let any direction have negative variance.

3.3.1 Quadratic forms #

A matrix is symmetric when it equals its transpose, Aij=AjiA_{ij} = A_{ji}. A covariance matrix is symmetric because Cov⁡[xi,xj]=Cov⁡[xj,xi]\Cov[x_i, x_j] = \Cov[x_j, x_i], and so is a kernel matrix, because k(x,x′)=k(x′,x)k(\vx, \vx') = k(\vx', \vx).

For a symmetric matrix A\mA, the expression w⊤Aw\vw^\T\mA\vw is a single number for each vector w\vw, called a quadratic form. In two dimensions, with A=[abbd]\mA = \begin{bmatrix} a & b \\ b & d \end{bmatrix},

w⊤Aw=aw12+2b w1w2+d w22,\vw^\T\mA\vw = a w_1^2 + 2b\, w_1 w_2 + d\, w_2^2,

a quadratic polynomial in the entries of w\vw. Quadratic forms appear whenever a covariance matrix meets a weighted sum, because of the following identity.

Derivation The variance of a weighted sum

Let x\vx be a random vector with mean μ\vmu and covariance matrix Σ\mSigma, and let w\vw be a fixed vector of weights.

  1. The weighted sum is w⊤x=∑iwixi\vw^\T\vx = \sum_i w_i x_i, with mean w⊤μ\vw^\T\vmu by linearity of expectation (Equation (2.10)).
  2. By the definition of variance (Equation (2.11)), Var⁡[w⊤x]=E[(∑iwi(xi−μi))2]\Var[\vw^\T\vx] = \E\big[(\sum_i w_i (x_i - \mu_i))^2\big].
  3. Expanding the square of the sum gives every pairwise product: E[∑i∑jwiwj(xi−μi)(xj−μj)]\E\big[\sum_i \sum_j w_i w_j (x_i - \mu_i)(x_j - \mu_j)\big].
  4. By linearity of expectation, this is ∑i∑jwiwj E[(xi−μi)(xj−μj)]=∑i∑jwiwjΣij\sum_i \sum_j w_i w_j\, \E[(x_i - \mu_i)(x_j - \mu_j)] = \sum_i \sum_j w_i w_j \Sigma_{ij}, by the definition of covariance.
  5. The double sum is the quadratic form, so Var⁡[w⊤x]=w⊤Σw\Var[\vw^\T\vx] = \vw^\T\mSigma\vw.

3.3.2 Every direction has nonnegative variance #

A variance cannot be negative. By the derivation, a covariance matrix must therefore satisfy w⊤Σw≥0\vw^\T\mSigma\vw \ge 0 for every w\vw. This property has a name.

Definition 3.1 Positive definite and semidefinite matrices

A symmetric matrix A\mA is positive semidefinite if w⊤Aw≥0\vw^\T\mA\vw \ge 0 for every vector w\vw, and positive definite if w⊤Aw>0\vw^\T\mA\vw > 0 for every nonzero w\vw.

Read with A=Σ\mA = \mSigma, positive definiteness says that every direction in the space has positive variance: no weighted combination of the variables is known exactly. A covariance matrix that is only semidefinite has some combination with zero variance, a combination that is fixed. That happens in a Gaussian process when two observed inputs coincide: the values f(x)f(\vx) and f(x′)f(\vx') are then the same random variable, and f(x)−f(x′)f(\vx) - f(\vx') is exactly zero. Every result below assumes the strict version, which is why implementations add a little noise or "jitter" to the diagonal (Section 8.4).

In two dimensions the condition is easy to check. A covariance matrix with standard deviations σ1,σ2\sigma_1, \sigma_2 and correlation ρ\rho is positive definite exactly when σ1,σ2>0\sigma_1, \sigma_2 > 0 and −1<ρ<1-1 < \rho < 1, and every correlation in that range is possible. From three dimensions on, this is no longer enough.

3.3.3 Three correlations that cannot coexist #

Suppose three quantities each have variance 1, the first is correlated 0.80.8 with the second and 0.80.8 with the third, and the second and third are uncorrelated:

Σ=[10.80.80.8100.801].\mSigma = \begin{bmatrix} 1 & 0.8 & 0.8 \\ 0.8 & 1 & 0 \\ 0.8 & 0 & 1 \end{bmatrix}.

Each correlation is between −1-1 and 11, and each pair on its own is a valid two-dimensional covariance. Yet the weights w=(−2,1,1)⊤\vw = (-2, 1, 1)^\T give

w⊤Σw=4+1+1+2 (−2)(0.8)+2 (−2)(0.8)+2 (1)(0)=−0.4,\vw^\T\mSigma\vw = 4 + 1 + 1 + 2\,(-2)(0.8) + 2\,(-2)(0.8) + 2\,(1)(0) = -0.4,

a negative variance for the quantity x2+x3−2x1x_2 + x_3 - 2x_1. No three random variables have these correlations. Intuitively, if x1x_1 moves closely with x2x_2 and closely with x3x_3, then x2x_2 and x3x_3 must move together to some extent; they cannot be unrelated. Exercise 3.2 finds how strongly they must be correlated.

This is not a curiosity. In finance, tables of correlations between many assets can fail to be positive semidefinite, and the problem of repairing such a table into the nearest valid correlation matrix has a literature of its own (Higham, 2002). For a Gaussian process it is the reason a kernel cannot be an arbitrary similarity score. A natural-looking rule such as "two inputs are fully correlated if they are closer than 1 and uncorrelated otherwise" fails on the inputs 00, 0.60.6, and 1.21.2: the first two are fully correlated, the last two are fully correlated, and the first and last are uncorrelated, an impossible triple of the same kind. The kernels of Chapter 9 are built so that this can never happen: a valid kernel is one whose matrices are positive semidefinite for every choice of inputs, and no other function is (Section 7.2).

Figure 3.2 in the next section lets you try to build such a matrix and see what goes wrong.

Sources cited in Section 3.3 1
  1. Higham (2002) Computing the Nearest Correlation Matrix: A Problem from Finance

3.4 Eigenvectors: the axes a matrix stretches #

The entries of a matrix say little at a glance about what it does. In Figure 3.1, most vectors x\vx are both turned and stretched by A\mA. Some directions, though, are only stretched: the image Ax\mA\vx points along x\vx itself. If we could find those directions, we could describe the map as a set of independent stretches.

Definition 3.2 Eigenvector and eigenvalue

A nonzero vector u\mathbf{u} is an eigenvector of a square matrix A\mA, with eigenvalue λ\lambda, if Au=λu\mA\mathbf{u} = \lambda\mathbf{u}.

An eigenvector is a direction the matrix does not turn; its eigenvalue is the factor by which the direction is stretched, and a negative eigenvalue reverses it. In Figure 3.1, drag x\vx around the circle until x\vx and Ax\mA\vx line up, and the readout reports the eigenvalue. For the default matrix this happens near 18°18°, with eigenvalue 1.4, and near 135°135°, with eigenvalue 0.6. Turn on Show eigenvectors to see both directions. The Rotation preset has no real eigenvectors at all, because it turns every direction.

3.4.1 Symmetric matrices #

For a general matrix the eigenvectors need not be perpendicular, as the default matrix shows. Symmetric matrices, the only kind a covariance can be, are much better behaved.

Theorem 3.1 Spectral theorem for symmetric matrices

A symmetric d×dd \times d matrix A\mA has dd real eigenvalues λ1,…,λd\lambda_1, \dots, \lambda_d and an orthonormal set of eigenvectors u1,…,ud\mathbf{u}_1, \dots, \mathbf{u}_d: unit vectors that are pairwise perpendicular. Collecting the eigenvectors as the columns of a matrix U\mathbf{U} and the eigenvalues on the diagonal of a matrix Λ\bm{\Lambda},

A=UΛU⊤=∑i=1dλi uiui⊤.\mA = \mathbf{U}\bm{\Lambda}\mathbf{U}^\T = \sum_{i=1}^d \lambda_i\, \mathbf{u}_i\mathbf{u}_i^\T.

The proof that the eigenvalues are real takes more space than it deserves here (Strang, 2016), but the perpendicularity is short.

Derivation Eigenvectors of a symmetric matrix are perpendicular

Let Au=λu\mA\mathbf{u} = \lambda\mathbf{u} and Av=μv\mA\mathbf{v} = \mu\mathbf{v} with λ≠μ\lambda \ne \mu.

  1. Multiply the first equation on the left by v⊤\mathbf{v}^\T: v⊤Au=λ v⊤u\mathbf{v}^\T\mA\mathbf{u} = \lambda\, \mathbf{v}^\T\mathbf{u}.
  2. Transpose the left side, a single number, which leaves it unchanged: v⊤Au=u⊤A⊤v=u⊤Av\mathbf{v}^\T\mA\mathbf{u} = \mathbf{u}^\T\mA^\T\mathbf{v} = \mathbf{u}^\T\mA\mathbf{v}, using A⊤=A\mA^\T = \mA.
  3. By the second equation, u⊤Av=μ u⊤v\mathbf{u}^\T\mA\mathbf{v} = \mu\, \mathbf{u}^\T\mathbf{v}.
  4. So λ v⊤u=μ u⊤v\lambda\, \mathbf{v}^\T\mathbf{u} = \mu\, \mathbf{u}^\T\mathbf{v}, and since u⊤v=v⊤u\mathbf{u}^\T\mathbf{v} = \mathbf{v}^\T\mathbf{u}, (λ−μ) u⊤v=0(\lambda - \mu)\, \mathbf{u}^\T\mathbf{v} = 0.
  5. Because λ≠μ\lambda \ne \mu, u⊤v=0\mathbf{u}^\T\mathbf{v} = 0.

The decomposition reads from right to left as a recipe. U⊤\mathbf{U}^\T turns space so that the eigenvectors line up with the coordinate axes, Λ\bm{\Lambda} stretches each axis by its eigenvalue, and U\mathbf{U} turns space back. A symmetric matrix is a set of perpendicular stretches, nothing more. Choose the Symmetric preset in Figure 3.1 and turn on the eigenvectors: they are perpendicular, and they are the axes of the ellipse.

3.4.2 Eigenvalues and positive definiteness #

The spectral theorem turns the definition of positive definiteness into a condition on the eigenvalues.

Derivation Positive definite means positive eigenvalues

Let A=UΛU⊤\mA = \mathbf{U}\bm{\Lambda}\mathbf{U}^\T be symmetric, and let w\vw be any vector.

  1. Substitute the decomposition: w⊤Aw=w⊤UΛU⊤w\vw^\T\mA\vw = \vw^\T\mathbf{U}\bm{\Lambda}\mathbf{U}^\T\vw.
  2. Write c=U⊤w\mathbf{c} = \mathbf{U}^\T\vw, the coordinates of w\vw along the eigenvectors, ci=ui⊤wc_i = \mathbf{u}_i^\T\vw. Then w⊤Aw=c⊤Λc=∑iλici2\vw^\T\mA\vw = \mathbf{c}^\T\bm{\Lambda}\mathbf{c} = \sum_i \lambda_i c_i^2.
  3. If every λi>0\lambda_i > 0, the sum is positive whenever some ci≠0c_i \ne 0, which holds for every nonzero w\vw because U\mathbf{U} is invertible.
  4. Conversely, choosing w=uj\vw = \mathbf{u}_j gives cj=1c_j = 1 and every other ci=0c_i = 0, so w⊤Aw=λj\vw^\T\mA\vw = \lambda_j, which must be positive.

For a covariance matrix the derivation says more. Take w\vw of length one, a direction. Its coordinates along the eigenvectors then satisfy ∑ici2=1\sum_i c_i^2 = 1, so the variance in direction w\vw is a weighted average of the eigenvalues, with weights ci2c_i^2. The eigenvalues are the variances along the eigenvectors, and every other direction's variance lies between the smallest and the largest. The eigenvector with the largest eigenvalue is the direction in which the uncertainty is widest. The shape of the uncertainty is an ellipsoid whose axes point along the eigenvectors, with half-lengths λi\sqrt{\lambda_i} (the standard deviations along the axes), which Section 4.2.1 derives for the Gaussian.

3.4.3 A covariance in three dimensions #

In two dimensions the ellipse summarizes everything. In three, two things appear that the plane cannot show. Figure 3.2 draws the ellipsoid of a three-dimensional covariance, {x:x⊤Σ−1x=1}\{\vx : \vx^\T\mSigma^{-1}\vx = 1\}, the set of points one standard deviation from the center in the sense that Section 4.2.1 makes precise, together with its three axes and its shadows on the walls of the cube. Beside it are the three pairwise views, the ellipses of the 2×22 \times 2 blocks of Σ\mSigma that involve two of the three coordinates.

positive definite: the ellipsoid existsx1x2x3√λ1√λ2√λ3pairwise views (2×2 blocks of Σ)x1, x2ρ = 0.70x1, x3ρ = 0.40x2, x3ρ = 0.20Σ =1.440.760.340.760.810.130.340.130.49eigenvalues 2.02, 0.45, 0.26det Σ = λ1 λ2 λ3 = 0.241Cholesky diagonal L11, L22, L33 = 1.20, 0.64, 0.64
positive definite: the ellipsoid existsx1x2x3√λ1√λ2√λ3pairwise views (2×2 blocks of Σ)x1, x2ρ = 0.70x1, x3ρ = 0.40x2, x3ρ = 0.20Σ =1.440.760.340.760.810.130.340.130.49eigenvalues 2.02, 0.45, 0.26det Σ = λ1 λ2 λ3 = 0.241Cholesky diagonal L11, L22, L33 = 1.20, 0.64, 0.64
Figure 3.2 A 3×3 covariance matrix as an ellipsoid. The sliders set three correlations and three standard deviations; drag the cube to turn it. The black lines are the eigenvector axes with half-lengths λi\sqrt{\lambda_i}, the gray shapes on the back walls are the ellipsoid's shadows, and the three panels beside the cube are the pairwise views, which match the shadows. After An impossible triple, each pairwise view is still a valid ellipse, but the matrix is not positive definite: no ellipsoid exists, one eigenvalue is negative (the red direction), and the Cholesky factorization of Section 3.5 fails. Show samples draws points Lz\mL\vz from a vector z\vz of independent random numbers with mean 0 and variance 1. The values are illustrative.

Some things to try:

  • Turn the cube. The ellipsoid has three perpendicular axes, the eigenvectors, and from most angles none of them is aligned with a coordinate axis. The readout lists the eigenvalues; their square roots are the axes' half-lengths.
  • Compare the shadows with the pairwise views. They are the same ellipses. The shadow of a covariance's ellipsoid on the plane of two coordinates is the ellipse of the corresponding 2×22 \times 2 block, which is why ignoring a variable of a Gaussian amounts to deleting its row and column (Section 4.4).
  • Press An impossible triple. The pairwise views show three valid ellipses, with correlations 0.8, 0.8, and 0. The ellipsoid vanishes, and the red dashed line marks the direction whose variance would be negative, the eigenvector of the eigenvalue −0.13-0.13. Now raise ρ23\rho_{23} slowly: the ellipsoid reappears once ρ23\rho_{23} passes 0.28 (at 0.29 on the slider), as a flat disk that thickens as ρ23\rho_{23} grows.
  • Press A chain. Here x1x_1 is linked to x2x_2, and x2x_2 to x3x_3, with correlation 0.8 each, and ρ13=0.64\rho_{13} = 0.64 is the product of the two. The ellipsoid becomes a cigar along the diagonal, with one eigenvalue much larger than the others: most of the uncertainty lies along a single direction.

The first lesson is that pairwise checks do not certify a covariance matrix; its validity is a property of the whole table. The second is that a covariance can be nearly flat in a direction that no single variable reveals: none of the pairwise views of a nearly singular matrix need look degenerate.

3.4.4 The eigenvalues of a kernel matrix #

That second lesson is the everyday situation of a Gaussian process. The kernel matrix of 100 inputs spaced evenly in [0,1][0, 1], under an RBF kernel with lengthscale 0.1, is a 100×100100 \times 100 positive definite matrix in exact arithmetic. Its largest eigenvalues are 23.9, 21.2, and 17.5, but they decay so quickly that only 28 of the 100 exceed 10−1010^{-10}. In double-precision arithmetic the smallest ones are lost in rounding error; in our run with NumPy the smallest computed eigenvalue even came out slightly negative, −4×10−15-4 \times 10^{-15}. The function values at nearby inputs are so strongly correlated that most directions in the 100-dimensional space have almost no variance. In the same run, NumPy's Cholesky factorization of this matrix failed; adding 10−610^{-6} to the diagonal, the jitter of Section 8.4, makes it succeed. A smooth prior over functions has, in effect, far fewer than 100 independent directions of uncertainty, which is part of why a Gaussian process can learn a smooth function from a few evaluations.

The ratio of the largest to the smallest eigenvalue, the condition number, measures how close to singular a matrix is. Double-precision numbers carry about 16 significant decimal digits, and solving a system with a matrix of condition number 10k10^k can lose about kk of them (Golub and Van Loan, 2013). Kernel matrices of smooth kernels on closely spaced inputs routinely reach condition numbers near 101610^{16}, where no digits remain, so jitter or observation noise is a numerical necessity, not a modeling choice.

Eigenvalues of exactly zero carry information too. In Section 18.5, the eigenvalues of a matrix built from a set of pairwise comparisons count the directions in which the comparisons say nothing at all about a person's preferences.

Sources cited in Section 3.4 2
  1. Strang (2016) Introduction to Linear Algebra
  2. Golub and Van Loan (2013) Matrix Computations

3.5 Solving systems without inverting #

The formulas of Gaussian process regression are full of inverse matrices: the posterior mean k(x)⊤K−1y\vk(\vx)^\T\mK^{-1}\vy and the variance k(x,x)−k(x)⊤K−1k(x)k(\vx, \vx) - \vk(\vx)^\T\mK^{-1}\vk(\vx) of Equation (8.3). Read literally, they say: invert K\mK, then multiply. Numerical practice says never to do that. This section explains why, and what to do instead.

3.5.1 Why not invert #

A formula containing K−1y\mK^{-1}\vy never needs K−1\mK^{-1} itself. It needs the vector α\bm{\alpha} that solves the linear system Kα=y\mK\bm{\alpha} = \vy. Two reasons favor solving over inverting. Forming the inverse costs more, several times as much as the factorization used below. And the computed inverse is less accurate: multiplying by a rounded inverse loses more digits than solving directly with a factorization (Golub and Van Loan, 2013). When the matrix is badly conditioned, as kernel matrices often are, the difference decides whether the answer is usable.

3.5.2 Triangular systems #

Some systems are easy. A matrix is lower triangular when every entry above the diagonal is zero. The system Lz=b\mL\vz = \mathbf{b} with such a matrix can be solved one entry at a time, top to bottom: the first equation involves only z1z_1, the second only z1z_1 and z2z_2, and so on.

zi=1Lii(bi−∑k<iLikzk),i=1,…,n.z_i = \frac{1}{L_{ii}}\Big(b_i - \sum_{k < i} L_{ik} z_k\Big), \qquad i = 1, \dots, n.
(3.3)

This forward substitution costs about n2n^2 operations, against n3n^3 for general methods. An upper triangular system, with zeros below the diagonal, is solved the same way from the bottom up, by back substitution. The whole strategy for symmetric positive definite systems is to reduce them to two triangular ones.

3.5.3 The Cholesky factorization #

Definition 3.3 Cholesky factorization

Every symmetric positive definite matrix A\mA can be written uniquely as

A=LL⊤,\mA = \mL\mL^\T,
(3.4)

where L\mL is lower triangular with positive diagonal entries. L\mL is the Cholesky factor of A\mA.

The factor is a square root of the matrix. For a 1×11 \times 1 matrix [a][a] it is [a][\sqrt{a}], which exists exactly when a>0a > 0. The 2×22 \times 2 case shows how the general algorithm works and why it needs positive definiteness.

Derivation The Cholesky factor of a 2×2 matrix

Let A=[abbd]\mA = \begin{bmatrix} a & b \\ b & d \end{bmatrix} and look for L=[l110l21l22]\mL = \begin{bmatrix} l_{11} & 0 \\ l_{21} & l_{22} \end{bmatrix}.

  1. Multiplying out, LL⊤=[l112l11l21l11l21l212+l222]\mL\mL^\T = \begin{bmatrix} l_{11}^2 & l_{11}l_{21} \\ l_{11}l_{21} & l_{21}^2 + l_{22}^2 \end{bmatrix}.
  2. Match the top-left entry: l112=al_{11}^2 = a, so l11=al_{11} = \sqrt{a}, which needs a>0a > 0.
  3. Match the off-diagonal entry: l11l21=bl_{11}l_{21} = b, so l21=b/al_{21} = b / \sqrt{a}.
  4. Match the bottom-right entry: l212+l222=dl_{21}^2 + l_{22}^2 = d, so l22=d−b2/al_{22} = \sqrt{d - b^2/a}, which needs d−b2/a>0d - b^2/a > 0.
  5. Both conditions hold exactly when A\mA is positive definite: aa is the variance in direction e1\mathbf{e}_1, and a (d−b2/a)=ad−b2a\,(d - b^2/a) = ad - b^2 is the determinant, the product of the two eigenvalues (Section 3.6).

The quantity d−b2/ad - b^2/a in step 4 is a first glimpse of the Schur complement of Section 3.7. For a covariance matrix with a=σ12a = \sigma_1^2, b=ρσ1σ2b = \rho\sigma_1\sigma_2, and d=σ22d = \sigma_2^2, it equals σ22(1−ρ2)\sigma_2^2(1 - \rho^2): the variance of the second variable that remains once the first is known, as Section 4.5 will show.

The general algorithm fills in L\mL one column at a time, in the same way.

Algorithm 3.1 Cholesky factorization

Input: a symmetric n×nn \times n matrix A\mA.

  1. For each column j=1,…,nj = 1, \dots, n:
  2. Compute s=Ajj−∑k<jLjk2s = A_{jj} - \sum_{k < j} L_{jk}^2. If s≤0s \le 0, stop: A\mA is not positive definite.
  3. Set Ljj=sL_{jj} = \sqrt{s}.
  4. For each row i=j+1,…,ni = j + 1, \dots, n, set Lij=(Aij−∑k<jLikLjk)/LjjL_{ij} = \big(A_{ij} - \sum_{k < j} L_{ik}L_{jk}\big) / L_{jj}.

Step 2 makes the algorithm a test as well as a factorization: it succeeds exactly when the matrix is positive definite, and it is the cheapest such test in practice. In Figure 3.2, the impossible triple fails at the third column, where ss comes out negative. The algorithm costs about n3/3n^3/3 floating-point operations (Golub and Van Loan, 2013).

3.5.4 Solving with the factor #

With A=LL⊤\mA = \mL\mL^\T, the system Ax=b\mA\vx = \mathbf{b} splits into two triangular systems. First solve Lz=b\mL\vz = \mathbf{b} by forward substitution, then L⊤x=z\mL^\T\vx = \vz by back substitution. Then Ax=L(L⊤x)=Lz=b\mA\vx = \mL(\mL^\T\vx) = \mL\vz = \mathbf{b}, as required. Once L\mL is known, each new right-hand side costs only O(n2)O(n^2). This is Algorithm 8.1: one factorization of the kernel matrix, then triangular solves for the weights α\bm{\alpha} and for each predictive variance.

In code NumPy and SciPy
import numpy as np
from scipy.linalg import cho_factor, cho_solve, solve_triangular

A = np.array([[4.0, 2.0], [2.0, 3.0]])
b = np.array([2.0, 4.0])

L = np.linalg.cholesky(A)                  # lower triangular, L @ L.T == A
z = solve_triangular(L, b, lower=True)     # forward substitution
x = solve_triangular(L.T, z, lower=False)  # back substitution
print(x)                                   # [-0.25  1.5 ]

c = cho_factor(A)                          # the same, packaged
print(cho_solve(c, b))                     # [-0.25  1.5 ]

3.5.5 The cost, in practice #

The cubic cost decides how many observations an exact Gaussian process can handle. Table 3.1 gives timings measured with NumPy in double precision on the laptop used to prepare this chapter (an Apple M5 Pro); other machines will differ, but the growth rate will not.

Table 3.1 Time to factorize and to invert a symmetric positive definite n × n matrix with NumPy, and the memory to store it (8 bytes per entry). Measured once on one laptop; the ratios are the point, not the absolute numbers.
nn Cholesky Inverse Memory
500 0.4 ms 2.3 ms 2 MB
1,000 2.0 ms 10 ms 8 MB
2,000 14 ms 82 ms 32 MB
4,000 132 ms 625 ms 128 MB

Doubling nn multiplies the time by roughly eight, as n3n^3 predicts, and the inverse costs about five times the factorization. A Bayesian optimization run rarely has more than a few hundred observations, so a single factorization is cheap. Fitting the kernel's own settings, such as its lengthscale, is what multiplies the cost, because every step of that fit refactorizes the matrix (Section 9.3). For larger data sets, the library GPyTorch replaces the factorization by conjugate gradients, an iterative method that needs only products of the kernel matrix with vectors, and runs them on graphics processors; its authors report that this reduces the asymptotic cost of exact inference from O(n3)O(n^3) to O(n2)O(n^2) (Gardner et al., 2018).

3.5.6 The square root that makes samples #

The Cholesky factor is also a recipe for building correlated randomness out of independent randomness. If z\vz has independent coordinates with variance 1, then Lz\mL\vz has covariance LL⊤=Σ\mL\mL^\T = \mSigma, a consequence of the rule Cov⁡[Az]=ACov⁡[z]A⊤\Cov[\mA\vz] = \mA\Cov[\vz]\mA^\T that Section 4.3 derives. Turn on Show samples in Figure 3.2 to see 160 such points: each is Lz\mL\vz for a draw z\vz of three independent random numbers with mean 0 and variance 1 (standard normal numbers, the bell curve of Section 4.1.1), and together they fill the ellipsoid's shape. Because L\mL is lower triangular, the first coordinate uses only z1z_1, the second z1z_1 and z2z_2, and the third all three, so each new coordinate is built from the randomness of the earlier ones plus a fresh part of its own. This is how the posterior samples of Section 8.5 are drawn.

Sources cited in Section 3.5 2
  1. Golub and Van Loan (2013) Matrix Computations
  2. Gardner et al. (2018) GPyTorch: Blackbox Matrix-Matrix Gaussian Process Inference with GPU Acceleration

3.6 Determinants as volume #

The density of a multivariate Gaussian (Section 4.2) and the marginal likelihood used to fit kernels (Section 9.3) both contain a determinant. Its meaning is geometric.

The determinant of a square matrix, det⁡A\det\mA or ∣A∣\lvert\mA\rvert, is the factor by which the map A\mA multiplies areas in two dimensions, volumes in three, and dd-dimensional volumes in general. Its sign records whether the map flips orientation, as a mirror does. For a 2×22 \times 2 matrix,

det⁡[abcd]=ad−bc,\det \begin{bmatrix} a & b \\ c & d \end{bmatrix} = ad - bc,

the signed area of the parallelogram spanned by the two columns. This is the yellow parallelogram in Figure 3.1, the image of the unit square; the Singular preset flattens it to zero area, and the Reflection preset turns the determinant negative.

Three properties follow from the picture. Applying two maps multiplies their volume factors, so det⁡(AB)=det⁡A⋅det⁡B\det(\mA\mathbf{B}) = \det\mA \cdot \det\mathbf{B}, and in particular det⁡(A−1)=1/det⁡A\det(\mA^{-1}) = 1/\det\mA. A matrix is invertible exactly when its determinant is nonzero, when it does not flatten space. And for a symmetric matrix A=UΛU⊤\mA = \mathbf{U}\bm{\Lambda}\mathbf{U}^\T, the rotations U\mathbf{U} and U⊤\mathbf{U}^\T preserve volume while Λ\bm{\Lambda} stretches axis ii by λi\lambda_i, so

det⁡A=∏i=1dλi.\det\mA = \prod_{i=1}^d \lambda_i.
(3.5)

For a covariance matrix the determinant is therefore a single number for the overall size of the uncertainty, sometimes called the generalized variance. The ellipsoid of Figure 3.2 has half-axes λi\sqrt{\lambda_i} and so volume 43πλ1λ2λ3=43πdet⁡Σ\frac{4}{3}\pi\sqrt{\lambda_1\lambda_2\lambda_3} = \frac{4}{3}\pi \sqrt{\det\mSigma}. Correlation shrinks it: for three variables with unit variance and correlations ρ12,ρ13,ρ23\rho_{12}, \rho_{13}, \rho_{23},

det⁡Σ=1+2ρ12ρ13ρ23−ρ122−ρ132−ρ232,\det\mSigma = 1 + 2\rho_{12}\rho_{13}\rho_{23} - \rho_{12}^2 - \rho_{13}^2 - \rho_{23}^2,

which falls from 1 for independent variables toward 0 as the ellipsoid flattens, and becomes negative for the impossible triple (1−0.64−0.64=−0.281 - 0.64 - 0.64 = -0.28). In Section 6.5, a determinant of this kind measures how much a set of evaluations can teach about the objective, and the evaluations that make it largest are the most informative.

3.6.1 The log-determinant from the Cholesky factor #

The determinant of a triangular matrix is the product of its diagonal entries. (In the 2×22 \times 2 formula, ad−bcad - bc with b=0b = 0 is adad. In general, a triangular matrix stretches coordinate axis ii by its ii-th diagonal entry and then shears, and a shear moves points parallel to the other axes without changing any volume, as the Shear preset of Figure 3.1 shows.) With A=LL⊤\mA = \mL\mL^\T, the product rule for determinants gives

log⁡det⁡A=2∑i=1nlog⁡Lii.\log\det\mA = 2\sum_{i=1}^n \log L_{ii}.
(3.6)

The logarithm is not decoration. A kernel matrix with many tiny eigenvalues has a determinant far below the smallest positive double-precision number, about 5×10−3245 \times 10^{-324}. For the 100-input example of Section 3.4.4, with the jitter that makes it factorizable, the product of the computed eigenvalues underflows to zero, while Equation (3.6) gives log⁡det⁡A≈−1139\log\det\mA \approx -1139, a moderate number that costs nothing once the factor is known. Every implementation of the Gaussian log density and of the log marginal likelihood computes the determinant this way (Section 4.7).

3.7 Block matrices and the Schur complement #

A Gaussian process works with two groups of variables at once: the function values already observed and those to be predicted. Its matrices are therefore naturally split into blocks, as in Equation (8.1). This section develops the algebra of such blocks and ends with the one formula Section 4.5 needs.

3.7.1 Partitioned matrices #

A symmetric matrix partitioned into blocks looks like

M=[ABB⊤D],\mathbf{M} = \begin{bmatrix} \mA & \mathbf{B} \\ \mathbf{B}^\T & \mathbf{D} \end{bmatrix},
(3.7)

where A\mA is p×pp \times p, D\mathbf{D} is q×qq \times q, and B\mathbf{B} is p×qp \times q. Blocks multiply like numbers, with the rule that the order of each product is kept, since matrices do not commute. For a covariance, A\mA and D\mathbf{D} are the covariances within each group and B\mathbf{B} holds the covariances across groups.

3.7.2 Elimination by blocks #

Solving two equations in two unknowns by hand, we use the first equation to eliminate the first unknown from the second. The same step with blocks produces the central object of this section.

Definition 3.4 Schur complement

For the partitioned matrix Equation (3.7) with A\mA invertible, the Schur complement of A\mA is

S=D−B⊤A−1B.\mathbf{S} = \mathbf{D} - \mathbf{B}^\T\mA^{-1}\mathbf{B}.
(3.8)

For a 2×22 \times 2 matrix with entries a,b,da, b, d, the Schur complement of aa is the number d−b2/ad - b^2/a from the Cholesky derivation. In general it is what remains of D\mathbf{D} after the part explained by the first group is removed.

Derivation Block elimination
  1. Subtract B⊤A−1\mathbf{B}^\T\mA^{-1} times the first block row from the second. As a matrix product, with E=[I0−B⊤A−1I]\mathbf{E} = \begin{bmatrix} \mI & \mathbf{0} \\ -\mathbf{B}^\T\mA^{-1} & \mI \end{bmatrix}, EM=[AB0S]\mathbf{E}\mathbf{M} = \begin{bmatrix} \mA & \mathbf{B} \\ \mathbf{0} & \mathbf{S} \end{bmatrix}, because the bottom-right block becomes D−B⊤A−1B\mathbf{D} - \mathbf{B}^\T\mA^{-1}\mathbf{B}.
  2. Do the same to the columns: multiplying on the right by E⊤\mathbf{E}^\T clears the top-right block, and since A\mA is symmetric, EME⊤=[A00S]\mathbf{E}\mathbf{M}\mathbf{E}^\T = \begin{bmatrix} \mA & \mathbf{0} \\ \mathbf{0} & \mathbf{S} \end{bmatrix}.
  3. E\mathbf{E} is invertible: its inverse is the same matrix with the sign of the off-diagonal block flipped. So M=E−1[A00S]E−⊤\mathbf{M} = \mathbf{E}^{-1}\begin{bmatrix} \mA & \mathbf{0} \\ \mathbf{0} & \mathbf{S} \end{bmatrix}\mathbf{E}^{-\T}.

Three consequences follow, each used later in the book.

Determinants. E\mathbf{E} is block triangular with identity blocks on the diagonal, so its determinant is 1 and det⁡M=det⁡A⋅det⁡S\det\mathbf{M} = \det\mA \cdot \det\mathbf{S}.

Positive definiteness. For any vector w\vw, set v=E−⊤w\mathbf{v} = \mathbf{E}^{-\T}\vw; then w⊤Mw=v1⊤Av1+v2⊤Sv2\vw^\T\mathbf{M}\vw = \mathbf{v}_1^\T\mA\mathbf{v}_1 + \mathbf{v}_2^\T\mathbf{S}\mathbf{v}_2, where v1\mathbf{v}_1 and v2\mathbf{v}_2 are the two parts of v\mathbf{v}. So M\mathbf{M} is positive definite exactly when both A\mA and its Schur complement S\mathbf{S} are. This is the precise version of the impossible triple: once x1x_1 is accounted for, what is left of x2x_2 and x3x_3 must still be a valid covariance (Exercise 3.2).

The inverse. Inverting step 3 block by block, using (E−1)−1=E(\mathbf{E}^{-1})^{-1} = \mathbf{E}, gives M−1=E⊤[A−100S−1]E\mathbf{M}^{-1} = \mathbf{E}^\T \begin{bmatrix} \mA^{-1} & \mathbf{0} \\ \mathbf{0} & \mathbf{S}^{-1} \end{bmatrix} \mathbf{E}, and multiplying out,

M−1=[A−1+A−1BS−1B⊤A−1−A−1BS−1−S−1B⊤A−1S−1].\mathbf{M}^{-1} = \begin{bmatrix} \mA^{-1} + \mA^{-1}\mathbf{B}\mathbf{S}^{-1}\mathbf{B}^\T\mA^{-1} & -\mA^{-1}\mathbf{B}\mathbf{S}^{-1} \\ -\mathbf{S}^{-1}\mathbf{B}^\T\mA^{-1} & \mathbf{S}^{-1} \end{bmatrix}.
(3.9)

The bottom-right block of the inverse is the inverse of the Schur complement. Section 4.5 uses this, with A\mA the covariance of the observed values and D\mathbf{D} that of the unobserved ones, to show that the covariance after conditioning is the Schur complement: D−B⊤A−1B\mathbf{D} - \mathbf{B}^\T\mA^{-1}\mathbf{B}, the prior covariance minus the part the observations explain. Section 8.1 is the same formula with kernel matrices in the blocks, and Section B.1 and Section B.2 collect it with related identities (Petersen and Pedersen, 2012).

3.7.3 Adding one observation #

The block view also says how to update a Cholesky factor cheaply, which a Bayesian optimizer needs after every evaluation. Suppose K=LL⊤\mK = \mL\mL^\T for the nn inputs observed so far, and a new input arrives with covariances k\vk to the old inputs and variance κ\kappa. The new kernel matrix and its factor have the block forms

[Kkk⊤κ]=[L0l⊤l∗][L⊤l0⊤l∗].\begin{bmatrix} \mK & \vk \\ \vk^\T & \kappa \end{bmatrix} = \begin{bmatrix} \mL & \mathbf{0} \\ \mathbf{l}^\T & l_{\ast} \end{bmatrix} \begin{bmatrix} \mL^\T & \mathbf{l} \\ \mathbf{0}^\T & l_{\ast} \end{bmatrix}.

Matching blocks, Ll=k\mL\mathbf{l} = \vk, one forward substitution costing O(n2)O(n^2), and l∗2=κ−l⊤ll_{\ast}^2 = \kappa - \mathbf{l}^\T\mathbf{l}. Since l⊤l=k⊤L−⊤L−1k=k⊤K−1k\mathbf{l}^\T\mathbf{l} = \vk^\T\mL^{-\T}\mL^{-1}\vk = \vk^\T\mK^{-1}\vk, the new diagonal entry squared is κ−k⊤K−1k\kappa - \vk^\T\mK^{-1}\vk, the Schur complement of K\mK, which is also the Gaussian process's posterior variance at the new input (Equation (8.3)). Growing the factor by one row costs O(n2)O(n^2) instead of the O(n3)O(n^3) of starting over, as long as the kernel's settings stay fixed. And if the new input repeats an old one without observation noise, l∗l_{\ast} is zero and the factorization breaks: the matrix has become singular, for the reason given in Section 3.3.2.

Sources cited in Section 3.7 1
  1. Petersen and Pedersen (2012) The Matrix Cookbook

3.8 Exercises #

Exercise 3.1

Let A=[4223]\mA = \begin{bmatrix} 4 & 2 \\ 2 & 3 \end{bmatrix}. (a) Compute its Cholesky factor by hand. (b) Use it to find det⁡A\det\mA and check with ad−bcad - bc. (c) Solve Ax=(2,4)⊤\mA\vx = (2, 4)^\T by forward and back substitution.

Solution

(a) By the 2×22 \times 2 derivation, l11=4=2l_{11} = \sqrt{4} = 2, l21=2/2=1l_{21} = 2/2 = 1, and l22=3−1=2l_{22} = \sqrt{3 - 1} = \sqrt{2}, so L=[2012]\mL = \begin{bmatrix} 2 & 0 \\ 1 & \sqrt2 \end{bmatrix}.

(b) det⁡A=(l11l22)2=(22)2=8\det\mA = (l_{11}l_{22})^2 = (2\sqrt2)^2 = 8, and 4⋅3−2⋅2=84 \cdot 3 - 2 \cdot 2 = 8.

(c) Forward: z1=2/2=1z_1 = 2/2 = 1 and z2=(4−1⋅1)/2=3/2z_2 = (4 - 1 \cdot 1)/\sqrt2 = 3/\sqrt2. Back, with L⊤=[2102]\mL^\T = \begin{bmatrix} 2 & 1 \\ 0 & \sqrt2 \end{bmatrix}: x2=(3/2)/2=1.5x_2 = (3/\sqrt2)/\sqrt2 = 1.5 and x1=(1−1.5)/2=−0.25x_1 = (1 - 1.5)/2 = -0.25. Check: 4(−0.25)+2(1.5)=24(-0.25) + 2(1.5) = 2 and 2(−0.25)+3(1.5)=42(-0.25) + 3(1.5) = 4. These are the numbers the code sketch in Section 3.5.4 prints.

Exercise 3.2

Three variables have unit variances, and ρ12=ρ13=0.8\rho_{12} = \rho_{13} = 0.8. Using the Schur complement of the first variable, find every value of ρ23\rho_{23} for which the covariance matrix is positive definite. Interpret the Schur complement.

Solution

Partition with A=[1]\mA = [1], B=(0.8,0.8)\mathbf{B} = (0.8, 0.8), and D=[1ρ23ρ231]\mathbf{D} = \begin{bmatrix} 1 & \rho_{23} \\ \rho_{23} & 1 \end{bmatrix}. Then

S=D−B⊤B=[0.36ρ23−0.64ρ23−0.640.36].\mathbf{S} = \mathbf{D} - \mathbf{B}^\T\mathbf{B} = \begin{bmatrix} 0.36 & \rho_{23} - 0.64 \\ \rho_{23} - 0.64 & 0.36 \end{bmatrix}.

Since A=[1]\mA = [1] is positive definite, the whole matrix is positive definite exactly when S\mathbf{S} is, which for a 2×22 \times 2 matrix with positive diagonal means det⁡S=0.362−(ρ23−0.64)2>0\det\mathbf{S} = 0.36^2 - (\rho_{23} - 0.64)^2 > 0, so ∣ρ23−0.64∣<0.36\lvert\rho_{23} - 0.64\rvert < 0.36 and 0.28<ρ23<10.28 < \rho_{23} < 1. This is where the ellipsoid of Figure 3.2 reappears. S\mathbf{S} is the covariance of x2x_2 and x3x_3 after the part explained by x1x_1 is removed (Section 4.5): each keeps variance 1−0.82=0.361 - 0.8^2 = 0.36, and their remaining covariance ρ23−0.64\rho_{23} - 0.64 must be a valid one, at most 0.36 in absolute value.

Exercise 3.3

Two inputs have kernel value ρ\rho, so their kernel matrix is K=[1ρρ1]\mK = \begin{bmatrix} 1 & \rho \\ \rho & 1 \end{bmatrix}. Find its eigenvalues and eigenvectors, and its condition number. How many decimal digits can solving with K\mK lose when ρ=0.9999\rho = 0.9999, as for two inputs very close together under a smooth kernel?

Solution

K(1,1)⊤=(1+ρ)(1,1)⊤\mK(1, 1)^\T = (1 + \rho)(1, 1)^\T and K(1,−1)⊤=(1−ρ)(1,−1)⊤\mK(1, -1)^\T = (1 - \rho)(1, -1)^\T, so the eigenvalues are 1±ρ1 \pm \rho with eigenvectors (1,1)/2(1, 1)/\sqrt2 (the average of the two values) and (1,−1)/2(1, -1)/\sqrt2 (their difference). The condition number is (1+ρ)/(1−ρ)(1 + \rho)/(1 - \rho), which for ρ=0.9999\rho = 0.9999 is 1.9999/0.0001≈20,0001.9999/0.0001 \approx 20{,}000, so about four of the sixteen digits can be lost. The small eigenvalue is the prior variance of the difference between the two function values: two close inputs leave almost no room for their values to differ, and that is the direction in which the matrix is nearly singular.

Exercise 3.4

In Section 3.7.3, suppose the observations are noisy, so the matrix to factor is K+σn2I\mK + \sigma_n^2\mI. What is the new diagonal entry l∗2l_{\ast}^2 now? Show that it can no longer be zero, even when the new input repeats an old one.

Solution

The new matrix has κ+σn2\kappa + \sigma_n^2 in the corner and K+σn2I\mK + \sigma_n^2\mI in the old block, so l∗2=κ+σn2−k⊤(K+σn2I)−1kl_{\ast}^2 = \kappa + \sigma_n^2 - \vk^\T(\mK + \sigma_n^2\mI)^{-1}\vk. The quantity κ−k⊤(K+σn2I)−1k\kappa - \vk^\T(\mK + \sigma_n^2\mI)^{-1}\vk is the noisy posterior variance of ff at the new input, σ2(x)\sigma^2(\vx) of Equation (8.6), so l∗2=σ2(x)+σn2l_{\ast}^2 = \sigma^2(\vx) + \sigma_n^2, the variance of a new noisy measurement there. Because σ2(x)≥0\sigma^2(\vx) \ge 0, l∗2≥σn2>0l_{\ast}^2 \ge \sigma_n^2 > 0: observation noise keeps the matrix positive definite, which is why noisy models rarely need jitter.

Further reading #

  • Strang (2016) is a patient introduction to matrices as maps, eigenvalues, and positive definite matrices, with the geometric emphasis of this chapter.
  • Golub and Van Loan (2013) is the standard reference for how these computations are done in floating point: triangular systems, the Cholesky factorization, operation counts, and conditioning.
  • Rasmussen and Williams (2006), appendix A, collects the matrix identities and the Cholesky-based computations that Gaussian process regression uses.
  • Petersen and Pedersen (2012) is a compact catalog of identities, including the block inverse and determinant formulas, useful for checking a derivation.
  • Sanderson (2016) is an animated video series that shows matrices acting on the plane and in space, the picture behind Figure 3.1.
  • Higham (2002) treats the problem of repairing an invalid correlation matrix, the practical side of Section 3.3.3.

References

  1. Gardner, J. R., Pleiss, G., Bindel, D., Weinberger, K. Q., and Wilson, A. G. (2018). GPyTorch: Blackbox Matrix-Matrix Gaussian Process Inference with GPU Acceleration. Advances in Neural Information Processing Systems 31 (NeurIPS 2018). Cited in §3.5
  2. Golub, G. H., and Van Loan, C. F. (2013). Matrix Computations. Johns Hopkins University Press. Cited in §3.4 §3.5
  3. Higham, N. J. (2002). Computing the Nearest Correlation Matrix: A Problem from Finance. IMA Journal of Numerical Analysis. Cited in §3.3
  4. Petersen, K. B., and Pedersen, M. S. (2012). The Matrix Cookbook. Technical University of Denmark. non-peer-reviewed Cited in §3.7
  5. Rasmussen, C. E., and Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press.
  6. Sanderson, G. (2016). Essence of Linear Algebra. Video series, 3Blue1Brown. non-peer-reviewed
  7. Snoek, J., Larochelle, H., and Adams, R. P. (2012). Practical Bayesian Optimization of Machine Learning Algorithms. Advances in Neural Information Processing Systems 25 (NeurIPS 2012). Cited in §3.1
  8. Strang, G. (2016). Introduction to Linear Algebra. Wellesley-Cambridge Press. Cited in §3.4