@@ -128,7 +128,7 @@ Now that we have implemented the AR(2) step, we can assign priors to the paramet
128
128
coords = {
129
129
"lags": range(-lags, 0),
130
130
"steps": range(timeseries_length - lags),
131
- "trials ": range(timeseries_length),
131
+ "timeseries_length ": range(timeseries_length),
132
132
}
133
133
with pm.Model(coords=coords, check_bounds=False) as model:
134
134
rho = pm.Normal(name="rho", mu=0, sigma=0.2, dims=("lags",))
@@ -146,7 +146,7 @@ with pm.Model(coords=coords, check_bounds=False) as model:
146
146
)
147
147
148
148
ar = pm.Deterministic(
149
- name="ar", var=pt.concatenate([ar_init, ar_innov], axis=-1), dims=("trials ",)
149
+ name="ar", var=pt.concatenate([ar_init, ar_innov], axis=-1), dims=("timeseries_length ",)
150
150
)
151
151
152
152
@@ -191,6 +191,7 @@ fig, ax = plt.subplots(
191
191
nrows=5, ncols=1, figsize=(12, 12), sharex=True, sharey=True, layout="constrained"
192
192
)
193
193
chosen_draw = 2
194
+
194
195
for i, axi in enumerate(ax, start=chosen_draw):
195
196
axi.plot(
196
197
prior.prior["ar"].isel(draw=i, chain=0),
@@ -207,10 +208,10 @@ Next, we want to condition the AR(2) model on some observed data so that we can
207
208
``` {code-cell} ipython3
208
209
# select a random draw from the prior
209
210
prior_draw = prior.prior.isel(chain=0, draw=chosen_draw)
210
- test_data = prior_draw["ar_dist"].values
211
+ test_data = prior_draw["ar_dist"].to_numpy()
211
212
212
213
with pm.observe(model, {"ar_dist": test_data}) as observed_model:
213
- trace = pm.sample()
214
+ trace = pm.sample(chains=4, random_seed=rng )
214
215
```
215
216
216
217
Let's plot the trace and the posterior distribution of the parameters.
@@ -229,7 +230,7 @@ axes = az.plot_trace(
229
230
("rho", {}, rho_true),
230
231
("sigma", {}, sigma_true),
231
232
],
232
- backend_kwargs={"figsize": (12, 5 ), "layout": "constrained"},
233
+ backend_kwargs={"figsize": (12, 7 ), "layout": "constrained"},
233
234
)
234
235
plt.gcf().suptitle("AR(2) Model Trace", fontsize=18, fontweight="bold");
235
236
```
@@ -263,15 +264,15 @@ for i, hdi_prob in enumerate((0.94, 0.64), 1):
263
264
lower = hdi.sel(hdi="lower")
264
265
upper = hdi.sel(hdi="higher")
265
266
ax.fill_between(
266
- x=post_pred_ar["trials "],
267
+ x=post_pred_ar["timeseries_length "],
267
268
y1=lower,
268
269
y2=upper,
269
270
alpha=(i - 0.2) * 0.2,
270
271
color="C0",
271
272
label=f"{hdi_prob:.0%} HDI",
272
273
)
273
274
ax.plot(
274
- post_pred_ar["trials "],
275
+ post_pred_ar["timeseries_length "],
275
276
post_pred_ar.mean(("chain", "draw")),
276
277
color="C0",
277
278
label="Mean",
@@ -343,7 +344,7 @@ We need to shift the coordinate `steps` forward by one! The reasons is that the
343
344
coords = {
344
345
"lags": range(-lags, 0),
345
346
"steps": range(-1, timeseries_length - lags - 1), # <- Coordinate shift!
346
- "trials ": range(1, timeseries_length + 1), # <- Coordinate shift!
347
+ "timeseries_length ": range(1, timeseries_length + 1), # <- Coordinate shift!
347
348
}
348
349
with pm.Model(coords=coords, check_bounds=False) as conditional_model:
349
350
y_data = pm.Data("y_data", ar_obs)
@@ -360,7 +361,7 @@ with pm.Model(coords=coords, check_bounds=False) as conditional_model:
360
361
)
361
362
362
363
ar = pm.Deterministic(
363
- name="ar", var=pt.concatenate([ar_init, ar_innov], axis=-1), dims=("trials ",)
364
+ name="ar", var=pt.concatenate([ar_init, ar_innov], axis=-1), dims=("timeseries_length ",)
364
365
)
365
366
366
367
post_pred_conditional = pm.sample_posterior_predictive(trace, var_names=["ar"], random_seed=rng)
@@ -384,27 +385,27 @@ for i, hdi_prob in enumerate((0.94, 0.64), 1):
384
385
lower = hdi.sel(hdi="lower")
385
386
upper = hdi.sel(hdi="higher")
386
387
ax.fill_between(
387
- x=conditional_post_pred_ar["trials "],
388
+ x=conditional_post_pred_ar["timeseries_length "],
388
389
y1=lower,
389
390
y2=upper,
390
391
alpha=(i - 0.2) * 0.2,
391
392
color="C1",
392
393
label=f"{hdi_prob:.0%} HDI",
393
394
)
394
395
ax.plot(
395
- conditional_post_pred_ar["trials "],
396
+ conditional_post_pred_ar["timeseries_length "],
396
397
conditional_post_pred_ar.mean(("chain", "draw")),
397
398
color="C1",
398
399
label="Mean",
399
400
)
400
401
ax.plot(ar_obs, color="black", label="Observed")
401
402
ax.plot(
402
- conditional_post_pred_ar["trials "],
403
+ conditional_post_pred_ar["timeseries_length "],
403
404
res.fittedvalues,
404
405
color="C2",
405
406
label="statsmodels",
406
407
)
407
- ax.legend(loc="upper right" )
408
+ ax.legend(loc="lower center", bbox_to_anchor=(0.5, -0.2), ncol=5 )
408
409
ax.set_xlabel("time")
409
410
ax.set_title("AR(2) Conditional Posterior Predictive Samples", fontsize=18, fontweight="bold");
410
411
```
@@ -438,7 +439,6 @@ The idea is to use the posterior samples and the latest available two data point
438
439
``` {code-cell} ipython3
439
440
coords = {
440
441
"lags": range(-lags, 0),
441
- "trials": range(timeseries_length),
442
442
"steps": range(timeseries_length, timeseries_length + forecast_steps),
443
443
}
444
444
with pm.Model(coords=coords, check_bounds=False) as forecasting_model:
@@ -481,7 +481,7 @@ for i, hdi_prob in enumerate((0.94, 0.64), 1):
481
481
lower = hdi.sel(hdi="lower")
482
482
upper = hdi.sel(hdi="higher")
483
483
ax.fill_between(
484
- x=conditional_post_pred_ar["trials "],
484
+ x=conditional_post_pred_ar["timeseries_length "],
485
485
y1=lower,
486
486
y2=upper,
487
487
alpha=(i - 0.2) * 0.2,
@@ -490,7 +490,7 @@ for i, hdi_prob in enumerate((0.94, 0.64), 1):
490
490
)
491
491
492
492
ax.plot(
493
- conditional_post_pred_ar["trials "],
493
+ conditional_post_pred_ar["timeseries_length "],
494
494
conditional_post_pred_ar.mean(("chain", "draw")),
495
495
color="C1",
496
496
label="Mean",
@@ -519,7 +519,7 @@ ax.plot(
519
519
520
520
ax.plot(ar_obs, color="black", label="Observed")
521
521
ax.plot(
522
- conditional_post_pred_ar["trials "],
522
+ conditional_post_pred_ar["timeseries_length "],
523
523
res.fittedvalues,
524
524
color="C2",
525
525
label="statsmodels",
0 commit comments