# 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