#************************************************************ # GARCH Out of Sample Forecasting Evaluation in GARCH models # Here is an example code to use R to evaluate the performance of GARCH type of model # User can modify the garch_forc function to find many interesting statistics # install.packages("rugarch") # install.packages("tseries") # install.packages("fGarch") # install.packages("AER") #------------------------------------------------------------ library(tseries) library(quantmod) library(rugarch) library(forecast) # Data from Yahoo and input data to dy getSymbols("GOOG",from="2000-01-02", to="2017-11-01") dy <- as.ts(100*diff(log(GOOG$GOOG.Adjusted))) dy <- na.omit(dy) plot(dy,main="Google Daily Stock Returns") in_obs = length(dy) forc_ahead = 10 # Defind the out of forecasting obs # Automatic ARIMA decision usint AIC of BIC pq <- arimaorder(auto.arima(dy,ic="bic",trace=TRUE)) d <- c(pq[1],pq[3]) # Function for out of sample forecasting using GARCH type of model garch_forc <- function(gtype) { g1 <- ugarchspec(variance.model = gtype , mean.model = list(armaOrder = d, include.mean = TRUE,archm=TRUE) ) fit1 <- ugarchfit(g1, dy,out.sample = 10) fout1 <- ugarchforecast(fit1,n.ahead=10,out.sample=10,n.roll=0) ffit <- fitted(fout1) fdy <- dy[(in_obs-forc_ahead+1):in_obs] rse <- ((fdy - ffit)^2)^0.5 ape <- abs(fdy - ffit)/abs(fdy) outstat <- c(mean(rse), mean(ape)) outstat <- as.matrix(outstat) rownames(outstat) <- c("RMSE","MAPE") return(outstat) } gtype1 <- list(garchOrder = c(1,1),model="sGARCH") test1 <- garch_forc(gtype1) colnames(test1) <- "GARCH" gtype2 <- list(garchOrder = c(1,1),model="fGARCH",submodel="TGARCH") test2 <- garch_forc(gtype2) colnames(test2) <- "TGARCH" gtype3 <- list(garchOrder = c(1,1),model="gjrGARCH") test3 <- garch_forc(gtype3) colnames(test3) <- "gjrGARCH" vol_forc <- data.frame(test1,test2,test3) vol_forc