
#Direction and clear cache
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

library(quantmod)
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)
library(fGarch)
library(tinytex)
#Basic libraries

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

getSymbols("GOOG", from="2004-08-19",to="2021-01-01")

dim(GOOG)
head(GOOG)
tail(GOOG)

#check dim, head, and tail
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

da_GOOG= GOOG

price_GOOG=da_GOOG[,6]

dim(price_GOOG)
head(price_GOOG)
tail(price_GOOG)
#create data sets and price
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------




par(mfrow=c(2,1))
logreturn_GOOG=diff((log(price_GOOG)))
logprice_GOOG=log(price_GOOG) 

plot(logprice_GOOG,type='l')
plot(price_GOOG,type='l') 


simplereturn_GOOG <-exp(logreturn_GOOG)-1
#These codes answer question a)


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------


table.Stats(logreturn_GOOG)
#These codes answer question b)


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#QQ-plots and tests for normality
newlogreturn_GOOG <- logreturn_GOOG[2:nrow(logreturn_GOOG)]


newsimplereturn_GOOG <- simplereturn_GOOG[2:nrow(logreturn_GOOG)]


par(mfrow=c(1,1))
qqnorm((newlogreturn_GOOG))
qqline(newlogreturn_GOOG, col = 2)
jarque.bera.test(newlogreturn_GOOG)


#These codes answer c)
#The data is heavy tailed, therefore the extreme values are more than what we are expected from the normal distribution 

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------

#Test mean = 0.08

t.test(logreturn_GOOG)

#These codes answer d) and e)

#H_0: E(r) = 0.08
#H_a: E(r) != 0.08
#The t value of logreturn_GOOG is less than the critical point,~2. The p-value is not significant at 95% level.
#Therefore we cannot reject that the mean of log GOOD is equal to 0
#The 95% confidence interval is between -6.513168e-05 and 1.041069e-03

#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#Test Skewness <= 0
#H_0: m_3 <= 0
#H_a: m_3 >  0

T_GOOG=length(newlogreturn_GOOG)
S_GOOG= skewness(newlogreturn_GOOG)
tstS_GOOG = S_GOOG/sqrt(6/T)
tstS_GOOG

pvS_GOOG = (pnorm(tstS_GOOG))
pvS_GOOG
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#These codes answer f)

#The t value of newlogreturn_GOOG is less than the critical point,~2. The p-value is not significant at 95% level.
#Therefore we cannot reject that the sknewness of GOOD is less than or equal to 0


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------


#Test excess kurtosis = 0

#H_0: K  =  3
#H_a: K !=  3

k_GOOG = kurtosis(newlogreturn_GOOG)
tstK_GOOG = k_GOOG/sqrt(24/T)
tstK_GOOG

pv_GOOG = 2*(1-pnorm(tstK_GOOG))
pv_GOOG

#These codes answer i)

#The t value of newlogreturn_GOOG is less than the critical point,~2. The p-value is not significant at 95% level.
#Therefore we cannot reject that the kurtosis of GOOG is equal to 3


#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------
