Can anyone help me with the stochvol package in R? I estimated the volatilities using this package but I am not being able to understand how to download the estimated volatilities. I used volplot function to plot the volatilities but not understanding how to download that output.

I have used the following code. When I use the volplot function, estimated volatilities look very different compared to the ones when I try to extract the median values in the m_export_oil dataframe.

The data required is here: https://docs.google.com/spreadsheets/d/14r-_SxCvOG9-_tTNLgXWU-FPkx_8OYLh/edit?usp=sharing&ouid=118324139963698395675&rtpof=true&sd=true


oil_qtrly <- read.xlsx("oil_qtrly.xlsx", sheetIndex = 1L)
oil_qtrly$log_real_op <- log(oil_qtrly$real_price)

sv_model_oil <- svsample(oil_qtrly$log_real_op, priormu = c(-10, 1), priorphi = c(20, 1.1),
                         priorsigma = 0.1,designmatrix = "ar1")
summary(sv_model_oil, showlatent = FALSE)

sigma_oil <- sv_model_oil$latent;
sigma_matrix_oil <- matrix(unlist(sigma_oil), ncol = 100, byrow = TRUE)

num_draws_oil <- dim(sigma_matrix_oil)[1]
num_quarters_oil <- dim(sigma_matrix_oil)[2]

m_export_oil <- matrix(ncol = 4, nrow = num_quarters_oil)

for (i in 1:num_quarters_oil) {
  qq_oil = quantile(100*exp(sigma_matrix_oil[,i]/2),  probs = c(0.05, 0.5,0.95))
  m_export_oil[i,1:3]<- qq_oil
  m_export_oil[i,4] <- oil_qtrly$real_price[i]

volplot(sv_model_oil, forecast = NULL, dates = oil_qtrly$date[-1])

1 Answer 1


The underlying data for this plot can be found in sv_model_oil$summary$sd columns c('5%', '50%', '95%'). It also has the mean and sd (sd of the simulations I presume).

A great trick in R(Studio) you can use to figure this out is just to inspect the source. In an R console just run volplot without parens or in RStudio select "volplot" and press F2. Another approach is to execute the function step by step by first running debug(volplot) followed by your original command. The debugger allows you to inspect values, step forward using the command n and continue running using the command c. To stop debugging run undebug(volplot).

  • 1
    $\begingroup$ That was exactly what I was looking for. Thanks a lot for the super helpful comment ! $\endgroup$
    – nusratecon
    Jun 7 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.