Learn R Programming

stochvol (version 2.0.4)

svlsample: Markov Chain Monte Carlo (MCMC) Sampling for the Stochastic Volatility Model with Leverage (SVL)

Description

svlsample simulates from the joint posterior distribution of the SVL parameters mu, phi, sigma, and rho, along with the latent log-volatilities h_1,...,h_n and returns the MCMC draws. If a design matrix is provided, simple Bayesian regression can also be conducted.

Usage

svlsample(y, draws = 10000, burnin = 1000, designmatrix = NA,
  priormu = c(0, 100), priorphi = c(5, 1.5), priorsigma = 1,
  priorrho = c(4, 4), priorbeta = c(0, 10000), thinpara = 1,
  thinlatent = 1, thintime = NULL, keeptime = "all", quiet = FALSE,
  startpara, startlatent, expert, ...)

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 warning is thrown.

draws

single number greater or equal to 1, indicating the number of draws after burn-in (see below). Will be automatically coerced to integer. The default value is 10000.

burnin

single number greater or equal to 0, indicating the number of draws discarded as burn-in. Will be automatically coerced to integer. The default value is 1000.

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).

priormu

numeric vector of length 2, indicating mean and standard deviation for the Gaussian prior distribution of the parameter mu, the level of the log-volatility. The default value is c(0, 100), which constitutes a practically uninformative prior for common exchange rate datasets, stock returns and the like.

priorphi

numeric vector of length 2, indicating the shape parameters for the Beta prior distribution of the transformed parameter (phi + 1) / 2, where phi denotes the persistence of the log-volatility. The default value is c(5, 1.5), which constitutes a prior that puts some belief in a persistent log-volatility but also encompasses the region where phi is around 0.

priorsigma

single positive real number, which stands for the scaling of the transformed parameter sigma^2, where sigma denotes the volatility of log-volatility. More precisely, sigma^2 ~ priorsigma * chisq(df = 1). The default value is 1, which constitutes a reasonably vague prior for many common exchange rate datasets, stock returns and the like.

priorrho

numeric vector of length 2, indicating the shape parameters for the Beta prior distribution of the transformed parameter (rho + 1) / 2, where rho denotes the conditional correlation between observation and the increment of the log-volatility. The default value is c(4, 4), which constitutes a slightly informative prior around 0 (the no leverage case) to boost convergence.

priorbeta

numeric vector of length 2, indicating the mean and standard deviation of the Gaussian prior for the regression parameters. The default value is c(0, 10000), which constitutes a very vague prior for many common datasets. Not used if designmatrix is NA.

thinpara

single number greater or equal to 1, coercible to integer. Every thinparath parameter draw is kept and returned. The default value is 1, corresponding to no thinning of the parameter draws i.e. every draw is stored.

thinlatent

single number greater or equal to 1, coercible to integer. Every thinlatentth latent variable draw is kept and returned. The default value is 1, corresponding to no thinning of the latent variable draws, i.e. every draw is kept.

thintime

Deprecated. Use 'keeptime' instead.

keeptime

Either 'all' (the default) or 'last'. Indicates which latent

quiet

logical value indicating whether the progress bar and other informative output during sampling should be omitted. The default value is FALSE, implying verbose output.

startpara

optional named list, containing the starting values for the parameter draws. If supplied, startpara must contain four elements named mu, phi, sigma, and rho, where mu is an arbitrary numerical value, phi is a real number between -1 and 1, sigma is a positive real number, and rho is a real number between -1 and 1. The default value is equal to the prior mean.

startlatent

optional vector of length length(y), containing the starting values for the latent log-volatility draws. The default value is rep(-10, length(y)).

expert

optional named list of expert parameters. For most applications, the default values probably work best. If expert is provided, it may contain the following named elements:

parameterization: Character string containing values "centered", and "noncentered". Alternatively, it can be a single element character vector of the form "asisX", where X is an integer, which is equivalent to rep(c("centered", "noncentered"), X). Defaults to "asis5".

gammaprior: Single logical value indicating whether a Gamma prior for sigma^2 should be used. If set to FALSE, a moment-matched Inverse Gamma prior is employed. Defaults to TRUE.

init.with.svsample: Single integer indicating the length of a ``pre-burnin'' run using the computationally much more efficient svsample. This run helps in finding good initial values for the latent states, giving svlsample a considerable initial boost for convergence. Defaults to 1000L.

mhcontrol: Either a single numeric value specifying the diagonal elements of a diagonal covariance matrix, or a list with two elements, both single numeric values (explained later), or a 4x4 covariance matrix. Argument mhcontrol controls the proposal density of a Metropolis-Hastings (MH) update step when jointly sampling mu, phi, sigma, and rho. It specifies the covariance matrix of a log-random-walk proposal. In case mhcontrol is a list of length two, its elements have to be scale and rho.var. In this case, the covariance matrix is calculated from the pre-burnin step with svsample, which gives an approximate posterior structure of the second moment for mu, phi, and sigma. This covariance matrix is then extended with mhcontrol$rho.var, specifying the variance for rho. The off-diagonal elements belonging to rho are set to 0. Finally, the whole covariance matrix is scaled by mhcontrol$scale. For this case to work, init.with.svsample has to be positive. Defaults to list(scale=0.35, rho.var=0.02).

