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,
...
)
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.
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).
number of time steps to predict from each time window.
the time horizon at the end of the data set that is used for backtesting.
optional the starting time point for backtesting.
Computed from forecast_length
if omitted.
the SV model is refit every refit_every
time steps.
Only the value 1
is allowed.
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.
vector of numbers between 0 and 1.
These quantiles are predicted using predict.svdraws
for each time window.
boolean. If TRUE
,
the n_ahead
predictive density is evaluated at the
n_ahead
time observation after each time window.
boolean. If TRUE
, the svdraws
and
the svpredict
objects are kept from each time window.
one of "no"
(default), "multicore"
, or "snow"
,
indicating what type of parallellism is to be applied. Option
"multicore"
is not available on Windows.
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"
.
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.
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
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.
the input parameter calculate_quantiles
.
the input parameter refit_every
.
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.
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
.
present only if keep_draws
is TRUE
. Then it is an
svdraws
object as returned by svsample
.
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.
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.
The function executes svsample
(length(y) - arorder - n_ahead - n_start + 2) %/% refit_every
times.
# \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.
# }