Explore the share price
startDate = as.Date("2015-07-01")
endDate = as.Date("2020-07-01")
getSymbols("SPY", from = startDate, to = endDate)
## [1] "SPY"
chartSeries(SPY)

head(SPY)
## SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 2015-07-01 207.73 208.03 206.56 207.50 135979900 187.8570
## 2015-07-02 208.07 208.27 206.81 207.31 104373700 187.6850
## 2015-07-06 205.77 207.65 205.53 206.72 117975400 187.1509
## 2015-07-07 206.96 208.17 204.11 208.02 173820200 188.3278
## 2015-07-08 206.42 206.76 204.25 204.53 164020100 185.1682
## 2015-07-09 207.04 207.35 204.77 204.90 144113100 185.5032
tail(SPY)
## SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 2020-06-15 298.02 308.28 296.74 307.05 135782700 305.7047
## 2020-06-16 315.48 315.64 307.67 312.96 137627500 311.5888
## 2020-06-17 314.07 314.39 310.86 311.66 82954600 310.2945
## 2020-06-18 310.01 312.30 309.51 311.78 80828700 310.4140
## 2020-06-19 314.17 314.38 306.53 308.64 135549600 308.6400
## 2020-06-22 307.99 311.05 306.75 310.62 74144300 310.6200
Univariate GARCH Model
ug_spec = ugarchspec()
ug_spec
##
## *---------------------------------*
## * GARCH Model Spec *
## *---------------------------------*
##
## Conditional Variance Dynamics
## ------------------------------------
## GARCH Model : sGARCH(1,1)
## Variance Targeting : FALSE
##
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model : ARFIMA(1,0,1)
## Include Mean : TRUE
## GARCH-in-Mean : FALSE
##
## Conditional Distribution
## ------------------------------------
## Distribution : norm
## Includes Skew : FALSE
## Includes Shape : FALSE
## Includes Lambda : FALSE
Example of AR(1) model
ug_spec <- ugarchspec(mean.model = list(armaOrder=c(1,0)))
Model Estimation
ugfit = ugarchfit(spec = ug_spec, data = rSPY)
ugfit
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(1,0,0)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.000941 0.000202 4.6569 0.000003
## ar1 -0.072958 0.032269 -2.2610 0.023761
## omega 0.000004 0.000003 1.3975 0.162256
## alpha1 0.259488 0.036192 7.1698 0.000000
## beta1 0.726268 0.031874 22.7855 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.000941 0.000965 0.97566 0.329234
## ar1 -0.072958 0.029793 -2.44885 0.014331
## omega 0.000004 0.000021 0.17438 0.861570
## alpha1 0.259488 0.154310 1.68160 0.092646
## beta1 0.726268 0.175222 4.14486 0.000034
##
## LogLikelihood : 4271.199
##
## Information Criteria
## ------------------------------------
##
## Akaike -6.8096
## Bayes -6.7891
## Shibata -6.8096
## Hannan-Quinn -6.8019
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 3.185 0.07433
## Lag[2*(p+q)+(p+q)-1][2] 3.245 0.02149
## Lag[4*(p+q)+(p+q)-1][5] 3.424 0.33254
## d.o.f=1
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.5174 0.4720
## Lag[2*(p+q)+(p+q)-1][5] 2.0405 0.6089
## Lag[4*(p+q)+(p+q)-1][9] 3.9133 0.6036
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.04099 0.500 2.000 0.8396
## ARCH Lag[5] 2.35047 1.440 1.667 0.3989
## ARCH Lag[7] 3.54595 2.315 1.543 0.4168
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 1.928
## Individual Statistics:
## mu 0.2915
## ar1 0.0637
## omega 0.1624
## alpha1 0.2763
## beta1 0.2022
##
## 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 2.92609 0.003495 ***
## Negative Sign Bias 0.07299 0.941829
## Positive Sign Bias 0.64362 0.519942
## Joint Effect 16.23251 0.001016 ***
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 77.93 4.211e-09
## 2 30 98.39 1.769e-09
## 3 40 108.50 1.813e-08
## 4 50 126.21 9.802e-09
##
##
## Elapsed time : 0.1855431
names(ugfit@model)
## [1] "modelinc" "modeldesc" "modeldata" "pars" "start.pars"
## [6] "fixed.pars" "maxOrder" "pos.matrix" "fmodel" "pidx"
## [11] "n.start"
names(ugfit@fit)
## [1] "hessian" "cvar" "var"
## [4] "sigma" "condH" "z"
## [7] "LLH" "log.likelihoods" "residuals"
## [10] "coef" "robust.cvar" "A"
## [13] "B" "scores" "se.coef"
## [16] "tval" "matcoef" "robust.se.coef"
## [19] "robust.tval" "robust.matcoef" "fitted.values"
## [22] "convergence" "kappa" "persistence"
## [25] "timer" "ipars" "solver"
ugfit@fit$coef
## mu ar1 omega alpha1 beta1
## 9.410404e-04 -7.295814e-02 3.653901e-06 2.594881e-01 7.262681e-01
ug_var <- ugfit@fit$var
ug_res2 <- (ugfit@fit$residuals)^2
plot(ug_res2, type = "l")
lines(ug_var, col ='green')

