# Using n-days forward prediction in Bayesian (Hierarchical) VAR model example

**URL:** <https://discourse.pymc.io/t/using-n-days-forward-prediction-in-bayesian-hierarchical-var-model-example/14175>\
**Category:** General\
**Created:** [April 3, 2024, 8:48pm UTC](https://discourse.pymc.io/t/using-n-days-forward-prediction-in-bayesian-hierarchical-var-model-example/14175 "2024-04-03T20:48:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Den](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@Den](https://discourse.pymc.io/u/Den)\
**Post date:** [April 3, 2024, 8:48pm UTC](https://discourse.pymc.io/t/using-n-days-forward-prediction-in-bayesian-hierarchical-var-model-example/14175/1 "2024-04-03T20:48:21Z")

</div>

Hello all,

In this example [blog post](https://www.pymc.io/projects/examples/en/latest/time_series/bayesian_var_model.html) on the Hierarchical VAR model - is there anyway to modify the model so that we can predict y\_{t} from inputs y\_{t-n},y\_{t-n-1},... (for say n=30) instead of just one day forward i.e. inputs y\_{t-1}, y\_{t-2}, ... ?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [April 3, 2024, 11:18pm UTC](https://discourse.pymc.io/t/using-n-days-forward-prediction-in-bayesian-hierarchical-var-model-example/14175/2 "2024-04-03T23:18:34Z")

</div>

I’m not totally sure what you mean. If you just want the conditional forecast y\_{t+1} | \{y\_{t-s}\}\_{s=0}^n, that will be governed by the data you show the model when you sample the posterior parameters. If you use 30 days of data, the one-step out-of-sample forecast will be that conditional distribution. If you mean that you want to use 30 days of data to make the t+1-th prediction, i.e. y\_t = f(y\_{t-1}, .., y\_{t-30}), you will have to set the number of lags to 30. Note that in this case, you’ll have a lot of trouble sampling, because the probability of sampling stationary and invertible coefficient matrices goes to zero quite quickly, see [here](https://github.com/jessegrabowski/VAR-Blog/blob/main/Big%20VAR%20Blog%20Post.ipynb).

---

<div class="post-metadata">

**Author:** ![Den](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@Den](https://discourse.pymc.io/u/Den)\
**Post date:** [April 4, 2024, 6:29am UTC](https://discourse.pymc.io/t/using-n-days-forward-prediction-in-bayesian-hierarchical-var-model-example/14175/3 "2024-04-04T06:29:34Z")

</div>

Thanks for gettting back. This is purely an implementation query - rather than AR of lag p, of the form:

y\_t = \alpha + B\_1y\_{t-1} + B\_2y\_{t-2}+\dots+B\_py\_{t-p}

I’d like the code to allow me to start the lag at an earlier time point n - days - (probably via the function calc\_ar\_step most likely ).I.e. for the above straightforwardly n=1, but I want the implemented code to be for any n  
y\_t = \alpha + B\_1y\_{t-n}+B\_2y\_{t-n-1}+\dots+B\_py\_{t-n-p}.

To be abundantly clear, for n=30, say, I want the code to allow me to fit the model:

y\_t = \alpha + B\_1y{t-30}+B\_2y\_{t-31}+\dots+B\_py\_{t-30-p}.

Hope that makes sense.

---

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [April 4, 2024, 10:25am UTC](https://discourse.pymc.io/t/using-n-days-forward-prediction-in-bayesian-hierarchical-var-model-example/14175/4 "2024-04-04T10:25:36Z")

</div>

You are right, the lagging happens in the `calc_ar_step` function. You need to change this line:

```auto
        ar = pm.math.sum(
            [
                pm.math.sum(lag_coefs[j, i] * df.values[n_lags - (i + 1) : -(i + 1)], axis=-1)
                for i in range(n_lags)
            ],
            axis=0,
        )

```

To account for the additional shifting that you want. I’d have to play with it to figure it out exactly, but I think you want something like `df.values[30 + n_lags - (i + 1) : -(i + 1)]`. That would align the 30th value with the first value in the design matrix, then lag from there.

---

<div class="post-metadata">

**Author:** ![Den](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@Den](https://discourse.pymc.io/u/Den)\
**Post date:** [April 4, 2024, 7:41pm UTC](https://discourse.pymc.io/t/using-n-days-forward-prediction-in-bayesian-hierarchical-var-model-example/14175/5 "2024-04-04T19:41:22Z")

</div>

Unfortunately, it seems like df is fed into that function in a non-intuitive way -it may be done incrementally - since for i = 0, the indices of the slice is 1 and -1. Is there any way you can have a quick play around and figure out a way? Apologies as I would do it on my own but I am very new to pymc and need something fairly quickly to test out - and debugging/writing new pymc code seems a bit esoteric to be at the moment haha.

Btw - thanks for the link to your blog/articles - currently working on Bayesian time series model so will take a look through them some point in the near future!
