svsample_fast_cpp {stochvol} | R Documentation |
Bindings to C++
Functions in stochvol
Description
All the heavy lifting in stochvol
is implemented in C++
with the help of R
packages Rcpp
and RcppArmadillo
.
These functions call the MCMC samplers in C++
directly without any
any validation and transformations, expert use only!
Usage
svsample_fast_cpp(
y,
draws = 1,
burnin = 0,
designmatrix = matrix(NA),
priorspec = specify_priors(),
thinpara = 1,
thinlatent = 1,
keeptime = "all",
startpara,
startlatent,
keeptau = !inherits(priorspec$nu, "sv_infinity"),
print_settings = list(quiet = TRUE, n_chains = 1, chain = 1),
correct_model_misspecification = FALSE,
interweave = TRUE,
myoffset = 0,
fast_sv = get_default_fast_sv()
)
svsample_general_cpp(
y,
draws = 1,
burnin = 0,
designmatrix = matrix(NA),
priorspec = specify_priors(),
thinpara = 1,
thinlatent = 1,
keeptime = "all",
startpara,
startlatent,
keeptau = !inherits(priorspec$nu, "sv_infinity"),
print_settings = list(quiet = TRUE, n_chains = 1, chain = 1),
correct_model_misspecification = FALSE,
interweave = TRUE,
myoffset = 0,
general_sv = get_default_general_sv(priorspec)
)
Arguments
y |
numeric vector of the observations |
draws |
single positive integer, the number of draws to return (after the burn-in) |
burnin |
single positive integer, length of warm-up period, this number of draws are discarded from the beginning |
designmatrix |
numeric matrix of covariates. Dimensions:
|
priorspec |
a |
thinpara |
single number greater or equal to 1, coercible to integer.
Every |
thinlatent |
single number greater or equal to 1, coercible to integer.
Every |
keeptime |
Either 'all' (the default) or 'last'. Indicates which latent volatility draws should be stored. |
startpara |
named list, containing the starting values for the parameter draws. It must contain elements
|
startlatent |
vector of length |
keeptau |
Logical value indicating whether the 'variance inflation factors' should be stored (used for the sampler with conditional t innovations only). This may be useful to check at what point(s) in time the normal disturbance had to be 'upscaled' by a mixture factor and when the series behaved 'normally'. |
print_settings |
List of three elements:
Please note that this function does not run multiple independent chains
but |
correct_model_misspecification |
Logical value. If |
interweave |
Logical value. If |
myoffset |
Single non-negative number that is used in
|
fast_sv |
named list of expert settings. We recommend the use of |
general_sv |
named list of expert settings. We recommend the use of |
Details
The sampling functions are separated into fast SV and general SV. See more details in the sections below.
Fast SV
Fast SV was developed in Kastner and Fruehwirth-Schnatter (2014). Fast SV estimates an approximate SV model without leverage, where the approximation comes in through auxiliary mixture approximations to the exact SV model. The sampler uses the ancillarity-sufficiency interweaving strategy (ASIS) to improve on the sampling efficiency of the model parameters, and it employs all-without-a-loop (AWOL) for computationally efficient Kalman filtering of the conditionally Gaussian state space. Correction for model misspecification happens as a post-processing step.
Fast SV employs sampling strategies that have been fine-tuned and specified for
vanilla SV (no leverage), and hence it can be fast and efficient but also more limited
in its feature set. The conditions for the fast SV sampler: rho == 0
; mu
has either a normal prior or it is also constant 0
; the prior for phi
is a beta distribution; the prior for sigma^2
is either a gamma distribution
with shape 0.5
or a mean- and variance-matched inverse gamma distribution;
either keeptime == 'all'
or correct_model_misspecification == FALSE
.
These criteria are NOT VALIDATED by fast SV on the C++
level!
General SV
General SV also estimates an approximate SV model without leverage, where the approximation comes in through auxiliary mixture approximations to the exact SV model. The sampler uses both ASIS and AWOL.
General SV employs adapted random walk Metropolis-Hastings as the proposal for
the parameters mu
, phi
, sigma
, and rho
. Therefore,
more general prior distributions are allowed in this case.
Examples
# Draw one sample using fast SV and general SV
y <- svsim(40)$y
params <- list(mu = -10, phi = 0.9, sigma = 0.1,
nu = Inf, rho = 0, beta = NA,
latent0 = -10)
res_fast <- svsample_fast_cpp(y,
startpara = params, startlatent = rep(-10, 40))
res_gen <- svsample_general_cpp(y,
startpara = params, startlatent = rep(-10, 40))
# Embed SV in another sampling scheme
## vanilla SV
len <- 40L
draws <- 1000L
burnin <- 200L
param_store <- matrix(NA, draws, 3,
dimnames = list(NULL,
c("mu", "phi", "sigma")))
startpara <- list(mu = 0, phi = 0.9, sigma = 0.1,
nu = Inf, rho = 0, beta = NA,
latent0 = 0)
startlatent <- rep(0, len)
for (i in seq_len(burnin+draws)) {
# draw the data in the bigger sampling scheme
# now we simulate y from vanilla SV
y <- svsim(len, mu = 0, phi = 0.9, sigma = 0.1)$y
# call SV sampler
res <- svsample_fast_cpp(y, startpara = startpara,
startlatent = startlatent)
# administrate values
startpara[c("mu","phi","sigma")] <-
as.list(res$para[, c("mu", "phi", "sigma")])
startlatent <- drop(res$latent)
# store draws after the burnin
if (i > burnin) {
param_store[i-burnin, ] <-
res$para[, c("mu", "phi", "sigma")]
}
}
### quick look at the traceplots
ts.plot(param_store, col = 1:3)