You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's see how to do this in PyMC! The key observation is that we need to pass the observed data explicitly into out "for loop" in the generative graph. That is, we need to pass it into the {meth}`scan <pytensor.scan.basic.scan>` function.
Then we can simply generate samples from the posterior predictive distribution. Observe we need to "rewrite" the generative graph to include te conditioned transition step. Nevertheless, we will use the posterior samples from the model above, this means we can put *any* "prior" distributions on the parameters we learned. For a detailed explanation on these type of cross model predictions, see the great blog post [Out of model predictions with PyMC](https://www.pymc-labs.com/blog-posts/out-of-model-predictions-with-pymc/).
332
334
335
+
+++
336
+
337
+
```{warning}
338
+
We need to shift the coordinate `steps` forward by one! The reasons is that the data at (for example) `step=1` is used to create the prediction for `step=2`. If one does not do the shift, the `step=0` prediction will be mislabeled as `step=0`, and the model will look better than it is.
@@ -388,6 +412,133 @@ We indeed see that these credible intervals are tighter than the unconditional o
388
412
389
413
+++
390
414
415
+
Here are some additional remarks:
416
+
417
+
- There's no prediction for $y_0$, because we don't observe $y_{t - 1}$.
418
+
- The predictions seem to "chase" the data, since that's exactly what we're doing. At each step, we reset to the observed data and make one prediction.
419
+
420
+
```{note}
421
+
Relative to the `statsmodel` reference, we're just a little different in the initialization. This makes sense, since they do some fancy MLE initialization trickery and we estimate it as a parameter. The difference should wash out as we iterate over the sequence, and we see that indeed it does.
422
+
```
423
+
424
+
+++
425
+
426
+
## Out of Sample Predictions
427
+
428
+
In this last section, wee describe how to generate out-of-sample predictions.
429
+
430
+
```{code-cell} ipython3
431
+
# Specify the number of steps to forecast
432
+
forecast_steps = 10
433
+
```
434
+
435
+
The idea is to use the posterior samples and the latest available two data points (because we have an AR(2) model) to generate the forecast:
436
+
437
+
```{code-cell} ipython3
438
+
coords = {
439
+
"lags": range(-lags, 0),
440
+
"trials": range(trials),
441
+
"steps": range(trials, trials + forecast_steps),
442
+
}
443
+
with pm.Model(coords=coords, check_bounds=False) as forecasting_model:
- Authored by [Jesse Grabowski](https://github.com/jessegrabowski), [Juan Orduz](https://juanitorduz.github.io/) and [Ricardo Vieira](https://github.com/ricardoV94) in March 2024
0 commit comments