Skip to contents

This vignette covers the derivatives of the surface created by a Gaussian process model with respect to the spatial dimensions. The other vignette, Derivatives for estimating Gaussian process parameters, has derivatives of the deviance (likelihood) with respect to the parameters. For an explanation of notation and basic equations, see the vignette Introduction to Gaussian Processes.

Here we assume that we have nn data points YXY_X that are the function values corresponding to the rows of the nn by dd design matrix XX. We have a mean function μ\mu and covariance function Σ\Sigma.

y(x)y(x) is the random variable representing the output YxY_x corresponding to input point xx conditional on the data YXY_X. Thus y(x)=Yx|YXy(x) = Y_x | Y_X, and y(x)y(x) is a random variable with distribution

y(x)N(μx+ΣxXΣx1(YXμX),ΣxΣxXΣX1ΣXx) y(x) \sim N \left( \mu_x + \Sigma_{xX} \Sigma_x^{-1}(Y_X - \mu_X) , ~\Sigma_x - \Sigma_{xX} \Sigma_X^{-1} \Sigma_{Xx}\right)

Gradient of mean function

The mean function is ŷ(x)=μ(x)+ΣxXΣX1(YXμX). \hat{y}(x) = \mu(x) + \Sigma_{xX} \Sigma_X^{-1}(Y_X - \mu_X). μ(x)\mu(x) and ΣxX\Sigma_{xX} are the only parts that depends on xx.

We can find the gradient of ŷ(x)\hat{y}(x) by taking the partial derivatives

ŷ(x)xi=μ(x)xi+Σx,XxiΣX1(YXμX)\frac{\partial \hat{y}(x)}{\partial x_i}= \frac{\partial \mu(x)}{\partial x_i} + \frac{\partial \Sigma_{x,X}}{\partial x_i} \Sigma_X^{-1}(Y_X - \mu_X) Remember that Σ\Sigma is overloaded and the second derivative on the right hand side is: Σx,Xxi=Σ(x,X)xi\frac{\partial \Sigma_{x,X}}{\partial x_i} = \frac{\partial \Sigma (x,X)}{\partial x_i} The calculation above is for a single partial derivative. The vector of these gives the gradient ŷ(x)x=xŷ(x)\frac{\partial \hat{y}(x)}{\partial x} = \nabla_x \hat{y}(x). Note that the partial derivative of a scalar with respect to a vector is another way of writing the gradient.

We usually use μ(x)\mu(x) equal to zero, a constant, or a linear model, meaning that its derivative is usually zero or a constant.

Hessian of mean function

The second derivatives can be calculated similarly 2ŷ(x)xixk=2μ(x)xixk+2ΣxXxixkΣX1(YXμX)\frac{\partial^2 \hat{y}(x)}{\partial x_i \partial x_k} = \frac{\partial^2 \mu(x)}{\partial x_i \partial x_k} +\frac{\partial^2 \Sigma_{xX}}{\partial x_i \partial x_k} \Sigma_X^{-1}(Y_X - \mu_X) For the typical choices of μ\mu its second order derivatives are zero, meaning that we usually only have the second term to worry about.

Gaussian correlation derivative

The equations above work for any covariance function, but then we need to have the derivatives of the covariance function with respect to the spatial variables. Here we calculate these derivatives for the Gaussian, or squared exponential, correlation function RR.

R(x,u)=exp(=1dθ(xu)2)R(x, u) = \exp \left(-\sum_{\ell =1}^d \theta_\ell (x_\ell - u_{\ell})^2 \right)

R(x,u)xi=2θi(xiui)exp(=1dθ(xu)2)\frac{\partial R(x, u)}{\partial x_i} = -2\theta_i (x_i - u_{i}) \exp \left(-\sum_{\ell =1}^d \theta_\ell (x_\ell - u_{\ell})^2 \right) The second derivative with respect to the same dimension is