Model forecasting
ugfore <- ugarchforecast(ugfit, n.ahead = 20)
ugfore
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 20
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2020-06-22]:
## Series Sigma
## T+1 0.0005417 0.01348
## T+2 0.0009702 0.01352
## T+3 0.0009389 0.01356
## T+4 0.0009412 0.01360
## T+5 0.0009410 0.01363
## T+6 0.0009410 0.01367
## T+7 0.0009410 0.01371
## T+8 0.0009410 0.01374
## T+9 0.0009410 0.01378
## T+10 0.0009410 0.01381
## T+11 0.0009410 0.01385
## T+12 0.0009410 0.01388
## T+13 0.0009410 0.01391
## T+14 0.0009410 0.01394
## T+15 0.0009410 0.01398
## T+16 0.0009410 0.01401
## T+17 0.0009410 0.01404
## T+18 0.0009410 0.01407
## T+19 0.0009410 0.01410
## T+20 0.0009410 0.01413
ug_f <- ugfore@forecast$sigmaFor
plot(ug_f, type = 'l')

ug_var_t <- c(tail(ug_var, 30), rep(NA, 20)) ## Get the last 30 observations
ug_res2_t <- c(tail(ug_res2, 30), rep(NA, 20)) ## Get the last 30 observations
ug_f <- c(rep(NA,30), (ug_f)^2)
plot(ug_res2_t, type = "l")
lines(ug_f, col = "orange")
lines(ug_var_t, col = "green")

