---
title: "Stochastic Volatility Models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Stochastic Volatility Models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r rmdsetup, include = FALSE}
knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  out.width = "70%",
  fig.align = "center",
  fig.width = 6,
  fig.asp = .618,
  error = TRUE # temporarily until investigating the error: 'recursive' must be of length 1
)
orig_opts <- options("digits")
options(digits = 3)
set.seed(1)
```

```{r setup}
library(bvhar)
```

```{r etfdat}
etf <- etf_vix[1:55, 1:3]
# Split-------------------------------
h <- 5
etf_eval <- divide_ts(etf, h)
etf_train <- etf_eval$train
etf_test <- etf_eval$test
```

# Models with Stochastic Volatilities

By specifying `cov_spec = set_sv()`,
`var_bayes()` and `vhar_bayes()` fits VAR-SV and VHAR-SV with shrinkage priors, respectively.

- Three different prior for innovation covariance, and specify through `bayes_spec`
    - Minneosta prior
        - BVAR: `set_bvar()`
        - BVHAR: `set_bvhar()` and `set_weight_bvhar()`
    - SSVS prior: `set_ssvs()`
    - Horseshoe prior: `set_horseshoe()`
    - NG prior: `set_ng()`
    - DL prior: `set_dl()`
- `sv_spec`: prior settings for SV, `set_sv()`
- `intercept`: prior for constant term, `set_intercept()`

```{r setsv}
set_sv()
```


## SSVS

```{r svssvs}
(fit_ssvs <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_ssvs(), cov_spec = set_sv(), include_mean = FALSE, minnesota = "longrun"))
```


## Horseshoe

```{r hssv}
(fit_hs <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_horseshoe(), cov_spec = set_sv(), include_mean = FALSE, minnesota = "longrun"))
```

## Normal-Gamma prior

```{r ngsv}
(fit_ng <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_ng(), cov_spec = set_sv(), include_mean = FALSE, minnesota = "longrun"))
```

## Dirichlet-Laplace prior

```{r dlsv}
(fit_dl <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_dl(), cov_spec = set_sv(), include_mean = FALSE, minnesota = "longrun"))
```

## Bayesian visualization

`autoplot()` also provides Bayesian visualization. `type = "trace"` gives MCMC trace plot.

```{r}
autoplot(fit_hs, type = "trace", regex_pars = "tau")
```

`type = "dens"` draws MCMC density plot.

```{r denshs}
autoplot(fit_hs, type = "dens", regex_pars = "tau")
```


```{r resetopts, include=FALSE}
options(orig_opts)
```