# R 3.3.1 # Dynamic Dependency: US stock to other stock indices # wrt returns or volatility # VAR(y, p = 1, type = c("const", "trend", "both", "none"), # season = NULL, exogen = NULL, lag.max = NULL, # ic = c("AIC", "HQ", "SC", "FPE")) library("tseries") library("zoo") library("vars") ### Read the data require(quantmod) # Finanzdaten download spc <- new.env() dax <- new.env() setDefaults(getSymbols,src="yahoo") getSymbols("^GSPC", env = spc, from = "1995-01-01", return.class = "xts", to = "2017-10-01" ) getSymbols("^GDAXI", env = dax, from = "1995-01-01", return.class = "xts", to = "2017-10-01" ) # read in series are of xts type, plot is different for xts and ts x.c <- spc$GSPC[,4]; y.c <- dax$GDAXI[,4]; nx <- length(x.c); ny <- length(y.c); ### Plotting univariate data plot( merge(as.zoo(x.c),as.zoo(y.c)) , main = "^GSPC , ^GDAXI") # plot price series, one scale, indiv NAs rx <- diff(log(x.c))[2:nx]; ry <- diff(log(y.c))[2:ny]; plot(merge(as.zoo(rx),as.zoo(ry)), main = "^GSPC , ^GDAXI") # plot returns # simultaneous analysis xy <- na.omit(merge(x.c,y.c, all=FALSE) ) # one scale no indiv NAs nxy <- length(xy[,1]) rxy <- diff(log(xy))[2:nxy] ### Testing for white noise rx_v <- as.vector(rxy[,1]); ry_v <- as.vector(rxy[,2]); acf(rx_v); # acf of returns acf(ry_v, na.action=na.pass); Box.test(rx_v, lag = 10, type = "Ljung-Box", fitdf = 0) # Ljung-Box tests Box.test(ry_v, lag = 10, type = "Ljung-Box", fitdf = 0) ## VAR pp <- 2 mod <- VAR(rxy, p = pp, type = "const", exogen = NULL ) summary(mod) ### total sample ### end ## choose the subperiods ## September 11, 2001 #start0_1 = "2001-06-10"; end0_1 = "2001-09-10"; #start0_2 = "2001-09-13"; end0_2 = "2001-12-31"; ## Lehman Brothers, Chapter 11 bankruptcy protection Sep 15,2008 start0_1 = "2006-01-01"; end0_1 = "2008-07-01"; start0_2 = "2008-10-01"; end0_2 = "2009-12-31"; i.x <- index(xy) i.x1 <- i.x[ as.Date(start0_1) <= i.x & i.x <= as.Date(end0_1) ] i.x2 <- i.x[ as.Date(start0_2) <= i.x & i.x <= as.Date(end0_2) ] # subsamples 1+2 are only a subsample of the total sample ### 1st subsample rxy1 <- rxy[i.x1] plot(merge(as.zoo(rxy1[,1]),as.zoo(rxy1[,2])), main = "Returns: ^GSPC , ^GDAXI, subsample 1") ## VAR pp <- 2 mod1 <- VAR(rxy1, p = pp, type = "const", exogen = NULL ) summary(mod1) ### 2nd subsample rxy2 <- rxy[i.x2] plot(merge(as.zoo(rxy2[,1]),as.zoo(rxy2[,2])), main = "Returns: ^GSPC , ^GDAXI, subsample 2") ## VAR pp <- 2 mod2 <- VAR(rxy2, p = pp, type = "const", exogen = NULL ) summary(mod2) ## Comment: if class(x) of a data-vector x is # "ts" or "zoo" lag(x,-1) means lag 1 of x # "xts" and "zoo" lag(x, 1) means lag 1 of x ! ## EXERCISE: # (1) Calculate a VAR with fixed order at least for one sample. # (2) Write down the estimated model in the conventional notation. # (3) Interprete the dynamic dependencies wrt # unidirectional, feedback and coupled relationship. # (4) Is there a linear dynamic relationship?