# install packages and load libraries
install.packages("quantmod")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
install.packages("rugarch")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(rugarch)
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
##
## sigma
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
# Get Google stock data from Yahoo Finance (GOOG)
getSymbols("GOOG", from = Sys.Date() - years(5), to = Sys.Date(), src="yahoo")
## [1] "GOOG"
# Extract the closing price
google_close <- Cl(GOOG)
# Compute Log returns and plot
google_returns <- na.omit(diff(log(google_close)))
plot(google_returns, main = "Log Returns of GOOG", col = "blue")

# Define the GARCH(1,1)
spec <- ugarchspec(
variance.model = list(model = "sGARCH", garchOrder = c(1,1)),
mean.model = list(armaOrder = c(0,0), include.mean = TRUE),
distribution.model = "std"
)
# Fit Garch MOdel
fit <- ugarchfit(spec = spec, data = google_returns)
show(fit)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(0,0,0)
## Distribution : std
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001537 0.000455 3.37858 0.000729
## omega 0.000003 0.000003 0.85385 0.393188
## alpha1 0.032516 0.008096 4.01619 0.000059
## beta1 0.963363 0.007956 121.08558 0.000000
## shape 4.315832 0.681257 6.33511 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001537 0.000394 3.90290 0.000095
## omega 0.000003 0.000011 0.24047 0.809969
## alpha1 0.032516 0.020559 1.58155 0.113752
## beta1 0.963363 0.022350 43.10415 0.000000
## shape 4.315832 1.494920 2.88700 0.003889
##
## LogLikelihood : 3249.895
##
## Information Criteria
## ------------------------------------
##
## Akaike -5.1711
## Bayes -5.1507
## Shibata -5.1712
## Hannan-Quinn -5.1635
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.4699 0.4930
## Lag[2*(p+q)+(p+q)-1][2] 0.5260 0.6815
## Lag[4*(p+q)+(p+q)-1][5] 4.9518 0.1570
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.5082 0.4759
## Lag[2*(p+q)+(p+q)-1][5] 0.9929 0.8613
## Lag[4*(p+q)+(p+q)-1][9] 1.3903 0.9640
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.05343 0.500 2.000 0.8172
## ARCH Lag[5] 0.07521 1.440 1.667 0.9912
## ARCH Lag[7] 0.39022 2.315 1.543 0.9872
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 40.2297
## Individual Statistics:
## mu 0.1279
## omega 3.5353
## alpha1 0.2102
## beta1 0.1772
## shape 0.2691
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.28 1.47 1.88
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.1263 0.8995
## Negative Sign Bias 0.4611 0.6448
## Positive Sign Bias 1.1784 0.2389
## Joint Effect 2.0508 0.5619
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 19.06 0.4533
## 2 30 28.88 0.4713
## 3 40 32.94 0.7419
## 4 50 48.78 0.4818
##
##
## Elapsed time : 0.7600656
# Plot volatility
plot(fit, which = "all")
##
## please wait...calculating quantiles...

# Forecasting volatility
forecast <- ugarchforecast(fit, n.ahead = 20)
plot(forecast, which = 1)

plot(forecast, which = 3)
