# EE435 Assigment 2 ID:6104640112
setwd("/Users/jharit/Desktop")
cat(rep("\n",50))  #clear R Console
library(quantmod) 
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)
getSymbols("CAT",from="2000-01-03",to="2021-01-31")
getSymbols("AOT.BK",from="2000-01-03",to="2021-01-31")
#get each adjusted price for CAT and AOT
PriceC=CAT[,6] 
PriceA=AOT.BK[,6]
logPriceC=log(PriceC)
logPriceA=log(PriceA)

#answer for question a.
#calculate for returns
logreturnC=diff(log(PriceC))
logreturnA=diff(log(PriceA))
simplereturnC=exp(logreturnC)-1
simplereturnA=exp(logreturnA)-1

#ploting for CAT return
par(mfrow=c(2,1))
plot(logreturnC,type='l')
plot(simplereturnC)

#ploting for AOT return
par(mfrow=c(2,1))
plot(logreturnA,type='l')
plot(simplereturnA)

#answer for question b.
#calculate for simplereturn statistics 
table.Stats(simplereturnC)
table.Stats(simplereturnA)

#answer for question c.
#ploting the density function
par(mfrow=c(1,1))
hist(simplereturnC, breaks=100, col="slateblue")
chart.Histogram(simplereturnC,methods = c("add.normal"))
#test for normality
newsimplereturnC <- simplereturnC[2:nrow(simplereturnC),]
jarque.bera.test(newsimplereturnC)

#answer for question d.
#calculate for logreturn statistics 
table.Stats(logreturnC)
table.Stats(logreturnA)

#answer for question e.
# Ho: logreturnC = 0
newlogreturnC <- logreturnC[2:nrow(logreturnC),]
t.test(newlogreturnC)
# Ho: logreturnA = 0
newlogreturnA <- logreturnA[2:nrow(logreturnA),]
t.test(newlogreturnA)

#answer for question f.
#ploting the density function
par(mfrow=c(2,1))
hist(logreturnC, breaks=100, col="slateblue")
hist(logreturnA, breaks=100, col="slateblue")

#answer for question g.
#construct the CI 95% using the information from t-test
t.test(newlogreturnC)

#answer for question h.
#Test Skewness = 0 CAT logreturn
TC=length(newlogreturnC)
SC=skewness(newlogreturnC)
tstC = SC/sqrt(6/TC)  
tstC
pvC = 2*pnorm(tstC)
pvC

#Test Skewness = 0 AOT logreturn
TA=length(newlogreturnA)
SA=skewness(newlogreturnA)
tstA = SA/sqrt(6/TA)  
tstA
pvA = 2*(1-pnorm(tstA))
pvA

#answer for question i.
# Test excess kurtosis =0 CAT logreturn
KC = kurtosis(newlogreturnC)
tstC = KC/sqrt(24/TC)  
tstC
pvC = 2*(1-pnorm(tstC))
pvC
# Test excess kurtosis =0 AOT logreturn
KA = kurtosis(newlogreturnA)
tstA = KA/sqrt(24/TA)  
tstA
pvA = 2*(1-pnorm(tstA))
pvA
