Skip to content
All library documents

Extracting Stochastic Volatility Estimates from R

Article Quant Q&A · Author: nusratecon

Summary

This discussion explains how to retrieve estimated volatility summaries from an R model fit with the stochvol package. The questioner compares a plot produced by the package’s plotting function with median values computed from the latent simulation draws and finds that they appear inconsistent. The reply points to the model object’s summary table, where the standard deviation estimates and selected posterior quantiles used for plotting are available.

It also suggests inspecting or stepping through the plotting function’s source to understand which data it displays. This is a practical debugging method for investigating differences between a visualization and a separately assembled export. The post gives no resolved diagnosis of the mismatch and does not examine the model specification or the time-series data, so it serves as guidance for extracting and tracing outputs rather than as evidence about the quality of the volatility estimates.

Key ideas

  • The fitted model object stores posterior volatility summaries that can be inspected directly.
  • The summary table includes selected quantiles that can help reproduce the plotted estimates.
  • Comparing extracted values with a plot requires checking which model output the plotting function uses.
  • Inspecting or debugging a function can reveal the data and transformations behind its display.
  • The discussion does not establish why the questioner’s manually calculated medians look different.

Tags

Full text
# Stochastic volatility estimation in R


# Stochastic volatility estimation in R












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

```
library(stochvol)
library(xlsx)

set.seed(123)
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]
}
m_export_oil<-data.frame(m_export_oil)

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

## Answer by Bob Jansen (score 2)

https://quant.stackexchange.com/a/75796

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

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.