# Differentiating a GP function

**URL:** <https://discourse.pymc.io/t/differentiating-a-gp-function/5026>\
**Category:** Questions\
**Created:** [May 6, 2020, 1:24pm UTC](https://discourse.pymc.io/t/differentiating-a-gp-function/5026 "2020-05-06T13:24:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aryaf](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@aryaf](https://discourse.pymc.io/u/aryaf)\
**Post date:** [May 6, 2020, 1:24pm UTC](https://discourse.pymc.io/t/differentiating-a-gp-function/5026/1 "2020-05-06T13:24:07Z")

</div>

Hello,

I was wondering if there is an easy way to differentiate a GP function respect to x\*. I can easily do it numerically, but I want to get a posterior interval for it as well.

```
with pm.Model() as model:
    x_scale = 2.0 
    m_scale = 8.0
    eta1 = pm.HalfNormal("eta1", sigma=2)

    cov_func1 = eta1 ** 2 * pm.gp.cov.Matern52(input_dim=input_dim, ls=x_scale, active_dims=[0]) \
                * pm.gp.cov.Matern52(input_dim=input_dim, ls=m_scale, active_dims=[1])

    eta2 = pm.HalfNormal("eta2", sigma=2)
    cov_func2 = eta2 ** 2 * pm.gp.cov.Matern52(input_dim=input_dim, ls=x_scale, active_dims=[0]) \
                * pm.gp.cov.Matern52(input_dim=input_dim, ls=m_scale, active_dims=[1])

    gp1 = pm.gp.MarginalSparse(cov_func=cov_func1, approx="VFE")
    gp2 = pm.gp.MarginalSparse(cov_func=cov_func2, approx="VFE")

    # set flat prior for Xu
    # Xu = pm.Flat("Xu1", shape=Xu_init.shape, testval=Xu_init)

    sigma_1 = pm.HalfCauchy("sigma_1", beta=5)
    sigma_2 = pm.HalfCauchy("sigma_2", beta=5)

    y1_err_ = tt.sqrt(Ys1err * Ys1err + sigma_1 * sigma_1)
    y2_err_ = tt.sqrt(Ys2err * Ys2err + sigma_2 * sigma_2)

    y1_ = gp1.marginal_likelihood("y1", X=Xs, Xu=Xu_init, y=Ys1, noise=y1_err_)
    y2_ = gp2.marginal_likelihood("y2", X=Xs, Xu=Xu_init, y=Ys2, noise=y2_err_)

with model:
    mp = pm.find_MAP(method="BFGS")

```

Thanks,

---

<div class="post-metadata">

**Author:** ![ckrapu](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ckrapu/32/1366_2.png) [@ckrapu](https://discourse.pymc.io/u/ckrapu)\
**Post date:** [May 6, 2020, 8:13pm UTC](https://discourse.pymc.io/t/differentiating-a-gp-function/5026/2 "2020-05-06T20:13:06Z")

</div>

To clarify, are you interested in automatically calculating df/dx where f \sim GP(\mu(x), K(x,x')) is a random function sampled from a GP?

---

<div class="post-metadata">

**Author:** ![aryaf](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@aryaf](https://discourse.pymc.io/u/aryaf)\
**Post date:** [May 6, 2020, 10:39pm UTC](https://discourse.pymc.io/t/differentiating-a-gp-function/5026/3 "2020-05-06T22:39:28Z")

</div>

Thank you for the clarification. Yes, that is correct.  
I want to compute \frac{\partial f}{\partial x\_i} where f \sim \mathcal{GP}(0, K({\bf x}^{\prime}, {\bf x})).

---

<div class="post-metadata">

**Author:** ![ckrapu](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ckrapu/32/1366_2.png) [@ckrapu](https://discourse.pymc.io/u/ckrapu)\
**Post date:** [May 6, 2020, 11:55pm UTC](https://discourse.pymc.io/t/differentiating-a-gp-function/5026/4 "2020-05-06T23:55:06Z")

</div>

I don’t have deep familiarity with the internals of the GP module and so perhaps there is a shortcut I’m unaware of. However, section 9.4 of [this book](http://www.gaussianprocess.org/gpml/chapters/RW9.pdf) suggests that \frac{df}{dx} \sim GP( \frac{\partial \mu}{\partial x}, \frac{\partial^2K}{\partial x \partial x'}). This means that you just need to figure out what the new mean function and kernel K' would be and then create a new GP with them in your model. I think this is a fairly straightforward solution and can try to show you if you are interested. Unfortunately, it will not be a generic solution applicable to each kernel as the derivative kernel will be different for every original kernel function.

---

<div class="post-metadata">

**Author:** ![aryaf](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@aryaf](https://discourse.pymc.io/u/aryaf)\
**Post date:** [May 7, 2020, 5:04pm UTC](https://discourse.pymc.io/t/differentiating-a-gp-function/5026/5 "2020-05-07T17:04:25Z")

</div>

Thank you for pointing out this book. I was wondering maybe PYMC has an implementation of this (at least for differentiable Kernels - such as Martern 3/2 and 5/2 kernels. I will try to figure it out how to implement it, but if you have an example I am sure it would be super helpful.

Thank you,

---

<div class="post-metadata">

**Author:** ![Gon\_F](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Gon\_F](https://discourse.pymc.io/u/Gon_F)\
**Post date:** [May 9, 2020, 4:32am UTC](https://discourse.pymc.io/t/differentiating-a-gp-function/5026/6 "2020-05-09T04:32:31Z")

</div>

That could indeed be an interesting addition to the GP module, hmmm…
