Skip to content

Commit 29ef29f

Browse files
committed
[Hierarchical BVAR pymc-devs#286] added image and scrollable view of image for full model
Signed-off-by: Nathaniel <[email protected]>
1 parent 1ea62a7 commit 29ef29f

File tree

3 files changed

+5447
-6929
lines changed

3 files changed

+5447
-6929
lines changed

examples/time_series/bayesian_var_model.ipynb

+5,428-6,922
Large diffs are not rendered by default.

examples/time_series/full_model.png

616 KB
Loading

myst_nbs/time_series/bayesian_var_model.myst.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import pandas as pd
3030
import pymc as pm
3131
import statsmodels.api as sm
3232
33+
from IPython.display import HTML, display
3334
from pymc.sampling_jax import sample_blackjax_nuts
3435
```
3536

@@ -309,17 +310,17 @@ def shade_background(ppc, ax, idx, palette="cividis"):
309310
310311
311312
def plot_ppc(idata, df, group="posterior_predictive"):
312-
fig, axs = plt.subplots(2, 1, figsize=(20, 15))
313+
fig, axs = plt.subplots(2, 1, figsize=(25, 15))
313314
df = pd.DataFrame(idata_fake_data["observed_data"]["obs"].data, columns=["x", "y"])
314315
axs = axs.flatten()
315316
ppc = az.extract_dataset(idata, group=group, num_samples=100)["obs"]
316317
# Minus the lagged terms and the constant
317-
shade_background(ppc, axs, 0, "plasma")
318+
shade_background(ppc, axs, 0, "magma")
318319
axs[0].plot(np.arange(ppc.shape[0]), ppc[:, 0, :].mean(axis=1), color="cyan", label="Mean")
319320
axs[0].plot(df["x"], "o", color="black", markersize=6, label="Observed")
320321
axs[0].set_title("VAR Series 1")
321322
axs[0].legend()
322-
shade_background(ppc, axs, 1, "plasma")
323+
shade_background(ppc, axs, 1, "magma")
323324
axs[1].plot(df["y"], "o", color="black", markersize=6, label="Observed")
324325
axs[1].plot(np.arange(ppc.shape[0]), ppc[:, 1, :].mean(axis=1), color="cyan", label="Mean")
325326
axs[1].set_title("VAR Series 2")
@@ -398,12 +399,12 @@ def plot_ppc_macro(idata, df, group="posterior_predictive"):
398399
axs = axs.flatten()
399400
ppc = az.extract_dataset(idata, group=group, num_samples=100)["obs"]
400401
401-
shade_background(ppc, axs, 0, "plasma")
402+
shade_background(ppc, axs, 0, "magma")
402403
axs[0].plot(np.arange(ppc.shape[0]), ppc[:, 0, :].mean(axis=1), color="cyan", label="Mean")
403404
axs[0].plot(df["dl_gdp"], "o", color="black", markersize=6, label="Observed")
404405
axs[0].set_title("Differenced and Logged GDP")
405406
axs[0].legend()
406-
shade_background(ppc, axs, 1, "plasma")
407+
shade_background(ppc, axs, 1, "magma")
407408
axs[1].plot(df["dl_cons"], "o", color="black", markersize=6, label="Observed")
408409
axs[1].plot(np.arange(ppc.shape[0]), ppc[:, 1, :].mean(axis=1), color="cyan", label="Mean")
409410
axs[1].set_title("Differenced and Logged Consumption")
@@ -533,7 +534,18 @@ idata_full_test
533534
We can see how the structure of the model has grown quite complicated.
534535

535536
```{code-cell} ipython3
536-
pm.model_to_graphviz(model_full_test)
537+
gv = pm.model_to_graphviz(model_full_test)
538+
gv.render(filename="full_model", format="png");
539+
```
540+
541+
```{code-cell} ipython3
542+
display(
543+
HTML(
544+
"""<div style='width: 4500px; overflow: scroll;'>
545+
<img src="full_model.png" alt="Model Structure">
546+
</div>"""
547+
)
548+
)
537549
```
538550

539551
```{code-cell} ipython3
@@ -643,7 +655,7 @@ for ax, country in zip(axs, countries):
643655
f"obs_{country}"
644656
]
645657
for i in range(3):
646-
shade_background(ppc, ax, i, "plasma")
658+
shade_background(ppc, ax, i, "magma")
647659
ax[0].plot(np.arange(ppc.shape[0]), ppc[:, 0, :].mean(axis=1), color="cyan", label="Mean")
648660
ax[0].plot(temp["dl_gdp"], "o", color="black", markersize=4, label="Observed")
649661
ax[0].set_title(f"Posterior Predictive GDP: {country}")

0 commit comments

Comments
 (0)