Path: blob/master/10_bayesian_machine_learning/05_stochastic_volatility.ipynb
2923 views
Stochastic Volatility model
Imports & Settings
Model assumptions
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.
Here, is the daily return series and is the latent log volatility process.
Get Return Data
First we load some daily returns of the S&P 500.
As you can see, the volatility seems to change over time quite a bit while clustering around certain time-periods, most notably the 2009 financial crash.
Specify Model in PyMC3
Specifying the model in PyMC3
mirrors its statistical specification.
Fit Model
For this model, the full maximum a posteriori (MAP) point is degenerate and has infinite density. NUTS, however, gives the correct posterior.
Optionally, persist result as pickle:
Evaluate results
Trace Plot
Looking at the returns over time and overlaying the estimated standard deviation we can see how the model tracks the volatility over time.