#Assignment 2
setwd("C:/R")
#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)

getSymbols("AOT.BK",from="2000-01-03",to="2021-01-31")
getSymbols("CAT",from="2000-01-03",to="2021-01-31")
price_AOT = AOT.BK[,6]
price_CAT = CAT[,6]
logprice_AOT = log(price_AOT)
logprice_CAT = log(price_CAT)
logreturn_AOT = diff(logprice_AOT)
logreturn_CAT = diff(logprice_CAT)
simplereturn_AOT <- exp(logreturn_AOT)-1
simplereturn_CAT <- exp(logreturn_CAT)-1

#a) plot AOT Return
par(mfrow=c(2,1))
plot(logreturn_AOT,type='l')
plot(simplereturn_AOT,type='l')

#a) plot CAT Return
par(mfrow=c(2,1))
plot(logreturn_CAT,type='l')
plot(simplereturn_CAT,type='l')

#b) sample statistics for simple return
table.Stats(simplereturn_AOT)
table.Stats(simplereturn_CAT)

#new return
nlogreturn_AOT = logreturn_AOT[2:nrow(logreturn_AOT)]
nlogreturn_CAT = logreturn_CAT[2:nrow(logreturn_CAT)]
nsimplereturn_AOT = simplereturn_AOT[2:nrow(logreturn_AOT)]
nsimplereturn_CAT = simplereturn_CAT[2:nrow(logreturn_CAT)]

#c) test for normal distribution
jarque.bera.test(nsimplereturn_CAT)

#d) sample statistics for log return
table.Stats(logreturn_AOT)
table.Stats(logreturn_CAT)

#e) hypotesis testing Test mean = 0
t.test(nlogreturn_CAT)
t.test(nlogreturn_AOT)

#f) density plot of daily log return
par(mfrow=c(1,1))
chart.Histogram(logreturn_AOT, methods = c("add.normal"))
chart.Histogram(logreturn_CAT, methods = c("add.normal"))

#g) 95% confidence interval for daily log return
t.test(nlogreturn_CAT)

#h) Test skewness = 0
T=length(nlogreturn_AOT)
m3 = skewness(nlogreturn_AOT)
tst = m3/sqrt(6/T)
tst

#i) Test excess kurtosis = 0
K=kurtosis(nlogreturn_AOT)
K
tst_K = K/sqrt(24/T)
tst_K

# test skewness and kurtosis of CAT stock
T_CAT=length(nlogreturn_CAT)
m3_CAT=skewness(nlogreturn_CAT)
tst_CAT = m3_CAT/sqrt(6/T_CAT)
tst_CAT
K_CAT=kurtosis(nlogreturn_CAT)
tst_K_CAT = K_CAT/sqrt(24/T_CAT)
tst_K_CAT