This dataset provides historical data of Alphabet Inc. (GOOG). The data is available at a daily level. Currency is USD.
library(astsa)
library(readr)
## Warning: package 'readr' was built under R version 4.4.3
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(quantmod)
## Loading required package: xts
## Loading required package: TTR
library(xts)
library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 4.4.3
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(rugarch)
## Warning: package 'rugarch' was built under R version 4.4.3
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
##
## sigma
GOOGLdata <- read_csv("GOOGL.csv")
## Rows: 4431 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (6): Open, High, Low, Close, Adj Close, Volume
## date (1): Date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
GOOGLdata <- na.omit(GOOGLdata)
chartSeries(GOOGLdata)
GOOGL_close <- GOOGLdata$Close
ggplot(GOOGLdata, aes(x = Date, y = Close)) +
geom_line() +
theme_minimal() +
labs(title = "GOOGLE Stock Closing Prices Over Time",
x = "Date",
y = "Closing Price")
# GARCH model (GARCH(1,1) and ARMA(0,0))
garch_spec <- ugarchspec(
variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0, 0)),distribution.model="norm")
# Fit the GARCH model and display model summary
garch_fit <- ugarchfit(data = GOOGL_close, spec = garch_spec)
print(garch_fit)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(0,0,0)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 261.846248 0.918987 284.9293 0.000000
## omega 17.401352 4.514731 3.8544 0.000116
## alpha1 0.904729 0.058492 15.4675 0.000000
## beta1 0.094266 0.055840 1.6881 0.091385
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 261.846248 6.704983 39.05248 0.00000
## omega 17.401352 18.372950 0.94712 0.34358
## alpha1 0.904729 0.057859 15.63677 0.00000
## beta1 0.094266 0.063999 1.47294 0.14077
##
## LogLikelihood : -29086.66
##
## Information Criteria
## ------------------------------------
##
## Akaike 13.131
## Bayes 13.136
## Shibata 13.131
## Hannan-Quinn 13.133
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 4100 0
## Lag[2*(p+q)+(p+q)-1][2] 6116 0
## Lag[4*(p+q)+(p+q)-1][5] 12044 0
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2116 0.6455
## Lag[2*(p+q)+(p+q)-1][5] 1.9136 0.6390
## Lag[4*(p+q)+(p+q)-1][9] 4.0494 0.5805
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.0001808 0.500 2.000 0.9893
## ARCH Lag[5] 0.3724233 1.440 1.667 0.9205
## ARCH Lag[7] 2.4542018 2.315 1.543 0.6219
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 10.1406
## Individual Statistics:
## mu 3.57786
## omega 0.03872
## alpha1 0.38118
## beta1 0.04685
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.07 1.24 1.6
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.6449 0.1001
## Negative Sign Bias 0.6823 0.4951
## Positive Sign Bias 0.8195 0.4125
## Joint Effect 3.1393 0.3706
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 30421 0
## 2 30 44589 0
## 3 40 57695 0
## 4 50 47633 0
##
##
## Elapsed time : 0.732353
# GARCH model (GARCH(1,1) and ARMA(2,2))
garch_spec2 <- ugarchspec(
variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(2, 2)),distribution.model="norm")
# Fit the GARCH model and display model summary
garch_fit2 <- ugarchfit(data = GOOGL_close, spec = garch_spec2)
print(garch_fit2)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 50.492708 4.994407 10.109850 0.000000
## ar1 0.222476 0.001303 170.713474 0.000000
## ar2 0.778476 0.001317 591.128677 0.000000
## ma1 0.765928 0.017228 44.458494 0.000000
## ma2 -0.000647 0.017362 -0.037292 0.970252
## omega 0.378272 0.083962 4.505285 0.000007
## alpha1 0.065380 0.007645 8.552041 0.000000
## beta1 0.933620 0.008037 116.160415 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 50.492708 0.312034 161.818194 0.00000
## ar1 0.222476 0.000578 384.849521 0.00000
## ar2 0.778476 0.000496 1569.749042 0.00000
## ma1 0.765928 0.019041 40.225872 0.00000
## ma2 -0.000647 0.019958 -0.032442 0.97412
## omega 0.378272 0.473610 0.798700 0.42446
## alpha1 0.065380 0.047045 1.389750 0.16461
## beta1 0.933620 0.051115 18.264935 0.00000
##
## LogLikelihood : -15779.33
##
## Information Criteria
## ------------------------------------
##
## Akaike 7.1259
## Bayes 7.1374
## Shibata 7.1258
## Hannan-Quinn 7.1299
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 3.403 0.06509
## Lag[2*(p+q)+(p+q)-1][11] 7.192 0.02926
## Lag[4*(p+q)+(p+q)-1][19] 9.991 0.46980
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1831 0.6687
## Lag[2*(p+q)+(p+q)-1][5] 0.3957 0.9725
## Lag[4*(p+q)+(p+q)-1][9] 0.7034 0.9954
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.001642 0.500 2.000 0.9677
## ARCH Lag[5] 0.057727 1.440 1.667 0.9939
## ARCH Lag[7] 0.354202 2.315 1.543 0.9896
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.0766
## Individual Statistics:
## mu 0.02051
## ar1 0.04276
## ar2 0.04311
## ma1 0.04330
## ma2 0.11770
## omega 0.72657
## alpha1 0.32796
## beta1 0.50758
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.89 2.11 2.59
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.24734 0.2123
## Negative Sign Bias 0.07125 0.9432
## Positive Sign Bias 1.42427 0.1544
## Joint Effect 2.70480 0.4394
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 304.8 2.038e-53
## 2 30 328.4 1.899e-52
## 3 40 344.3 1.641e-50
## 4 50 348.8 7.777e-47
##
##
## Elapsed time : 4.537446
# GARCH model (GARCH(1,2) and ARMA(2,2))
garch_spec3 <- ugarchspec(
variance.model = list(model = "sGARCH", garchOrder = c(1, 2)),
mean.model = list(armaOrder = c(2, 2)),distribution.model="norm")
# Fit the GARCH model and display model summary
garch_fit3 <- ugarchfit(data = GOOGL_close, spec = garch_spec3)
print(garch_fit3)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,2)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 50.252821 4.427669 11.349723 0.000000
## ar1 0.170426 0.001511 112.798464 0.000000
## ar2 0.830798 0.001524 545.092602 0.000000
## ma1 0.819732 0.017732 46.228197 0.000000
## ma2 -0.001033 0.017479 -0.059074 0.952893
## omega 0.529274 0.105749 5.004978 0.000001
## alpha1 0.094665 0.009285 10.194955 0.000000
## beta1 0.343568 0.061379 5.597473 0.000000
## beta2 0.560767 0.058752 9.544672 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 50.252821 0.268327 187.282343 0.000000
## ar1 0.170426 0.000409 416.809214 0.000000
## ar2 0.830798 0.000525 1582.261518 0.000000
## ma1 0.819732 0.018829 43.535804 0.000000
## ma2 -0.001033 0.019643 -0.052568 0.958076
## omega 0.529274 0.433232 1.221685 0.221827
## alpha1 0.094665 0.041833 2.262936 0.023640
## beta1 0.343568 0.082621 4.158336 0.000032
## beta2 0.560767 0.083281 6.733461 0.000000
##
## LogLikelihood : -15770.78
##
## Information Criteria
## ------------------------------------
##
## Akaike 7.1224
## Bayes 7.1354
## Shibata 7.1224
## Hannan-Quinn 7.1270
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 2.555 0.1099
## Lag[2*(p+q)+(p+q)-1][11] 6.671 0.1343
## Lag[4*(p+q)+(p+q)-1][19] 9.598 0.5363
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.01595 0.8995
## Lag[2*(p+q)+(p+q)-1][8] 0.28076 0.9993
## Lag[4*(p+q)+(p+q)-1][14] 0.76115 0.9999
## d.o.f=3
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[4] 0.08839 0.500 2.000 0.7662
## ARCH Lag[6] 0.14877 1.461 1.711 0.9793
## ARCH Lag[8] 0.46823 2.368 1.583 0.9842
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.5851
## Individual Statistics:
## mu 0.02505
## ar1 0.03916
## ar2 0.03978
## ma1 0.03059
## ma2 0.13080
## omega 0.63993
## alpha1 0.31146
## beta1 0.49106
## beta2 0.50258
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 2.1 2.32 2.82
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.2976 0.1945
## Negative Sign Bias 0.2854 0.7754
## Positive Sign Bias 1.2068 0.2276
## Joint Effect 2.3135 0.5099
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 298.8 3.571e-52
## 2 30 310.9 5.666e-49
## 3 40 344.5 1.502e-50
## 4 50 350.1 4.543e-47
##
##
## Elapsed time : 1.632625
# GARCH model (GARCH(3,3) and ARMA(2,2))
garch_spec4 <- ugarchspec(
variance.model = list(model = "sGARCH", garchOrder = c(3, 3)),
mean.model = list(armaOrder = c(2, 2)),distribution.model="norm")
# Fit the GARCH model and display model summary
garch_fit4 <- ugarchfit(data = GOOGL_close, spec = garch_spec4)
print(garch_fit4)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(3,3)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 50.400773 0.004563 1.1045e+04 0.000000
## ar1 0.000844 0.000017 4.9226e+01 0.000000
## ar2 1.000612 0.000092 1.0864e+04 0.000000
## ma1 0.984788 0.000007 1.3395e+05 0.000000
## ma2 -0.017095 0.000016 -1.0732e+03 0.000000
## omega 0.660466 0.141587 4.6648e+00 0.000003
## alpha1 0.117729 0.014045 8.3825e+00 0.000000
## alpha2 0.000002 0.024851 7.7000e-05 0.999938
## alpha3 0.000000 0.020374 7.0000e-06 0.999994
## beta1 0.092347 0.223964 4.1233e-01 0.680099
## beta2 0.539723 0.047796 1.1292e+01 0.000000
## beta3 0.249198 0.108873 2.2889e+00 0.022086
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 50.400773 0.073028 6.9015e+02 0.000000
## ar1 0.000844 0.000068 1.2425e+01 0.000000
## ar2 1.000612 0.001423 7.0334e+02 0.000000
## ma1 0.984788 0.000033 2.9901e+04 0.000000
## ma2 -0.017095 0.000050 -3.4383e+02 0.000000
## omega 0.660466 0.473597 1.3946e+00 0.163144
## alpha1 0.117729 0.043840 2.6854e+00 0.007244
## alpha2 0.000002 0.074457 2.6000e-05 0.999979
## alpha3 0.000000 0.053636 3.0000e-06 0.999998
## beta1 0.092347 0.681547 1.3550e-01 0.892220
## beta2 0.539723 0.265572 2.0323e+00 0.042123
## beta3 0.249198 0.392197 6.3539e-01 0.525174
##
## LogLikelihood : -15765.76
##
## Information Criteria
## ------------------------------------
##
## Akaike 7.1215
## Bayes 7.1389
## Shibata 7.1215
## Hannan-Quinn 7.1276
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 3.262 0.0708973
## Lag[2*(p+q)+(p+q)-1][11] 8.088 0.0007919
## Lag[4*(p+q)+(p+q)-1][19] 11.027 0.3100182
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.007073 0.933
## Lag[2*(p+q)+(p+q)-1][17] 0.973091 1.000
## Lag[4*(p+q)+(p+q)-1][29] 2.117588 1.000
## d.o.f=6
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[7] 0.4242 0.500 2.000 0.5149
## ARCH Lag[9] 0.6144 1.485 1.796 0.8734
## ARCH Lag[11] 0.8825 2.440 1.677 0.9535
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 6.7417
## Individual Statistics:
## mu 0.02126
## ar1 0.02146
## ar2 0.02074
## ma1 0.02063
## ma2 0.02065
## omega 0.62487
## alpha1 0.31348
## alpha2 0.27745
## alpha3 0.34282
## beta1 0.49510
## beta2 0.50968
## beta3 0.51137
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 2.69 2.96 3.51
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.3222 0.1862
## Negative Sign Bias 0.3792 0.7045
## Positive Sign Bias 1.0636 0.2876
## Joint Effect 2.1384 0.5442
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 309.4 2.374e-54
## 2 30 323.4 1.812e-51
## 3 40 340.5 8.842e-50
## 4 50 347.8 1.219e-46
##
##
## Elapsed time : 3.213814
We can see in this experiment that GARCH order(3,3) and ARMA order (2,2) has the least AIC value, so we are going to use this one in the forecasting.
#Use GARCH(3,3) and ARMA(2,2) for next 30 days forecasting
f <- ugarchforecast(fitORspec = garch_fit4, n.ahead = 30)
plot(fitted(f))
# plot
plot(sigma(f))
# Get model diagnostics
garch_diagnostics <- residuals(garch_fit4, standardize = TRUE)
acf(garch_diagnostics)
pacf(garch_diagnostics)
# Forecast volatility for next 30 days
garch_forecast <- ugarchforecast(garch_fit4, n.ahead = 30)
plot(as.zoo(fitted(garch_forecast)), main="Time Series Prediction (Unconditional)", ylab="Value", xlab="Horizon")