Assignment – 1
Submitted by:
Vidushi
MB18GID283
ARIMA Model for SAIL stock data
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-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
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
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
Coefficients:
intercept
69.5450
s.e. 0.5143
Coefficients:
ar1 ar2 ma1 ma2
0.1074 0.8916 0.9127 0.0513
s.e. 0.1353 0.1352 0.1422 0.0464
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
library(timeSeries);library(forecast);library(xts);
stock_prices = SAIL.NS[,4]
stock = diff(log(stock_prices),lag=1)
stock = stock[!is.na(stock)]
print(adf.test(stock))
breakpoint = floor(nrow(stock)*(2.9/3))
par(mfrow = c(1,1))
auto.arima(stock)
fit_residual=residuals(fit)
Actual_series = xts(0,as.Date("2014-11-25","%Y-%m-%d"))
for (b in breakpoint:(nrow(stock)-1)) {
stock_train = stock[1:b, ]
stock_test = stock[(b+1):nrow(stock), ]
summary(fit)
acf(fit$residuals,main="Residuals plot")
arima.forecast = forecast(fit, h = 1,level=99)
summary(arima.forecast)
par(mfrow=c(1,1))
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))
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)
print(Accuracy_percentage)
library(quantmod);
library(rugarch);
library(forecast);
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
summary(fit1)
#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(1,1) = when looking at current stock
price
plot(sPredict1, which = 2)
#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(0,0) = when looking at current stock
price
summary(fit2)
plot(sPredict2, which = 2)
#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(2,2) = when looking at current stock
price
summary(fit3)
s3 <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)), mean.model =
list(armaOrder=c(2,2)), distribution.model = "std")
plot(sPredict3, which = 2)
#Building a GARCH model (sGARCH = Standard GARCH), armaOrder(3,1) = when looking at current stock
price
summary(fit4)
plot(sPredict4, which = 2)