#setwd("D:/MyGames/RStudio/works")
#(rep("\n",50))
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------


library(quantmod)
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)
library(fGarch)
library(Matrix)
library(tinytex)
library(timeDate)


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#question1



getSymbols("CAT", from="2006-01-03",to="2017-04-14")
head(CAT)
tail(CAT)
dim(CAT)

rt <- diff(log(as.numeric(CAT[,6])))
head(rt)
tail(rt)

ts.plot(rt)
Box.test(rt,lag=10,type="Ljung")
t.test(rt)

#Question 1.a
#H0:p1=p2=..=p10=0
#Ha:pi /=0
#since p-value > 0.05, H0 is significance, therefore is no serial correlation


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------


Box.test(rt^2,lag=10,type="Ljung")
#Question 1.b
#H0:p1=p2=..=p10=0
#Ha:pi /=0
#since p-value < 0.05, H0 is not significance, therefore is  serial correlation


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Q1m1 = garchFit(~garch(1,1),data=rt,trace=F)
summary(Q1m1)

#plot(Q1m1)
#13

#Question 1.c

#mean equation:
#rt = 4.9258e-4
#(3.075e-04)
#variance equation:
#VAR(rt) = 4.424e-06+4.949e-02a^2+9.390e-01(o')^2
#           (1.259e-6)  (8.096e-03) (1.016e-2)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

0
Q1m2 = garchFit(~garch(1,1),data=rt,cond.dist="std",trace=F)
summary(Q1m2)

plot(Q1m2)
#13


#Question 1.c
#From the QQ-plot, the model is not adequate because the normality assumption is not accepted

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#Question 1.e
#mean equation:
#rt = 5.901e-4
#     (2.703e-04)
#variance equation:
#VAR(rt) =4.214e-06 +7.239e-02 a^2+9.203e-01(o')^2
#           ( 1.574e-06)  (1.375e-02) (1.474e-02)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------


#1.f
pm2 = predict(Q1m2,5)
pm2

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#1.g
qstd(0.975,nu=5.1)
ucl = pm2$meanForecast+1.99*pm2$standardDeviation
lcl = pm2$meanForecast-1.99*pm2$standardDeviation
CI = cbind(ucl,lcl)
CI

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#Question2

da1= read.table("m-kovw-5116.txt", header = T)
head(da1)
tail(da1)
dim(da1)
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#2.a

ko = log(da1$ko+1)
ko
t.test(ko)
Box.test(ko,lag=12,type="Ljung")
acf(ko,lag.max =20)
#H0 = E(rt)=0
#Ha= E(rt)!= 0
#P-value is less than 0.05, therefore, the H0 is rejected. 
#As for Box test, P-value is 0.147, therefore, there is no serial correlations in returns.

at = ko-mean(ko)
Box.test(at^2,lag=12,type="Ljung")
#since p-value is less than 0.05,therefore, there is ARCH effect in CI

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#2.b



Q2m1 = garchFit(~arma(1,0)+garch(1,1),data=ko,trace=F)
summary(Q2m1)

plot(Q2m1)

#13
#mean equation:
#rt = 1.125e-02-2.634e-02  
#     (1.897e-03)(3.881e-02)
#variance equation:
#VAR(rt) =1.811e-04  +9.535e-02  a^2+8.486e-01(o')^2
#           (5.852e-05)  (1.915e-02) (2.766e-02)


#mean equation and volatility are adequate but QQ-plot is not


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#2.c

Q2m2 = garchFit(~arma(1,0)+garch(1,1),cond.dist = "std",data=ko,trace=F)
summary(Q2m2)
plot(Q2m2)
#

#mean equation:
#rt = 1.124e-02-1.888e-02  
#     (1.810e-03)(3.691e-02)
#variance equation:
#VAR(rt) =1.739e-04  +9.643e-02  a^2+8.504e-01(o')^2
#           (6.596e-05)  (2.338e-02) (3.267e-02)

#mean equation and volatility and  QQ-plot are adequate


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#2.d

Q2m3 = garchFit(~garch(1,1),data = ko, trace =F)
plot(Q2m3)
#13
summary(Q2m3)
# this model is not adequate because the normality assumption is not accepted in QQ-plot

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#2.e

