Demetri Chokshi-Fox
library(readxl)
library(forecast)
library(fpp2)
library(fGarch)
library(rugarch)
library(quantmod)
#This part is from before I found the video showing me how to work with GARCH models
#VSLR.stock.price.Aug.5.2017...Aug.5.2019 <- read.csv("C:/Users/Demetri/Google Drive/Grad School/Forecasting and Predictive Analytics/VSLR stock price Aug 5 2017 - Aug 5 2019.csv")
#vslr = ts(VSLR.stock.price.Aug.5.2017...Aug.5.2019["Adj.Close"], frequency = 5)
#vslr.garch = garchFit(~ garch(1,1), data=vslr)
#summary(vslr.garch)
Import data
startDate = as.Date("2017-08-07")
endDate = as.Date("2019-08-07")
getSymbols("VSLR", from = startDate, to=endDate)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "VSLR"
Get returns data
rVSLR = dailyReturn(VSLR)
Get specifications for model
ugspec = ugarchspec()
ugspec
##
## *---------------------------------*
## * 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
Estimate model
ugfit = ugarchfit(spec=ugspec, data=rVSLR)
ugfit
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(1,0,1)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001975 0.001499 1.31789 0.187540
## ar1 0.395222 0.623582 0.63379 0.526216
## ma1 -0.378321 0.626926 -0.60345 0.546207
## omega 0.000215 0.000064 3.34639 0.000819
## alpha1 0.137947 0.041396 3.33235 0.000861
## beta1 0.697593 0.068352 10.20591 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001975 0.001444 1.3676 0.171438
## ar1 0.395222 0.158098 2.4999 0.012424
## ma1 -0.378321 0.152254 -2.4848 0.012962
## omega 0.000215 0.000068 3.1353 0.001717
## alpha1 0.137947 0.053450 2.5809 0.009856
## beta1 0.697593 0.057326 12.1690 0.000000
##
## LogLikelihood : 982.2499
##
## Information Criteria
## ------------------------------------
##
## Akaike -3.8817
## Bayes -3.8314
## Shibata -3.8820
## Hannan-Quinn -3.8620
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.01344 0.9077
## Lag[2*(p+q)+(p+q)-1][5] 1.70143 0.9915
## Lag[4*(p+q)+(p+q)-1][9] 3.42559 0.8162
## d.o.f=2
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.01358 0.9072
## Lag[2*(p+q)+(p+q)-1][5] 3.56052 0.3142
## Lag[4*(p+q)+(p+q)-1][9] 6.00627 0.2975
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.4218 0.500 2.000 0.51606
## ARCH Lag[5] 6.0603 1.440 1.667 0.05814
## ARCH Lag[7] 6.4839 2.315 1.543 0.11189
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 1.1109
## Individual Statistics:
## mu 0.31999
## ar1 0.04326
## ma1 0.04140
## omega 0.04522
## alpha1 0.06679
## beta1 0.08370
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.49 1.68 2.12
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.3875 0.6985
## Negative Sign Bias 0.4041 0.6863
## Positive Sign Bias 0.2041 0.8384
## Joint Effect 0.4339 0.9332
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 159.2 3.585e-24
## 2 30 243.8 8.158e-36
## 3 40 178.7 8.686e-20
## 4 50 281.8 1.937e-34
##
##
## Elapsed time : 0.2298839
Get coefficients
ugfit@fit$coef
## mu ar1 ma1 omega alpha1
## 0.0019749910 0.3952220436 -0.3783213081 0.0002146198 0.1379471589
## beta1
## 0.6975932811
Plot conditional variance with squared residuals for model
ugvar = ugfit@fit$var #estimated conditional variances
ugres2 = (ugfit@fit$residuals)^2 #estimated squared residuals
plot(ugres2, type="line")
## Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to
## first character
lines(ugvar, col="red") #plot conditional variance with squared residuals for model

Forecast with GARCH model
ugfor = ugarchforecast(ugfit, n.ahead=10)
ugfor
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2019-08-06]:
## Series Sigma
## T+1 0.001890 0.03246
## T+2 0.001941 0.03309
## T+3 0.001962 0.03361
## T+4 0.001970 0.03404
## T+5 0.001973 0.03439
## T+6 0.001974 0.03468
## T+7 0.001975 0.03492
## T+8 0.001975 0.03512
## T+9 0.001975 0.03529
## T+10 0.001975 0.03543
Plot forecasted conditional volatility
ugfor.vol = ugfor@forecast$sigmaFor
plot(ugfor.vol, type="line")
## Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to
## first character
