I’m wondering if there are some examples or docs on the canonical way to forecast time series. In particular, I’m a bit unclear on how to use `coords`

and `Data`

when updating covariates which are used for both inference and out of sample forecasting. Concretely, I have a simple model as follows (sudo code, full code below)

```
coords = {"date": train_dates}
with pm.Model() as ts_model:
for k in coords.keys():
ts_model.add_coord(k, coords[k], mutable=True)
x = pm.MutableData(
name="x",
value=x_train,
dims=["date"],
)
beta = pm.Normal("beta", mu=0, sigma=1)
mu = pm.Deterministic(
"mu",
var=beta * x,
dims="date",
)
sigma = pm.HalfNormal("sigma", sigma=1)
y = pm.Normal(
"y",
mu=mu,
sigma=sigma,
observed=y_train,
dims="date",
)
```

- Should covariate
`x`

that is used for inference and as inputs for forecasting be`MutableData`

or should these be two different random variables in the model DAG, i.e.`x_train`

and`x_test`

? - How should
`coords`

be used when they need to change for in sample inference and out of sample forecasting? My impression is this is only supported outside of the context manager. - What are the advantages / use cases of using of forecasting using
`pymc.sample_posterior_predictive(predictions=True)`

vs. updating the observed data via`model.set_data`

and then using`pymc.sample_posterior_predictive(predictions=False)`

I’ve come across several overviews of timeseries and model predictions (linked below) but am struggling to distill out the current best practices (for reference I am using pymc v4.2.1).

This example takes the approach of using separate coords for forecasting and fitting and also uses `pymc.sample_posterior_predictive(predictions=True)`

Forecasting with Structural AR Timeseries — PyMC example gallery

This recommends using `MutableData`

for the observed data and then resetting the observed data for the forecast period Predict with new coords leads to conflicting sizes - #2 by lucianopaz

Here is a full example of a simple model `Y ~ Normal(beta*X, sigma)`

where both `Y`

and `X`

are time series. I have set my model up to train on a training set and then forecast on an out of sample test set. The model code I have is set up as follows

```
import pymc as pm
import arviz as az
import numpy as np
import pandas as pd
from scipy.signal import medfilt
np.random.seed(1337)
train_dates = pd.date_range("2020-01-01", "2020-12-31")
test_dates = pd.date_range("2021-01-01", "2021-01-31")
x_train = pd.Series(np.random.randn(len(train_dates)), index=train_dates) * 3
x_test = pd.Series(np.random.randn(len(test_dates)), index=test_dates) * 3
real_beta = 0.8
y_train = real_beta * x_train + pd.Series(
np.random.randn(len(train_dates)), index=train_dates
)
y_test = real_beta * x_test + pd.Series(
np.random.randn(len(test_dates)), index=test_dates
)
y_test_dummy = pd.Series(0, index=test_dates)
coords = {"date": train_dates}
with pm.Model() as ts_model:
for k in coords.keys():
ts_model.add_coord(k, coords[k], mutable=True)
x = pm.MutableData(
name="x",
value=x_train,
dims=["date"],
)
beta = pm.Normal("beta", mu=0, sigma=1)
mu = pm.Deterministic(
"mu",
var=beta * x,
dims="date",
)
sigma = pm.HalfNormal("sigma", sigma=1)
y = pm.Normal(
"y",
mu=mu,
sigma=sigma,
observed=y_train,
dims="date",
)
# Fit to the data
model_trace = pm.sample(
chains=4,
idata_kwargs={"log_likelihood": True},
)
model_posterior_predictive = pm.sample_posterior_predictive(trace=model_trace)
# Predict
test_coords = {"date": test_dates}
with ts_model:
ts_model.set_data("x", x_test, coords=test_coords)
pred_posterior_predictive = pm.sample_posterior_predictive(model_trace.posterior, predictions=True)
```

This gives the following outputs

```
az.extract_dataset(model_posterior_predictive.posterior_predictive).y.mean("sample").to_series().plot(title="Train Data")
model_posterior_predictive.observed_data.y.to_series().plot(color="k", marker="x", linestyle="None")
plt.figure()
az.extract_dataset(pred_posterior_predictive.predictions).y.mean("sample").to_series().plot(title="Test Data")
y_test.plot(color="k", marker="x", linestyle="None")
```