---
title: "STAT 436 / 536 - Lecture 18"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(fig.align= 'center')
knitr::opts_chunk$set(fig.height= 4)
knitr::opts_chunk$set(fig.width = 7)
library(datasets)
library(ggfortify)
library(MASS)
library(forecast)
library(tseries)
```


## Heteroskedastic Variance Models

- Hetoreskedastic variance models account for
\vfill
- Recall the airline passenger's dataset, where the 

```{r}
data(AirPassengers)
autoplot(AirPassengers)
```

\vfill

- Another example includes the daily returns from the S & P 500 index. Daily returns are displayed as $100 \times \log(\frac{y_t}{y_{t-1}}),$ where $y_t$ is the value of the S&P 500 on day t.
\vfill
- This series exhibits times of 
\vfill
- Time series with periods of increased volatility are referred to as 
\vfill

- What are the implications of conditional heteroskedastic model?
\newpage

```{r, echo = FALSE}
data(SP500)
autoplot(ts(SP500, frequency = 365, start = c(1990,1))) + ggtitle("Daily Returns of S & P 500")
```

- Models with stochastic variability are not stationary, 
\vfill
- Stochastic Volatility is typically not obvious from an ACF plot alone, but can be visualized with the squared values of the sequence (that have been de-meaned). why?
```{r}
ggAcf(SP500 - mean(SP500))
```

\newpage

```{r}
ggAcf((SP500 - mean(SP500))^2)
```

#### Modeling Conditional Heteroskedasticity

- A model that accounts for changing variance is necessary for this situation and a common way to do this
\vfill
- Let $\{\epsilon_t\}$ be a first-order autoregressive conditional heteroskedastic (ARCH(1)), where $\{w_t\}$ is white noise with zero mean and variance of 1, then

\vfill
\vfill

- The variance of $\epsilon_t$ can be calculated as:
\begin{eqnarray*}
Var(\epsilon_t) &=& E(\epsilon_t^2) - E(\epsilon_t)^2\\
&=& E(w_t^2) E(\alpha_0 + \alpha_1 \epsilon_{t-1}^2)\\
&=& E(\alpha_0 + \alpha_1 \epsilon_{t-1}^2)\\
&=& \alpha_0 + \alpha_1 Var(\epsilon_{t-1})\\
\end{eqnarray*}
\vfill

- ARCH models should be applied to an uncorrelated series with no trends or seasonal components. Hence, we need to control for ARIMA type procedures first.

\vfill

\newpage

- ARCH models can also include $p$-th order lags, where
\vfill

\vfill

- Furthermore, a more flexible model known as Generalized ARCH or GARCH(p,q) model can be specified. First, define $\epsilon_t = w_t \sqrt{h_t}$, then 

\vfill
\vfill

#### Simulating GARCH Models

- To demonstrate the structure of GARCH models and the impacts of $\alpha$ and $\beta$, consider the following simulation.

\vfil

```{r}
set.seed(12042018)
alpha0 <- .1
alpha1 <- .9
beta1 <- 0

num.pts <- 1000

w <- rnorm(num.pts)
eps <- rep(0, num.pts)
h <- rep(0, num.pts)

for (time.pt in 2:num.pts){
  h[time.pt] <- alpha0 + alpha1 * (eps[time.pt - 1]^2) + beta1 * h[time.pt - 1]
  eps[time.pt] <- w[time.pt] * sqrt(h[time.pt])
}
plot(eps, type = 'l')
acf(eps)
acf(eps^2)
```

\vfill
- Modify the values for $\alpha_0$, $\alpha_1$, and $\beta_1$ to assess the impacts of these parameters.

\vfill

#### Fitting GARCH Models

- The `garch()` function in the `tseries` package can be used to fit GARCH models.

\vfill

```{r}
g1 <- garch(SP500, trace = F, grad = 'numerical')
g1
confint(g1)
g2 <- garch(eps, trace = F, grad = 'numerical')
g2
confint(g2)
```

\vfill

- The `rugarch` package in R contains the functionality to specify the mean structure of the model, with covariates, and fit a GARCH model to the error terms.
\vfill

- GARCH models do not influence the mean prediction (point estimate) in most situations, but do impact the width of the prediction intervals.

\vfill

### State Space Models

- Recall, the generic state space model formulation.
\begin{eqnarray*}
y_t &=& F_t x_t + v_t \\
x_t &=& G_t x_{t-1} + w_t,\\
\end{eqnarray*}
where, 

\vfill
