#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)

#GOOG
getSymbols("GOOG",from="2004-08-19",to="2021-01-01")
dim(GOOG)   
head(GOOG) 
tail(GOOG)
da=GOOG
chartSeries(GOOG,theme="white") 
price=da[,6]
plot(price,type='l')
logprice=log(price)
plot(logprice,type='l')
logreturn=diff(log(price))
simplereturn <-exp(logreturn)-1

#a [GOOG] Plot the series of log return and simple return of CAT
par(mfrow=c(2,1))
plot(logreturn,type='l')
plot(simplereturn)

newlogreturn <- logreturn[2:nrow(logreturn),]
newsimplereturn <- simplereturn[2:nrow(logreturn),]

#b CAT (simple return) sample statistics
table.Stats(simplereturn)

#c GOOG Simple EDF & Tests for normality
par(mfrow=c(1,1))
hist(logreturn, breaks=100, col="red")
chart.Histogram(newlogreturn,methods = c("add.normal"))
jarque.bera.test(newlogreturn)

#d&e GOOG (logreturn) sample statistics & (95% CI)
t.test(newlogreturn,mu=0.08)


#f Test the skewness of the return ()
#GOOG
T=length(newlogreturn)
s3=skewness(newlogreturn)
tst = s3/sqrt(6/T)  
tst
pv = 2*pnorm(tst)
pv

#i Test the kurtosis (Excess Kurtosis = 0)
#GOOG
k4 = kurtosis(newlogreturn)
tst = k4/sqrt(24/T)  
tst
pv = 2*(1-pnorm(tst))
pv