correct.latent.draws: Single logical value indicating whether to correct the draws obtained from the auxiliary model of Omori, et al. (2007). Defaults to TRUE.

Any extra arguments will be forwarded to updatesummary, controlling the type of statistics calculated for the posterior draws.

Value

The value returned is a list object of class svldraws holding

para

mcmc object containing the parameter draws from the posterior distribution.

latent

mcmc object containing the latent instantaneous log-volatility draws from the posterior distribution.

beta

mcmc object containing the regression coefficient draws from the posterior distribution (optional).

y

the argument y.

runtime

proc_time object containing the run time of the sampler.

priors

list containing the parameter values of the prior distribution, i.e. the arguments priormu, priorphi, priorsigma, and priorrho, and potentially priorbeta.

thinning

list containing the thinning parameters, i.e. the arguments thinpara, thinlatent and keeptime.

summary

list containing a collection of summary statistics of the posterior draws for para, and latent.

meanmodel

character containing information about how designmatrix was used.

To display the output, use print, summary and plot. The print method simply prints the posterior draws (which is very likely a lot of output); the summary method displays the summary statistics currently stored in the object; the plot method plot.svdraws gives a graphical overview of the posterior distribution by calling volplot, traceplot and densplot and displaying the results on a single page.

References

Yu, Y. and Meng, X.-L. (2011). To Center or not to Center: That is not the Question---An Ancillarity-Sufficiency Interweaving Strategy (ASIS) for Boosting MCMC Efficiency. Journal of Computational and Graphical Statistics, 20(3), 531--570, http://dx.doi.org/10.1198/jcgs.2011.203main

Omori, Y. and Chib, S. and Shephard, N. and Nakajima, J. (2007). Stochastic Volatility with Leverage: Fast and Efficient Likelihood Inference. Journal of Econometrics, 140(2), 425--449, http://dx.doi.org/10.1016/j.jeconom.2006.07.008

See Also

svsim, svsample, updatesummary, predict.svdraws, plot.svdraws.

Examples

Run this code
# NOT RUN {
# Example 1
## Simulate a short SVL process
sim <- svsim(200, mu = -10, phi = 0.95, sigma = 0.2, rho = -0.4)

## Obtain 5000 draws from the sampler (that's not a lot)
draws <- svlsample(sim$y)

## Check out the results
summary(draws)
plot(draws, simobj = sim)


# Example 2
## AR(1) structure for the mean
data(exrates)
len <- 1200
ahead <- 100
y <- head(exrates$USD, len)

## Fit AR(1)-SVL model to EUR-USD exchange rates
res <- svlsample(y, designmatrix = "ar1")

## Use predict.svdraws to obtain predictive distributions
preddraws <- predict(res, steps = ahead)

## Calculate predictive quantiles
predquants <- apply(preddraws$y, 2, quantile, c(.1, .5, .9))

## Visualize
expost <- tail(head(exrates$USD, len+ahead), ahead)
ts.plot(y, xlim = c(length(y)-4*ahead, length(y)+ahead),
	       ylim = range(c(predquants, expost, tail(y, 4*ahead))))
for (i in 1:3) {
  lines((length(y)+1):(length(y)+ahead), predquants[i,],
        col = 3, lty = c(2, 1, 2)[i])
}
lines((length(y)+1):(length(y)+ahead), expost,
      col = 2)


# Example 3
## Predicting USD based on JPY and GBP in the mean
data(exrates)
len <- 1200
ahead <- 30
## Calculate log-returns
logreturns <- apply(exrates[, c("USD", "JPY", "GBP")], 2,
                    function (x) diff(log(x)))
logretUSD <- logreturns[2:(len+1), "USD"]
regressors <- cbind(1, as.matrix(logreturns[1:len, ]))  # lagged by 1 day

## Fit SV model to EUR-USD exchange rates
res <- svlsample(logretUSD, designmatrix = regressors)

## Use predict.svdraws to obtain predictive distributions
predregressors <- cbind(1, as.matrix(logreturns[(len+1):(len+ahead), ]))
preddraws <- predict(res, steps = ahead,
                     newdata = predregressors)
predprice <- exrates[len+2, "USD"] * exp(t(apply(preddraws$y, 1, cumsum)))

## Calculate predictive quantiles
predquants <- apply(predprice, 2, quantile, c(.1, .5, .9))

## Visualize
priceUSD <- exrates[3:(len+2), "USD"]
expost <- exrates[(len+3):(len+ahead+2), "USD"]
ts.plot(priceUSD, xlim = c(len-4*ahead, len+ahead+1),
	       ylim = range(c(expost, predquants, tail(priceUSD, 4*ahead))))
for (i in 1:3) {
  lines(len:(len+ahead), c(tail(priceUSD, 1), predquants[i,]),
        col = 3, lty = c(2, 1, 2)[i])
}
lines(len:(len+ahead), c(tail(priceUSD, 1), expost),
      col = 2)
# }

Run the code above in your browser using DataLab