############# EMH Hypothesis: Autocorrelation & VR Tests & (Code #EMH) ############# ########## Autocorrelation & VR Tests ## Need to install package vrtest ###### Reading Monthly Data EMH_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/crsp_ew_vw_m.csv",head=TRUE,sep=",") x_date <- EMH_da$DATE lr_vw <- EMH_da$vwretd # Value weighted CRSP Return (including distributions) lr_ew <- EMH_da$ewretd # Equal weighted CRSP Return (including distributions) lr_sp <- EMH_da$sprtn # SP500 total return T <- length(lr_vw) #### Descriptive stats and ACF y <- lr_vw summary(y) SE_rho <- 1/sqrt(T) # Asymptotic SE for rho’s: |rho| > 2 * SE => significant SE_rho y_acf <- acf(y) y_acf #### Q & LB Autocorrelation Tests & Q* Automatic Lag Selection Test Box.test(y, lag = 4, type="Box-Pierce") Box.test(y, lag = 12, type="Box-Pierce") Box.test(y, lag = 4, type="Ljung-Box") Box.test(y, lag = 12, type="Ljung-Box") library(vrtest) Auto.Q(y, 12) # Q* test automatic selection of p #### Q & LB Heteroscedasticity Tests y2 <- y^2 Box.test(y2, lag = 12, type="Ljung-Box") Box.test(y2, lag = 6, type="Ljung-Box") Box.test(y2, lag = 3, type="Ljung-Box") #### AR(1) Regression y_1 <- y[1:(T-1)] # Estimation period data y_0 <- y[2:T] # Estimation period data fit_ar1 <- lm(y_0 ~ y_1) summary(fit_ar1) #### AR(p) Regression p_lag <- 12 y_lag <- matrix(0,T-p_lag,p_lag) a <- 1 while (a <= p_lag) { za <- y[a:(T-p_lag+a-1)] y_lag[,a] <- za a <- a+1 } y_lag[1:12,] fit_ar_p <- lm(y[(p_lag+1):T] ~ y_lag) summary(fit_ar_p) b_ar_p <- fit_ar_p$coefficients library(sandwich) NW <- NeweyWest(fit_ar_p, lag = 12, prewhite = FALSE) # with 12 lags SE_NW <- diag(sqrt(abs(NW))) t_NW <- b_ar_p/SE_NW SE_NW t_NW ####### VR Tests with vrtest package (need to install it first) kvec <- c(2,3,12) # Vector with different q vr_1 <- VR.minus.1(y, kvec) # Stat should be close to 0 if RW vr_1 sqrt(T*2)/sqrt(2)*vr_1$VR.kvec[1] # VR test for q=kvec[1] = 2 (Standard Normal dist) sqrt(T*kvec)/sqrt(2*(kvec-1))*vr_1$VR.kvec # VR test for each q=kvec[i] (Standard Normal dist) Auto.VR(y) # VR Test with automatic q selection AutoBoot.test(y, nboot=1000, wild="Normal",prob=c(0.025,0.975)) # Bootstrap for Auto.VR, as in Choi (1999) Lo.Mac(y, kvec) # Lo-MacKinlay's M1 and M2 tests Boot.test(y, kvec, nboot=1000, wild="Normal", prob=c(0.025,0.975)) # Bootstrap for Lo.Mac M2 tests, as in Kim (2006) Wald(y, kvec) # RS Wald test Chen.Deo(y, kvec) # QP Wald test ###### Reading Daily Data EMH_d_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/crsp_ew_vw_d.csv",head=TRUE,sep=",") lr_vw_d <- EMH_d_da$vwretd # Value weighted CRSP returns (including distributions) lr_ew_d <- EMH_d_da$ewretd # Equal weighted CRSP returns (including distributions) T <- length(lr_ew_d) y <- lr_vw_d kvec <- c(5,20,60) # Vector with different q vr_1 <- VR.minus.1(y, kvec) # Stat should be close to 0 if RW vr_1 sqrt(T*kvec)/sqrt(2*(kvec-1))*vr_1$VR.kvec # VR test for each q=kvec[i] (Standard Normal dist) AutoBoot.test(y, nboot=1000, wild="Normal",prob=c(0.025,0.975)) # Bootstrap for Auto.VR, as in Choi (1999) Lo.Mac(y, kvec) # Lo-MacKinlay's M1 and M2 tests Boot.test(y, kvec, nboot=1000, wild="Normal", prob=c(0.025,0.975)) # Bootstrap for Lo.Mac M2 tests, as in Kim (2006) Wald(y, kvec) # RS Wald test Chen.Deo(y, kvec) # QP Wald test ###### Reading Annual Data EMH_d_a <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/goyal-welch-a_27.csv",head=TRUE,sep=",") names(EMH_d_a) x_sp_a <- EMH_d_a$sp500index # Value weighted CRSP returns (including distributions) T <- length(x_sp) lr_sp_a <- log(x_sp_a[-1]/x_sp_a[-T]) y <- lr_sp_a kvec <- c(5,20,60) # Vector with different q vr_1 <- VR.minus.1(y, kvec) # Stat should be close to 0 if RW vr_1 sqrt(T*kvec)/sqrt(2*(kvec-1))*vr_1$VR.kvec # VR test for each q=kvec[i] (Standard Normal dist) AutoBoot.test(y, nboot=1000, wild="Normal",prob=c(0.025,0.975)) # Bootstrap for Auto.VR, as in Choi (1999) Lo.Mac(y, kvec) # Lo-MacKinlay's M1 and M2 tests Boot.test(y, kvec, nboot=1000, wild="Normal", prob=c(0.025,0.975)) # Bootstrap for Lo.Mac M2 tests, as in Kim (2006)