# Chapter 2. Univariate Volatility Modeling

Copyright 2016 Jon Danielsson. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
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');
p=price.AdjClose(end:-1:1);
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')