2R(x,u)xi2=(2θi+4θi2(xiui)2)exp(=1dθ(xu)2)=(2θi+4θi2(xiui)2)R(x,u) \begin{align} \frac{\partial^2 R(x, u)}{\partial x_i^2} &= \left(-2\theta_i + 4\theta_i^2 (x_i - u_{i})^2 \right) \exp \left(-\sum_{\ell =1}^d \theta_\ell (x_\ell - u_{\ell})^2 \right) \\ &= \left(-2\theta_i + 4\theta_i^2 (x_i - u_{i})^2 \right) R(x, u) \end{align}

The cross derivative for iki \neq k is 2R(x,u)xixk=4θiθk(xiui)(xkuk)exp(=1dθ(xu)2)=4θiθk(xiui)(xkuk)R(x,u) \begin{align} \frac{\partial^2 R(x, u)}{\partial x_i \partial x_k} &= 4\theta_i \theta_k (x_i - u_{i}) (x_k - u_{k}) \exp \left(-\sum_{\ell =1}^d \theta_\ell (x_\ell - u_{\ell})^2 \right) \\ &= 4\theta_i \theta_k (x_i - u_{i}) (x_k - u_{k}) R(x, u) \end{align}

The second derivative with respect to each component, which is needed for the gradient distribution below, is the following for the same dimension ii:

2R(x,u)xiui=(2θi4θi2(xiui)2)exp(=1dθ(xu)2)=(2θi4θi2(xiui)2)R(x,u) \begin{align} \frac{\partial^2 R(x, u)}{\partial x_i \partial u_i} &= \left(2\theta_i - 4\theta_i^2 (x_i - u_{i})^2 \right) \exp \left(-\sum_{\ell =1}^d \theta_\ell (x_\ell - u_{\ell})^2 \right) \\ &= \left(2\theta_i - 4\theta_i^2 (x_i - u_{i})^2 \right) R(x, u) \end{align}

And the following for iki \neq k:

2R(x,u)xiuk=4θiθk(xiui)(xkuk)exp(=1dθ(xu)2)=4θiθk(xiui)(xkuk)R(x,u) \begin{align} \frac{\partial^2 R(x, u)}{\partial x_i \partial u_k} &= -4\theta_i \theta_k (x_i - u_{i}) (x_k - u_{k}) \exp \left(-\sum_{\ell =1}^d \theta_\ell (x_\ell - u_{\ell})^2 \right) \\ &= -4\theta_i \theta_k (x_i - u_{i}) (x_k - u_{k}) R(x, u) \end{align}

Gradient distribution

A big problem with using the gradient of the mean function of a GP is that it doesn’t give an idea of its distribution/randomness. The mean of the gradient could be predicted to be zero in a region where the surface is not flat simply because it has no information in that region yet.

First we want to know what type of distribution the gradient follows. Since the derivative is a linear operator, and a linear operator applied to a normal r.v. is also normal, the gradient must be a multivariate random variable. For a more intuitive explanation, consider a δ\delta approximation to the gradient.

y(x)x=limδ01δ([y(x+δe1)y(x+δed)][y(x)y(x)]) \frac{\partial y(x)}{\partial x} = \lim_{\delta \rightarrow 0} \frac{1}{\delta} \left( \begin{bmatrix} y(x+\delta e_1) \\ \vdots \\ y(x+\delta e_d) \end{bmatrix} - \begin{bmatrix} y(x) \\ \vdots \\ y(x) \end{bmatrix} \right)

For any finite δ\delta, this vector’s components are a linear combination of normal random variables, and thus the vector has a multivariate distribution. We still need to show that in the limit as δ0\delta \rightarrow 0, it retains a multivariate distribution, but I won’t do that here.

Thus the gradient follows a multivariate distribution, and now we will find its mean and covariance.

Gradient expected value

The expected value of the gradient is easily found since it equals the gradient of the expected value. This is true because both gradients and expected value and linear operators and thus can be exchanged.

