Академический Документы
Профессиональный Документы
Культура Документы
R
Nitesh Kumar
PGP17116
Thu Sep 27 14:02:09 2018
rm(list=ls())
setwd("E:/ Term V/FA/Data")
data=read.table("Dataset.csv", header=T, sep=",")
ret1=diff(log(data[,2]))
ret2=diff(log(data[,3]))
#Done by running auto correlation function with its own lag, lag till 10 of
return
acf(ret2,10)
#of volatility
acf(ret2^2,10)
#of Index
aa=acf(ret1,10)
aa
##
## Autocorrelations of series 'ret1', by lag
##
## 0 1 2 3 4 5 6 7 8 9
## 1.000 0.029 -0.012 -0.029 0.030 0.005 -0.056 -0.005 0.023 0.045
## 10
## 0.020
#joint test of autocorrelation
Box.test(ret2,lag=10,type="Box-Pierce")
##
## Box-Pierce test
##
## data: ret2
## X-squared = 55.8, df = 10, p-value = 2.238e-08
#there is no autocorrelation in the series, hence we can make the predictio
n using time series
Box.test(ret2,lag=10,type="Ljung")
##
## Box-Ljung test
##
## data: ret2
## X-squared = 55.93, df = 10, p-value = 2.116e-08
#pacf
pacf(ret2,10)
#pacf(series, lag)
bb=pacf(ret2,10)
bb
##
## Partial autocorrelations of series 'ret2', by lag
##
## 1 2 3 4 5 6 7 8 9 10
## -0.082 -0.055 -0.005 0.010 -0.066 -0.004 -0.038 0.028 -0.049 0.029
#lag 1,2,5 are significant
# Another way for autoselection of AR model
library(forecast)
Box.test(out_3$residuals,lag=10,type="Ljung")
##
## Box-Ljung test
##
## data: out_3$residuals
## X-squared = 29.554, df = 10, p-value = 0.001013
predict(out_3,1)
## $pred
## Time Series:
## Start = 2786
## End = 2786
## Frequency = 1
## [1] 7.096956e-05
##
## $se
## Time Series:
## Start = 2786
## End = 2786
## Frequency = 1
## [1] 0.01263159
l=length(ret2)
l1=1210
pred1=0
pred2=0
for(i in 1:22){
out_11=arima(ret2[i:(l1+i-1)],order=c(5,0,0),fixed=c(NA,NA,0,0,NA,0))
out_31=arima(ret2[i:(l1+i-1)],order=c(2,0,0),fixed=c(NA,NA,0))
pre1=predict(out_11,1)
pred1[i]=pre1$pred[1]
pre2=predict(out_31,1)
pred2[i]=pre2$pred[1]
}
## Warning in arima(ret2[i:(l1 + i - 1)], order = c(5, 0, 0), fixed = c(NA,
:
## some AR parameters were fixed: setting transform.pars = FALSE
out_4=arima(ret1,order=c(2,0,0),include.mean = T)
#Since only intercept need to be included
out_4
##
## Call:
## arima(x = ret1, order = c(2, 0, 0), include.mean = T)
##
## Coefficients:
## ar1 ar2 intercept
## 0.0293 -0.0124 4e-04
## s.e. 0.0189 0.0189 3e-04
##
## sigma^2 estimated as 0.0002295: log likelihood = 7716.6, aic = -15425.
2
## Series: ret1
## ARIMA(0,0,2) with non-zero mean
##
## Coefficients:
## ma1 ma2 mean
## -0.0539 -0.0536 0.0011
## s.e. 0.0284 0.0283 0.0006
##
## sigma^2 estimated as 0.0005657: log likelihood=2859.47
## AIC=-5710.94 AICc=-5710.91 BIC=-5690.47
##
## Call:
## arima(x = ret1, order = c(0, 0, 2), include.mean = F)
##
## Coefficients:
## ma1 ma2
## -0.0513 -0.0511
## s.e. 0.0284 0.0282
##
## sigma^2 estimated as 0.0005657: log likelihood = 2857.93, aic = -5709.
85
##
## Box-Ljung test
##
## data: out_5$residuals
## X-squared = 1.8984, df = 10, p-value = 0.9971
predict(out_5,10)
## $pred
## Time Series:
## Start = 1233
## End = 1242
## Frequency = 1
## [1] -0.0007484639 -0.0008466105 0.0000000000 0.0000000000 0.00000000
00
## [6] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.00000000
00
##
## $se
## Time Series:
## Start = 1233
## End = 1242
## Frequency = 1
## [1] 0.02378530 0.02381655 0.02384751 0.02384751 0.02384751 0.02384751
## [7] 0.02384751 0.02384751 0.02384751 0.02384751
l=length(ret1)
l1=1210
pred3=0
for(i in 1:22){
out_51=arima(ret2[i:(l1+i-1)],order=c(0,0,2),fixed=c(NA,NA,0))
pre3=predict(out_51,1)
pred3[i]=pre3$pred[1]
}
pred_HEX=cbind(pred3)
pred_HEX
## pred3
## [1,] -1.577178e-04
## [2,] -4.293572e-04
## [3,] -4.214603e-04
## [4,] 1.082602e-03
## [5,] -9.061039e-04
## [6,] 1.117286e-03
## [7,] 2.114036e-04
## [8,] -1.309785e-04
## [9,] 5.141840e-05
## [10,] -2.860653e-04
## [11,] 1.316109e-03
## [12,] -2.294810e-04
## [13,] -4.946275e-04
## [14,] 7.943946e-05
## [15,] 5.840339e-05
## [16,] -8.706262e-04
## [17,] -7.459375e-05
## [18,] -1.745783e-04
## [19,] 7.105980e-04
## [20,] 1.446148e-04
## [21,] -1.506083e-03
## [22,] -6.277753e-04
write.csv(pred_HEX,"Pred_HEX.csv")
# EACF
library(TSA)
##
## Attaching package: 'TSA'
eacf_out=eacf(ret1,10,10)
## AR/MA
## 0 1 2 3 4 5 6 7 8 9 10
## 0 o o o o o o o o o o o
## 1 x o o o o o o o o o o
## 2 x o o o o o o o o o o
## 3 x o o o o o o o o o o
## 4 x o o x o o o o o o o
## 5 x x x o o o o o o o o
## 6 x x x o o o o o o o o
## 7 x x x x x o o o o o o
## 8 x x x x x o o o o o o
## 9 x x x x x o x o o o o
## 10 x x x x x x o x o o o
# Automatic selection of ARMA model
auto.arima(ret1, max.p=10,max.q=10,d=0,ic="aic")
## Series: ret1
## ARIMA(0,0,2) with non-zero mean
##
## Coefficients:
## ma1 ma2 mean
## -0.0539 -0.0536 0.0011
## s.e. 0.0284 0.0283 0.0006
##
## sigma^2 estimated as 0.0005657: log likelihood=2859.47
## AIC=-5710.94 AICc=-5710.91 BIC=-5690.47
auto.arima(ret1, max.p=10,max.q=10,d=0,ic="bic")
## Series: ret1
## ARIMA(0,0,0) with zero mean
##
## sigma^2 estimated as 0.0005687: log likelihood=2854.68
## AIC=-5707.35 AICc=-5707.35 BIC=-5702.24
##
## Call:
## arima(x = ret2, order = c(1, 0, 1), include.mean = F)
##
## Coefficients:
## ar1 ma1
## -0.6556 0.7510
## s.e. 0.1186 0.1035
##
## sigma^2 estimated as 7.071e-05: log likelihood = 4138.89, aic = -8273.
79
Box.test(out_6$residuals,lag=10,type="Ljung")
##
## Box-Ljung test
##
## data: out_6$residuals
## X-squared = 5.6758, df = 10, p-value = 0.8417
predict(out_6,10)
## $pred
## Time Series:
## Start = 1233
## End = 1242
## Frequency = 1
## [1] 1.026401e-03 -6.729070e-04 4.411569e-04 -2.892218e-04 1.896134e-
04
## [6] -1.243103e-04 8.149766e-05 -5.342976e-05 3.502847e-05 -2.296462e-
05
##
## $se
## Time Series:
## Start = 1233
## End = 1242
## Frequency = 1
## [1] 0.008409064 0.008447271 0.008463640 0.008470666 0.008473684
## [6] 0.008474980 0.008475538 0.008475777 0.008475880 0.008475925