#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")
da=GOOG
GOOG_price=da[,6]
logprice=log(GOOG_price)
logreturn=diff(log(GOOG_price))
par(mfrow=c(2,1))
plot(logreturn,type='l')
plot(GOOG)


#2 GOOG's  log return mean, variance, standard deviation, skewness, excess kurtosis, minimum and maximum
table.Stats(logreturn)


#3
newlogreturn <- logreturn[2:nrow(logreturn),]
par(mfrow=c(1,1))
qqnorm(newlogreturn)
qqline(newlogreturn, col = 2)
jarque.bera.test(newlogreturn)
#since the calculated p-value=2.2e-16 which is less than alpha=0.05. Thus, GOOG's log return data is not normally distributed

#4 Test for GOOG's mean=0 given H0 :E(r) = 0.8, H1 : E(r) =/ 0.8
t.test(newlogreturn)
#The calculated t = 2.8957 is greater than critical value = 1.96. Therefore, we can reject null hypothesis : E(r) = 0.8 at 95% confidence interval

#5
table.Stats(logreturn)
qnorm(.95)
qnorm(0.05)

#7 Test Skewness = 0
T1=length(newlogreturn)
GOOG_s3=skewness(newlogreturn)
GOOG_s3
tst = GOOG_s3/sqrt(6/T1)
tst
pv1 = 2*pnorm(tst)
pv1
# Since the calculated s* = 11.8664 is greater than 1.96 (critical  value) we can reject null hypothesis = H0:m3 is less than or equal to 0 at 95% confidence interval

#9 Test excess kurtosis = 0
GOOG_k4 = kurtosis(newlogreturn)
GOOG_k4
tst3 = GOOG_k4/sqrt(24/T1)
tst3
pv3 = 2*(1-pnorm(tst3))
pv3
# since the calculated p-value = 0 which is less than alpha = 0.05. we can reject null hypothesis : k = 3 at 95% confidence interval
#install.packages("tinytex")
library(tinytex)
