Created
July 6, 2017 22:53
-
-
Save alexchinco/a147f0f19ad8f353d6f2b3b8945bb0ea to your computer and use it in GitHub Desktop.
add upper/lower bounds and test for constant variance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
options(width = 80, digits = 6, digits.secs = 6) | |
sessionInfo() | |
rm(list=ls()) | |
library(foreign) | |
library(wmtsa) | |
library(foreach) | |
library(doMC) | |
registerDoMC(7) | |
################################################################################ | |
## define directories | |
dat_dir <- "~/Dropbox/research/fast_trading_priced_noise/data/" | |
################################################################################ | |
## create vector of permnos | |
permnos <- list.files(path = paste(dat_dir, "nysef10/csv/", sep = "")) | |
num_permnos <- length(permnos) | |
for (i in 1:num_permnos) { | |
permnos[i] <- unlist(strsplit(permnos[i], "\\."))[1] | |
} | |
################################################################################ | |
## Compute following statistics for first 20 permnos | |
## 1. wavelet variance (value) | |
## 2. upper/lower bounds (upper_/lower_bound) | |
## 3. probability that variance is constant | |
if (TRUE == TRUE) { | |
for (i in 1:20) { | |
print(i) | |
data <- read.csv(paste(dat_dir, "nysef10/csv/", permnos[i], ".csv", sep = ""), stringsAsFactors = FALSE) | |
data <- data[, c("SYMBOL", "DATE", "time", "vol")] | |
names(data) <- c("ticker", "date", "time", "vol") | |
data$date <- as.Date(as.character(data$date), format = "%Y%m%d") | |
data <- data[data$date >= as.Date("2002-01-01"), ] | |
data <- data[data$ticker != "", ] | |
if (dim(data)[1] > 0) { | |
data$month <- format(data$date, format = "%Y-%m") | |
months <- sort(unique(data$month)) | |
num_months <- length(months) | |
wvar <- foreach(m = 1:num_months, .combine = "rbind", .errorhandling = "remove") %dopar% { | |
volume <- data[data$month == months[m], ]$vol | |
results <- wavVar(volume, wavelet = "haar") | |
len_results <- dim(summary(results)$conf.mat)[2] | |
test <- wavVarTest(volume, wavelet = "haar")$pass | |
len_test <- dim(test)[1] | |
test <- test[len_test:1, ] | |
if (len_test < len_results) { | |
test <- rbind(test, | |
matrix(rep(TRUE, 3 * (len_results - len_test)), ncol = 3) | |
) | |
} | |
total_var <- sum(summary(results)$vmat[2, ]) | |
panel <- data.frame(permno = permnos[i], | |
t = months[m], | |
h = summary(results)$conf.mat[2, ], | |
value = summary(results)$vmat[2, ] / total_var, | |
lower_bound = results$confidence$n3$low / total_var, | |
upper_bound = results$confidence$n3$high / total_var, | |
var_is_const = test[, 2] | |
) | |
return(panel) | |
} | |
save(wvar, file = paste(dat_dir, "add_se/", "permno-", permnos[i],".Rdata", sep = "")) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment