Anda di halaman 1dari 14

Predictive Analysis

Assignment – 1

Analysis of ARIMA and GARCH Models

Submitted by:
Vidushi
MB18GID283
ARIMA Model for SAIL stock data

Log Return Plot

Augmented Dickey-Fuller Test

data: stock
Dickey-Fuller = -8.1109, Lag order = 9, p-value = 0.01
alternative hypothesis: stationary

Interpretation:
p-value is less than 0.05, so the series is stationery.
To model a time series with ARIMA model, the series needs to be stationery which is
in our case.
If the series is non-stationery we need to convert it to stationery using the differencing
method. So the value of differencing order d=0
ACF and PACF Plots
For AR models, the ACF will dampen exponentially and the PACF will be used to identify the
p order of the AR model.
For MA models, the PACF will dampen exponentially and the ACF plot will be used to
identify the q order of the MA process
Interpretation: The value of p=1, q=1, d=0. So the ARIMA parameters are (1,0,1)
To test whether we have chosen the correct order, we do the Ljung-Box test. If p-
value > 0.05 then the order selected is correct.

> Box.test(fit_residual, lag=10, type = "Ljung-Box")

Box-Ljung test

data: fit_residual
X-squared = 3.8172, df = 10, p-value = 0.9552

Since p-value is 0.9552 which is greater than 0.05 so the ARIMA order (1,0,1) is
correct.

ARIMA Forecast
Model Information:

Call:
arima(x = stock_train, order = c(1, 0, 1), include.mean = F
ALSE)

Coefficients:
ar1 ma1
-0.0022 0.0390
s.e. 1.2302 1.2195

sigma^2 estimated as 0.0006478: log likelihood = 1659.74,


aic = -3313.48

Error measures:
ME RMSE MAE MPE MAPE
MASE ACF1
Training set 0.0002117675 0.02545206 0.01910519 NaN Inf 0.
6908884 0.0001679105

Forecasts:
Point Forecast Lo 99 Hi 99
738 0.001041755 -0.0645184 0.06660191
SAIL.NS.Close
2018-12-27 51.5
SAIL.NS.Close
2018-12-28 54.65

Interpretation: From the coefficient we can get the following return equation:
Y(t) = -0.0022*Y(t-1) + 0.0390*E(t-1)
The forecasted point return is 0.001041755
AIC score is -3313.48. Lower the score, better the model
Forecasted Returns versus Actual Returns

> print(Accuracy_percentage)
[1] 56

We can run the model for other possible combinations of


(p,d,q) to select the best optimal parameters to run the model.
GARCH Model for SAIL stock data

Model 1
Building a GARCH model (sGARCH = Standard GARCH),
armaOrder(1,1) = when looking at current stock price

Coefficients:
ar1 ma1 intercept
0.9879 0.0211 65.6790
s.e. 0.0068 0.0433 6.2751

sigma^2 estimated as 3.426: log likelihood = -1006.98, aic = 2021.96


Model 2
Building a GARCH model (sGARCH = Standard GARCH),
armaOrder(0,0) - looking at current stock price

Coefficients:
intercept
69.5450
s.e. 0.5143

sigma^2 estimated as 130.7: log likelihood = -1904.51, aic = 3813.02


Model 3
Building a GARCH model (sGARCH = Standard GARCH),
armaOrder(2,2) - when looking at current stock price

Coefficients:
ar1 ar2 ma1 ma2
0.1074 0.8916 0.9127 0.0513
s.e. 0.1353 0.1352 0.1422 0.0464

sigma^2 estimated as 3.425: log likelihood = -1008.53, aic = 2027.06


Model 4
Building a GARCH model (sGARCH = Standard GARCH),
armaOrder(3,1) - when looking at current stock price

Coefficients:
ar1 ar2 ar3 ma1 intercept
0.1615 0.8755 -0.0595 0.8537 65.6778
s.e. 0.1635 0.1415 0.0477 0.1587 6.1854

sigma^2 estimated as 3.406: log likelihood = -1005.54, aic = 2023.08

Analysis of GARCH Model


We have built here various GARCH models for different order (p, q) ARIMA model.
Lower the AIC score, better the model. We found that AIC score of the model is least in the
ARIMA model (1,1) i.e. 2021.96
The same results were found when we were doing the ARIMA method.
ARIMA Model R Code
library(quantmod);library(tseries);

library(timeSeries);library(forecast);library(xts);

# Step 1: Testing for stationarity

getSymbols('SAIL.NS', from='2016-01-01', to='2019-01-01')

stock_prices = SAIL.NS[,4]

stock = diff(log(stock_prices),lag=1)

stock = stock[!is.na(stock)]

plot(stock,type='l', main='log returns plot')

print(adf.test(stock))

# Step 2: Find values of p and q

breakpoint = floor(nrow(stock)*(2.9/3))

par(mfrow = c(1,1))

acf.stock = acf(stock[c(1:breakpoint),], main='ACF Plot', lag.max=100)

pacf.stock = pacf(stock[c(1:breakpoint),], main='PACF Plot', lag.max=100)

auto.arima(stock)

fit=arima(stock, order = c(1,0,1))

fit_residual=residuals(fit)

Box.test(fit_residual, lag=10, type = "Ljung-Box")

# Step 3: Estimation and Forecasting

Actual_series = xts(0,as.Date("2014-11-25","%Y-%m-%d"))

forecasted_series = data.frame(Forecasted = numeric())]

for (b in breakpoint:(nrow(stock)-1)) {

stock_train = stock[1:b, ]

stock_test = stock[(b+1):nrow(stock), ]

fit = arima(stock_train, order = c(1, 0, 1),include.mean=FALSE)

summary(fit)

acf(fit$residuals,main="Residuals plot")
arima.forecast = forecast(fit, h = 1,level=99)

summary(arima.forecast)

par(mfrow=c(1,1))

plot(arima.forecast, main = "ARIMA Forecast")

forecasted_series = rbind(forecasted_series,arima.forecast$mean[1])

colnames(forecasted_series) = c("Forecasted")

Actual_return = stock[(b+1),]

Actual_series = c(Actual_series,xts(Actual_return))

rm(Actual_return)

print(stock_prices[(b+1),])

print(stock_prices[(b+2),])

# Step 4: Check the accuracy of the ARIMA model by comparing the forecasted returns versus the actual
returns.

Actual_series = Actual_series[-1]

forecasted_series = xts(forecasted_series,index(Actual_series))

plot(Actual_series,type='l',main='Actual Returns Vs Forecasted Returns')

lines(forecasted_series,lwd=1.5,col='red')

legend('bottomright',c("Actual","Forecasted"),lty=c(1,1),lwd=c(1.5,1.5),col=c('black','red'))

comparsion = merge(Actual_series,forecasted_series)

comparsion$Accuracy = sign(comparsion$Actual_series)==sign(comparsion$Forecasted)

print(comparsion)

Accuracy_percentage = sum(comparsion$Accuracy == 1)*100/length(comparsion$Accuracy)

print(Accuracy_percentage)

# The accuracy percentage of the model is 56%


GARCH Model R Code

library(quantmod);

library(rugarch);

library(forecast);

s<-getSymbols('SAIL.NS', from='2017-01-01', to='2019-01-01')

chartSeries(s)

stock= SAIL.NS[,4]

stock = stock[!is.na(stock)]

head(stock)

#Using Arima model to Know AIC & BIC Value for same order as GARCH

fit1 <- Arima(stock, order=c(1,0,1))

summary(fit1)

#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(1,1) = when looking at current stock
price

s1 <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)), mean.model =


list(armaOrder=c(1,1)), distribution.model = "std")

sGarch1 <- ugarchfit(spec=s1, data = stock)

sPredict1<- ugarchboot(sGarch1, n.ahead=10, method =c("Partial", "Full")[1])

plot(sPredict1, which = 2)

#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(0,0) = when looking at current stock
price

fit2 <- Arima(stock, order=c(0,0,0))

summary(fit2)

s2 <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)), mean.model =


list(armaOrder=c(0,0)), distribution.model = "std")

sGarch2 <- ugarchfit(spec=s2, data = stock)

sPredict2<- ugarchboot(sGarch2, n.ahead=10, method =c("Partial", "Full")[1])

plot(sPredict2, which = 2)

#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(2,2) = when looking at current stock
price

fit3 <- Arima(stock, order=c(2,0,2),include.mean = FALSE)

summary(fit3)
s3 <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)), mean.model =
list(armaOrder=c(2,2)), distribution.model = "std")

sGarch3 <- ugarchfit(spec=s3, data = stock)

sPredict3<- ugarchboot(sGarch3, n.ahead=10, method =c("Partial", "Full")[1])

plot(sPredict3, which = 2)

#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(3,1) = when looking at current stock
price

fit4<- Arima(stock, order=c(3,0,1))

summary(fit4)

s4 <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)), mean.model =


list(armaOrder=c(3,1)), distribution.model = "std")

sGarch4 <- ugarchfit(spec=s4, data = stock)

sPredict4<- ugarchboot(sGarch4, n.ahead=10, method =c("Partial", "Full")[1])

plot(sPredict4, which = 2)

Anda mungkin juga menyukai