Predict the next 20 months of closing share price (Dec 2019)
SPYclose <- SPY$SPY.Close
Example of of Model
SPY_spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)), mean.model = list(armaOrder=c(2,2)), distribution.model = "std")
SPY_spec
##
## *---------------------------------*
## * GARCH Model Spec *
## *---------------------------------*
##
## Conditional Variance Dynamics
## ------------------------------------
## GARCH Model : sGARCH(1,1)
## Variance Targeting : FALSE
##
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model : ARFIMA(2,0,2)
## Include Mean : TRUE
## GARCH-in-Mean : FALSE
##
## Conditional Distribution
## ------------------------------------
## Distribution : std
## Includes Skew : FALSE
## Includes Shape : TRUE
## Includes Lambda : FALSE
Model Estimation
SPY_fit = ugarchfit(spec = SPY_spec, data = SPYclose)
SPY_fit
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(2,0,2)
## Distribution : std
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 207.569383 1.399678 148.29793 0.000000
## ar1 0.494097 0.003043 162.34692 0.000000
## ar2 0.511773 0.003056 167.45314 0.000000
## ma1 0.434023 0.029933 14.49959 0.000000
## ma2 -0.027717 0.030388 -0.91211 0.361711
## omega 0.125835 0.041508 3.03157 0.002433
## alpha1 0.202053 0.034730 5.81780 0.000000
## beta1 0.796947 0.030741 25.92446 0.000000
## shape 4.739561 0.631657 7.50338 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 207.569383 0.292989 708.45356 0.000000
## ar1 0.494097 0.000488 1012.31788 0.000000
## ar2 0.511773 0.000491 1041.52228 0.000000
## ma1 0.434023 0.028202 15.38954 0.000000
## ma2 -0.027717 0.030894 -0.89715 0.369640
## omega 0.125835 0.041968 2.99836 0.002714
## alpha1 0.202053 0.038506 5.24734 0.000000
## beta1 0.796947 0.032045 24.87000 0.000000
## shape 4.739561 0.616613 7.68645 0.000000
##
## LogLikelihood : -2592.355
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.1522
## Bayes 4.1891
## Shibata 4.1521
## Hannan-Quinn 4.1661
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 4.207 0.04026
## Lag[2*(p+q)+(p+q)-1][11] 5.522 0.78264
## Lag[4*(p+q)+(p+q)-1][19] 7.883 0.81019
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 2.069 0.1503
## Lag[2*(p+q)+(p+q)-1][5] 4.095 0.2425
## Lag[4*(p+q)+(p+q)-1][9] 6.749 0.2211
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.0007654 0.500 2.000 0.9779
## ARCH Lag[5] 3.8833532 1.440 1.667 0.1848
## ARCH Lag[7] 5.4617043 2.315 1.543 0.1818
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 1.0939
## Individual Statistics:
## mu 0.12508
## ar1 0.02991
## ar2 0.03183
## ma1 0.08456
## ma2 0.06252
## omega 0.37380
## alpha1 0.35005
## beta1 0.39502
## shape 0.36925
##
## 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 3.39592 0.0007055 ***
## Negative Sign Bias 0.04313 0.9656023
## Positive Sign Bias 0.45027 0.6525953
## Joint Effect 20.17092 0.0001564 ***
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 31.74 0.03342
## 2 30 41.60 0.06101
## 3 40 48.48 0.14195
## 4 50 66.83 0.04594
##
##
## Elapsed time : 0.497664
Forecast SPY stock price for next 20 days
SPYforecast <- ugarchforecast(SPY_fit, n.ahead = 20)
SPYforecast
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 20
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2020-06-22]:
## Series Sigma
## T+1 310.9 4.835
## T+2 311.3 4.846
## T+3 311.7 4.856
## T+4 312.1 4.867
## T+5 312.5 4.877
## T+6 312.9 4.888
## T+7 313.3 4.898
## T+8 313.7 4.909
## T+9 314.2 4.919
## T+10 314.6 4.929
## T+11 315.0 4.939
## T+12 315.4 4.950
## T+13 315.8 4.960
## T+14 316.2 4.970
## T+15 316.7 4.980
## T+16 317.1 4.990
## T+17 317.5 5.001
## T+18 317.9 5.011
## T+19 318.4 5.021
## T+20 318.8 5.031
Predict stock price for next 20 days
SPYPredict <- ugarchboot(SPY_fit, n.ahead = 20, method = c("Partial", "Full")[1])
SPYPredict
##
## *-----------------------------------*
## * GARCH Bootstrap Forecast *
## *-----------------------------------*
## Model : sGARCH
## n.ahead : 20
## Bootstrap method: partial
## Date (T[0]): 2020-06-22
##
## Series (summary):
## min q.25 mean q.75 max forecast[analytic]
## t+1 295.32 308.67 311.05 313.86 325.83 310.87
## t+2 273.85 307.97 311.23 314.94 335.98 311.31
## t+3 253.66 307.20 311.33 316.09 337.14 311.70
## t+4 261.47 307.13 311.86 317.36 338.93 312.11
## t+5 264.39 307.12 312.43 318.23 343.29 312.51
## t+6 264.86 307.05 312.75 318.91 349.27 312.92
## t+7 272.54 307.22 313.51 320.06 359.76 313.33
## t+8 256.15 306.57 313.36 321.10 361.20 313.74
## t+9 253.88 306.37 313.59 321.47 358.31 314.16
## t+10 237.06 306.37 313.72 322.06 359.32 314.57
## .....................
##
## Sigma (summary):
## min q0.25 mean q0.75 max forecast[analytic]
## t+1 4.8351 4.8351 4.8351 4.8351 4.8351 4.8351
## t+2 4.3310 4.3658 4.7360 4.8637 8.2228 4.8457
## t+3 3.8851 4.0367 4.7082 5.0790 14.4532 4.8563
## t+4 3.4869 3.8175 4.7004 5.0657 18.1413 4.8668
## t+5 3.1629 3.6631 4.6857 5.0862 22.3415 4.8773
## t+6 2.8655 3.5651 4.6061 5.1217 21.2309 4.8877
## t+7 2.6003 3.3674 4.5611 5.1318 18.9566 4.8981
## t+8 2.4515 3.2573 4.5427 5.0334 17.0106 4.9085
## t+9 2.3020 3.1334 4.4843 5.0351 30.3816 4.9189
## t+10 2.0871 3.0290 4.3721 4.9126 27.1436 4.9292
## .....................
plot(SPYPredict, which = 2)
