## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 7,
  fig.height = 4
)

## ----setup, message = FALSE---------------------------------------------------
library(forecastdom)
library(ggplot2)
data(rrz2016)

## ----plot, fig.height = 3.5---------------------------------------------------
ggplot(rrz2016, aes(date, SII)) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "grey60") +
  geom_line(colour = "#47A5C5", linewidth = 0.6) +
  labs(x = NULL, y = "SII",
       title = "Short interest index, 1973-2014") +
  theme_minimal()

## ----table-a2-----------------------------------------------------------------
horizons <- c(1, 3, 6, 12)

results <- do.call(rbind, lapply(horizons, function(h) {

  ivx <- ivx_wald(rrz2016$r, matrix(-rrz2016$SII, ncol = 1),
                  K = h, M_n = 0L, beta = 0.99)

  T_ <- nrow(rrz2016)
  P  <- T_ - h
  y_h <- sapply(seq_len(P), function(t) mean(rrz2016$r[(t + 1):(t + h)]))
  X_h <- matrix(rrz2016$SII[1:P], ncol = 1)
  Z_h <- matrix(1, P, 1)
  qll <- qll_hat(y_h, X_h, Z = Z_h, L = h)

  data.frame(h = h, IVX_Wald = ivx$statistic, qLL = qll$statistic)
  
}))

knitr::kable(results, digits = 3, row.names = FALSE,
             col.names = c("$h$", "IVX-Wald", "$\\widehat{qLL}$"))

