# Forecasting using distributions\\timeseries in pymc 4.4.0

**URL:** <https://discourse.pymc.io/t/forecasting-using-distributions-timeseries-in-pymc-4-4-0/11680>\
**Category:** Questions\
**Created:** [March 23, 2023, 5:45am UTC](https://discourse.pymc.io/t/forecasting-using-distributions-timeseries-in-pymc-4-4-0/11680 "2023-03-23T05:45:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@andrew1](https://discourse.pymc.io/u/andrew1)\
**Post date:** [March 23, 2023, 5:45am UTC](https://discourse.pymc.io/t/forecasting-using-distributions-timeseries-in-pymc-4-4-0/11680/1 "2023-03-23T05:45:14Z")

</div>

Is there yet a general way to forecast n steps ahead using timeseries models, like pm.AR ?

I thing that ideally there would be an ‘extra steps’ argument, or something similar to generate extra points beyond the end of the observed data for these models.

Or an accessible iterator function supplied by the ts class, which allowed stepping to be coded manually, given the necessary state.

I would like something that can be used in the main inference sampling as well as ppc. So far, I have tried a few approaches

(i) passing in a pandas series with padded nans at the end, as suggested here:

> [@Predicting out-of-sample for autoregressive models](https://discourse.pymc.io/t/predicting-out-of-sample-for-autoregressive-models/984/5):
>
> I believe I have a basic working version of this. Assumes your trace has a rho and scale. This also allows for multiple observations for the same date, which is often a case I’m working with. But if you want to just have one observation per date you could just remove the date\_idx parts. def predict\_outofsample(trace, date\_idx): """ trace: a pymc3 MultiTrace object date\_idx: np.ndarray with shape (N\_obs), indicating for each observation what date it corresponds to …

this looks like it should be a generic solution, but I get the error:

Automatic inputation is only supported for univariate RandomVariables. lk of type \<class ‘pymc.distributions.timeseries.AutoRegressiveRV’\> is not supported

(which I suspect should read imputation)

(ii) I tried also having two calls to pm.AR() in the model, the first including the observed data, and the second intended to carry on (forecasting n steps) where the first one left off. However, I could not figure out how to set up the latter correctly - because init\_dist in the second call is not going to accept a tensor value passed out of the first call.

The only way that I have been able to get this to work so far, is to manually code an iterator function that steps the AR process forward separately from the pm.AR. this is obviously not ideal, as you end up having to recode the process which is inside the time-series distribution.

Thanks for any insights

A

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [March 23, 2023, 7:02am UTC](https://discourse.pymc.io/t/forecasting-using-distributions-timeseries-in-pymc-4-4-0/11680/2 "2023-03-23T07:02:15Z")

</div>

Do you want to forecast, conditioning on the last observed value or also redrawing the observed timepoints?

If it’s the first, the easiest is to add a Deterministic:

```python
with pm.Model() as m:
  ...
  obs = pm.AR("obs", ..., observed=data)
  pred = pm.Deterministic(
    "preds", 
    pm.AR.dist(
      init_dist=pm.DiracDelta.dist(data[-1]), 
      ...,
      steps=100,
  ),
)

```

Passing the same parameters that you use for the observed timeseries, which I omitted with `...`

If the second, just use the default `sample_posterior_predictive`. You can make the shape depend on a MutableData to increase the number of steps that are simulated.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [March 23, 2023, 7:14am UTC](https://discourse.pymc.io/t/forecasting-using-distributions-timeseries-in-pymc-4-4-0/11680/3 "2023-03-23T07:14:40Z")

</div>

This is also a pretty comprehensive set of examples: [Forecasting with Structural AR Timeseries — PyMC example gallery](https://www.pymc.io/projects/examples/en/latest/time_series/Forecasting_with_structural_timeseries.html)

---

<div class="post-metadata">

**Author:** ![andrew1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@andrew1](https://discourse.pymc.io/u/andrew1)\
**Post date:** [March 23, 2023, 7:23am UTC](https://discourse.pymc.io/t/forecasting-using-distributions-timeseries-in-pymc-4-4-0/11680/4 "2023-03-23T07:23:28Z")

</div>

Thanks, that is what I was looking for
