\documentclass[notitlepage]{article}
\usepackage{../../Math545}
\usepackage{listings}
\usepackage{numprint}
\usepackage{enumerate}
\usepackage{bbm}
\usepackage{bm}
\usepackage{verbatim}

\usepackage{amsfonts,amsmath,amssymb}

\newtheorem{alg}{Algorithm}
\newtheorem{ex}{Example}
\newtheorem{note}{Note}
\newenvironment{proof}{\par\noindent{\normalfont{\bf{Proof
}}}}
{\hfill \small$\blacksquare$\\[2mm]}


\setlength{\parindent}{0in}

\def\E{\mathbb{E}}
\def\Var{\text{Var}}
\def\Cov{\text{Cov}}
\def\Corr{\text{Corr}}

\def\bW{\mathbf{W}}

\newcommand{\bs}[1]{\bm{#1}}
\newcommand{\Rho}{\mathrm{P}}
\newcommand{\cif}{\text{ if }}
\newcommand{\Ra}{\Longrightarrow}
\newcommand{\ra}{\longrightarrow}

\def\bphi{\boldsymbol \phi}


\lstloadlanguages{R}

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

\lstdefinestyle{Rsettings}{
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp}, % Write as many keywords
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

\lstset{basicstyle=\ttfamily, numbers=none, literate={~} {$\sim$}{2}}

\begin{document}

<<setup, cache=FALSE, echo=FALSE>>=
library(knitr)
# global chunk options
opts_chunk$set(cache=TRUE, autodep=TRUE)
options(scipen=999)
options(repos=c(CRAN="https://cloud.r-project.org/"))
@
\begin{center}
{\textsclarge{Fitting Conditional Heteroscedastic Models}}
\end{center}


In a \textit{conditional heteroscedastic model}, we have an observed data set with \textit{time-varying variance}.  Typically, these models can be considered as special forms of state space models.

\bigskip
\textbf{Stochastic Volatility:} The \textit{stochastic volatility} model uses a stochastic state space form that allows the variance of the observed process to vary stochastically with time.   The simplest form specifies that
\[
Y_t = \exp\{X_t/2\} W_t \qquad \{W_t\} \sim \WN(0,\sigma_W^2)
\]
and then that
\[
X_{t+1} = \phi X_t + V_{t}  \qquad \{V_t\} \sim \WN(0,\sigma_V^2)
\]
that is, given $X_t = x_t$, $Y_t$ has mean zero and variance $\exp\{x_t\}$, and $\{X_t\} \sim \AR(1)$.  We can reparameterize the model as follows:
\[
Y_t = \exp\{X_t/2\} Z_t \qquad \{Z_t\} \sim \WN(0,1)
\]
and
\[
X_{t+1} = \mu+ \phi(X_t-\mu) + V_{t}  \qquad \{V_t\} \sim \WN(0,\sigma_V^2).
\]
where $\mu = 2 \log \sigma_W$.

<<CH0,comment='+', fig.width=7.25, fig.height=5.0, fig.align='center'>>=
set.seed(24)
n<-1000;phi<-0.98;sigW<-0.01;sigV<-0.05
X<-arima.sim(n,model=list(ar=phi),rand.gen = function(n) sqrt(sigV) * rnorm(n))
Z<-rnorm(n)*sigW
Y<-exp(X/2)*Z
var(Y)
par(mar=c(4,3,2,0));plot(Y,type='l',ylim=range(Y));
points(Y,pch=19,cex=0.3)

<<CH1,comment='+', fig.width=7.25, fig.height=5.0, fig.align='center'>>=
set.seed(24)
mu<-2*log(sigW)  #Reparameterization
X<-arima.sim(n,model=list(ar=phi),rand.gen = function(n) sqrt(sigV) * rnorm(n))
X<-X+mu
Z<-rnorm(n)
Y<-exp(X/2)*Z
var(Y)
par(mar=c(4,3,2,0));plot(Y,type='l',ylim=range(Y));
points(Y,pch=19,cex=0.3)
@

Inference for the SV model can be carried out using Bayesian methods and Markov chain Monte Carlo (MCMC) and the \texttt{stochvol} library.
\begin{itemize}
  \item \texttt{svsim} simulates a SV model
  \item \texttt{svsample} carries out MCMC analysis
\end{itemize}
<<CH2,comment='+', fig.width=7.25, fig.height=6.5, fig.align='center'>>=
library(stochvol)
y.sim <- svsim(1000, mu = 2*log(sigW), phi = phi, sigma = sigV)
names(y.sim)
plot(y.sim)
@

The algorithm draws 5000 samples from the \textit{posterior distribution} for the three parameters of the model $\mu,\phi$ and $\sigma_V$.

<<CH3,comment='+', fig.width=7.0, fig.height=9.0, fig.align='center'>>=
mcmc.draws <- svsample(y.sim$y, draws = 5000, burnin = 1000, priormu = c(0, 10),
                      priorphi = c(20, 1.2), priorsigma = 100)
names(mcmc.draws)
par(mfrow=c(2,2),mar=c(4,4,2,0))
hist(mcmc.draws$para[,1],col='gray',nclass=30,xlab='',
          main=expression(paste('Posterior for ',mu)))
box();abline(v=mu,col='red',lwd=2)   #Mark the true values with red lines.
hist(mcmc.draws$para[,2],col='gray',nclass=30,xlab='',
          main=expression(paste('Posterior for ',phi)))
box();abline(v=phi,col='red',lwd=2)
hist(mcmc.draws$para[,3],col='gray',nclass=30,xlab='',
          main=expression(paste('Posterior for ',sigma[V]^2)))
box();abline(v=sigV,col='red',lwd=2)
@
Parameter estimates can be obtained from the posterior distribution by summarizing the posterior samples:
<<CH3a,comment='+', fig.width=7.0, fig.height=7.0, fig.align='center'>>=
apply(mcmc.draws$para,2,summary)
@
We can also forecast using the model, with commands
<<CH4,comment='+', fig.width=7.0, fig.height=7.0, fig.align='center'>>=
fore <- predict(mcmc.draws, 20)
plot(mcmc.draws, forecast = fore)
@

\pagebreak
\textbf{ARCH/GARCH MODEL:} The \textit{ARCH} and \textit{GARCH} models use \textit{deterministic} state space forms that allows the variance of the observed process to vary with time.   The ARCH(1) model specifies
\[
Y_t = \sqrt{X_t} Z_t \qquad \{Z_t\} \sim \WN(0,1)
\]
and then that
\[
X_{t+1} = \alpha_0 + \alpha_1 Y_t^2
\]
whereas the GARCH(1,1) model specifies
\[
X_{t+1} = \alpha_0 + \alpha_1 Y_t^2 + \beta_1 X_t
\]
where, in both cases, given $X_t = x_t$, $Y_t$ has mean zero and variance $\sqrt{x_t}$.  The most general form of the GARCH model is the $\textrm{GARCH}(p,q)$ model where
\[
X_{t+1} = \alpha_0 + \sum_{j=1}^p \alpha_j Y_{t-j}^2 + \sum_{j=1}^q \beta_j X_{t-j}
\]



<<CH5,comment='+', fig.width=7.25, fig.height=5.25, fig.align='center'>>=
set.seed(345)
n<-1000;al0<-0.01;al1<-0.95
X<-Y<-rep(0,n);X[1]<-al0;Y[1]<-rnorm(1)*sqrt(X[1])  #ARCH(1) MODEL
for(j in 2:n){
    X[j]<-al0+al1*Y[j-1]^2
    Y[j]<-rnorm(1)*sqrt(X[j])
}
@

GARCH models can be fitted using the \texttt{fGarch} library.

<<CH6,comment='+', fig.width=7.25, fig.height=5.25, fig.align='center'>>=
library(fGarch)
fit.10<-garchFit(Y~garch(1,0),trace=F)  #ARCH(1)
summary(fit.10)
fit.11<-garchFit(Y~garch(1,1),trace=F)  #GARCH(1,1)
summary(fit.11)
c(fit.10@fit$ics[1],fit.11@fit$ics[1])  #AIC comparison
@
The ARCH(1) model is preferred.  The parameter referred to as \texttt{omega} is $\alpha_0$, and \texttt{mu} is the mean of the $\{Y_t\}$ series.
\bigskip

For an analysis of a real data set, we consider the S \& P 500 index over the period 2007-2017.

<<CH7,comment='+', fig.width=7.25, fig.height=3.75, fig.align='center'>>=
SP.2017<-read.csv('GSPC.csv',header=TRUE)
X<-SP.2017$Close
Tv<-as.Date(as.character(SP.2017$Date),'%d/%m/%Y')
par(mar=c(4,4,2,0))
plot(Tv,X,type='l',main='S & P 500 index',xlab='Date')
@
If we let $X_t$ denote the index value, we then transform to the percentage log returns series
\[
Y_t = \log (X_{t+1}/X_t) = 100(\log X_{t+1} - \log X_{t})
\]

<<CH8,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
Y<-100*diff(log(X))
par(mar=c(4,4,2,0))
plot(Tv[-1],Y,type='l',main='S & P 500 log returns',xlab='Date')
hist(Y,nclass=50,col='gray',main='Histogram of log returns');box()
@
The histogram indicates a \textit{leptokurtic} (or heavy-tailed) marginal distribution for the log returns.

<<CH9,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
par(mar=c(4,4,4,0))
acf(Y,lag.max=100,main='ACF of Y')
acf(Y^2,lag.max=100,main='ACF of Y squared')
@
The log returns series is uncorrelated, buy the squared series has significant autocorrelation.

\bigskip

We can carry out an analysis using the stochastic volatility model.

<<CH10,comment='+', fig.width=7.25, fig.height=6.0, fig.align='center'>>=
mcmc.draws <- svsample(Y, draws = 5000, burnin = 1000, priormu = c(0, 10),
                      priorphi = c(20, 1.2), priorsigma = 100)
par(mfrow=c(2,2),mar=c(4,4,2,0))
hist(mcmc.draws$para[,1],col='gray',nclass=30,xlab='',
          main=expression(paste('Posterior for ',mu)));box()
hist(mcmc.draws$para[,2],col='gray',nclass=30,xlab='',
          main=expression(paste('Posterior for ',phi)));box()
hist(mcmc.draws$para[,3],col='gray',nclass=30,xlab='',
          main=expression(paste('Posterior for ',sigma[V]^2)));box()
fore <- predict(mcmc.draws, 20)
plot(mcmc.draws, forecast = fore)
@

\bigskip

The ARCH(1) and the GARCH(1,1) models can also be used.

<<CH11,comment='+', fig.width=7.25, fig.height=5.25, fig.align='center'>>=
fit.10<-garchFit(Y~garch(1,0),trace=F)  #ARCH(1)
summary(fit.10)
fit.11<-garchFit(Y~garch(1,1),trace=F)  #GARCH(1,1)
summary(fit.11)
c(fit.10@fit$ics[1],fit.11@fit$ics[1])  #AIC comparison
@
Here the GARCH(1,1) model is preferred.
\end{document}
