#EE435 Wasin Siwasarit  Lecture1  Spring/2019
setwd("/Users/wasin_siwasarit/Desktop/EE435")
cat(rep("\n",50))  #clear R Console
#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("^GSPC",from="2000-01-03",to="2020-01-13")
dim(GSPC)   
head(GSPC) 
tail(GSPC)
da=GSPC
chartSeries(GSPC,theme="white") 
price=da[,6]
plot(price,type='l')
logprice=log(price)
plot(logprice,type='l')
logreturn=diff(log(price))
simplereturn <-exp(logreturn)-1
#1 Plot the series of log return and simple return

par(mfrow=c(1,1))
plot(logreturn,type='l')
plot(simplereturn)

newlogreturn <- logreturn[2:nrow(logreturn),]
newsimplereturn <- simplereturn[2:nrow(logreturn),]

#2 Histogram and sample statistics
hist(logreturn, breaks=100, col="slateblue")
chart.Histogram(logreturn,methods = c("add.normal"))
table.Stats(logreturn)

#3 QQ-plots and tests for normality
#
# use qqnorm function 
par(mfrow=c(1,1))
qqnorm(newlogreturn)
qqline(newlogreturn, col = 2)
jarque.bera.test(newlogreturn)



#4 Test mean = 0
t.test(newlogreturn)

#5 Test Skewness = 0
T=length(newlogreturn)
s3=skewness(newlogreturn)
s3
tst = s3/sqrt(6/T)  
tst
pv = 2*pnorm(tst)
pv

#6 Test excess kurtosis =0
k4 = kurtosis(newlogreturn)
k4
tst = k4/sqrt(24/T)  
tst
pv = 2*(1-pnorm(tst))
pv
