├── .gitattributes ├── data ├── BA.xlsx ├── BAC.xlsx ├── WMT.xlsx └── AAPL.xlsx ├── garch_regression.png ├── out_of_sample_vol.png ├── result ├── fit summary.pdf └── Fan_Gao_project.pdf ├── garch_reg_scatterplot.png ├── in_sample_scatterplot.png ├── out_of_sample_simple_reg.png ├── Reference ├── Tese_JoseSalgado.pdf ├── Implied Volatility Forecasting.pdf ├── Improved methods of combining forcasts.pdf ├── Multiplicative Models for Implied Volatility.pdf ├── modeling and forecasting implied volatility.pdf ├── Joint Modeling of Call and Put Implied Volatility.pdf ├── GARCH-based Volatility Forcasts for Implied Volatility Indices.pdf └── A multiple indicators model for volatility using intra-daily data.pdf ├── out_of_sample_scatterplot.png ├── in_sample_conditional_volatility.png ├── test.R └── test_outofsample.R /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /data/BA.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/data/BA.xlsx -------------------------------------------------------------------------------- /data/BAC.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/data/BAC.xlsx -------------------------------------------------------------------------------- /data/WMT.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/data/WMT.xlsx -------------------------------------------------------------------------------- /data/AAPL.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/data/AAPL.xlsx -------------------------------------------------------------------------------- /garch_regression.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/garch_regression.png -------------------------------------------------------------------------------- /out_of_sample_vol.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/out_of_sample_vol.png -------------------------------------------------------------------------------- /result/fit summary.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/result/fit summary.pdf -------------------------------------------------------------------------------- /garch_reg_scatterplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/garch_reg_scatterplot.png -------------------------------------------------------------------------------- /in_sample_scatterplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/in_sample_scatterplot.png -------------------------------------------------------------------------------- /result/Fan_Gao_project.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/result/Fan_Gao_project.pdf -------------------------------------------------------------------------------- /out_of_sample_simple_reg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/out_of_sample_simple_reg.png -------------------------------------------------------------------------------- /Reference/Tese_JoseSalgado.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/Tese_JoseSalgado.pdf -------------------------------------------------------------------------------- /out_of_sample_scatterplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/out_of_sample_scatterplot.png -------------------------------------------------------------------------------- /in_sample_conditional_volatility.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/in_sample_conditional_volatility.png -------------------------------------------------------------------------------- /Reference/Implied Volatility Forecasting.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/Implied Volatility Forecasting.pdf -------------------------------------------------------------------------------- /Reference/Improved methods of combining forcasts.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/Improved methods of combining forcasts.pdf -------------------------------------------------------------------------------- /Reference/Multiplicative Models for Implied Volatility.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/Multiplicative Models for Implied Volatility.pdf -------------------------------------------------------------------------------- /Reference/modeling and forecasting implied volatility.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/modeling and forecasting implied volatility.pdf -------------------------------------------------------------------------------- /Reference/Joint Modeling of Call and Put Implied Volatility.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/Joint Modeling of Call and Put Implied Volatility.pdf -------------------------------------------------------------------------------- /Reference/GARCH-based Volatility Forcasts for Implied Volatility Indices.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/GARCH-based Volatility Forcasts for Implied Volatility Indices.pdf -------------------------------------------------------------------------------- /Reference/A multiple indicators model for volatility using intra-daily data.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Iangao25/comparison-between-GARCH-type-models/HEAD/Reference/A multiple indicators model for volatility using intra-daily data.pdf -------------------------------------------------------------------------------- /test.R: -------------------------------------------------------------------------------- 1 | library(rugarch) 2 | library(xlsx) 3 | library(xts) 4 | library(olsrr) 5 | df <- read.xlsx("D:/NYU/Engle- Financial Econometrics/research/AAPL.xlsx",1) 6 | date <- as.POSIXct(strptime(df[,1], "%Y-%m-%d")) 7 | df = xts(df[,c(2,3)], order.by=date) 8 | returns <- 100 * diff(log(df$AAPL_Price)) 9 | IV_insample <- df$AAPL_IV['2008-04-30/2018-01-29'] 10 | 11 | ###################################################################################################### 12 | # GARCH(1,1) IN-SAMPLE ESTIMATION 13 | prediction_garch <- vector() 14 | spec_garch = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 15 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE)) 16 | fit_garch = ugarchfit(spec_garch, returns['2008-04-29/2018-01-26'], solver = 'hybrid') 17 | show(fit_garch) 18 | plot(sigma(fit_garch)*sqrt(255)) 19 | 20 | # GARCH(1,1) ROLLING 21 | for (i in (length(returns)-63):(length(returns)-1)) { 22 | spec = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 23 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE)) 24 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 25 | pre <- ugarchforecast(fit, n.ahead = 22) 26 | prediction_garch <- c(prediction_garch, mean(sigma(pre)*sqrt(255))) 27 | } 28 | IV <- tail(df$AAPL_IV, 63) 29 | mae_garch <- sum(abs(prediction_garch - IV)) 30 | mae_garch 31 | 32 | # REGRESSION 33 | pred_garch <- vector() 34 | for (i in 1005:1259){ 35 | lr_garch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:i]])~log(df_prediction$GARCH[index(df_prediction$GARCH)[1:i]])+lag(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:i]]))) 36 | pred_log_garch <- cbind(1,log(df_prediction$GARCH[index(df_prediction$GARCH)[i+1]]))%*%lr_garch$coefficients 37 | pred_garch <- c(pred_garch, exp(pred_log_garch)) 38 | } 39 | mae_garch_lr <- sum(abs(pred_garch - tail(IV, 255)))/255 40 | mae_garch_lr 41 | mae_garch 42 | 43 | lr_garch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:1005]])~log(df_prediction$GARCH[index(df_prediction$GARCH)[1:1005]])) 44 | pred_log_garch <- cbind(1,log(df_prediction$GARCH[index(df_prediction$GARCH)[1006:1260]]))%*%lr_garch$coefficients 45 | pred_garch <- exp(pred_log_garch) 46 | mae_garch_lr <- sum(abs(pred_garch - tail(IV, 255)))/255 47 | mae_garch_lr 48 | 49 | lr_egarch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:1005]])~log(df_prediction$EGARCH[index(df_prediction$EGARCH)[1:1005]])) 50 | pred_log_egarch <- cbind(1,log(df_prediction$EGARCH[index(df_prediction$EGARCH)[1006:1260]]))%*%lr_egarch$coefficients 51 | pred_egarch <- exp(pred_log_egarch) 52 | mae_egarch_lr <- sum(abs(pred_egarch - tail(IV, 255)))/255 53 | mae_egarch_lr 54 | 55 | lr_gjrgarch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:1005]])~log(df_prediction$GJRGARCH[index(df_prediction$GJRGARCH)[1:1005]])) 56 | pred_log_gjrgarch <- cbind(1,log(df_prediction$GJRGARCH[index(df_prediction$GJRGARCH)[1006:1260]]))%*%lr_gjrgarch$coefficients 57 | pred_gjrgarch <- exp(pred_log_gjrgarch) 58 | mae_gjrgarch_lr <- sum(abs(pred_gjrgarch - tail(IV, 255)))/255 59 | mae_gjrgarch_lr 60 | 61 | lr_tgarch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:1005]])~log(df_prediction$TGARCH[index(df_prediction$TGARCH)[1:1005]])) 62 | pred_log_tgarch <- cbind(1,log(df_prediction$TGARCH[index(df_prediction$TGARCH)[1006:1260]]))%*%lr_tgarch$coefficients 63 | pred_tgarch <- exp(pred_log_tgarch) 64 | mae_tgarch_lr <- sum(abs(pred_tgarch - tail(IV, 255)))/255 65 | mae_tgarch_lr 66 | 67 | plot(lr_garch) 68 | ols_test_normality(lr_tgarch) 69 | summary(lr_garch) 70 | a <- log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:1006]]) 71 | a_lag <- lag(a) 72 | b <- log(df_prediction$GARCH[index(df_prediction$TGARCH)[1:1006]]) 73 | b_lag <- lag(b) 74 | ar(a) 75 | ar(b) 76 | lr <- lm(a~b+a_lag) 77 | summary(lr) 78 | ols_test_normality(lr) 79 | ols_plot_resid_hist(lr_egarch) 80 | plot(lr) 81 | 82 | 83 | mod.resi <- resid(lr) 84 | qqnorm(mod.resi) 85 | qqline(mod.resi) 86 | ###################################################################################################### 87 | # GJRGARCH(1,1,1) IN-SAMPLE ESTIMATION 88 | prediction_gjrgarch <- vector() 89 | spec_gjrgarch = ugarchspec(variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)), 90 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 91 | distribution.model = "norm") 92 | fit_gjrgarch = ugarchfit(spec_gjrgarch, returns['2008-04-29/2018-01-26'], solver = 'hybrid') 93 | show(fit_gjrgarch) 94 | plot(sigma(fit_gjrgarch)*sqrt(255)) 95 | 96 | # GJRGARCH(1,1,1) ROLLING 97 | for (i in (length(returns)-63):(length(returns)-1)) { 98 | spec = ugarchspec(variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)), 99 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 100 | distribution.model = "norm") 101 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 102 | pre <- ugarchforecast(fit, n.ahead = 22) 103 | prediction_gjrgarch <- c(prediction_gjrgarch, mean(sigma(pre)*sqrt(255))) 104 | } 105 | prediction_gjrgarch 106 | IV <- tail(df$AAPL_IV, 63) 107 | mae_gjrGARCH <- sum(abs(prediction_gjrgarch - IV)) 108 | mae_gjrGARCH 109 | 110 | 111 | # REGRESSION 112 | pred_gjrgarch <- vector() 113 | for (i in 1005:1259){ 114 | lr_gjrgarch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:i]])~log(df_prediction$GJRGARCH[index(df_prediction$GJRGARCH)[1:i]])) 115 | pred_log_gjrgarch <- cbind(1,log(df_prediction$GJRGARCH[index(df_prediction$GJRGARCH)[i+1]]))%*%lr_gjrgarch$coefficients 116 | pred_gjrgarch <- c(pred_gjrgarch, exp(pred_log_gjrgarch)) 117 | } 118 | mae_gjrgarch_lr <- sum(abs(pred_gjrgarch - tail(IV, 255)))/255 119 | mae_gjrgarch_lr 120 | mae_gjrgarch 121 | summary(lr_gjrgarch) 122 | ###################################################################################################### 123 | # TGARCH(1,1) IN-SAMPLE ESTIMATION 124 | prediction_tgarch <- vector() 125 | spec_tgarch = ugarchspec(variance.model = list(model = "fGARCH", submodel = "TGARCH", garchOrder = c(1, 1)), 126 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 127 | distribution.model = "norm") 128 | fit_tgarch = ugarchfit(spec_tgarch, returns['2008-04-29/2018-01-26'], solver = 'hybrid') 129 | show(fit_tgarch) 130 | plot(sigma(fit_tgarch)*sqrt(255)) 131 | 132 | # TGARCH(1,1) ROLLING 133 | for (i in (length(returns)-63):(length(returns)-1)) { 134 | spec = ugarchspec(variance.model = list(model = "fGARCH", submodel = "TGARCH", garchOrder = c(1, 1)), 135 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 136 | distribution.model = "norm") 137 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 138 | pre <- ugarchforecast(fit, n.ahead = 22) 139 | prediction_tgarch <- c(prediction_tgarch, mean(sigma(pre)*sqrt(255))) 140 | } 141 | prediction_tgarch 142 | IV <- tail(df$AAPL_IV, 63) 143 | mae_tgarch <- sum(abs(prediction_tgarch - IV)) 144 | mae_tgarch 145 | 146 | # REGRESSION 147 | pred_tgarch <- vector() 148 | for (i in 1005:1259){ 149 | lr_tgarch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:i]])~log(df_prediction$TGARCH[index(df_prediction$TGARCH)[1:i]])) 150 | pred_log_tgarch <- cbind(1,log(df_prediction$TGARCH[index(df_prediction$TGARCH)[i+1]]))%*%lr_tgarch$coefficients 151 | pred_tgarch <- c(pred_tgarch, exp(pred_log_tgarch)) 152 | } 153 | mae_tgarch_lr <- sum(abs(pred_tgarch - tail(IV, 255)))/255 154 | mae_tgarch_lr 155 | mae_tgarch 156 | summary(lr_tgarch) 157 | ###################################################################################################### 158 | # EGARCH(1,1,1) IN-SAMPLE ESTIMATION 159 | prediction_egarch <- vector() 160 | spec_egarch = ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1, 1)), 161 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 162 | distribution.model = "norm") 163 | fit_egarch = ugarchfit(spec_egarch, returns['2008-04-29/2018-01-26'], solver = 'hybrid') 164 | show(fit_egarch) 165 | plot(sigma(fit_egarch)*sqrt(255)) 166 | title(main = "conditional volatility estimated in-sample using EGARCH model") 167 | 168 | # EGARCH(1,1,1) ROLLING 169 | for (i in (length(returns)-63):(length(returns)-1)) { 170 | spec = ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1, 1)), 171 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 172 | distribution.model = "norm") 173 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 174 | pre <- ugarchforecast(fit, n.ahead = 22) 175 | prediction_egarch <- c(prediction_egarch, mean(sigma(pre)*sqrt(255))) 176 | } 177 | prediction_egarch 178 | IV <- tail(df$AAPL_IV, 63) 179 | 180 | # REGRESSION 181 | pred_egarch <- vector() 182 | for (i in 1005:1259){ 183 | lr_egarch <- lm(log(df_prediction$AAPL_IV[index(df_prediction$AAPL_IV)[1:i]])~log(df_prediction$EGARCH[index(df_prediction$EGARCH)[1:i]])) 184 | pred_log_egarch <- cbind(1,log(df_prediction$EGARCH[index(df_prediction$EGARCH)[i+1]]))%*%lr_egarch$coefficients 185 | pred_egarch <- c(pred_egarch, exp(pred_log_egarch)) 186 | } 187 | mae_egarch_lr <- sum(abs(pred_egarch - tail(IV, 255)))/255 188 | mae_egarch_lr 189 | mae_egarch 190 | summary(lr_egarch) 191 | 192 | 193 | 194 | #PCA REGRESSION 195 | 196 | 197 | ##################################################################################################### 198 | df_insample <- data.frame("IV"=IV_insample, "GARCH"=sigma(fit_garch)*sqrt(255), "EGARCH"=sigma(fit_egarch)*sqrt(255), 199 | "GJRGARCH"=sigma(fit_gjrgarch)*sqrt(255), "TGARCH"=sigma(fit_tgarch)*sqrt(255)) 200 | df_prediction <- data.frame("IV"=IV, "GARCH"=prediction_garch, "EGARCH"=prediction_egarch, 201 | "GJRGARCH"=prediction_gjrgarch, "TGARCH"=prediction_tgarch) 202 | df_simple_reg <- data.frame("IV"=tail(IV, 255), "GARCH"=pred_garch, "EGARCH"=pred_egarch, 203 | "GJRGARCH"=pred_gjrgarch, "TGARCH"=pred_tgarch) 204 | 205 | pairs(~AAPL_IV+GARCH+EGARCH+GJRGARCH+TGARCH, data=df_insample, main="Graph 1 Estimated In-Sample Conditional Volatility Scatterplot Matrix") 206 | pairs(~AAPL_IV+GARCH+EGARCH+GJRGARCH+TGARCH, data=df_prediction, main="Estimated Out-Of-Sample Volatility Scatterplot Matrix") 207 | pairs(~AAPL_IV+GARCH+EGARCH+GJRGARCH+TGARCH, data=df_simple_reg, main="Graph 5 Out-of-Sample Regression Volatility Forecast Scatterplot Matrix") 208 | 209 | myColors <- c("red", "darkgreen", "goldenrod", "darkblue", "darkviolet") 210 | plot(x = IV_insample, type="l", xlab = "Time", ylab = "Volatility(%)",ylim = c(10, 150), major.ticks="auto", minor.ticks=TRUE, main = "Graph 2 in-sample estimate of conditional volatility", col = "red") 211 | lines(x = as.xts(df_insample["GARCH"]), col = "darkgreen") 212 | lines(x = as.xts(df_insample["EGARCH"]), col = "goldenrod") 213 | lines(x = as.xts(df_insample["GJRGARCH"]), col = "darkblue") 214 | lines(x = as.xts(df_insample["TGARCH"]), col = "darkviolet") 215 | legend(x = 'right', legend = c("IV", "GARCH", "EGARCH", "GJR-GARCH", "TGARCH"), 216 | lty = 1, col = myColors) 217 | 218 | myColors <- c("red", "darkgreen", "goldenrod", "darkblue", "darkviolet") 219 | plot(x = IV, type="l", xlab = "Time", ylab = "Volatility(%)",ylim = c(15, 40), major.ticks="auto", minor.ticks=TRUE, main = "out-of-sample estimate of volatility", col = "red") 220 | lines(x = as.xts(df_prediction["GARCH"]), col = "darkgreen") 221 | lines(x = as.xts(df_prediction["EGARCH"]), col = "goldenrod") 222 | lines(x = as.xts(df_prediction["GJRGARCH"]), col = "darkblue") 223 | lines(x = as.xts(df_prediction["TGARCH"]), col = "darkviolet") 224 | legend(x = 'bottomright', legend = c("IV", "GARCH", "EGARCH", "GJR-GARCH", "TGARCH"), 225 | lty = 1, col = myColors) 226 | 227 | myColors <- c("red", "darkgreen", "goldenrod", "darkblue", "darkviolet") 228 | plot(x = tail(IV, 255), type="l", xlab = "Time", ylab = "Volatility(%)",ylim = c(10, 60), major.ticks="auto", minor.ticks=TRUE, main = "Graph 6 out-of-sample regression forcast of volatility", col = "red") 229 | lines(x = as.xts(df_simple_reg["GARCH"]), col = "darkgreen") 230 | lines(x = as.xts(df_simple_reg["EGARCH"]), col = "goldenrod") 231 | lines(x = as.xts(df_simple_reg["GJRGARCH"]), col = "darkblue") 232 | lines(x = as.xts(df_simple_reg["TGARCH"]), col = "darkviolet") 233 | legend(x = 'right', legend = c("IV", "GARCH", "EGARCH", "GJR-GARCH", "TGARCH"), 234 | lty = 1, col = myColors) 235 | 236 | length(IV) 237 | library(stats) 238 | ar(IV) 239 | ar(prediction_garch) 240 | -------------------------------------------------------------------------------- /test_outofsample.R: -------------------------------------------------------------------------------- 1 | library(rugarch) 2 | library(xlsx) 3 | library(xts) 4 | df <- read.xlsx("D:/NYU/Engle- Financial Econometrics/research/AAPL.xlsx",1) 5 | date <- as.POSIXct(strptime(df[,1], "%Y-%m-%d")) 6 | df = xts(df[,c(2,3)], order.by=date) 7 | returns <- 100 * diff(log(df$AAPL_Price)) 8 | IV_insample <- df$AAPL_IV['2008-04-29/2013-04-26'] 9 | IV_pred <- df$AAPL_IV['2013-04-27/2018-04-27'] 10 | 11 | ###################################################################################################### 12 | # GARCH(1,1) IN-SAMPLE ESTIMATION 13 | prediction_garch <- vector() 14 | spec_garch = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 15 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE)) 16 | fit_garch = ugarchfit(spec_garch, returns['2008-04-29/2013-04-26'], solver = 'hybrid') 17 | show(fit_garch) 18 | plot(sigma(fit_garch)*sqrt(255)) 19 | 20 | RANG <- df$AAPL_IV['2013-04-27/2018-04-27'] 21 | length(RANG) 22 | 23 | 24 | # GARCH(1,1) ROLLING 25 | for (i in (length(returns)-1260):(length(returns)-1)) { 26 | spec = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 27 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE)) 28 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 29 | pre <- ugarchforecast(fit, n.ahead = 22) 30 | prediction_garch <- c(prediction_garch, mean(sigma(pre)*sqrt(255))) 31 | } 32 | prediction_garch 33 | IV <- tail(df$AAPL_IV, 1260) 34 | index(IV)[1005:1006] 35 | 36 | # ROLLING REGRESSION WITH GARCH ERROR TERM 37 | pred_vv <- vector() 38 | for (i in (length(IV)-255):(length(IV)-1)) { 39 | spec_GARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 40 | mean.model = list(armaOrder = c(0, 0), 41 | external.regressors = cbind(log(prediction_garch[index(prediction_garch)[1:i]])))) 42 | fit_GARCH_reg = ugarchfit(spec=spec_GARCH_reg, data=log(IV[index(IV)[1:i]]), solver='hybrid') 43 | pre <- ugarchforecast(fit_GARCH_reg, n.ahead = 1) 44 | pred_vv <- c(pred_vv, sigma(pre)) 45 | } 46 | length(pred_vv) 47 | 48 | # OUT OF SAMPLE REGRESSION PREDICTION 49 | spec_GARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 50 | mean.model = list(armaOrder = c(0, 0), 51 | external.regressors = cbind(log(prediction_garch[index(prediction_garch)[1:1005]])))) 52 | fit_GARCH_reg = ugarchfit(spec=spec_GARCH_reg, data=log(IV[index(IV)[1:1005]])) 53 | show(fit_GARCH_reg) 54 | rand <- rnorm(255, 0, 1) 55 | pre_loggarch_reg <- 1.348545 + 0.543384 * log(prediction_garch[index(prediction_garch)[1006:1260]]) + rand * pred_vv 56 | pre_garch_reg <- exp(pre_loggarch_reg) 57 | mae_garch_reg <- sum(abs(pre_garch_reg - tail(IV, 255)))/255 58 | mae_garch_reg 59 | 60 | mae_garch <- sum(abs(prediction_garch[index(prediction_garch)[1006:1260]] - tail(IV, 255)))/255 61 | mae_garch 62 | 63 | ###################################################################################################### 64 | # GJRGARCH(1,1,1) IN-SAMPLE ESTIMATION 65 | prediction_gjrgarch <- vector() 66 | spec_gjrgarch = ugarchspec(variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)), 67 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 68 | distribution.model = "norm") 69 | fit_gjrgarch = ugarchfit(spec_gjrgarch, returns['2008-04-29/2013-04-26'], solver = 'hybrid') 70 | show(fit_gjrgarch) 71 | plot(sigma(fit_gjrgarch)*sqrt(255)) 72 | 73 | # GJRGARCH(1,1,1) ROLLING 74 | for (i in (length(returns)-1260):(length(returns)-1)) { 75 | spec = ugarchspec(variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)), 76 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 77 | distribution.model = "norm") 78 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 79 | pre <- ugarchforecast(fit, n.ahead = 22) 80 | prediction_gjrgarch <- c(prediction_gjrgarch, mean(sigma(pre)*sqrt(255))) 81 | } 82 | prediction_gjrgarch 83 | 84 | 85 | # ROLLING REGRESSION WITH GARCH ERROR TERM 86 | pred_vv <- vector() 87 | for (i in (length(IV)-255):(length(IV)-1)) { 88 | spec_gjrGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 89 | mean.model = list(armaOrder = c(0, 0), 90 | external.regressors = cbind(log(prediction_gjrgarch[index(prediction_gjrgarch)[1:i]])))) 91 | fit_gjrGARCH_reg = ugarchfit(spec=spec_gjrGARCH_reg, data=log(IV[index(IV)[1:i]]), solver='hybrid') 92 | pre <- ugarchforecast(fit_gjrGARCH_reg, n.ahead = 1) 93 | pred_vv <- c(pred_vv, sigma(pre)) 94 | } 95 | length(pred_vv) 96 | 97 | # OUT OF SAMPLE REGRESSION PREDICTION 98 | spec_gjrGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 99 | mean.model = list(armaOrder = c(0, 0), 100 | external.regressors = cbind(log(prediction_gjrgarch[index(prediction_gjrgarch)[1:1006]])))) 101 | fit_gjrGARCH_reg = ugarchfit(spec=spec_gjrGARCH_reg, data=log(IV[index(IV)[1:1006]])) 102 | show(fit_gjrGARCH_reg) 103 | rand <- rnorm(255, 0, 1) 104 | pre_loggjrgarch_reg <- 0.832368 + 0.697903 * log(prediction_gjrgarch[index(prediction_gjrgarch)[1006:1260]]) + rand * pred_vv 105 | pre_gjrgarch_reg <- exp(pre_loggjrgarch_reg) 106 | mae_gjrgarch_reg <- sum(abs(pre_gjrgarch_reg - tail(IV, 255)))/255 107 | mae_gjrgarch_reg 108 | 109 | mae_gjrgarch <- sum(abs(prediction_gjrgarch[index(prediction_gjrgarch)[1006:1260]] - tail(IV, 255)))/255 110 | mae_gjrgarch 111 | ###################################################################################################### 112 | # TGARCH(1,1) IN-SAMPLE ESTIMATION 113 | prediction_tgarch <- vector() 114 | spec_tgarch = ugarchspec(variance.model = list(model = "fGARCH", submodel = "TGARCH", garchOrder = c(1, 1)), 115 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 116 | distribution.model = "norm") 117 | fit_tgarch = ugarchfit(spec_tgarch, returns['2008-04-29/2013-04-26'], solver = 'hybrid') 118 | show(fit_tgarch) 119 | plot(sigma(fit_tgarch)*sqrt(255)) 120 | 121 | # TGARCH(1,1) ROLLING 122 | for (i in (length(returns)-1260):(length(returns)-1)) { 123 | spec = ugarchspec(variance.model = list(model = "fGARCH", submodel = "TGARCH", garchOrder = c(1, 1)), 124 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 125 | distribution.model = "norm") 126 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 127 | pre <- ugarchforecast(fit, n.ahead = 22) 128 | prediction_tgarch <- c(prediction_tgarch, mean(sigma(pre)*sqrt(255))) 129 | } 130 | prediction_tgarch 131 | 132 | # ROLLING REGRESSION WITH GARCH ERROR TERM 133 | pred_vv <- vector() 134 | for (i in (length(IV)-255):(length(IV)-1)) { 135 | spec_tGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 136 | mean.model = list(armaOrder = c(0, 0), 137 | external.regressors = cbind(log(prediction_tgarch[index(prediction_tgarch)[1:i]])))) 138 | fit_tGARCH_reg = ugarchfit(spec=spec_tGARCH_reg, data=log(IV[index(IV)[1:i]]), solver='hybrid') 139 | pre <- ugarchforecast(fit_tGARCH_reg, n.ahead = 1) 140 | pred_vv <- c(pred_vv, sigma(pre)) 141 | } 142 | length(pred_vv) 143 | 144 | # OUT OF SAMPLE REGRESSION PREDICTION 145 | spec_tGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 146 | mean.model = list(armaOrder = c(0, 0), 147 | external.regressors = cbind(log(prediction_tgarch[index(prediction_tgarch)[1:1006]])))) 148 | fit_tGARCH_reg = ugarchfit(spec=spec_tGARCH_reg, data=log(IV[index(IV)[1:1006]])) 149 | show(fit_tGARCH_reg) 150 | rand <- rnorm(255, 0, 1) 151 | pre_logtgarch_reg <- 0.999438 + 0.654764 * log(prediction_tgarch[index(prediction_tgarch)[1006:1260]]) + rand * pred_vv 152 | pre_tgarch_reg <- exp(pre_logtgarch_reg) 153 | mae_tgarch_reg <- sum(abs(pre_tgarch_reg - tail(IV, 255)))/255 154 | mae_tgarch_reg 155 | 156 | mae_tgarch <- sum(abs(prediction_tgarch[index(prediction_tgarch)[1006:1260]] - tail(IV, 255)))/255 157 | mae_tgarch 158 | 159 | ###################################################################################################### 160 | # EGARCH(1,1,1) IN-SAMPLE ESTIMATION 161 | prediction_egarch <- vector() 162 | spec_egarch = ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1, 1)), 163 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 164 | distribution.model = "norm") 165 | fit_egarch = ugarchfit(spec_egarch, returns['2008-04-29/2013-04-26'], solver = 'hybrid') 166 | show(fit_egarch) 167 | plot(sigma(fit_egarch)*sqrt(255)) 168 | title(main = "conditional volatility estimated in-sample using EGARCH model") 169 | 170 | # EGARCH(1,1,1) ROLLING 171 | for (i in (length(returns)-1260):(length(returns)-1)) { 172 | spec = ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1, 1)), 173 | mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), 174 | distribution.model = "norm") 175 | fit = ugarchfit(spec, returns[index(returns)[2:i]], solver = 'hybrid') 176 | pre <- ugarchforecast(fit, n.ahead = 22) 177 | prediction_egarch <- c(prediction_egarch, mean(sigma(pre)*sqrt(255))) 178 | } 179 | prediction_egarch 180 | 181 | # ROLLING REGRESSION WITH GARCH ERROR TERM 182 | pred_vv <- vector() 183 | for (i in (length(IV)-255):(length(IV)-1)) { 184 | spec_eGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 185 | mean.model = list(armaOrder = c(0, 0), 186 | external.regressors = cbind(log(prediction_egarch[index(prediction_egarch)[1:i]])))) 187 | fit_eGARCH_reg = ugarchfit(spec=spec_eGARCH_reg, data=log(IV[index(IV)[1:i]]), solver='hybrid') 188 | pre <- ugarchforecast(fit_eGARCH_reg, n.ahead = 1) 189 | pred_vv <- c(pred_vv, sigma(pre)) 190 | } 191 | length(pred_vv) 192 | 193 | # OUT OF SAMPLE REGRESSION PREDICTION 194 | spec_eGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 195 | mean.model = list(armaOrder = c(0, 0), 196 | external.regressors = cbind(log(prediction_egarch[index(prediction_egarch)[1:1006]])))) 197 | fit_eGARCH_reg = ugarchfit(spec=spec_eGARCH_reg, data=log(IV[index(IV)[1:1006]])) 198 | show(fit_eGARCH_reg) 199 | rand <- rnorm(255, 0, 1) 200 | pre_logegarch_reg <- 1.41585 + 0.53543 * log(prediction_egarch[index(prediction_egarch)[1006:1260]]) + rand * pred_vv 201 | pre_egarch_reg <- exp(pre_logegarch_reg) 202 | mae_egarch_reg <- sum(abs(pre_egarch_reg - tail(IV, 255)))/255 203 | mae_egarch_reg 204 | 205 | mae_egarch <- sum(abs(prediction_egarch[index(prediction_egarch)[1006:1260]] - tail(IV, 255)))/255 206 | mae_egarch 207 | 208 | ##################################################################################################### 209 | df_insample <- data.frame("IV"=IV_insample, "GARCH"=sigma(fit_garch)*sqrt(255), "EGARCH"=sigma(fit_egarch)*sqrt(255), 210 | "GJRGARCH"=sigma(fit_gjrgarch)*sqrt(255), "TGARCH"=sigma(fit_tgarch)*sqrt(255)) 211 | df_prediction <- data.frame("IV"=IV_pred, "GARCH"=prediction_garch, "EGARCH"=prediction_egarch, 212 | "GJRGARCH"=prediction_gjrgarch, "TGARCH"=prediction_tgarch) 213 | df_pre_reg <- data.frame("IV"=tail(IV, 255), "GARCH"=pre_garch_reg, "EGARCH"=pre_egarch_reg, 214 | "GJRGARCH"=pre_gjrgarch_reg, "TGARCH"=pre_tgarch_reg) 215 | 216 | pairs(~AAPL_IV+GARCH+EGARCH+GJRGARCH+TGARCH, data=df_insample, main="Graph 1 Estimated In-Sample Conditional Volatility Scatterplot Matrix") 217 | pairs(~AAPL_IV+GARCH+EGARCH+GJRGARCH+TGARCH, data=df_prediction, main="Graph 3 Out-Of-Sample Volatility Forecast Scatterplot Matrix") 218 | pairs(~AAPL_IV+GARCH+EGARCH+GJRGARCH+TGARCH, data=df_pre_reg, main="Graph 5 GARCH Regression Volatility Forecast Scatterplot Matrix") 219 | 220 | myColors <- c("red", "darkgreen", "goldenrod", "darkblue", "darkviolet") 221 | plot(x = IV_insample, type="l", xlab = "Time", ylab = "Volatility(%)",ylim = c(10, 140), major.ticks="auto", minor.ticks=TRUE, main = "Graph 2 in-sample estimates of conditional volatility", col = "red") 222 | lines(x = as.xts(df_insample["GARCH"]), col = "darkgreen") 223 | lines(x = as.xts(df_insample["EGARCH"]), col = "goldenrod") 224 | lines(x = as.xts(df_insample["GJRGARCH"]), col = "darkblue") 225 | lines(x = as.xts(df_insample["TGARCH"]), col = "darkviolet") 226 | legend(x = 'right', legend = c("IV", "GARCH", "EGARCH", "GJR-GARCH", "TGARCH"), 227 | lty = 1, col = myColors) 228 | 229 | myColors <- c("red", "darkgreen", "goldenrod", "darkblue", "darkviolet") 230 | plot(x = IV_pred, type="l", xlab = "Time", ylab = "Volatility(%)",ylim = c(10, 80), major.ticks="auto", minor.ticks=TRUE, main = "Graph 4 out-of-sample estimate of volatility", col = "red") 231 | lines(x = as.xts(df_prediction["GARCH"]), col = "darkgreen") 232 | lines(x = as.xts(df_prediction["EGARCH"]), col = "goldenrod") 233 | lines(x = as.xts(df_prediction["GJRGARCH"]), col = "darkblue") 234 | lines(x = as.xts(df_prediction["TGARCH"]), col = "darkviolet") 235 | abline(v = as.POSIXct("2017-04-25"), lheight=0.5, lwd=2) 236 | legend(x = 'right', legend = c("IV", "GARCH", "EGARCH", "GJR-GARCH", "TGARCH"), 237 | lty = 1, col = myColors) 238 | 239 | myColors <- c("red", "darkgreen", "goldenrod", "darkblue", "darkviolet") 240 | plot(x = tail(IV, 255), type="l", xlab = "Time", ylab = "Volatility(%)",ylim = c(10, 80), major.ticks="auto", minor.ticks=TRUE, main = "Graph 6 GARCH regression estimate of volatility", col = "red") 241 | lines(x = as.xts(df_pre_reg["GARCH"]), col = "darkgreen") 242 | lines(x = as.xts(df_pre_reg["EGARCH"]), col = "goldenrod") 243 | lines(x = as.xts(df_pre_reg["GJRGARCH"]), col = "darkblue") 244 | lines(x = as.xts(df_pre_reg["TGARCH"]), col = "darkviolet") 245 | legend(x = 'right', legend = c("IV", "GARCH", "EGARCH", "GJR-GARCH", "TGARCH"), 246 | lty = 1, col = myColors) 247 | 248 | mae_garch <- sum(abs(prediction_garch - IV))/1260 249 | mae_garch 250 | 251 | mae_egarch <- sum(abs(prediction_egarch - IV))/1260 252 | mae_egarch 253 | 254 | mae_gjrgarch <- sum(abs(prediction_gjrgarch - IV))/1260 255 | mae_gjrgarch 256 | 257 | mae_tgarch <- sum(abs(prediction_tgarch - IV))/1260 258 | mae_tgarch 259 | 260 | acf(diff(log(IV))[2: length(IV)], lag=1) 261 | dm.test(prediction_egarch - IV, prediction_tgarch - IV) 262 | 263 | p_ca <- prcomp(df_prediction[, 2:5], center=TRUE, scale=TRUE ) 264 | predd1 <- scale(as.matrix(df_prediction[,2:5]), 265 | center = p_ca$center, scale = p_ca$scale)%*% p_ca$rotation[, 1] 266 | predd2 <- scale(as.matrix(df_prediction[,2:5]), 267 | center = p_ca$center, scale = p_ca$scale)%*% p_ca$rotation[, 2] 268 | # ROLLING REGRESSION WITH GARCH ERROR TERM 269 | pred_vv <- vector() 270 | for (i in (length(IV)-255):(length(IV)-1)) { 271 | spec_pcaGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 272 | mean.model = list(armaOrder = c(0, 0), 273 | external.regressors = cbind(predd1[1:i]))) 274 | fit_pcaGARCH_reg = ugarchfit(spec=spec_pcaGARCH_reg, data=log(IV[index(IV)[1:i]]), solver='hybrid') 275 | pre <- ugarchforecast(fit_pcaGARCH_reg, n.ahead = 1) 276 | pred_vv <- c(pred_vv, sigma(pre)) 277 | } 278 | length(pred_vv) 279 | 280 | # OUT OF SAMPLE REGRESSION PREDICTION 281 | spec_pcaGARCH_reg = ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 282 | mean.model = list(armaOrder = c(0, 0), 283 | external.regressors = cbind(predd1[1:1006]))) 284 | 285 | fit_pcaGARCH_reg = ugarchfit(spec=spec_pcaGARCH_reg, data=log(IV[index(IV)[1:1006]])) 286 | show(fit_pcaGARCH_reg) 287 | pre_logpcagarch_reg <- 3.160514 - 0.048081 * predd1[1006:1260] + rand * pred_vv 288 | pre_pcagarch_reg <- exp(pre_logpcagarch_reg) 289 | mae_pcagarch_reg <- sum(abs(pre_pcagarch_reg - tail(IV, 255)))/255 290 | mae_pcagarch_reg 291 | 292 | --------------------------------------------------------------------------------