**GARCH Forecasting Model **

This is a GARCH forecasting model problem which needs to be completed in R.

The Westinghouse file is attached. The question is:

**Using the stock data, build GARCH models either using the fGARCH or other packages and compare.**** **

**Solution**** **

#read the file

library(readr)

WAB <- read_csv(“~/Desktop/Krista 26th/WAB.csv”) #change as per the location i your PC

x <-WAB

#code to transform date into the variable TIME , taking 15th Sep 2015 as TIME=0.

DF <- data.frame(Date = x$Date)

DF$Date<- as.Date(x$Date, “%m/%d/%Y”)

Diff <- function(x, start) as.numeric(x – as.Date(cut(start, “year”)))

y<-transform(DF, NumDays = Diff(Date, Date), TotalDays = Diff(Date, Date[1]))

z=y$TotalDays-257

x$Date=z

#filling missing values . There are lots of dates for which Adj close price isn’t available

a <- c(rep(“NA”,731))

for(i in 0:730) for ( j in 1:505) {

if (x$Date[j]==i) a[i+1]=x$`Adj Close`[j]

} #this a represents the ts of CLOSE with missing values.

TIME=c(0:730)

df<-data.frame(TIME,a)

library(zoo)

ts<-na.approx(df)[,2] #data for all dates are filled by interpolatrion

# ts ready for working on

#fitting GARCH model- default GARCH(1,1)

library(fGarch)

gfit.fg<- garchFit(data=ts) #this is the GARCH(1,1) model

coef(gfit.fg) #gives the coefficients of the GARCH model

volatility(gfit.fg) #Extracts conditional volatility from a fitted ???fGARCH??? object

formula(gfit.fg) #Extracts formula expression from a fitted ???fGARCH??? object.

summary(gfit.fg)

z <-predict(gfit.fg,30)

z$standardDeviation #gives predicted volatility(standard deviation) for next 30 days

#find RMSE of this GARCH(1,1) MODEL

res<- residuals(gfit.fg) #residuals

RSS <- sum(res^2) #residual sum of squares

MSE <- RSS/length(ts) #mean sqaure error

rmsegarch11 <- sqrt(MSE)#root mean sqaure error RMSE

print(rmsegarch11)#prints RMSE for GARCH(1,1)

#GARCH(2,2)

fit2 <-garchFit(~garch(2,2),ts)

summary(fit2)

res<- residuals(fit2) #residuals

RSS <- sum(res^2) #residual sum of squares

MSE <- RSS/length(ts) #mean sqaure error

rmsegarch22 <- sqrt(MSE)#root mean sqaure error RMSE

print(rmsegarch22)#prints RMSE for GARCH(2,2)

# comparison with prev models such as ARIMA , ETS etc.

#auto.arima

library(forecast)

arima<- auto.arima(ts)

accuracy(arima)#look at the RMSE VALUE for comparison purpose

#ets model

ETS <-ets(ts)

accuracy(ETS)#look at the RMSE VALUE for comparison purpose

#building my own ARIMA ,i.e. to choose p , d , q . Call it myarima

y=ts

azfinal.aic<- Inf #initiation with large value

azfinal.order<- c(0,0,0)

for (p in 0:3) for (d in 0:1) for (q in 0:3) {

azcurrent.aic<- AIC(Arima(y, order=c(p, d, q)))

if (azcurrent.aic<azfinal.aic) {

azfinal.aic<- azcurrent.aic

azfinal.order<- c(p, d, q)

azfinal.arima<- Arima(y, order=azfinal.order)

}

} #we are taking d=0,1, p=0,1,2,3 , q=0,1,2,3 and choosing which combination of p,d,qminimises the AIC

myarima<-azfinal.arima

accuracy(myarima)#look at the RMSE VALUE for comparison purpose

#NOTE:

# arima and myarima are basically the same model , i.e. ARIMA(1,1,0).

#This is because the function auto.arima chooses the order (p,d,q) by AIC minimzation only,

#which was done manually while building myarima

#ANSWER:

#Lower the RMSE , better is the model . GARCH models have extremely high RMSE compared to ETS & ARIMA models here.

#So,by comparing RMSE , our order is , RMSE_ARIMA < RMSE_ETS << RMSE_GARCH(2,2) < RMSE_GARCH(1,1)

# So , order of performance : ARIMA > ETS > GARCH(2,2) >GARCH(1,1)

#So,for this dataset , ETS & ARIMA models perform far better than the GARCH models