$$ E \left[\frac{\partial y(x)}{\partial x} \right] = \frac{\partial E[y(x)]}{\partial x} \\ = \frac{\partial \Sigma(x,X)}{\partial x_i} \Sigma_X^{-1}(Y_X - \mu_X) $$

Variance of the gradient

The variance is harder to calculate. I used this reference (McHutchon, n.d.) to get started, but the derivation presented here is much simpler.

We need to find the covariance of the gradient vector of yy at a point xx. Cov(xy(x)) \text{Cov}(\nabla_x y(x)) The (i,j)(i,j) entry of this matrix is Cov(y(x)xi,y(x)xj) \text{Cov}(\frac{\partial y(x)}{\partial x_i},\frac{\partial y(x)}{\partial x_j}) We can write this as a limit.

limδ0Cov(y(x+δei)y(x)δ,y(x+δej)y(x)δ) \lim_{\delta \rightarrow 0} \text{Cov}\left(\frac{y(x+\delta e_i) - y(x)}{\delta}, \frac{y(x+\delta e_j) - y(x)}{\delta}\right) The covariance function is bilinear, so we can split it into four terms.

$$ \begin{align} \lim_{\delta \rightarrow 0} \frac{1}{\delta^2} \left( \text{Cov}\left[y(x+\delta e_i), y(x+\delta e_j)\right] - \text{Cov}\left[y(x+\delta e_i), y(x)\right] \\ - \text{Cov}\left[y(x), y(x+\delta e_j)\right] + \text{Cov}\left[y(x), y(x)\right] \right) \end{align} $$

This can be recognized as the second order derivative of Cov(y(u),y(v))\text{Cov}(y(u), y(v)) with respect to uiu_i and vjv_j evaluated at u=v=xu=v=x. See finite difference differentiation for more details. We have to use uu and vv instead of a single xx to be clear which component of the covariance function is being differentiated.

Thus we have the following. It looks obvious, so I’m not sure I even needed the previous step. Cov(y(x)xi,y(x)xj)=Cov(y(u),y(v))uv|u=x,v=x \text{Cov}(\frac{\partial y(x)}{\partial x_i},\frac{\partial y(x)}{\partial x_j}) = \frac{\partial \text{Cov}(y(u), y(v))}{\partial u \partial v} |_{u=x, v=x} Let UU be the matrix with rows uu and vv. Recall that Cov([y(u)y(v)])=ΣUΣUXΣX1ΣXU \text{Cov}\left( \begin{bmatrix} y(u) \\ y(v) \end{bmatrix} \right) = \Sigma_{U} - \Sigma_{UX} \Sigma_{X}^{-1} \Sigma_{XU} The (1,2)(1,2) element of this is the covariance we are looking for. Cov(y(u),y(v))=Σu,vΣu,XΣX1ΣX,v \text{Cov}(y(u), y(v)) = \Sigma_{u,v} - \Sigma_{u,X} \Sigma_X^{-1} \Sigma_{X,v} Now we need to differentiate this with respect to uiu_i and vjv_j.

Cov(y(x)xi,y(x)xj)=2Σu,vuivjΣu,XuiΣX1ΣX,vvj|u=x,v=x \text{Cov} \left( \frac{\partial y(x)}{\partial x_i},\frac{\partial y(x)}{\partial x_j} \right) = \frac{\partial^2 \Sigma_{u,v}}{\partial u_i \partial v_j} - \frac{\partial \Sigma_{u,X}}{\partial u_i} \Sigma_X^{-1} \frac{\partial \Sigma_{X,v}}{\partial v_j} |_{u=x,v=x} Therefore we have found the distribution of the gradient.

y(x)x|YXN(Σ(x,X)xTΣX1(YXμX),2Σ(x1,x2)x1x2Σ(x1,X)x1Σx1Σ(X,x2)x2|x1=x,x2=x) \frac{\partial y(x)}{\partial x} | Y_X \sim N(\frac{\partial \Sigma(x,X)}{\partial x}^T \Sigma_X^{-1} (Y_X - \mu_X), \frac{\partial^2 \Sigma(x_1, x_2)}{\partial x_1 \partial x_2} - \frac{\partial \Sigma(x_1, X)}{\partial x_1} \Sigma_x^{-1} \frac{\partial \Sigma(X, x_2)}{\partial x_2} |_{x_1=x,x_2=x})

