knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
stochvolTMB
is a package for fitting stochastic volatility (SV) models to time series data. It is inspired by the package stochvol, but parameter estimates are obtained through optimization and not MCMC, leading to significant speed up. It is built on Template Model Builder for fast and efficient estimation. The latent volatility is integrated out of the likelihood using the Laplace approximation and automatic differentiation (AD) is used for accurate evaluation of derivatives.
Four distributions for the observational error are implemented:
To install the current stable release from CRAN, use
install.packages("stochvolTMB")
To install the current development version, use
``` {r eval = FALSE}
remotes::install_github("JensWahl/stochvolTMB")
If you would also like to build and view the vignette locally, use ```r remotes::install_github("JensWahl/stochvolTMB", dependencies = TRUE, build_vignettes = TRUE)
The main function for estimating parameters is estimate_parameters
:
library(stochvolTMB, warn.conflicts = FALSE) # load s&p500 data from 2005 to 2018 data(spy) # find the best model using AIC gaussian <- estimate_parameters(spy$log_return, model = "gaussian", silent = TRUE) t_dist <- estimate_parameters(spy$log_return, model = "t", silent = TRUE) skew_gaussian <- estimate_parameters(spy$log_return, model = "skew_gaussian", silent = TRUE) leverage <- estimate_parameters(spy$log_return, model = "leverage", silent = TRUE) # the leverage model stands out with an AIC far below the other models AIC(gaussian, t_dist, skew_gaussian, leverage) # get parameter estimates with standard error estimates <- summary(leverage) head(estimates, 10) # plot estimated volatility with 95 % confidence interval plot(leverage, include_ci = TRUE, dates = spy$date)
Given the estimated parameters we can simulate future volatility and log-returns using predict
.
set.seed(123) # simulate future prices with or without parameter uncertainty pred = predict(leverage, steps = 10) # Calculate the mean, 2.5% and 97.5% quantiles from the simulations pred_summary = summary(pred, quantiles = c(0.025, 0.975), predict_mean = TRUE) print(pred_summary) # plot predicted volatility with 0.025 and 0.975 quantiles plot(leverage, include_ci = TRUE, forecast = 50, dates = spy$d) + ggplot2::xlim(c(spy[.N, date] - 150, spy[.N, date] + 50))
By running demo()
you start a shiny application where you can visually inspect the effect of choosing different models and parameter configurations
demo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.