R Labs 4a

ARMA, ARCH and GARCH models.

R Example 4.1: We explore the possible existence of autocorrelation in the EUR/USD exchange rates.

load(quantmod)
getFX("EUR/USD") #download EUR/USD rates from oanda.com
plot(EURUSD)
acf(EURUSD)
pacf(EURUSD)

The ACF is shown in Fig. 1 (a). One can see a slow exponential decay for successive lags, hence may suspect that the series EUR/USD  behaves as an AR process. The Partial ACF (PACF) function, and plotted in Fig. (b),  confirms that the order  of the underlying autoregressive process is 1. But BEWARE: Check the possible unit root. Note that the first coefficient . Do a Dickey-Fuller test. Then fit an ARIMA model.

eurusdACFeurusdPACF

R Example 4.4:  Adjusting an AIC-optimal ARMA(p,q) model to Robert Shiller's  S&P 500 PE 10 series. The data can be obtained here, as SP500_shiller.csv.

library(fArma)
sp500=read.csv("SP500_shiller.csv")
sp500PE = na.omit(sp500$P.E10)
ts=diff(log(sp500PE))  ##compute returns
ts=ts-mean(ts) ##subtract mean
bestAIC=1e9
for(p in 0:5) for (q in 0:5)
{ formula = as.formula(paste(sep="","ts~arma(",p,",",q,")"))
tsARMA = tryCatch(armaFit(formula, data=ts),
error=function(e) FALSE,
warning=function(w) FALSE )
if( !is.logical(tsARMA) ){
AIC = tsARMA@fit$aic
if(AIC < bestAIC){
bestAIC=AIC
bestARMA=tsARMA
bestPQ= c(p,q) }
}
else print("FALSE")
}
bestPQ
summary(bestARMA)

R Example 4.5: Fit an ARCH model to the daily log-return of Allianz (DAX), sample from January 6 to December 30, 2009.

library(fGarch);
daxRl = read.table("daxRlog", header=T)
alvRl =na.omit(daxRl$alvR) ##extract log returns of alv
alvRl2 <- alvRl^2  ##compute square log returns

##estimate the order p with acf, pacf criteria
acf(alvRl2)
pacf(alvRl2)

arch5=garchFit(formula=~garch(5,0),data=alvRl,cond.dist="norm")
summary(arch5)

R Example 4.6: Fit an ARMA+GARCH(1,1) model for the series of returns of Shiller's PE 10 series.

library(fGarch)
sp500=read.csv("SP500_shiller.csv")
sp500PE = na.omit(sp500$P.E10)
ts=diff(log(sp500PE))  ##compute returns
ts=ts-mean(ts)

##Apply a Ljung-Box test for ARCH effect
Box.test(ts^2)

##Now fit the ARMA+GARCH(1,1)
armaGarch = garchFit(formula=~arma(2,4)+garch(1,1),data=ts)
summary(armaGarch)