#ee435 assignment 5 by Jaruvit Chantana 6104641482
library(quantmod)
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)
#install.packages("fGarch")
library(fGarch)
#question1 download data first
getSymbols("CAT",from="2006-01-03",to="2017-04-13")
rt <- diff(log(as.numeric(CAT[,6])))
#1a are there anny serial corr in the log return series rt?
pacf(rt)
#optimal lag seems to be the 10th
Box.test(rt,lag=10,type='Ljung')
#as p-value = 0.09159 which is > than alpha= 0.05
#so we cannot reject then H0 that there is no corr among them

#1b ARCH effect check
Box.test(rt^2,lag=10,type="Ljung")
#since p-value < 0.05 we reject H0
#meaning that there's an ARCH effect

#1c
m1 <- garchFit(~arma(1,0)+garch(1,1), data = rt,trace=F)
summary(m1)
plot(m1 , which = 13)
#mean equation rt= 4.8298e-04 + 1.687e-02rt-1 with std. error (3.075e-04) and(2.006e-02) respectively
#volatility eq sigma^2t= 4.478e-06 + 4.972e-02 + 9.387e-01 with std error (1.278e-06) and (8.191e-03) and (1.031e-02) respectively
#as seen, with the qq-plot, the model is not adequate as the normality assumption is rejected
#1d
m2 <- garchFit(~garch(1,1), data = rt,trace=F, cond.dist = "std")
summary(m2)
#seems to be adequate now

#1e fitted model
#I choose m2 due to the lower value in information criteria
#mean eq rt= 5.978e-04 with std error (2.702e-04)
#volatility eq = sigma^2t= 4.203e-06 + 7.238e-02 + 9.203e-01 with std error (1.571e-06) and (1.374e-02) and (1.472e-02) respectively

#1f 1to5 step ahead forecasting
predict(m2,5)

#1g
predict(m2,5,interval="confidence", level=0.95)


#question2
KOdata = read.table(file.choose(), header = T)
#In my computer, I don't know why i cant type the name of file directly i need to use this instead so if it cant run in your computer im so sorry
KOrt <- (log(as.numeric(KOdata[,3]+1)))
#2a
t.test(KOrt)
pacf(KOrt)
Box.test(KOrt,lag=10,type='Ljung')
#cannot reject H0 there is no corr
Box.test(KOrt^2,lag=10,type='Ljung')
#reject H0 there is an ARCH effect

#2b
m3 <- garchFit(~arma(1,0)+garch(1,1), data = KOrt,trace=F)
summary(m3)
plot(m3, which=13)
#mean eq = rt = 1.125e-02 - 2.634e-02rt-1 with std error (1.897e-03) and (3.881e-02) respectively
#volatility eq = sigma^2t = 1.811e-04 + 9.535e-02 +8.486e-01 with std error (5.852e-05) and (1.915e-02) and (2.766e-02) respectively
#with the qq-plot normality assumption is rejected so this model is not adequate

#2c
m4 <- garchFit(~arma(1,0)+garch(1,1), data = KOrt,trace=F, cond.dist = "std")
summary(m4)
plot(m4, which=13)
#mean eq = rt = 1.124e-02 - 1.888e-02rt-1 with std error (1.810e-03) and (3.691e-02) respectively
#volatility eq = sigma^2t = 1.739e-04 + 9.643e-02 + 8.504e-01 with std error (6.596e-05) and (2.338e-02) and (3.267e-02) respectively
#the model seems to be adequate

#2d
m5 <- garchFit(~garch(1,1), data = KOrt,trace=F)
summary(m5)
plot(m5, which=13)
#mean eq = rt = 1.098e-02 with std error (1.84e-03)
#volatility eq = sigma^2t = 1.850e-04 + 9.480e-02 + 8.478e-01 with std error (5.899-05) and (1.912e-02) and (2.787e-02) respectively
#with qq-plot again this model seems not to be adequate

#2e
m6 <- garchFit(~garch(1,1), data = KOrt,trace=F, cond.dist = "std")
summary(m6)
plot(m6, which=13)
#mean eq = 1.105e-02 with std error (1.757e-03)
#volatility eq = sigma^2t = 1.753e-04 + 9.633e-02 + 8.501e-01 with std error (6.627e-05) and (2.337e-02) and (3.277e-02) respectively
#the model seems adequate