Q2m4 = garchFit(~garch(1,1),data = ko, cond.dist = "std",trace =F)
plot(Q2m4)
#13

summary(Q2m4)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#2.f
#we compare the models by measuring the BIC. We select m4 because it has the least BIC out of other models and the model is adequate.

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#question 3



getSymbols("^GSPC", from="2005-01-02",to="2021-03-31")

head(GSPC)
tail(GSPC)
dim(GSPC)


rt2 <- diff(log(as.numeric(GSPC[,6])))
t.test(rt2)
acf(rt2,lag.max=20)
Q3m1 = garchFit(~arma(0,1)+garch(1,1),data=rt2,trace=F)
Q3m2 = garchFit(~arma(0,1)+garch(1,1),data=rt2,cond.dist = "std",trace=F)

summary(Q3m1)
plot(Q3m1)
#13


summary(Q3m2)
plot(Q3m2)
Box.test(rt2,lag=10,type="Ljung")
#13



Q3pm2 = predict(Q3m2,5)
Q3pm2
#Question 3.a
#mean equation:
#rt = 8.434e-04
#     (1.047e-04  )
#variance equation:
#VAR(rt) = 1.750e-06 +4.1.434e-01 a^2+8.540e-01 (o')^2
#           (3.696e-07)  (1.476e-02 ) (1.301e-02)

#Question 3.b

#mean equation:
#rt = 8.434e-04 
#     (1.047e-04 )
#variance equation:
#VAR(rt) = 1.750e-06+4.1.434e-01a^2+8.540e-01 (o')^2
#           (3.696e-07)  (1.476e-02 ) (1.301e-02)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------



#Question4



da3 = read.table("m-deciles.txt",head= T)
head(da3)
tail(da3)
dim(da3)
Sim_rt4= da3$CAP9RET
head(Sim_rt4)
Sim_rt4 = log(1+Sim_rt4)
acf(Sim_rt4,lag.max =20)
t.test(Sim_rt4)
Box.test(Sim_rt4,lag=12,type="Ljung")
#4.a
#H0:u=0
#Ha:u !=0
#The t-value is  greater than 1.96, therefore, The expected value of log return is not 0
#From box test p-value is less than 0.05, there is ArCh effect or serial correlation

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#4.b

at = Sim_rt4-mean(Sim_rt4)
Box.test(at^2,lag=10,type="Ljung")
acf(at^2,lag.max =20)

#the p-value is less than 0.05, therefore, there is no ARCH effect in log return

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#4.c

Q4m1 = garchFit(~arma(1,0)+garch(1,0),data = Sim_rt4,trace =F)
summary(Q4m1)

#mean equation:
#rt = 0.0105300   + 0.1470670rt-1
#     (0.0019275)   (0.0424301)
#variance equation:
#VAR(rt) = 0.0020000+4.0.1815182a^2
#           (0.0424301)  (0.0651142 )


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#4.d

Q4m2 = garchFit(~arma(1,0)+garch(1,0),data=Sim_rt4,cond.dist = "std",trace=F)
summary(Q4m2)

#mean equation:
#rt = 0.0116189  + 0.1076982rt-1
#     (0.0017710)   (0.0403169)
#variance equation:
#VAR(rt) = 0.0019203+0.1900830a^2
#           (0.0001818)  (0.0713692)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#4.e

Q4m3 = garchFit(~garch(1,0),data=Sim_rt4,trace = F)
summary(Q4m3)
#rt = 0.012346
#     (.001923)
#variance equation:
#VAR(rt) = 0.002016+0.194126a^2
#           (0.000145)  (0.062209)


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#4.f

Q4m4 = garchFit(~garch(1,1),data = Sim_rt4, cond.dist = "std",trace =F)
summary(Q4m4)


#rt = 1.253e-02
#     (1.538e-03)
#variance equation:
#VAR(rt) = 1.301e-04+1.144e-01a^2+8.367e-01(o')^2
#           (5.817e-05)  (3.494e-02)(4.591e-02)

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#4.g

#AIC, the model Q4m4 is the most proffered because it has th least amount of BIC