svsample_roll performs rolling window estimation based on svsample. A convenience function for backtesting purposes.

svsample_roll(
  y,
  designmatrix = NA,
  n_ahead = 1,
  forecast_length = 500,
  n_start = NULL,
  refit_every = 1,
  refit_window = c("moving", "expanding"),
  calculate_quantile = c(0.01),
  calculate_predictive_likelihood = TRUE,
  keep_draws = FALSE,
  parallel = c("no", "multicore", "snow"),
  n_cpus = 1L,
  cl = NULL,
  ...
)

svtsample_roll(
  y,
  designmatrix = NA,
  n_ahead = 1,
  forecast_length = 500,
  n_start = NULL,
  refit_every = 1,
  refit_window = c("moving", "expanding"),
  calculate_quantile = c(0.01),
  calculate_predictive_likelihood = TRUE,
  keep_draws = FALSE,
  parallel = c("no", "multicore", "snow"),
  n_cpus = 1L,
  cl = NULL,
  ...
)

svlsample_roll(
  y,
  designmatrix = NA,
  n_ahead = 1,
  forecast_length = 500,
  n_start = NULL,
  refit_every = 1,
  refit_window = c("moving", "expanding"),
  calculate_quantile = c(0.01),
  calculate_predictive_likelihood = TRUE,
  keep_draws = FALSE,
  parallel = c("no", "multicore", "snow"),
  n_cpus = 1L,
  cl = NULL,
  ...
)

svtlsample_roll(
  y,
  designmatrix = NA,
  n_ahead = 1,
  forecast_length = 500,
  n_start = NULL,
  refit_every = 1,
  refit_window = c("moving", "expanding"),
  calculate_quantile = c(0.01),
  calculate_predictive_likelihood = TRUE,
  keep_draws = FALSE,
  parallel = c("no", "multicore", "snow"),
  n_cpus = 1L,
  cl = NULL,
  ...
)

Arguments

y

numeric vector containing the data (usually log-returns), which must not contain zeros. Alternatively, y can be an svsim object. In this case, the returns will be extracted and a message is signalled.

designmatrix

regression design matrix for modeling the mean. Must have length(y) rows. Alternatively, designmatrix may be a string of the form "arX", where X is a nonnegative integer. To fit a constant mean model, use designmatrix = "ar0" (which is equivalent to designmatrix = matrix(1, nrow = length(y))). To fit an AR(1) model, use designmatrix = "ar1", and so on. If some elements of designmatrix are NA, the mean is fixed to zero (pre-1.2.0 behavior of stochvol).

n_ahead

number of time steps to predict from each time window.

forecast_length

the time horizon at the end of the data set that is used for backtesting.

n_start

optional the starting time point for backtesting. Computed from forecast_length if omitted.

refit_every

the SV model is refit every refit_every time steps. Only the value 1 is allowed.

refit_window

one of "moving" or "expanding". If "expanding", then the start of the time window stays at the beginning of the data set. If "moving", then the length of the time window is constant throughout backtesting.

calculate_quantile

vector of numbers between 0 and 1. These quantiles are predicted using predict.svdraws for each time window.

calculate_predictive_likelihood

boolean. If TRUE, the n_ahead predictive density is evaluated at the n_ahead time observation after each time window.

keep_draws

boolean. If TRUE, the svdraws and the svpredict objects are kept from each time window.

parallel

one of "no" (default), "multicore", or "snow", indicating what type of parallellism is to be applied. Option "multicore" is not available on Windows.

n_cpus

optional positive integer, the number of CPUs to be used in case of parallel computations. Defaults to 1L. Ignored if parameter cl is supplied and parallel != "snow".

cl

optional so-called SNOW cluster object as implemented in package parallel. Ignored unless parallel == "snow".

...

Any extra arguments will be forwarded to svsample, controlling the prior setup, the starting values for the MCMC chains, the number of independent MCMC chains, thinning and other expert settings.

Value

The value returned is a list object of class svdraws_roll

holding a list item for every time window. The elements of these list items are

indices

a list object containing two elements: train is the vector of indices used for fitting the model, and test is the vector of indices used for prediction. The latter is mainly useful if a designmatrix is provided.

quantiles

the input parameter calculate_quantiles.

refit_every

the input parameter refit_every.

predictive_likelihood

present only if calculate_predictive_likelihood is TRUE. Then it is a number, the expected predictive density of the observation. The expecation is taken over the joint n_ahead predictive distribution of all model parameters.

predictive_quantile

present only if calculate_quantile is a non-empty vector. Then it is a vector of quantiles from the n_ahead predictive distribution of y. It is based on MCMC simulation by using predict.

fit

present only if keep_draws is TRUE. Then it is an svdraws object as returned by svsample.

prediction

present only if keep_draws is TRUE. Then it is an svpredict object as returned by predict.svdraws.

To display the output, use print and summary. The print method simply prints a short summary of the setup; the summary method displays the summary statistics of the backtesting.

Details

Functions svtsample_roll, svlsample_roll, and svtlsample_roll are wrappers around svsample_roll with convenient default values for the SV model with t-errors, leverage, and both t-errors and leverage, respectively.

Note

The function executes svsample (length(y) - arorder - n_ahead - n_start + 2) %/% refit_every times.

Examples

# \donttest{
# Simulate from the true model
sim <- svsim(200)

# Perform rolling estimation using the vanilla SV
# model and default priors
roll <- svsample_roll(sim, draws = 5000, burnin = 2000,
                      keep_draws = TRUE,
                      forecast_length = 10,
                      n_ahead = 1, refit_every = 1,
                      refit_window = "moving",
                      calculate_predictive_likelihood = TRUE,
                      calculate_quantile = c(0.01, 0.05))
#> Extracted data vector from 'svsim'-object.
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...
#> Done!
#> Summarizing posterior draws...

# Perform rolling estimation by making use
# of two CPU cores, advanced priors, and multiple
# chains with pre-set initial values. Let us combine
# that with an AR(2) specification
prior_beta <- sv_multinormal(c(1,0,-1), rbind(c(1, 0, 0.1),
                                              c(0, 0.3, -0.04),
                                              c(0.1, -0.04, 0.1)))
priorspec <- specify_priors(rho = sv_beta(4, 4),
                            latent0_variance = sv_constant(1),
                            beta = prior_beta,
                            nu = sv_exponential(0.05))
startpara <- list(list(mu = -9, phi = 0.3),
                  list(mu = -11, sigma = 0.1, phi = 0.95),
                  list(phi = 0.99))
roll <- svsample_roll(sim, draws = 5000, burnin = 2000,
                      designmatrix = "ar2",
                      priorspec = priorspec,
                      startpara = startpara,
                      parallel = "snow", n_cpus = 2,
                      n_chains = 3,
                      keep_draws = TRUE,
                      forecast_length = 10,
                      n_ahead = 1, refit_every = 1,
                      refit_window = "expanding",
                      calculate_predictive_likelihood = TRUE,
                      calculate_quantile = c(0.01, 0.05))
#> Extracted data vector from 'svsim'-object.
#> 
#> Starting cluster...
#> 
#> Cluster stopped.
# }