setwd("C:/Work/University/EE435/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("CAT",from="2000-01-03",to="2021-01-31")
dim(CAT)   
head(CAT) 
tail(CAT)
dataCAT=CAT
chartSeries(CAT,theme="balck") 
price=dataCAT[,6]
plot(price,type='l')
logprice=log(price)
plot(logprice,type='l')
logreturn=diff(log(price))
simplereturn <-exp(logreturn)-1
plot(logreturn,type='l')

#1 Plot the series of log return and simple return

par(mfrow=c(2,1))
plot(logreturn,type='l')
plot(simplereturn)

newlogreturn <- logreturn[2:nrow(logreturn),]
newsimplereturn <- simplereturn[2:nrow(logreturn),]

#2 Histogram and sample statistics
par(mfrow=c(2,1))
hist(simplereturn, breaks=100, col="slateblue")
chart.Histogram(simplereturn,methods = c("add.normal"))
table.Stats(logreturn)

#3 QQ-plots and tests for normality
#
# use qqnorm function 
par(mfrow=c(1,1))
qqnorm(newsimplereturn)
qqline(newsimplereturn, col = 2)
jarque.bera.test(newsimplereturn)


#4 Test mean = 0
t.test(logreturn)

#5 Test Skewness = 0
T=length(newlogreturn)
s3=skewness(newlogreturn)
tst = s3/sqrt(6/T)  
tst
pv = 2*pnorm(tst)
pv

#6 Test excess kurtosis =3
k4 = kurtosis(newlogreturn)
tst = k4/sqrt(24/T)  
tst
pv = 2*(1-pnorm(tst))
pv


