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:
You can install stochvolTMB
from github by running
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)
AIC(gaussian, t_dist, skew_gaussian, leverage)
#> df AIC
#> gaussian 3 -23430.57
#> t_dist 4 -23451.69
#> skew_gaussian 4 -23440.87
#> leverage 4 -23608.85
# The leverage model stand out with an AIC for below the other models
opt <- estimate_parameters(spy$log_return, model = "leverage", silent = TRUE)
# get parameter estimates with standard error
estimates <- summary(opt)
head(estimates, 10)
#> parameter estimate std_error z_value p_value
#> 1: sigma_y 0.008338425 0.0004163323 20.0282918 3.121840e-89
#> 2: sigma_h 0.273445746 0.0182642070 14.9716736 1.124552e-50
#> 3: phi 0.967720808 0.0043682334 221.5359687 0.000000e+00
#> 4: rho -0.748692587 0.0322489934 -23.2159986 3.138829e-119
#> 5: log_sigma_y -4.786880902 0.0499293705 -95.8730474 0.000000e+00
#> 6: log_sigma_h -1.296652044 0.0667927998 -19.4130512 5.986433e-84
#> 7: phi_logit 4.110208379 0.1375465458 29.8823090 3.341130e-196
#> 8: rho_logit -1.939946750 0.1467666528 -13.2178987 6.916940e-40
#> 9: h -0.536253306 0.5182212112 -1.0347961 3.007641e-01
#> 10: h -0.207810839 0.4245275458 -0.4895108 6.244801e-01
#> type
#> 1: transformed
#> 2: transformed
#> 3: transformed
#> 4: transformed
#> 5: fixed
#> 6: fixed
#> 7: fixed
#> 8: fixed
#> 9: random
#> 10: random
## plot estimated volatility with 95 % confidence interval
plot(opt, include_ci = TRUE, dates = spy$date)
By running demo()
you start a shiny application where you can visually inspect the effect of choosing different models and parameter configurations