Skip to content

Commit 0f3a715

Browse files
Migrate Stochastic Volatility notebook for v4 (pymc-devs#343)
1 parent 437e6f2 commit 0f3a715

File tree

2 files changed

+162
-156
lines changed

2 files changed

+162
-156
lines changed

examples/case_studies/stochastic_volatility.ipynb

Lines changed: 150 additions & 144 deletions
Large diffs are not rendered by default.

myst_nbs/case_studies/stochastic_volatility.myst.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupytext:
66
format_version: 0.13
77
jupytext_version: 1.13.7
88
kernelspec:
9-
display_name: Python 3
9+
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
1212
---
@@ -20,9 +20,9 @@ import arviz as az
2020
import matplotlib.pyplot as plt
2121
import numpy as np
2222
import pandas as pd
23-
import pymc3 as pm
23+
import pymc as pm
2424
25-
rng = np.random.default_rng(0)
25+
rng = np.random.RandomState(1234)
2626
az.style.use("arviz-darkgrid")
2727
```
2828

@@ -67,7 +67,7 @@ ax.set(xlabel="time", ylabel="returns")
6767
ax.legend();
6868
```
6969

70-
Specifying the model in `PyMC3` mirrors its statistical specification.
70+
Specifying the model in `PyMC` mirrors its statistical specification.
7171

7272
```{code-cell} ipython3
7373
def make_stochastic_volatility_model(data):
@@ -96,9 +96,9 @@ pm.model_to_graphviz(stochastic_vol_model)
9696

9797
```{code-cell} ipython3
9898
with stochastic_vol_model:
99-
trace = az.from_pymc3(prior=pm.sample_prior_predictive(500))
99+
idata = pm.sample_prior_predictive(500, random_seed=rng)
100100
101-
prior_predictive = trace.prior_predictive.stack(pooled_chain=("chain", "draw"))
101+
prior_predictive = idata.prior_predictive.stack(pooled_chain=("chain", "draw"))
102102
```
103103

104104
We plot and inspect the prior predictive. This is *many* orders of magnitude larger than the actual returns we observed. In fact, I cherry-picked a few draws to keep the plot from looking silly. This may suggest changing our priors: a return that our model considers plausible would violate all sorts of constraints by a huge margin: the total value of all goods and services the world produces is ~$\$10^9$, so we might reasonably *not* expect any returns above that magnitude.
@@ -130,23 +130,23 @@ Once we are happy with our model, we can sample from the posterior. This is a so
130130

131131
```{code-cell} ipython3
132132
with stochastic_vol_model:
133-
trace.extend(pm.sample(2000, tune=2000, return_inferencedata=True))
133+
idata.extend(pm.sample(2000, tune=2000, random_seed=rng))
134134
135-
posterior = trace.posterior.stack(pooled_chain=("chain", "draw"))
135+
posterior = idata.posterior.stack(pooled_chain=("chain", "draw"))
136136
posterior["exp_volatility"] = np.exp(posterior["volatility"])
137137
```
138138

139139
```{code-cell} ipython3
140140
with stochastic_vol_model:
141-
trace.extend(az.from_pymc3(posterior_predictive=pm.sample_posterior_predictive(trace)))
141+
idata.extend(pm.sample_posterior_predictive(idata, random_seed=rng))
142142
143-
posterior_predictive = trace.posterior_predictive.stack(pooled_chain=("chain", "draw"))
143+
posterior_predictive = idata.posterior_predictive.stack(pooled_chain=("chain", "draw"))
144144
```
145145

146146
Note that the `step_size` parameter does not look perfect: the different chains look somewhat different. This again indicates some weakness in our model: it may make sense to allow the step_size to change over time, especially over this 11 year time span.
147147

148148
```{code-cell} ipython3
149-
az.plot_trace(trace, var_names=["step_size", "nu"]);
149+
az.plot_trace(idata, var_names=["step_size", "nu"]);
150150
```
151151

152152
Now we can look at our posterior estimates of the volatility in S&P 500 returns over time.
@@ -186,5 +186,5 @@ axes[1].set_title("Posterior volatility");
186186

187187
```{code-cell} ipython3
188188
%load_ext watermark
189-
%watermark -n -u -v -iv -w -p theano,xarray
189+
%watermark -n -u -v -iv -w -p aesara,xarray
190190
```

0 commit comments

Comments
 (0)