#2f
# i m6 as the smallest BIC provided and the adequacy of the model


#3
getSymbols("^GSPC",from="2005-01-02",to="2021-03-31")
SPrt <- diff(log(as.numeric(GSPC[,6])))

#3a
t.test(SPrt)
#pvalue > alpha we cannot reject H0 that mean is 0
pacf(SPrt)
Box.test(SPrt,lag=10,type='Ljung')
#reject H0 there are some corrs among them

#3b
m7 = auto.arima(SPrt)
m7
pacf(m7$residuals^2)
Box.test(m7$residuals^2,lag=10,type='Ljung')
m8 <- garchFit(~arma(1,0)+garch(1,1), data = SPrt,trace=F)
summary(m8)
plot(m8 , which =13)
#mean eq = 7.413e-04 - 7.541e-02rt-1 with std error (1.178e-04) and (1.730e-02) respectively
#volatility eq = sigma^2t = 2.717e-06 + 1.418e-01 + 8.374e-01 with std error (3.504e-07) and (1.202e-02) and (1.196e-02) respectively


#3c
m9 <- garchFit(~arma(1,0)+garch(1,1), data = SPrt,trace=F,cond.dist = "std")
summary(m9)
#mean eq = 9.136e-04 - 7.019e-02 with std error 1.061e-04 and 1.570e-02 respectively
#volatility eq = sigma^2t = 1.695e-06 + 1.413e-01 + 8.566e-01 with std error 3.624e-07 and 1.460e-02 and 1.286e-02 respectively


#3d
predict(m9)


#4
Deciledata = read.table(file.choose(), header = T)
#again in my computer it says cannot open file 'm-deciles.txt': No such file or directory even though I already downloaded the file so I need to use this code 
DCrt = log(Deciledata[,10]+1)

#4a
t.test(DCrt)
#reject H0 mean is not equal  to 0
Box.test(DCrt,lag=10,type='Ljung')
#reject H0 there are some corrs
m10 <- auto.arima(DCrt)
m10

#4b
Box.test(DCrt^2,lag=10,type='Ljung')
#pvalue < alpha reject H0 there is arch effect

#4c
m11 <- garchFit(~arma(1,0)+garch(1,0), data= DCrt,trace=F)
summary(m11)
plot(m11, which=13)
#mean eq = 0.0105300 + 0.1470670rt-1 with std error 0.0019275 and 0.0424301 respectively
#volatility eq = sigma^2t = 0.002 + 0.1815182 with std error 0.0001482 and  0.0651142 respectively

#4d
m12 <- garchFit(~arma(1,0)+garch(1,0),data = DCrt,trace=F, cond.dist ='std')
summary(m12)
plot(m12, which=13)
#mean eq = 0.0116189 + 0.1076982rt-1 with std error 0.0017710 and 0.0403169 respectively
#volatility eq = sigma^2t = 0.00119203 + 0.1900830 with std error 0.0001818 and 0.0713692 respectively

#4e
m13 <- garchFit(~garch(1,0), data = DCrt,trace=F)
summary(m13)
plot(m13, which=13)
#mean eq = 0.012346 with std error 0.001923
#volatility eq = sigma^2t = 0.002016 + 0.194126 with std error 0.000145 and 0.062209 respectively

#4f
m14 <- garchFit(~garch(1,0), data = DCrt,trace=F, cond.dist ='std')
summary(m14)
plot(m14, which=13)
#mean eq =  0.013356 with std error 0.001685
#volatility eq = sigma^2t = 0.001928 + 0.204163 with std error 0.000185 and 0.072375

#4g
#analyzing from information criteria I choose the one with lowest AIC and BIC which is m12

update.packages(ask = FALSE, checkBuilt = TRUE)
tinytex::tlmgr_update()
tinytex::reinstall_tinytex()
options(tinytex.verbose = TRUE)
#install.packages("knitr")
#install.packages("pdfcrop")
#install.packages("ghostcript")
