#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 DAta
getSymbols("CAT",from="2000-01-03",to="2021-01-31")
dim(CAT)   
head(CAT) 
tail(CAT)
da1=CAT
chartSeries(CAT,theme="white") 
price1=da1[,6]
plot(price1,type='l')
logprice1=log(price1)
plot(logprice1,type='l')
logreturn1=diff(log(price1))
simplereturn1 <-exp(logreturn1)-1

#AOT.BK Data
getSymbols("AOT.BK",from="2000-01-03",to="2021-01-31")
dim(AOT.BK)   
head(AOT.BK) 
tail(AOT.BK)
da2=AOT.BK
chartSeries(AOT.BK,theme="white") 
price2=da2[,6]
plot(price2,type='l')
logprice2=log(price2)
plot(logprice2,type='l')
logreturn2=diff(log(price2))
simplereturn2 <-exp(logreturn2)-1

#(a.1) The figure of log returns and simple returns of CAT
par(mfrow=c(2,1))
plot(logreturn1,type='l')
plot(simplereturn1)

#(a.2) The figure of log returns and simple returns of AOT.BK
par(mfrow=c(2,1))
plot(logreturn2,type='l')
plot(simplereturn2)

#(b.1)table.Stats of CAT
newlogreturn1 <- logreturn1[2:nrow(logreturn1),]
newsimplereturn1 <- simplereturn1[2:nrow(logreturn1),]
table.Stats(newsimplereturn1)

#(b.2)table.Stats of AOT.BK
newlogreturn2 <- logreturn2[2:nrow(logreturn2),]
newsimplereturn2 <- simplereturn2[2:nrow(logreturn2),]
table.Stats(newsimplereturn2)

#(C.1) CAT
jarque.bera.test(newsimplereturn1)
par(mfrow=c(1,1))
hist(logreturn1, breaks=100, col="slateblue")
chart.Histogram(logreturn1,methods = c("add.normal"))

#(D.1) CAT
table.Stats(newlogreturn1)

#(D.2) AOT.BK
table.Stats(newlogreturn2)

#(E.1) t.test CAT(newlogreturn1)
t.test(newlogreturn1)

#(E.2) t.test AOT.BK
t.test(newlogreturn2)

#(F) AOT density plot
par(mfrow=c(1,1))
hist(logreturn2, breaks=100, col="slateblue")
chart.Histogram(logreturn2,methods = c("add.normal"))

#(H.1) CAT Test Skewness =0 
T=length(newlogreturn1)
m3=skewness(newlogreturn1)
tst=m3/sqrt(6/T)
tst
pv = 2*pnorm(tst)
pv

#(H.2) AOT.BK Test Skewness =0 
T=length(newlogreturn2)
m3=skewness(newlogreturn2)
tst=m3/sqrt(6/T)
tst
pv = 2*(1-pnorm(tst))
pv

#(i.1) Test excess kurtosis =0
k4 = kurtosis(newlogreturn1)
tst = k4/sqrt(24/T)  
tst
pv = 2*(1-pnorm(tst))
pv

#(i.2) Test excess kurtosis =0
k4 = kurtosis(newlogreturn2)
tst = k4/sqrt(24/T)  
tst
pv = 2*(1-pnorm(tst))
pv

