## Loading package library(tvgarch) ## Computing smooth transition functions # Creating transition variable, st = t/n # Time scaled between 0 and 1 n <- 1000 stime <- seq(1:n)/n # Creating labels for plots xlabExpr <- expression(paste("Scaled time: ", s[t], " = ", t,"/", n, sep = "")) ylabExpr <- expression(paste(G[t]), sep = "") logisticExpr1 <- expression(paste(G(gamma,c,t/n), " = ", bgroup("{", 1+exp(-gamma(t/n-c)), "}" )^-1, sep = "")) logisticExpr2 <- expression(paste(G(gamma,bold(c),t/n), " = ", bgroup("{", 1+exp(-gamma(t/n-c[1]) (t/n-c[2])), "}" )^-1, sep = "")) # Figure 1 (left part): Plot with one transition G1t <- tv(speed = 10, location = 0.5, n = 1000) plot(stime, G1t, xlab = xlabExpr, ylab = ylabExpr, type = "l", main = logisticExpr1) lines(stime, tv(speed = 25, location = 0.5, n = 1000)) lines(stime, tv(speed = 50, location = 0.5, n = 1000)) # Figure 1 (right part): Plot with two transitions G2t <- tv(speed = 10, location = c(0.25, 0.75), n = 1000) plot(stime, G2t, xlab = xlabExpr, ylab = ylabExpr, type = "l", main = logisticExpr2, ylim =c(0,1)) lines(stime, tv(speed = 25, location = c(0.25, 0.75), n = 1000)) lines(stime, tv(speed = 50, location = c(0.25, 0.75), n = 1000)) ## Simulating the TV-component n <- 1000 stime <- seq(1:n)/n G1t <- tv(speed = 10, location = 0.5, n = 1000) G2t <- tv(speed = 10, location = c(0.25, 0.75), n = 1000) ## Simulating a univariate TV-GARCH model # Default: single transition with one location parameter at c = 0.5 set.seed(123) uSim <- tvgarchSim(n = 1000) # Figure 2: plot(uSim, xlab = "", ylab = "Simulated Return") ## Simulating a multivariate TV-GARCH model # With no covariates, X mSim <- mtvgarchSim(n = 1000, order.h = c(1,1,0, 1,1,1), asym = 0.01) # With covariates, X mX <- matrix(rnorm(1000 * 2)^2, 1000, 2) # simulate the covariates mSim2 <- mtvgarchSim(n = 1000, order.x = c(1,0,0,1), xreg = mX, par.xreg = c(0.01, 0.02)) # With volatility spillovers (lagged returns 2 affects variance 1) mSim3 <- mtvgarchSim(n = 1000, order.x = c(0,1,0,0), par.xreg = 0.02, verbose = TRUE) mSim3$par.h ## Empirical illustration # Section 5.1: Data library(dccmidas) stocks_p <- merge(ftse100[,"close_price"], sp500[,"close_price"], join = "left") stocks_p <- merge(stocks_p, nasdaq[,"close_price"], join = "left") stocks_p <- na.omit(stocks_p) colnames(stocks_p) <- c("ftse100", "sp500", "nasdaq") stocks_p <- stocks_p[index(stocks_p) >= "2003-12-31 GMT",] stocks_p <- stocks_p[index(stocks_p) < "2014-01-01 GMT",] stocks_r <- (diff(log(stocks_p))*100)[-1,] # Section 5.2: Univariate case nasdaq_r <- stocks_r[,"nasdaq"] plot(index(nasdaq_r), nasdaq_r, main = "", type = "l", ylab = "", xlab = "") # Identifying the number of transitions tvgarchTestObj <- tvgarchTest(y = nasdaq_r) tvgarchTestObj # Estimating a nonstationary TV(2)-GARCH(1,1) model tvgarchEst <- tvgarch(y = nasdaq_r, order.g = 2) tvgarchEst # Figure 4: Plotting the estimated logistic transition function stime2 <- tvgarchEst$xtv nasdaqG <- tv(speed = tvgarchEst$par.g[3], location = tvgarchEst$par.g[4:5], xtv = stime2, opt = 2, order.g = 2) plot(index(nasdaq_r), nasdaqG, type = "l", ylab = "", xlab = "") title("Logistic transition function") deltasEst <- coef(tvgarchEst, spec = "tv")[c("intercept.g","size1")] sum(deltasEst) # Post estimation # Dating transitions coef.g <- coef(tvgarchEst, spec = "tv") # extracts transition coefficients c11 <- coef.g["location1"] # extracts first location stime2 <- tvgarchEst$xtv # extracts transition variable stime2[which.min(abs(stime2-c11))] c12 <- coef.g["location2"] # extracts second location stime2[which.min(abs(stime2-c12))] # Figure 5: Plotting volatilities of estimated model plot(tvgarchEst, spec = "tv", ylim = c(0.5,4.5), main = "") plot(tvgarchEst, spec = "garch", ylim = c(0.5,4.5)) plot(sqrt(fitted(tvgarchEst)), ylim = c(0.5,4.5), type = "l", ylab = "", xlab = "") title("TV-GARCH") # Estimated equations in LATEX format toLatex(tvgarchEst) # Section 5.3: Multivariate case: m <- ncol(stocks_r) stocks_k <- matrix(0, m, 1) for (i in 1:m){ stocks_k[i] <- summary(tvgarchTest(stocks_r[,i])) } rownames(stocks_k) <- colnames(stocks_r) colnames(stocks_k) <- "k1" stocks_k # Nonstationary variance equations: CCC-TV-GARCH model ccctvgarchEst <- mtvgarch(y = stocks_r, order.g = stocks_k) ccctvgarchEst # Post estimation plot(ccctvgarchEst, spec = "tv") toLatex(ccctvgarchEst) # Stationary variance equations: CCC-GARCH model cccgarchEst <- mtvgarch(y = stocks_r, order.g = NULL) # Estimating the multivariate model with dynamic conditional correlations dcctvgarchEst <- mtvgarch(y = stocks_r, order.g = stocks_k, dcc = TRUE) dcctvgarchEst par.dccEst <- coef(dcctvgarchEst, spec = "cc")$par.dcc sum(par.dccEst) # Figure 6: Plotting the conditional correlations CCC <- fitted(ccctvgarchEst, spec = "cc") DCC <- fitted(dcctvgarchEst, spec = "cc") plot(index(stocks_r), DCC[,1], main = "FTSE 100 and S&P 500", type = "l", ylab = "", xlab = "") lines(index(stocks_r), CCC[,1], col = "blue") plot(index(stocks_r), DCC[,2], main = "FTSE 100 and NASDAQ", type = "l", ylab = "", xlab = "") lines(index(stocks_r), CCC[,2], col = "blue") plot(index(stocks_r), DCC[,3], main = "S&P 500 and NASDAQ", type = "l", ylab = "", xlab = "") lines(index(stocks_r), CCC[,3], col = "blue") # Comparing TV-GARCH models using BIC: # CCC-TV-GARCH LL_CCC_TV_GARCH <- logLik(ccctvgarchEst)$ccc LL_CCC_TV_GARCH # DCC-TV-GARCH LL_DCC_TV_GARCH <- logLik(dcctvgarchEst)$dcc LL_DCC_TV_GARCH # Compare BICs BIC(logLik(ccctvgarchEst)$ccc) BIC(logLik(dcctvgarchEst)$dcc) ## Output file sessionInfo()