setwd("D:/")
library(quantmod) 
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)
library(fGarch)

getSymbols("ETH-USD", from="2015-01-01",to="2021-05-24")
data <- `ETH-USD`
# Propose the reseach paper
# I have read the paper of an empirical study on bitcoin using
# Garch and stochastic volatility models by Peter Jochumzen
# The paper concludes that the Garch(1,1) is the model that performs best
# regarding forecast accuracy compared to BEKK(1,1) and stochastic model

eth_t <- diff(log((as.numeric(data[,6]))))
dim(eth_t)
tail(eth_t)
eth_t <- eth_t[!is.na(eth_t)]
plot(eth_t, type='l')

#It seems like we have a stationary log series,
#However, we need to test

t.test(eth_t)
acf(eth_t)
pacf(eth_t)
# Since the computed value  falls into the rejection region (P-value<0.05).
# We reject the null hypothesis that is the mean is not zero at 0.05 level of
# confidence.

Box.test(eth_t,lag=10,type="Ljung")
# Since the computed value  falls into the rejection region.
# There is serial correlations in eth_t series, 
#That is we reject H0 at 0.05 level of confidence.


acf(eth_t^2)
# it seems like we could try to use lag-1
pacf(eth_t^2)
Box.test(eth_t^2,lag=10,type='Ljung')
# Since the computed value falls into the rejection region.
# There is arch in eth_tt, 
# That is we reject H0 at 0.05 level of confidence


m1=garchFit(formula = ~arma(1,0)+garch(1,1),data = eth_t, trace=F)
summary(m1)
#all variables significant at 0.05 level of significant
#Mean equations are not Adequate
#Variance equations are adequate
#AIC = -2.9221, BIC = -2.908769, but not normal
#No ARCH effect leftover

m2=garchFit(formula =~garch(1,1),data = eth_t,trace=F)
summary(m2)
#omega, alpha1,beta1 are sig at 0.05 level of confident
#Mean is Adequate at 0.1 significant level
#Variance is adequate at 0.01 significant level
#AIC = -2.765593, BIC = -2.754873, but not normal
#No arch effect leftover

#Redefine 
m3 = garchFit(formula =~arma(1,0)+garch(1,1),data = eth_t, cond.dist="std" ,trace=F)
summary(m3)

#ar1, omega,alpha1, and beta1 are sig at 0.05 level of confident with shape2.83e
#Mean equations are not adequate
#Variance equations are adequate
#AIC = -3.126167, BIC = -3.110088, sTD
#No arch effect leftover

m4 = garchFit(formula = ~garch(1,1),data = eth_t, cond.dist="std", trace=F)
summary(m4)
#all variables except mu are significant at 0.05 level of significant shape 2.93
#Mean equations are not adequate
#Variance equations are adequate
#AIC = -3.114375, BIC = -3.100976, STD
#No arch effect leftover


#Therefore among all model, we choose ,using AIC and BIC criteria, model m3
#M2 model, garch(1,1) with normal distribution. We prefer normal distribution.
#Since the mean and variance equations are both adequate at 0.1 level of
#significant

#The model equation
# rthat = 1.814e-03
# std error = (1.145e-03)

#The model variance:
# sigma^2that = 3.051e-04 + 2.258e-01(at^2) + 7.310e-01(sigma^2that)
# std error     (5.238e-05) (3.316e-02)        (3.061e-02)

predict(m2,1)
predict(m2,5)
predict(m2,99)


     