#Krittaphong Kaewvichian ID 6104640609
setwd("C:/Users/kritt/Desktop/EE435-R")
cat(rep("\n",50))  #clear R Console

#install.packages("tinytex")
#install.packages("quantmod")  
#install.packages("fBasics") 
#install.packages("sn")  
#install.packages("PerformanceAnalytics") 
#install.packages("car") 
#install.packages("tseries")  
#install.packages("forecast") 
library(quantmod) 
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)

#CAT
getSymbols("CAT",from="2000-01-03",to="2021-01-31")
dim(CAT)   
head(CAT) 
tail(CAT)
daCAT=CAT
chartSeries(CAT,theme="white") 
priceCAT=daCAT[,6]
plot(priceCAT,type='l')
logpriceCAT=log(priceCAT)
plot(logpriceCAT,type='l')
logreturnCAT=diff(log(priceCAT))
simplereturnCAT <-exp(logreturnCAT)-1

#AOT.bk
getSymbols("AOT.bk",from="2000-01-03",to="2021-01-31")
dim(AOT.BK)   
head(AOT.BK) 
tail(AOT.BK)
daAOT=AOT.BK
chartSeries(AOT.BK,theme="white") 
priceAOT=daAOT[,6]
plot(priceAOT,type='l')
logpriceAOT=log(priceAOT)
plot(logpriceAOT,type='l')
logreturnAOT=diff(log(priceAOT))
simplereturnAOT <-exp(logreturnAOT)-1

#a [CAT] Plot the series of log return and simple return of CAT
par(mfrow=c(2,1))
plot(logreturnCAT,type='l')
plot(simplereturnCAT)

newlogreturnCAT <- logreturnCAT[2:nrow(logreturnCAT),]
newsimplereturnCAT <- simplereturnCAT[2:nrow(logreturnCAT),]

#a [AOT.BK] Plot the series of log return and simple return of CAT
par(mfrow=c(2,1))
plot(logreturnAOT,type='l')
plot(simplereturnAOT)

newlogreturnAOT <- logreturnAOT[2:nrow(logreturnAOT),]
newsimplereturnAOT <- simplereturnAOT[2:nrow(logreturnAOT),]

#b CAT (simple return) sample statistics
table.Stats(simplereturnCAT)

#b AOT (simple return) sample statistics
table.Stats(simplereturnAOT)

#c CAT Simple EDF & Tests for normality
par(mfrow=c(1,1))
hist(simplereturnCAT, breaks=100, col="red")
chart.Histogram(simplereturnCAT,methods = c("add.normal"))
jarque.bera.test(newsimplereturnCAT)

#c AOT Simple EDF & Tests for normality
hist(simplereturnAOT, breaks=100, col="red")
chart.Histogram(simplereturnAOT,methods = c("add.normal"))
jarque.bera.test(newsimplereturnAOT)

#d CAT (logreturn) sample statistics
table.Stats(logreturnCAT)

#d AOT (logreturn) sample statistics
table.Stats(logreturnAOT)

#e and g (CAT Test mean = 0)&(95% CI)
t.test(newlogreturnCAT)

#e and g (AOT Test mean = 0)&(95% CI)
t.test(newlogreturnAOT)

#f CAT log EDF & Tests for normality
hist(logreturnCAT, breaks=100, col="red")
chart.Histogram(logreturnCAT,methods = c("add.normal"))


#f AOT log EDF & Tests for normality
hist(logreturnAOT, breaks=100, col="red")
chart.Histogram(logreturnAOT,methods = c("add.normal"))

#h Test the skewness of the return ()
#CAT
T=length(newlogreturnCAT)
s3=skewness(newlogreturnCAT)
tstCATs = s3/sqrt(6/T)  
tstCATs
pvCATs = 2*pnorm(tstCATs)
pvCATs
#AOT
T=length(newlogreturnAOT)
s3=skewness(newlogreturnAOT)
tstAOTs = s3/sqrt(6/T)  
tstAOTs
pvAOTs = 2*pnorm(tstAOTs)
pvAOTs
#i Test the kurtosis (Excess Kurtosis = 0)
#CAT
k4 = kurtosis(newlogreturnCAT)
tstCATk = k4/sqrt(24/T)  
tstCATk
pvCATk = 2*(1-pnorm(tstCATk))
pvCATk

#AOT
k4 = kurtosis(newlogreturnAOT)
tstAOTk = k4/sqrt(24/T)  
tstAOTk
pvAOTk = 2*(1-pnorm(tstAOTk))
pvAOTk

