{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(stochastic_volatility)=\n", "# Stochastic Volatility model\n", "\n", ":::{post} June 17, 2022\n", ":tags: time series, case study\n", ":category: beginner\n", ":author: John Salvatier, Colin Carroll, Abhipsha Das\n", ":::" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "import arviz as az\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import pymc as pm\n", "\n", "rng = np.random.RandomState(1234)\n", "az.style.use(\"arviz-darkgrid\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`.\n", "\n", "$$ \\sigma \\sim Exponential(50) $$\n", "\n", "$$ \\nu \\sim Exponential(.1) $$\n", "\n", "$$ s_i \\sim Normal(s_{i-1}, \\sigma^{-2}) $$\n", "\n", "$$ \\log(r_i) \\sim t(\\nu, 0, \\exp(-2 s_i)) $$\n", "\n", "Here, $r$ is the daily return series and $s$ is the latent log volatility process." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build Model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we load daily returns of the S&P 500, and calculate the daily log returns. This data is from May 2008 to November 2019." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Close | \n", "change | \n", "
---|---|---|
Date | \n", "\n", " | \n", " |
2008-05-05 | \n", "1407.489990 | \n", "-0.004544 | \n", "
2008-05-06 | \n", "1418.260010 | \n", "0.007623 | \n", "
2008-05-07 | \n", "1392.569946 | \n", "-0.018280 | \n", "
2008-05-08 | \n", "1397.680054 | \n", "0.003663 | \n", "
2008-05-09 | \n", "1388.280029 | \n", "-0.006748 | \n", "