Spatial derivatives of Gaussian process models
Collin Erickson
2026-05-11
Source:vignettes/surface_derivatives.Rmd
surface_derivatives.RmdThis 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 data points that are the function values corresponding to the rows of the by design matrix . We have a mean function and covariance function .
is the random variable representing the output corresponding to input point conditional on the data . Thus , and is a random variable with distribution
Gradient of mean function
The mean function is and are the only parts that depends on .
We can find the gradient of by taking the partial derivatives
Remember that is overloaded and the second derivative on the right hand side is: The calculation above is for a single partial derivative. The vector of these gives the gradient . Note that the partial derivative of a scalar with respect to a vector is another way of writing the gradient.
We usually use 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 For the typical choices of 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 .
The second derivative with respect to the same dimension is
The cross derivative for is
The second derivative with respect to each component, which is needed for the gradient distribution below, is the following for the same dimension :
And the following for :
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 approximation to the gradient.
For any finite , 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 , 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 at a point . The entry of this matrix is We can write this as a limit.
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 with respect to and evaluated at . See finite difference differentiation for more details. We have to use and instead of a single 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. Let be the matrix with rows and . Recall that The element of this is the covariance we are looking for. Now we need to differentiate this with respect to and .
Therefore we have found the distribution of the gradient.
Distribution of the gradient norm squared
Let This is a vector. We are often interested in the gradient norm, or its square, . This is the sum of correlated squared normal variables, i.e. the sum of correlated chi-squared variables. Since 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
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 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 be a random vector with multivariate distribution with mean and covariance matrix . Let be a quadratic form of defined by the matrix . Let . is a decorrelated version of , so .
Let . is a version of with mean zero, so .
Now we have
The spectral theorem allows the middle term to be decomposed as below, where P is the orthogonal matrix of eigenvectors and is the diagonal matrix with positive diagonal elements .
Let . Since is orthogonal and using the distribution of , we also have that .
Putting these together, we can change 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 .
Since is diagonal, we have
The have standard normal distribution and are independent of each other. is thus the square of normal variable with mean and variance 1, meaning it has a noncentral chi-squared distribution with mean and variance . Thus is distributed as a linear combination of noncentral chi-squared variables. Since is different for each, 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 are
Relating this back to
For we had , , and
Thus we need and from the eigendecomposition . will give the .
Then we need . We can calculate as the square root of the matrix using the eigendecomposition again. We can decompose (using different symbols from before to avoid confusion), where is diagonal and W is orthogonal. Then , where is the diagonal matrix whose elements are the square roots of the elements of . This can easily be proven as follows.
Now that we know how to calculate and , we can calculate the distribution of .