# Covariance of derivative of Gaussian Process Regression

There are a quite a few questions and answers which discuss how to calculate the gradients/derivatives of the posterior of Gaussian Process Regression (see here, here). These include the equations for calculating the mean but the calculation of the covariance is less clear. How can we calculate the covariance of the gradient?

• Hi do you have the terminology correct ? maybe you're referrring to the hessian which is the derivative of the gradient ? I've never heard of the covariance of the gradient but hopefully others have. – mlofton Jul 2 '20 at 2:11
• @mlofton it is possible my terminology is wrong. Essentially what I want is to be able to calculate the uncertainty of the gradient in the form of the variance/covariance matrix. Previous answers covered calculating the mean estimate of the gradient but not the uncertainty. – Robin Jul 2 '20 at 2:38
• @mlofton I don't know if I'd say the terminology is wrong, per se. Robin is looking for the posterior distribution of the derivative of a Gaussian process. The linked answers discuss the mean of that distribution, which is the same as the derivative of the mean of the GP posterior, or the gradient of the GP posterior, but Robin needs the covariance of the posterior of the derivative of the GP. See my answer for details. – duckmayr Jul 2 '20 at 13:58
• Okay. Thanks. I was hoping someone else would respond with knowledge and you did. I will print out and try to understand at some point. It looks like it's not something to just glance at and absorb. Thanks for intelligent response and apologies for claim that the terminology was incorrect. – mlofton Jul 3 '20 at 20:14
• @mlofton No worries; I don't think your original comment sounding judgmental or anything, mostly questioning and/or curious. Yeah, for the person unfamiliar with inference in Gaussian process models, some of the terminology or way of thinking about the inference can be strange at first, as the inference occurs in a function space rather than a typical parameter space. Cheers – duckmayr Jul 3 '20 at 21:59

You may find McHutchon (2013) useful; everything you need is there, but in case the link goes dead, I'll put a streamlined version here.

As you noted, multiple answers here cover

$$\mathbb{E} \left[ \frac{\partial \mathbf{f}_\ast}{\partial \mathbf{x}_\ast} \right] = \frac{\partial k\left(\mathbf{x}_\ast, \mathbf{X}\right)}{\partial \mathbf{x}_\ast} K^{-1} \mathbf{y},$$

but how do we get

$$\mathbb{V} \left[ \frac{\partial \mathbf{f}_\ast}{\partial \mathbf{x}_\ast} \right]?$$

We consider an additional test point $$\mathbf{x}_\ast + \boldsymbol\delta$$. Then

\begin{align} f \left( \mathbf{x}_\ast \right) & = \bar{f} \left( \mathbf{x}_\ast \right) + \mathbf{z}_\ast \\ f \left( \mathbf{x}_\ast + \boldsymbol\delta \right) & = \bar{f} \left( \mathbf{x}_\ast + \boldsymbol\delta \right) + \mathbf{z}_\delta \\ \end{align}

and

$$\begin{bmatrix} \mathbf{z}_\ast \\ \mathbf{z}_\delta \end{bmatrix} \sim \mathcal{N} \left( \mathbf{0}, \begin{bmatrix} k_{\ast\ast} - \mathbf{k}_\ast^T K^{-1} \mathbf{k}_\ast & k_{\ast\delta} - \mathbf{k}_\ast^T K^{-1} \mathbf{k}_\delta \\ k_{\delta\ast} - \mathbf{k}_\delta^T K^{-1} \mathbf{k}_\ast & k_{\delta\delta} - \mathbf{k}_\delta^T K^{-1} \mathbf{k}_\delta \\ \end{bmatrix} \right).$$

Taking the limit as $$\boldsymbol\delta \to \mathbf{0}$$,

\begin{align} \frac{\partial \mathbf{f}_\ast}{\partial \mathbf{x}_\ast} & = \lim_{\boldsymbol\delta \to \mathbf{0}} \frac{f \left( \mathbf{x}_\ast + \boldsymbol\delta \right) - f \left( \mathbf{x}_\ast \right)}{\mathbf{x}_\ast + \boldsymbol\delta - \mathbf{x}_\ast} \\ & = \frac{\partial \bar{\mathbf{f}}_\ast}{\partial \mathbf{x}_\ast} + \lim_{\boldsymbol\delta \to \mathbf{0}} \frac{\mathbf{z}_\delta - \mathbf{z}_\ast}{\boldsymbol\delta}, \end{align}

we find

$$\mathbb{V} \left[ \lim_{\boldsymbol\delta \to \mathbf{0}} \frac{\mathbf{z}_\delta - \mathbf{z}_\ast}{\boldsymbol\delta} \right] = \mathbb{V} \left[ \frac{\partial \mathbf{f}_\ast}{\partial \mathbf{x}_\ast} \right] = \frac{\partial^2 k \left(\mathbf{x}_1^\ast, \mathbf{x}_2^\ast \right)}{\partial \mathbf{x}_1^\ast \partial \mathbf{x}_2^\ast} - \frac{\partial k \left(\mathbf{x}_\ast, \mathbf{X} \right)}{\partial \mathbf{x}_\ast} K^{-1} \frac{\partial k \left(\mathbf{x}_\ast, \mathbf{X} \right)}{\partial \mathbf{x}_\ast}^T .$$

$$\frac{\partial^2 k \left(\mathbf{x}_1^\ast, \mathbf{x}_2^\ast \right)}{\partial \mathbf{x}_1^\ast \partial \mathbf{x}_2^\ast},$$
$$\mathbf{x}_1^\ast = \mathbf{x}_2^\ast = \mathbf{x}_\ast$$, but we must do it in this cross-partial way to avoid negatives on the diagonal. All credit for this presentation of the derivation goes to McHutchon (2013), like I say, I simply reproduce relevant parts here for completeness of the answer.