# Forecasting with pymc3

**URL:** <https://discourse.pymc.io/t/forecasting-with-pymc3/578>\
**Category:** Questions\
**Created:** [November 17, 2017, 7:44pm UTC](https://discourse.pymc.io/t/forecasting-with-pymc3/578 "2017-11-17T19:44:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![datascientist](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@datascientist](https://discourse.pymc.io/u/datascientist)\
**Post date:** [November 17, 2017, 7:44pm UTC](https://discourse.pymc.io/t/forecasting-with-pymc3/578/1 "2017-11-17T19:44:54Z")

</div>

I’m using pymc3 to model time series in a state-space framework. In order to make forecasts of the future, I find myself essentially re-coding my pymc3 model in python so as to roll the model forward and simulate possible futures.

I read the section [“Prediction”](http://docs.pymc.io/notebooks/posterior_predictive.html#Prediction) in the docs, which describes how to use shared variables to plug in different values for the independent variables in a logistic regression. But I don’t think that paradigm works for a time series with hidden states, because I need the last “observed” value of the hidden states in order to roll the model forward.

As an example, think of the [AR1 distribution](https://github.com/pymc-devs/pymc3/blob/master/pymc3/distributions/timeseries.py#L22) - in order to forecast, you need the last value of `x`. So to simulate a possible future, you’d need to do something like:

```auto
point = trace.point(j)
k, tau_e = point['k'], point['tau_e']
last_x = point['x'][-1]
x = []
for i in range(horizon):
     new_x = k*last_x + np.random.normal(loc=0, scale=tau_e**-.5)
     x.append(new_x)
     last_x = new_x

```

…and you see what I mean about re-coding the model.

So my question is: am I missing something, or is this par for the course with time series models?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [November 24, 2017, 6:51am UTC](https://discourse.pymc.io/t/forecasting-with-pymc3/578/2 "2017-11-24T06:51:45Z")

</div>

I would do it this way as well. You can also add a new Variable to the model for prediction, but personally, I find it cleaner to separate the inference with the prediction.