Distribution of the gradient norm squared

Let g(x)=y(x)x|YXg(x) = \frac{\partial y(x)}{\partial x} | Y_X g(x)=xy(x)|YXg(x) = \nabla_x y(x) | Y_X This is a vector. We are often interested in the gradient norm, or its square, g(x)Tg(x)=||g(x)||2g(x)^T g(x) = ||g(x)||^2. This is the sum of correlated squared normal variables, i.e. the sum of correlated chi-squared variables. Since g(x)g(x) has a multivariate normal distribution, the square of its norm is probably distributed according to some kind of chi-squared distribution. We can first try to find its expectation using.

Mean of gradient norm squared

||g(x)||2||g(x)||^2

E[||g(x)||2]=E[i=1dgi(x)2]=i=1dE[gi(x)2] E \left[ ||g(x)||^2 \right] = E \left[ \sum_{i=1}^d g_i(x)^2 \right] = \sum_{i=1}^d E \left[g_i(x)^2 \right]

E[gi(x)2]=Var[gi(x)]+E[gi(x)]2 E \left[ g_i(x)^2 \right] = \text{Var} \left[g_i(x) \right] + E \left[g_i(x) \right]^2 We just found these variances and expectations, so this is a closed form equation.

Full distribution of gradient norm squared

In this section we will derive the full distribution of ||g(x)||2||g(x)||^2 following the fantastic answer from this Math Stack Exchange answer (halvorsen 2015), all credit for this section goes there.

The general idea is that if we could decorrelate the chi-squared variables, then it would be a sum of chi-squared variables, which is a known distribution that is easy to work with.

General derivation

Let XX be a random vector with multivariate distribution with mean μ\mu and covariance matrix Σ\Sigma. XN(μ,Σ) X \sim N(\mu, \Sigma) Let Q(X)Q(X) be a quadratic form of XX defined by the matrix AA. Q(X)=XTAXQ(X) = X^TAX Let Y=Σ1/2XY = \Sigma^{-1/2}X. YY is a decorrelated version of XX, so YN(Σ1/2μ,I)Y \sim N(\Sigma^{-1/2} \mu, I).

Let Z=YΣ1/2μZ = Y - \Sigma^{-1/2}\mu. ZZ is a version of YY with mean zero, so ZN(0,I)Z \sim N(0, I).

Now we have

Q(X)=XTAX=(Z+Σ1/2μ)TΣ1/2AΣ1/2(Z+Σ1/2μ)Q(X) = X^TAX = (Z + \Sigma^{-1/2} \mu)^T \Sigma^{1/2} A \Sigma^{1/2} (Z + \Sigma^{-1/2} \mu)

The spectral theorem allows the middle term to be decomposed as below, where P is the orthogonal matrix of eigenvectors and Λ\Lambda is the diagonal matrix with positive diagonal elements λ1,,λn\lambda_1, \ldots, \lambda_n.

Σ1/2AΣ1/2=PTΛP\Sigma^{1/2} A \Sigma^{1/2} = P^T \Lambda P Let U=PZU=PZ. Since PP is orthogonal and using the distribution of ZZ, we also have that UN(0,I)U \sim N(0, I).

Putting these together, we can change Q(X)Q(X) as follows.

$$ Q(X) = X^TAX = (Z + \Sigma^{-1/2} \mu)^T \Sigma^{1/2} A \Sigma^{1/2} (Z + \Sigma^{-1/2} \mu) \\ = (Z + \Sigma^{-1/2} \mu)^T P^T \Lambda P (Z + \Sigma^{-1/2} \mu) \\ = (PZ + P\Sigma^{-1/2} \mu)^T \Lambda (PZ + P\Sigma^{-1/2} \mu) \\ = (U + b)^T \Lambda (U + b) \\ $$ Here we defined b=PΣ1/2μb= P \Sigma^{-1/2} \mu.

