setwd("/Users/Orachai/Documents/BE/4TH YEAR 2ND SEMESTER/EE435")
cat(rep("\n",50)) 
library(quantmod) 
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)
getSymbols("GOOG",from="2004-08-19",to="2021-01-01")
da=GOOG

chartSeries(GOOG,theme="white")

price=da[,6] 
logprice=log(price) 
plot(logprice,type='l')
logreturn=diff(log(price)) 
simplereturn <-exp(logreturn)-1 

par("mar")
par(mar=c(1,1,1,1))
par(mfrow=c(2,1)) 
plot(logreturn,type='l')
plot(simplereturn)
newlogreturn <- logreturn[2:nrow(logreturn),] 

newsimplereturn <- simplereturn[2:nrow(logreturn),]
table.Stats(logreturn)
par(mfrow=c(1,1))
hist(logreturn, breaks=100, col="slateblue")
# use qqnorm function 
par(mfrow=c(1,1))
qqnorm(newlogreturn) #test whether the return has nd. or not
qqline(newlogreturn, col = 2)
jarque.bera.test(newlogreturn)

#4 Test mean = 0.08
t.test(newlogreturn)
#the HO: the mean of the log return = 0: R(r_t)=0.08
#H1: The mean of the log return is not = 0.08

#5 Test Skewness = 0
T=length(newlogreturn) #create the length of the new log return

s3=skewness(newlogreturn)
tst = s3/sqrt(6/T)  
tst

pv = 2*(1-pnorm(tst)) #if the tst is positive, use this command instead
pv

#6 Test excess kurtosis =0
k4 = kurtosis(newlogreturn)
tst = k4/sqrt(24/T)  
tst
pv = 2*(1-pnorm(tst)) #if the tst is positive, use this command instead
pv