Ten problems on Gaussian process regression: the predictive mean and covariance from the joint Gaussian, two training points worked by hand, the posterior mean as a sum of kernels and why the variance only falls, the RBF kernel's derivatives, the gradient of the log marginal likelihood with respect to the lengthscale, signal variance and noise, the noise gradient through the eigenvalues, conditioning one observation at a time, the linear kernel as ridge regression, the lengthscale limits, and the derivative of the posterior mean with respect to the lengthscale, with worked solutions and the mistakes that leave out the noise, the log-determinant, or the second term of a derivative through an inverse.
Before you start
A Gaussian process is a prior over functions under which any finite set of function values is jointly Gaussian with a covariance given by a kernel, and Gaussian process regression is nothing more than the conditioning formula of the multivariate-Gaussian page applied to that joint: condition the unseen values on the observed ones. Every formula in the subject, the predictive mean and variance, the marginal likelihood, its hyperparameter gradients, comes from that one step plus the inverse-and-determinant calculus of the previous page. These ten problems do the step, run it on two data points by hand, read what the result says (a weighted sum of kernels, a variance that can only fall), differentiate the kernel and then the marginal likelihood with respect to the lengthscale, the signal variance and the noise, condition one observation at a time, show that a linear kernel recovers ridge regression, and take the lengthscale to its two limits. The five mistakes are each a missing term: the noise left out of the matrix that is inverted, noise added where none belongs, a marginal likelihood gradient without its log-determinant, a lengthscale derivative short a factor of ,2, and a derivative of the posterior mean that holds the inverse fixed.
f is a function with a Gaussian process prior with zero mean and kernel :k: for any inputs ,x1,…,xn, the vector (f(x1),…,f(xn)) is Gaussian with mean 0 and covariance matrix .Kij=k(xi,xj). Kernels here are positive definite, so every such matrix is symmetric positive semidefinite.
Training inputs X=(x1,…,xn) with function values f=(f(x1),…,f(xn))⊤ and noisy observations ,y=f+ε,ε∼N(0,σ2I) independent of .f. Test inputs X∗=(x1∗,…,xm∗) with values .f∗∈Rm.
Kernel blocks: K=k(X,X) is ,n×n,K∗=k(X,X∗) is n×m with ,(K∗)ij=k(xi,xj∗), and K∗∗=k(X∗,X∗) is .m×m. For a single test point, k∗=k(X,x∗)∈Rn and .k∗∗=k(x∗,x∗).Ky=K+σ2I and .α=Ky−1y.
Gaussian conditioning (the multivariate-Gaussian page, Problem 9): if (xa,xb) is jointly Gaussian with mean (μa,μb) and covariance blocks ,Σaa,,Σab,,Σbb, then .xa∣xb∼N(μa+ΣabΣbb−1(xb−μb),Σaa−ΣabΣbb−1Σba).
The RBF (squared-exponential) kernel on scalar or vector inputs is k(x,x′)=s2exp(−∥x−x′∥2/(2ℓ2)) with signal variance s2 and lengthscale .ℓ>0. Its hyperparameters, with ,σ2, are written .θ. Numbers on this page use scalar inputs.
From the previous page: ,d(A−1)=−A−1(dA)A−1,,dlogdetA=tr(A−1dA), and tr is cyclic. tr(A) of a symmetric matrix with eigenvalues λi is .∑iλi.
Write the joint distribution of (f∗,y) and condition on y to obtain the predictive distribution f∗∣y∼N(μ∗,Σ∗) with μ∗=K∗⊤Ky−1y and .Σ∗=K∗∗−K∗⊤Ky−1K∗.
··
Two training points ,x1=0,x2=1 with ,y=(1,2)⊤, RBF kernel with ,s2=1,,ℓ=1, noise .σ2=0.1. Compute the predictive mean and variance at x∗=0.5 to four decimal places.
··
Show that the posterior mean at any x∗ is μ(x∗)=∑i=1nαik(xi,x∗) with α=Ky−1y not depending on ;x∗; that the posterior variance never exceeds the prior variance; and that with σ2=0 the posterior mean passes through every observation with zero variance there.
·
For the RBF kernel with ,r=∥x−x′∥, compute ,∂k/∂ℓ,∂k/∂s2 and, for a scalar test input, .∂k(xi,x∗)/∂x∗. Use the last to write the slope μ′(x∗) of the posterior mean.
···
The log marginal likelihood is .logp(y∣X,θ)=−21y⊤Ky−1y−21logdetKy−2nlog2π. For a hyperparameter θ with ∂Ky/∂θ known, show that ,∂θ∂logp(y∣X,θ)=21tr((αα⊤−Ky−1)∂θ∂Ky), and write the two terms for .θ=ℓ.
··
Let K have eigenvalues .λ1,…,λn≥0. Show that logdetKy=∑ilog(λi+σ2) and ,tr(Ky−1)=∑i1/(λi+σ2), hence that ,∂σ2∂logp=21(∥α∥2−trKy−1), and state the condition at a maximum over .σ2.
···
After conditioning on n observations, the posterior over (f∗,f(xn+1)) is Gaussian with mean ,(μ∗,μn+1), covariance Σ∗ for ,f∗, cross-covariance c=Cov(f∗,f(xn+1)) and variance .v=Var(f(xn+1)). A new observation yn+1=f(xn+1)+εn+1 arrives. Show that the updated posterior is μ∗′=μ∗+v+σ2c(yn+1−μn+1) and ,Σ∗′=Σ∗−v+σ2cc⊤, and say why this equals the batch posterior on all n+1 points.
··
Take the linear kernel k(x,x′)=x⊤x′ with inputs as rows of X∈Rn×d and .X∗∈Rm×d. Show that the posterior mean is X∗w with ,w=(X⊤X+σ2I)−1X⊤y, the ridge solution with penalty ,σ2, and that the posterior covariance is .σ2X∗(X⊤X+σ2I)−1X∗⊤.
··
With the RBF kernel, find the posterior mean and variance at a test point as ℓ→∞ and as ℓ→0 (for x∗ not equal to any training input).
···
For a single test point, μ∗=k∗⊤Ky−1y depends on ℓ through both k∗ and .K. Compute dℓdμ∗ in terms of ,α,∂k∗/∂ℓ and .∂K/∂ℓ.
Answers
f∗∣y∼N(K∗⊤Ky−1y,K∗∗−K∗⊤Ky−1K∗)
μ∗≈1.5514 and Σ∗=1−0.9128≈0.0873
;μ(x∗)=∑iαik(xi,x∗);Var(f(x∗)∣y)≤k∗∗ everywhere; with ,σ2=0,μ(xj)=yj and the variance at xj is 0
,∂k/∂ℓ=kr2/ℓ3,,∂k/∂s2=k/s2,,∂k(xi,x∗)/∂x∗=−k(xi,x∗)(x∗−xi)/ℓ2, and μ′(x∗)=−ℓ21∑iαi(x∗−xi)k(xi,x∗)
,logdetKy=∑ilog(λi+σ2),,trKy−1=∑i(λi+σ2)−1,,∂logp/∂σ2=21(∥α∥2−trKy−1), and at the optimum ∥α∥2=trKy−1
,μ∗′=μ∗+v+σ2c(yn+1−μn+1),,Σ∗′=Σ∗−v+σ2cc⊤, identical to the batch posterior on n+1 points
μ∗=X∗(X⊤X+σ2I)−1X⊤y and Σ∗=σ2X∗(X⊤X+σ2I)−1X∗⊤
:ℓ→∞:μ∗→ns2+σ2ns2yˉ and Σ∗→ns2+σ2s2σ2 everywhere; :ℓ→0:μ∗→0 and Σ∗→s2 away from the data
dℓdμ∗=(∂ℓ∂k∗)⊤α−k∗⊤Ky−1∂ℓ∂Kα
Worked solutions
Problem 1
Write the joint distribution of (f∗,y) and condition on y to obtain the predictive distribution f∗∣y∼N(μ∗,Σ∗) with μ∗=K∗⊤Ky−1y and .Σ∗=K∗∗−K∗⊤Ky−1K∗.
(f,f∗) is jointly Gaussian with mean 0 and covariance .(KK∗⊤K∗K∗∗).The prior applied to the n+m inputs X and X∗ together; the blocks are the kernel evaluated between the two sets.
y=f+ε is Gaussian with .Cov(y)=K+σ2I=Ky.A sum of independent Gaussians is Gaussian and their covariances add; .Cov(ε)=σ2I.
.Cov(f∗,y)=Cov(f∗,f)+Cov(f∗,ε)=K∗⊤+0.Covariance is bilinear, and ε is independent of everything in .f∗. The noise touches only the y block.
.(f∗,y)∼N(0,(K∗∗K∗K∗⊤Ky)).Steps 1 to 3 assembled with xa=f∗ and ;xb=y;(f∗,y) is jointly Gaussian because it is a linear function of the Gaussian vector (f,f∗,ε) (the multivariate-Gaussian page, Problem 7).
μ∗=0+K∗⊤Ky−1(y−0) and .Σ∗=K∗∗−K∗⊤Ky−1K∗.The conditioning formula with ,Σab=K∗⊤,,Σbb=Ky,Σaa=K∗∗ and both means .0.
f∗∣y∼N(K∗⊤Ky−1y,K∗∗−K∗⊤Ky−1K∗)μ∗ is m×1 and Σ∗ is .m×m. The noise appears once, in the matrix that is inverted, because the observations are noisy and the quantity predicted, ,f∗, is not; to predict a noisy y∗ instead, add σ2I to Σ∗ afterwards. The inverse of Ky is the whole cost, O(n3) once, after which each test point needs O(n) for the mean and O(n2) for the variance.
Problem 2
Two training points ,x1=0,x2=1 with ,y=(1,2)⊤, RBF kernel with ,s2=1,,ℓ=1, noise .σ2=0.1. Compute the predictive mean and variance at x∗=0.5 to four decimal places.
,K=(1e−1/2e−1/21), and ,e−1/2≈0.6065, so .Ky=(1.10.60650.60651.1).;k(0,1)=exp(−1/2);;k(x,x)=1; add σ2=0.1 to the diagonal.
k∗=(e−1/8,e−1/8)⊤≈(0.8825,0.8825)⊤ and .k∗∗=1.Both training points are at distance 0.5 from :x∗:.exp(−0.25/2)=exp(−1/8).
Ky−1=a2−b21(a−b−ba) with ,a=1.1,,b=0.6065,.a2−b2≈1.21−0.3679=0.8421.The 2×2 inverse of a symmetric matrix with equal diagonal: swap nothing, negate the off-diagonal, divide by the determinant.
.α=Ky−1y=0.84211(1.1−2(0.6065),−0.6065+2(1.1))⊤≈(−0.1343,1.8922)⊤.Multiply out: 1.1−1.2131=−0.1131 and ,−0.6065+2.2=1.5935, each divided by .0.8421.
.μ∗=k∗⊤α=0.8825(−0.1343+1.8922)=0.8825×1.7580≈1.5514.Both entries of k∗ are equal, so the dot product is 0.8825 times the sum of .α.
.k∗⊤Ky−1k∗=0.88252×(sum of all entries of Ky−1)=0.7788×a2−b22(a−b)=0.7788×0.84210.9870≈0.7788×1.1720≈0.9128.With ,k∗=c1,,k∗⊤Mk∗=c21⊤M1, the sum of the entries; for step 3's inverse that is .2(a−b)/(a2−b2).
μ∗≈1.5514 and Σ∗=1−0.9128≈0.0873A little above the midpoint 1.5 of the two observations, because the second observation has the larger weight α2 and the first a small negative one: α is not a set of averaging weights but the solution of .Kyα=y. The variance has fallen from the prior 1 to ,0.0873, near the noise level ,0.1, as a point half a lengthscale from two observations should.
Problem 3
Show that the posterior mean at any x∗ is μ(x∗)=∑i=1nαik(xi,x∗) with α=Ky−1y not depending on ;x∗; that the posterior variance never exceeds the prior variance; and that with σ2=0 the posterior mean passes through every observation with zero variance there.
.μ(x∗)=k∗⊤Ky−1y=k∗⊤α=∑i(k∗)iαi=∑iαik(xi,x∗).Problem 1 for one test point; α depends on the training data only, so it is computed once.
K∗⊤Ky−1K∗=(Ky−1/2K∗)⊤(Ky−1/2K∗) is positive semidefinite.Ky is positive definite (a positive semidefinite K plus ),σ2I), so it has a symmetric square root and ;Ky−1=Ky−1/2Ky−1/2; a matrix of the form B⊤B is positive semidefinite.
So ,Σ∗=K∗∗−K∗⊤Ky−1K∗⪯K∗∗, and in particular .Var(f(x∗)∣y)=k∗∗−k∗⊤Ky−1k∗≤k∗∗.Subtracting a positive semidefinite matrix can only lower each diagonal entry. The data cannot increase uncertainty about f anywhere.
With σ2=0 and :x∗=xj:k∗=Kej (column j of )K) and .k∗∗=Kjj.k(xi,xj) is entry (i,j) of .K.
μ(xj)=ej⊤K⊤K−1y=ej⊤y=yj and .Var=Kjj−ej⊤KK−1Kej=Kjj−Kjj=0.K⊤=K and ,KK−1=I, assuming K is invertible.
;μ(x∗)=∑iαik(xi,x∗);Var(f(x∗)∣y)≤k∗∗ everywhere; with ,σ2=0,μ(xj)=yj and the variance at xj is 0The posterior mean is a fixed linear combination of n copies of the kernel, one centred on each training input, so it inherits the kernel's shape: smooth for the RBF, returning to the prior mean 0 far from the data (Problem 9). The variance depends on the inputs X and x∗ but not on :y: where the data are is what reduces uncertainty, not what they say. Noise-free conditioning interpolates; with σ2>0 the mean passes near, not through, the observations, and the variance at a training input is positive.
Problem 4
For the RBF kernel with ,r=∥x−x′∥, compute ,∂k/∂ℓ,∂k/∂s2 and, for a scalar test input, .∂k(xi,x∗)/∂x∗. Use the last to write the slope μ′(x∗) of the posterior mean.
.∂ℓ∂k=k⋅ℓ3r2.Chain rule through the exponential: ,dℓdeu(ℓ)=euu′, and .s2eu=k.
.∂s2∂k=exp(−2ℓ2r2)=s2k.k is linear in .s2.
.∂x∗∂k(xi,x∗)=k(xi,x∗)⋅∂x∗∂(−2ℓ2(x∗−xi)2)=−k(xi,x∗)ℓ2x∗−xi.Chain rule; dx∗d(x∗−xi)2=2(x∗−xi) and the 2 cancels.
.μ′(x∗)=∑iαi∂x∗∂k(xi,x∗)=−ℓ21∑iαi(x∗−xi)k(xi,x∗).Differentiate Problem 3's representer form term by term; α does not depend on .x∗.
,∂k/∂ℓ=kr2/ℓ3,,∂k/∂s2=k/s2,,∂k(xi,x∗)/∂x∗=−k(xi,x∗)(x∗−xi)/ℓ2, and μ′(x∗)=−ℓ21∑iαi(x∗−xi)k(xi,x∗)The lengthscale derivative is largest at r≈2ℓ and vanishes at r=0 (the diagonal of K never depends on )ℓ) and at large ;r; the signal-variance derivative is the kernel with s2 stripped out. The slope formula is how a Gaussian process gives derivatives of its mean function for free, and the same ∂k/∂x∗ is the cross-covariance between f and .f′.
Problem 5
The log marginal likelihood is .logp(y∣X,θ)=−21y⊤Ky−1y−21logdetKy−2nlog2π. For a hyperparameter θ with ∂Ky/∂θ known, show that ,∂θ∂logp(y∣X,θ)=21tr((αα⊤−Ky−1)∂θ∂Ky), and write the two terms for .θ=ℓ.
.∂θ∂(y⊤Ky−1y)=y⊤∂θ∂Ky−1y=−y⊤Ky−1∂θ∂KyKy−1y.y is data, so only the inverse moves; the previous page's Problem 4 gives .∂(A−1)/∂θ=−A−1(∂A/∂θ)A−1.
.=−α⊤∂θ∂Kyα=−tr(αα⊤∂θ∂Ky).Ky−1y=α and y⊤Ky−1=α⊤ since Ky is symmetric; a scalar is its own trace and the cyclic property moves α to the back.
.∂θ∂logdetKy=tr(Ky−1∂θ∂Ky).The previous page's Problem 4 again, for .logdet.
.∂θ∂logp=21tr(αα⊤∂θ∂Ky)−21tr(Ky−1∂θ∂Ky)=21tr((αα⊤−Ky−1)∂θ∂Ky).−21 times step 2 and −21 times step 3; the constant term has no ;θ; the trace is linear.
For :θ=ℓ:∂ℓ∂Ky=∂ℓ∂K with entries ,Kij(xi−xj)2/ℓ3, and the two terms are 21α⊤∂ℓ∂Kα and .−21tr(Ky−1∂ℓ∂K).σ2I does not depend on ;ℓ; Problem 4 entry by entry.
∂θ∂logp=21tr((αα⊤−Ky−1)∂θ∂Ky)=21α⊤∂θ∂Kyα−21tr(Ky−1∂θ∂Ky)The first term is the derivative of the data fit −21y⊤Ky−1y and the second of the complexity penalty ;−21logdetKy; the marginal likelihood trades them off automatically, which is why its maximum is a usable choice of hyperparameters without a validation set. The same ∂Ky/∂θ with K/s2 (Problem 4) gives the signal-variance gradient, and with I the noise gradient (Problem 6). Each gradient costs one n×n product once Ky−1 and α are known.
Problem 6
Let K have eigenvalues .λ1,…,λn≥0. Show that logdetKy=∑ilog(λi+σ2) and ,tr(Ky−1)=∑i1/(λi+σ2), hence that ,∂σ2∂logp=21(∥α∥2−trKy−1), and state the condition at a maximum over .σ2.
K=QΛQ⊤ with Q orthogonal, so .Ky=Q(Λ+σ2I)Q⊤.K is symmetric; I=QQ⊤ puts the noise into the same basis, as on the previous page's Problem 4.
,detKy=det(Λ+σ2I)=∏i(λi+σ2), so .logdetKy=∑ilog(λi+σ2).det(QDQ⊤)=detQdetDdetQ⊤=detD since ;detQ=±1; a diagonal determinant is the product of the diagonal.
Ky−1=Q(Λ+σ2I)−1Q⊤ and .trKy−1=∑iλi+σ21.Inverse of QDQ⊤ is ;QD−1Q⊤; the trace is cyclic, .tr(QD−1Q⊤)=tr(D−1).
,∂σ2∂Ky=I, so Problem 5 gives .∂σ2∂logp=21α⊤α−21trKy−1.Substitute ∂Ky/∂θ=I into both terms.
At a maximum over σ2 (an interior one), .∥α∥2=trKy−1=∑iλi+σ21.Set the derivative to zero.
,logdetKy=∑ilog(λi+σ2),,trKy−1=∑i(λi+σ2)−1,,∂logp/∂σ2=21(∥α∥2−trKy−1), and at the optimum ∥α∥2=trKy−1∥α∥2=y⊤Ky−2y measures how hard the model is working to explain y and trKy−1 is what that quantity would be on average for data drawn from the model itself (E[y⊤Ky−2y]=tr(Ky−2Ky) for );y∼N(0,Ky)); the noise is tuned until the two agree. The sum form shows the log-determinant is dominated by the small eigenvalues of ,K, which σ2 lifts: the noise is also the regulariser that keeps Ky invertible, and the "jitter" added to a noise-free kernel matrix is exactly a small .σ2.
Problem 7
After conditioning on n observations, the posterior over (f∗,f(xn+1)) is Gaussian with mean ,(μ∗,μn+1), covariance Σ∗ for ,f∗, cross-covariance c=Cov(f∗,f(xn+1)) and variance .v=Var(f(xn+1)). A new observation yn+1=f(xn+1)+εn+1 arrives. Show that the updated posterior is μ∗′=μ∗+v+σ2c(yn+1−μn+1) and ,Σ∗′=Σ∗−v+σ2cc⊤, and say why this equals the batch posterior on all n+1 points.
Given the first n observations, (f∗,yn+1) is jointly Gaussian with mean ,(μ∗,μn+1),,Cov(f∗)=Σ∗,Cov(f∗,yn+1)=c and .Var(yn+1)=v+σ2.The posterior after n points is a Gaussian (Problem 1), and yn+1 adds independent noise of variance σ2 to ,f(xn+1), which changes its variance but not its covariance with f∗ (as in Problem 1, step 3).
Condition on :yn+1:.μ∗′=μ∗+c(v+σ2)−1(yn+1−μn+1).The conditioning formula with Σab=c ()m×1) and ,Σbb=v+σ2, a scalar.
.Σ∗′=Σ∗−c(v+σ2)−1c⊤.The same formula's covariance; cc⊤ is m×m of rank one.
Conditioning on y1,…,yn and then on yn+1 is conditioning on all n+1 at once.p(f∗∣y1:n+1)∝p(f∗,yn+1∣y1:n) by the definition of conditional probability, applied to the posterior after n points as the new "prior"; the Gaussian family is closed under conditioning, so the sequential answer is the batch answer.
,μ∗′=μ∗+v+σ2c(yn+1−μn+1),,Σ∗′=Σ∗−v+σ2cc⊤, identical to the batch posterior on n+1 pointsThe update is the residual yn+1−μn+1 (what the new observation says beyond what was predicted) spread over the test points in proportion to their posterior covariance with the new input, divided by the predicted variance of the observation; the variance drops by a rank-one amount that again does not involve .yn+1. In the Ky picture this is a rank-one update of the inverse, the previous page's Sherman–Morrison formula, and it is the step a Kalman filter takes for one scalar measurement.
Problem 8
Take the linear kernel k(x,x′)=x⊤x′ with inputs as rows of X∈Rn×d and .X∗∈Rm×d. Show that the posterior mean is X∗w with ,w=(X⊤X+σ2I)−1X⊤y, the ridge solution with penalty ,σ2, and that the posterior covariance is .σ2X∗(X⊤X+σ2I)−1X∗⊤.
,K=XX⊤,,K∗=XX∗⊤,.K∗∗=X∗X∗⊤.Entry (i,j) of each is the dot product of the corresponding rows.
.X⊤(XX⊤+σ2I)=(X⊤X+σ2I)X⊤.Multiply out: both sides are .X⊤XX⊤+σ2X⊤.
.(X⊤X+σ2I)−1X⊤=X⊤(XX⊤+σ2I)−1.Multiply step 2 by (X⊤X+σ2I)−1 on the left and (XX⊤+σ2I)−1 on the right; both matrices are positive definite, hence invertible. This is the push-through identity: a d×d inverse on one side, an n×n inverse on the other.
.μ∗=K∗⊤Ky−1y=X∗X⊤(XX⊤+σ2I)−1y=X∗(X⊤X+σ2I)−1X⊤y=X∗w.Problem 1, then step 3.
.Σ∗=X∗X∗⊤−X∗X⊤(XX⊤+σ2I)−1XX∗⊤=X∗(I−(X⊤X+σ2I)−1X⊤X)X∗⊤.Step 3 applied inside the second term, then factor X∗ and X∗⊤ out.
.I−(X⊤X+σ2I)−1X⊤X=(X⊤X+σ2I)−1(X⊤X+σ2I−X⊤X)=σ2(X⊤X+σ2I)−1.Write I as (X⊤X+σ2I)−1(X⊤X+σ2I) and subtract.
μ∗=X∗(X⊤X+σ2I)−1X⊤y and Σ∗=σ2X∗(X⊤X+σ2I)−1X∗⊤Gaussian process regression with a linear kernel is Bayesian linear regression with prior :w∼N(0,I): the mean is the ridge fit with λ=σ2 (the regression page, in the units of ),21∥Xw−y∥2+2λ∥w∥2), and the covariance is X∗ times the posterior covariance of ,w, which is .σ2(X⊤X+σ2I)−1. The two sides of step 3 are the two ways to compute it: in weight space ()d×d) when ,d<n, in function space ()n×n) when the kernel has no finite ,d, which is the point of kernels.
Problem 9
With the RBF kernel, find the posterior mean and variance at a test point as ℓ→∞ and as ℓ→0 (for x∗ not equal to any training input).
As ,ℓ→∞,k(x,x′)→s2 for every pair, so ,K→s211⊤,k∗→s21 and .k∗∗=s2..exp(−r2/2ℓ2)→1.
,(s211⊤+σ2I)1=(ns2+σ2)1, so .Ky−11=ns2+σ21.:1⊤1=n:1 is an eigenvector of Ky with eigenvalue ,ns2+σ2, and the inverse has the reciprocal eigenvalue on the same vector.
.μ∗=s21⊤Ky−1y=ns2+σ2s21⊤y=ns2+σ2ns2yˉ.1⊤Ky−1=(Ky−11)⊤ by symmetry, and .1⊤y=nyˉ.
.Σ∗=s2−s41⊤Ky−11=s2−ns2+σ2ns4=ns2+σ2s2σ2.Step 2 again; put over the common denominator.
As ,ℓ→0,k(x,x′)→0 for ,x=x′, so ,K→s2I,k∗→0 and .k∗∗=s2.exp(−r2/2ℓ2)→0 for ,r>0, and is 1 at .r=0.
μ∗→0 and .Σ∗→s2.k∗=0 kills both the mean and the correction to the variance.
:ℓ→∞:μ∗→ns2+σ2ns2yˉ and Σ∗→ns2+σ2s2σ2 everywhere; :ℓ→0:μ∗→0 and Σ∗→s2 away from the dataA very long lengthscale says the function is constant, so the model fits one number, the sample mean shrunk towards the prior mean 0 by the factor ,ns2/(ns2+σ2), with the variance of a mean of n noisy observations; a very short one says the observations tell nothing about any other point, so the posterior is the prior except on the data. The marginal likelihood of Problem 5 picks the ℓ between these at which the data look most like a draw from the prior.
Problem 10
For a single test point, μ∗=k∗⊤Ky−1y depends on ℓ through both k∗ and .K. Compute dℓdμ∗ in terms of ,α,∂k∗/∂ℓ and .∂K/∂ℓ.
.dℓdμ∗=(∂ℓ∂k∗)⊤Ky−1y+k∗⊤∂ℓ∂Ky−1y.Product rule on the three factors; y is constant.
.∂ℓ∂Ky−1=−Ky−1∂ℓ∂KKy−1.The previous page's Problem 4; ∂Ky/∂ℓ=∂K/∂ℓ because the noise term has no .ℓ.
.dℓdμ∗=(∂ℓ∂k∗)⊤α−k∗⊤Ky−1∂ℓ∂Kα.Ky−1y=α in both terms.
The entries are (∂k∗/∂ℓ)i=k(xi,x∗)(xi−x∗)2/ℓ3 and .(∂K/∂ℓ)ij=Kij(xi−xj)2/ℓ3.Problem 4.
dℓdμ∗=(∂ℓ∂k∗)⊤α−k∗⊤Ky−1∂ℓ∂KαThe first term is how the prediction changes because the test point's correlations with the data change; the second, through the inverse, is how it changes because the data's correlations with each other change, which reweights .α. The second term needs one more solve with Ky (of ,∂ℓ∂Kα, or of k∗ by symmetry), and dropping it is the last mistake below. The same two-term structure gives the derivative of any prediction with respect to any hyperparameter, which is what makes the predictions themselves differentiable for downstream use.
Where this goes wrong
1. Inverting K instead of K + σ²I
The conditioning formula is written with the prior covariance, and K is the prior covariance.
(f,f∗) has prior covariance with blocks ,K,,K∗,K∗∗Right so far: Problem 1, step 1.
“Condition f∗ on the observed values using the prior covariance of what was observed.”The slip that causes the mistake: what was observed is ,y=f+ε, whose covariance is ,K+σ2I, not .K.
μ∗=K∗⊤K−1yThis is the σ2=0 posterior (Problem 3), which interpolates every noisy observation exactly, so the mean wiggles through the noise and the variance is 0 at the data, when the data are known to be noisy. It also needs K invertible, which a kernel matrix with two nearby inputs is not, numerically; σ2I is what makes Ky well conditioned (Problem 6). In Problem 2 the wrong mean is 1.6480 in place of 1.5514 and the variance 0.0305 in place of .0.0873.
2. Noise added to the cross-covariance
A convenient implementation folds the noise into the kernel as ky(x,x′)=k(x,x′)+σ2[x=x′] and uses ky for every block.
Ky=K+σ2I is correct for the training blockRight so far: the diagonal of K is where the observation noise lives.
“Use the same noisy kernel ky to build ,K∗, so a test point equal to a training input gets the same treatment.”The shortcut that causes the mistake: the noise is a property of the observations, not of the function, and f∗ is a function value; Cov(f(x∗),yi)=k(x∗,xi) with no σ2 even when x∗=xi (Problem 1, step 3).
(K∗)ij=k(xi,xj∗)+σ2 when xj∗=xiAt a test point that coincides with a training input the cross-covariance is too large by ,σ2, so the posterior mean leans towards that observation's noisy value and the posterior variance comes out too small (it can go negative). The bug is invisible at test points away from the data and appears exactly where predictions are compared with the training targets. The noise belongs in Ky only, and in Σ∗+σ2I if a noisy y∗ is being predicted.
3. Marginal likelihood gradient without the log-determinant
The data-fit term is the one with y in it, and its derivative is the one that looks like a gradient of a loss.
∂θ∂(−21y⊤Ky−1y)=21α⊤∂θ∂KyαRight so far: Problem 5, steps 1 and 2.
“The logdet term is a normalising constant.”The assumption that causes the mistake: it is constant in ,y, not in ;θ; it is how the marginal likelihood charges for a kernel that could explain anything.
∂θ∂logp=21α⊤∂θ∂KyαThe term −21tr(Ky−1∂Ky/∂θ) is missing (Problem 5). For θ=σ2 the surviving term is ,21∥α∥2>0, so the "gradient" always says to increase the noise; for s2 it is ,21α⊤Kα/s2>0, so it always says to increase the signal variance; the optimiser drives the hyperparameters off to infinity, where the data fit is best because everything is explained as a draw from a huge prior. Problem 6's balance ∥α∥2=trKy−1 exists only because the second term is there.
4. Lengthscale derivative short a factor of 2
The exponent −r2/(2ℓ2) has a 2 in it, and it is tempting to let it cancel something.
k=s2exp(−r2/(2ℓ2))Right so far.
“Differentiate the exponent: .−2r2⋅dℓdℓ−2=−2r2⋅(−ℓ−3).”The slip that causes the mistake: ,dℓdℓ−2=−2ℓ−3, and the 2 is exactly what the 21 in the exponent cancels.
∂ℓ∂k=k2ℓ3r2The derivative is kr2/ℓ3 (Problem 4): every entry of ∂K/∂ℓ is half its true value, so the lengthscale gradient of the marginal likelihood (Problem 5) is halved. The stationary point is unchanged, because a zero gradient is still zero, so a gradient-based optimiser still finds the right ,ℓ, only more slowly; a finite-difference check of ∂logp/∂ℓ catches it at once, and a derivative-based test of μ′(x∗) does not, since that uses .∂k/∂x∗.
5. Derivative of the posterior mean with the inverse held fixed
α=Ky−1y is computed once and stored, and it is easy to forget that it moves with the kernel.
μ∗=k∗⊤αRight so far: Problem 3.
“α is the fitted weight vector; differentiate the kernel features k∗ and leave the weights alone.”The analogy that causes the mistake: in a linear model the weights are parameters, independent of the features; here α=Ky−1y is a function of ℓ through .K.
dℓdμ∗=(∂ℓ∂k∗)⊤αThe second term −k∗⊤Ky−1(∂K/∂ℓ)α (Problem 10) is missing: it is the derivative through the inverse, ,d(A−1)=−A−1(dA)A−1, and it is of the same order as the first. A gradient check against finite differences in ℓ fails. In the marginal likelihood (Problem 5) every dependence on ℓ runs through ,Ky, so there the derivative through the inverse is not a correction but the whole data-fit term.