# Predicting on new data with gp.conditional

**URL:** https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229
**Category:** Questions
**Created:** [November 8, 2020, 8:17am UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229 "2020-11-08T08:17:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ravindu\_Fernando](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ravindu_fernando/32/3254_2.png) [@Ravindu\_Fernando](https://discourse.pymc.io/u/Ravindu_Fernando)
#### Post date: [November 8, 2020, 8:17am UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/1 "2020-11-08T08:17:39Z")

</div>

Hi,

My current models is as follows;

```auto
with pm.Model() as model:

  l = pm.Gamma("l", alpha=2, beta=1)
  offset = pm.Gamma("offset", alpha=2, beta=1)
  nu = pm.HalfCauchy("nu", beta=1)

  cov = nu ** 2 * pm.gp.cov.Polynomial(X.shape[1], l, 2, offset)

  gp = pm.gp.Marginal(cov_func=cov)

  sigma = pm.HalfCauchy("sigma", beta=1)
  y_ = gp.marginal_likelihood("y", X=X, y=Y, noise=sigma)

  map_trace = [pm.find_MAP()]

```

```auto
with model:
  f_pred = gp.conditional('f_pred', X_New)

```

```auto
with model:
  pred_samples = pm.sample_posterior_predictive(map_trace, vars=[f_pred], samples=2000)
  y_pred_custom, uncer = pred_samples['f_pred'].mean(axis=0), pred_samples['f_pred'].std(axis=0)

```

This works okay.

But, when I try to predict on some new data, say X\_New2, it throws the error  
_Variable name f\_pred already exists._ from the gp.conditional statement.

I tried using data container (shared variables) for this, but I cannot seem to configure that properly.

Can someone point me in the right direction as to how I can use this model to predict on different data / datasets?

Thanks 🙂

---

<div class="post-metadata">

### Author: ![Ravindu\_Fernando](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ravindu_fernando/32/3254_2.png) [@Ravindu\_Fernando](https://discourse.pymc.io/u/Ravindu_Fernando)
#### Post date: [November 9, 2020, 5:23am UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/2 "2020-11-09T05:23:42Z")

</div>

[junpenglao](https://discourse.pymc.io/u/junpenglao)  
Read the forum a bit and thought you’d have some input on this. I just want to use this same model to get predictions over and over again. Do you have any thoughts on how to go about this? Thanks 🙂

---

<div class="post-metadata">

### Author: ![BioGoertz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/biogoertz/32/4489_2.png) [@BioGoertz](https://discourse.pymc.io/u/BioGoertz)
#### Post date: [November 9, 2020, 11:14am UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/3 "2020-11-09T11:14:01Z")

</div>

So what you’re doing here is drawing full samples from the GP, then calculating the pointwise mean and std:

```python
pred_samples = pm.sample_posterior_predictive(map_trace, vars=[f_pred], samples=2000)
  y_pred_custom, uncer = pred_samples['f_pred'].mean(axis=0), pred_samples['f_pred'].std(axis=0)

```

This is unnecessary, since the mean and std at each point can be calculated analytically. Use [`gp.predict`](https://docs.pymc.io/notebooks/GP-Marginal.html#Making-predictions) instead, it’ll be _much_ faster, and you can dynamically specify `Xnew`'s the way you want to. You can even provide it new observed data on the fly via the `given` argument (see [here](https://docs.pymc.io/api/gp/implementations.html#pymc3.gp.gp.Marginal.conditional), [here](https://docs.pymc.io/api/gp/implementations.html#pymc3.gp.gp.Marginal.predict)). Note this doesn’t re-optimize the hyperparameters for each new set of observations you provide, it merely estimates the GP on the new data using the already-optimized hyperparameters, which may or may not be what you want.

Now, if you _do_ need full samples from the GP, a sort of hacky way is to just give each new variable a new name, e.g.:

```python
with model:
  f_pred2 = gp.conditional('f_pred2', X_New2)

```

```python
with model:
  pred_samples = pm.sample_posterior_predictive(map_trace, vars=[f_pred2], samples=2000)
  y_pred_custom, uncer = pred_samples['f_pred2'].mean(axis=0), pred_samples['f_pred2'].std(axis=0)

```

If you wanted to keep things programmatically simpler (though likely harder to interpret), you could keep the external variable name the same and just change the internal name:

```python
with model:
  f_pred = gp.conditional('f_pred2', X_New2)

```

```python
with model:
  pred_samples = pm.sample_posterior_predictive(map_trace, vars=[f_pred], samples=2000)
  y_pred_custom, uncer = pred_samples['f_pred2'].mean(axis=0), pred_samples['f_pred2'].std(axis=0)

```

That being said, I share your pain. It seems like there should be a way to overwrite variables inside a model, although perhaps the compiled nature of the model gets in the way. Or, if not that, then an ephemeral way to specify prediction points for drawing full samples (similar to `gp.predict` or the `given` input) would be really useful.

---

<div class="post-metadata">

### Author: ![Ravindu\_Fernando](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ravindu_fernando/32/3254_2.png) [@Ravindu\_Fernando](https://discourse.pymc.io/u/Ravindu_Fernando)
#### Post date: [November 9, 2020, 11:28am UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/4 "2020-11-09T11:28:34Z")

</div>

Thank you for this lengthy reply. Really appreciate it.

I was going over this for a long time looking for a way to implement this. Finally settled on using a ‘uuid’ for variable name, which changes with every iteration, just like you suggested. I will definitely try using this gp.predict method aswell.

Thanks again!

---

<div class="post-metadata">

### Author: ![bwengals](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bwengals/32/1237_2.png) [@bwengals](https://discourse.pymc.io/u/bwengals)
#### Post date: [November 9, 2020, 11:28pm UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/5 "2020-11-09T23:28:37Z")

</div>

I’m able to use a theano.shared for X\_new, does this not work?

```auto
X_New_shared = theano.shared(X_New)

with model:
  f_pred = gp.conditional('f_pred', X_New_shared, shape=(X_New.shape[0], )) # needed to specify shape

```

then run ppc sampling

```auto
with model:
  pred_samples = pm.sample_posterior_predictive(map_trace, vars=[f_pred], samples=2000)

```

then swap out the shared value

```auto
X_New_shared.set_value(different_X_New)

```

then rerun `sample_posterior_predictive`,

```auto
with model:
  pred_samples = pm.sample_posterior_predictive(map_trace, vars=[f_pred], samples=2000)

```

Is this what you meant? But yes, agree with @BioGoertz, it would be nice to overwrite variables. I think it must be possible, but I’m not sure.

---

<div class="post-metadata">

### Author: ![Ravindu\_Fernando](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ravindu_fernando/32/3254_2.png) [@Ravindu\_Fernando](https://discourse.pymc.io/u/Ravindu_Fernando)
#### Post date: [November 10, 2020, 3:46am UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/6 "2020-11-10T03:46:46Z")

</div>

Hi @bwengals

Should we not re-run

```auto
with model:
  f_pred = gp.conditional('f_pred', X_New_shared, shape=(different_X_New.shape[0], ))

```

every time I need to predict? However, everytime this function is run, it needs a new variable.

---

<div class="post-metadata">

### Author: ![bwengals](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bwengals/32/1237_2.png) [@bwengals](https://discourse.pymc.io/u/bwengals)
#### Post date: [November 10, 2020, 4:32am UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/7 "2020-11-10T04:32:01Z")

</div>

I don’t think you need to. If you’ve already specified the model, and you just want to keep changing X\_new, but you still want predictions from `gp`, then i think using a shared var here will do the trick right?

---

<div class="post-metadata">

### Author: ![BioGoertz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/biogoertz/32/4489_2.png) [@BioGoertz](https://discourse.pymc.io/u/BioGoertz)
#### Post date: [November 10, 2020, 7:57pm UTC](https://discourse.pymc.io/t/predicting-on-new-data-with-gp-conditional/6229/8 "2020-11-10T19:57:18Z")

</div>

That’s good to know! I always forget the power of `shared` variables…
