Skip to content

Commit

Permalink
Update stochastic volatility notebook for best practices (pymc-devs#378)
Browse files Browse the repository at this point in the history
* Bring stochastic volatility notebook in line with style guide

Fixing dashes in reference entry

Removing extra lines from reference bib file

* Adding original author information to stochastic volatility notebook

* Adding updated myst file

* Fixing misspecified tags and updating author list

* Update stochastic_volatility.myst.md
  • Loading branch information
ckrapu authored Jul 28, 2022
1 parent e3a0e5c commit a9e4656
Show file tree
Hide file tree
Showing 3 changed files with 325 additions and 134 deletions.
401 changes: 276 additions & 125 deletions examples/case_studies/stochastic_volatility.ipynb

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions examples/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ @book{hayes2017introduction
year = {2017},
publisher = {Guilford publications}
}
@article{hoffman2014nuts,
title = {The No-U-Turn Sampler: Adaptively Setting Path Lengths in Hamiltonian Monte Carlo},
author = {Hoffman, Matthew and Gelman, Andrew},
year = {2014},
journal = {Journal of Machine Learning Research},
volume = {15},
issue = {1},
pages = {1593--1623},
url = {https://dl.acm.org/doi/10.5555/2627435.2638586}
}
@misc{hogg2010data,
title = {Data analysis recipes: Fitting a model to data},
author = {David W. Hogg and Jo Bovy and Dustin Lang},
Expand Down
48 changes: 39 additions & 9 deletions myst_nbs/case_studies/stochastic_volatility.myst.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ jupytext:
format_version: 0.13
jupytext_version: 1.13.7
kernelspec:
display_name: Python 3 (ipykernel)
display_name: pymc_env
language: python
name: python3
name: pymc_env
---

(stochastic_volatility)=
# Stochastic Volatility model

:::{post} June 17, 2022
:tags: time series, case study
:category: beginner
:author: John Salvatier
:::

```{code-cell} ipython3
import os
Expand All @@ -26,15 +33,15 @@ rng = np.random.RandomState(1234)
az.style.use("arviz-darkgrid")
```

Asset prices have time-varying volatility (variance of day over day `returns`). In some periods, returns are highly variable, while in others very stable. Stochastic volatility models model this with a latent volatility variable, modeled as a stochastic process. The following model is similar to the one described in the No-U-Turn Sampler paper, Hoffman (2011) p21.
Asset prices have time-varying volatility (variance of day over day `returns`). In some periods, returns are highly variable, while in others very stable. Stochastic volatility models model this with a latent volatility variable, modeled as a stochastic process. The following model is similar to the one described in the No-U-Turn Sampler paper, {cite:p}`hoffman2014nuts`.

$$ \sigma \sim Exponential(50) $$

$$ \nu \sim Exponential(.1) $$

$$ s_i \sim Normal(s_{i-1}, \sigma^{-2}) $$

$$ log(r_i) \sim t(\nu, 0, exp(-2 s_i)) $$
$$ \log(r_i) \sim t(\nu, 0, \exp(-2 s_i)) $$

Here, $r$ is the daily return series and $s$ is the latent log volatility process.

Expand Down Expand Up @@ -180,11 +187,34 @@ axes[0].set_title("True log returns (black) and posterior predictive log returns
axes[1].set_title("Posterior volatility");
```

## References

1. Hoffman & Gelman. (2011). [The No-U-Turn Sampler: Adaptively Setting Path Lengths in Hamiltonian Monte Carlo](http://arxiv.org/abs/1111.4246).

```{code-cell} ipython3
%load_ext watermark
%watermark -n -u -v -iv -w -p aesara,xarray
%watermark -n -u -v -iv -w -p aesara,aeppl,xarray
```

## References

:::{bibliography}
:filter: docname in docnames
:::

+++

## Authors

+++

* Written by John Salvatier
* Updated by Kyle Meyer
* Updated by Thomas Wiecki
* Updated by Chris Fonnesbeck
* Updated by Aaron Maxwell on May 18, 2018 ([pymc#2978](https://github.com/pymc-devs/pymc/pull/2978))
* Updated by Colin Carroll on November 16, 2019 ([pymc#3682](https://github.com/pymc-devs/pymc/pull/3682))
* Updated by Abhipsha Das on July 24, 2021 ([pymc-examples#155](https://github.com/pymc-devs/pymc-examples/pull/155))
* Updated by Michael Osthege on June 1, 2022 ([pymc-examples#343](https://github.com/pymc-devs/pymc-examples/pull/343))
* Updated by Christopher Krapu on June 17, 2022 ([pymc-examples#378](https://github.com/pymc-devs/pymc-examples/pull/378))

+++

:::{include} ../page_footer.md
:::

0 comments on commit a9e4656

Please sign in to comment.