# Chapter 2. Univariate Volatility Modeling

The R code from 2011 runs unmodified, this just updates the end date. The Matlab code would not run, so these are new functions. The GARCH functionality in the econometric toolbox in Matlab is trying to be too clever, but can't deliver and could well be buggy. If you want to try that, here are the docs (estimate). Besides, it can only do univariate GARCH and so can't be used in Chapter 3.

Kevin Sheppard's MFE toolbox is much better, while not as user friendly, it is much better written and is certainly more comprehensive. Its whats used below. It can be downloaded here and the documentation here is quite comprehensive.

##### Listing 2.1: ARCH and GARCH estimation in R Last edited: August 2016

library(tseries)
p = get.hist.quote(instrument = "^gspc", start = "2005-01-01",quote="AdjClose",quiet=T)
y=diff(log(p))*100
y=y-mean(y)
library(fGarch)
garchFit(~ garch(1,1), data = y,include.mean=FALSE)
garchFit(~ garch(4,0), data = y,include.mean=FALSE)
garchFit(~ garch(4,1), data = y,include.mean=FALSE)
garchFit(~ garch(1,1), data = y,include.mean=FALSE,cond.dist="std",trace=F)
res=garchFit(~ garch(1,1), data = y,include.mean=FALSE,cond.dist="sstd",trace=F)
plot(res)

##### Listing 2.2: ARCH and GARCH estimation in Matlab Last edited: August 2016

price = hist_stock_data('01012000','30082016','^gspc');
y=diff(log(p))*100;
y=y-mean(y);
tarch(y,1,0,1)
tarch(y,4,0,0)
tarch(y,4,0,1)
tarch(y,1,0,1,'STUDENTST')


##### Listing 2.3: Advanced ARCH and GARCH estimation in R Last edited: 2011

print(garchFit(~ aparch(1,1),data=y,include.mean=FALSE,trace=F))
print(garchFit(~ aparch(1,1),data=y,include.mean=FALSE,trace=F,include.delta=F,delta=2))
print(garchFit(~ aparch(1,1),data=y,include.mean=FALSE,cond.dist="std",trace=F))
print(garchFit(~ aparch(2,2),data=y,include.mean=FALSE,trace=F))

##### Listing 2.4: Advanced ARCH and GARCH estimation in Matlab Last edited: August 2016

aparch(y,1,1,1)
aparch(y,2,2,1)
aparch(y,1,1,1,'STUDENTST')