Since Λ\Lambda is diagonal, we have Q(X)=XTAX=j=1nλj(Uj+bj)2Q(X) = X^TAX = \sum_{j=1}^n \lambda_j (U_j + b_j)^2

The UjU_j have standard normal distribution and are independent of each other. (Uj+bj)2(U_j + b_j)^2 is thus the square of normal variable with mean bjb_j and variance 1, meaning it has a noncentral chi-squared distribution with mean bj2+1b_j^2 +1 and variance 4bj2+24b_j^2 + 2. Thus Q(X)Q(X) is distributed as a linear combination of nn noncentral chi-squared variables. Since λj\lambda_j is different for each, Q(X)Q(X) does not have a noncentral chi-squared distribution. However, we can easily find its mean, variance, sample from it, etc.

The mean and variance of Q(X)Q(X) are E[Q(X)]=j=1nλjE[(Uj+bj)2]=j=1nλj(bj2+1) \begin{align} E[Q(X)] &= \sum_{j=1}^n \lambda_j E[(U_j + b_j)^2] \\ &= \sum_{j=1}^n \lambda_j (b_j^2 + 1) \\ \end{align}

Var[Q(X)]=j=1nλj2Var[(Uj+bj)2]=j=1nλj2(4bj2+2) \begin{align} \text{Var}[Q(X)] &= \sum_{j=1}^n \lambda_j^2 \text{Var} \left[ (U_j + b_j)^2 \right] \\ &= \sum_{j=1}^n \lambda_j^2 (4b_j^2 + 2) \\ \end{align}

Relating this back to ||g||2||g||^2

For ||g||2||g||^2 we had A=IA=I, μ=Σx,XxTΣX1(YXμX)\mu=\frac{\partial \Sigma_{x,X}}{\partial x}^T \Sigma_X^{-1} (Y_X - \mu_X), and Σ=2Σ(x1,x2)x1x2+Σ(x1,X)x1Σx1Σ(X,x2)x2\Sigma=\frac{\partial^2 \Sigma(x_1, x_2)}{\partial x_1 \partial x_2} + \frac{\partial \Sigma(x_1, X)}{\partial x_1} \Sigma_x^{-1} \frac{\partial \Sigma(X, x_2)}{\partial x_2}

Thus we need PP and Λ\Lambda from the eigendecomposition Σ=PTΛP\Sigma = P^T \Lambda P. Λ\Lambda will give the λj\lambda_j.

Then we need b=PΣ1/2μb = P \Sigma^{-1/2} \mu. We can calculate Σ1/2\Sigma^{-1/2} as the square root of the matrix Σ1\Sigma^{-1} using the eigendecomposition again. We can decompose Σ1=WTDW\Sigma^{-1} = W^TDW (using different symbols from before to avoid confusion), where DD is diagonal and W is orthogonal. Then Σ1/2=WTSW\Sigma^{-1/2} = W^T S W, where SS is the diagonal matrix whose elements are the square roots of the elements of DD. This can easily be proven as follows. Σ1/2Σ1/2=WTSWWTSW=WTSSW=WTDW=Σ1 \Sigma^{-1/2} \Sigma^{-1/2} = W^T S W W^T S W = W^T S S W = W^T D W = \Sigma^{-1}

Now that we know how to calculate λ\lambda and UU, we can calculate the distribution of ||g||2||g||^2.

References

halvorsen, kjetil b. 2015. “Sum of Squares of Dependent Gaussian Random Variables.” Mathematics Stack Exchange. Mathematics Stack Exchange. https://math.stackexchange.com/q/442916.
McHutchon, Andrew. n.d. “Differentiating Gaussian Processes.” https://mlg.eng.cam.ac.uk/pub/pdf/Mch14.pdf.