Skip to content

Instantly share code, notes, and snippets.

@alexchinco
Created July 26, 2014 21:03
Show Gist options
  • Save alexchinco/103f458b16b495268478 to your computer and use it in GitHub Desktop.
Save alexchinco/103f458b16b495268478 to your computer and use it in GitHub Desktop.
Create figures for wavelet variance post.
## Prep workspace
rm(list=ls())
library(foreign)
library(grid)
library(plyr)
library(ggplot2)
library(tikzDevice)
library(wmtsa)
print(options('tikzLatexPackages'))
options(tikzLatexPackages =
c("\\usepackage{tikz}\n",
"\\usepackage[active,tightpage,psfixbb]{preview}\n",
"\\PreviewEnvironment{pgfpicture}\n",
"\\setlength\\PreviewBorder{0pt}\n",
"\\usepackage{amsmath}\n",
"\\usepackage{xfrac}\n"
)
)
setTikzDefaults(overwrite = FALSE)
print(options('tikzLatexPackages'))
library(reshape)
library(vars)
library(scales)
library(zoo)
set.seed(12345)
scl.str.DAT_DIR <- "~/Dropbox/research/correlation_term_structure/data/"
scl.str.FIG_DIR <- "~/Dropbox/research/correlation_term_structure/figures/"
## Define simulation parameters
scl.int.T_DAY <- (6.5 * 60)
scl.int.DAYS <- 63
scl.int.T <- scl.int.T_DAY * scl.int.DAYS
vec.int.T <- seq(1, scl.int.T)
scl.flt.SIG <- 0.010
scl.flt.TET <- 0.50/scl.int.T_DAY
scl.flt.SHOCK_PROB <- 0.05
## Simulate time series
vec.flt.X <- rep(0, scl.int.T)
vec.flt.X[1] <- 1
vec.flt.dX <- rep(0, scl.int.T)
vec.flt.SHOCK <- rep(NA, scl.int.T)
vec.flt.DRAW <- runif(scl.int.DAYS, 0, 1)
for (d in 1:scl.int.DAYS) {
vec.int.ROWS <- seq((d-1) * scl.int.T_DAY + 1,d * scl.int.T_DAY)
if (vec.flt.DRAW[d] < scl.flt.SHOCK_PROB) {
scl.flt.SIGN <- runif(1, 0, 1)
if (scl.flt.SIGN > 0.50) {
vec.flt.SHOCK[vec.int.ROWS] <- scl.flt.TET
} else {
vec.flt.SHOCK[vec.int.ROWS] <- - scl.flt.TET
}
} else {
vec.flt.SHOCK[vec.int.ROWS] <- 0
}
}
for (t in 2:scl.int.T) {
vec.flt.dX[t] <- vec.flt.SHOCK[t] + scl.flt.SIG * rnorm(1,0,1)
vec.flt.X[t] <- vec.flt.X[t-1] + vec.flt.dX[t]
}
mat.df.DATA <- data.frame(t = seq(1, scl.int.T),
x = vec.flt.dX
)
## Create white noise process
vec.flt.dX2 <- rnorm(scl.int.T, 0, sd(vec.flt.dX))
mat.df.DATA$x2 <- vec.flt.dX2
## Plot time series
if (TRUE == TRUE) {
mat.df.PLOT <- data.frame(x = seq(1,scl.int.T)/scl.int.T_DAY,
y = vec.flt.dX,
s = vec.flt.SHOCK
)
mat.df.PLOT$d <- ceiling(mat.df.PLOT$x)
mat.df.PLOT <- ddply(mat.df.PLOT, c('d'), function(X)c(sum(X$y), round(mean(X$s),4)))
names(mat.df.PLOT) <- c("x", "y", "s")
theme_set(theme_bw())
scl.str.RAW_FILE <- 'plot--why-use-wavelet-variance--daily-shocks--time-series--25jul2014'
scl.str.TEX_FILE <- paste(scl.str.RAW_FILE,'.tex',sep='')
scl.str.PDF_FILE <- paste(scl.str.RAW_FILE,'.pdf',sep='')
scl.str.PNG_FILE <- paste(scl.str.RAW_FILE,'.png',sep='')
scl.str.AUX_FILE <- paste(scl.str.RAW_FILE,'.aux',sep='')
scl.str.LOG_FILE <- paste(scl.str.RAW_FILE,'.log',sep='')
tikz(file = scl.str.TEX_FILE, height = 2, width = 7, standAlone=TRUE)
obj.gg2.PLOT <- ggplot()
obj.gg2.PLOT <- obj.gg2.PLOT + scale_colour_brewer(palette="Set1")
for (r in 1:dim(mat.df.PLOT)[1]) {
if (mat.df.PLOT$s[r] != 0) {
obj.gg2.PLOT <- obj.gg2.PLOT + geom_vline(xintercept = mat.df.PLOT$x[r], size = 2, colour = "red", alpha = 0.25)
}
}
obj.gg2.PLOT <- obj.gg2.PLOT + geom_path(data = mat.df.PLOT,
aes(x = x,
y = y
),
size = 0.50
)
obj.gg2.PLOT <- obj.gg2.PLOT + xlab("$t$ $[\\text{days}]$")
obj.gg2.PLOT <- obj.gg2.PLOT + ylab("$r_t$")
obj.gg2.PLOT <- obj.gg2.PLOT + scale_x_continuous(breaks = c(0, 21, 42, 63))
obj.gg2.PLOT <- obj.gg2.PLOT + theme(plot.margin = unit(c(1,0.15,0.15,-0.15), "lines"),
legend.position = c(0.25,0.51),
legend.background = element_blank(),
axis.title = element_text(size = 10),
axis.text = element_text(size = 6),
plot.title = element_text(vjust = 1.75),
panel.grid.minor = element_blank()
)
obj.gg2.PLOT <- obj.gg2.PLOT + ggtitle("Daily Time Series")
print(obj.gg2.PLOT)
dev.off()
system(paste('lualatex', file.path(scl.str.TEX_FILE)), ignore.stdout = TRUE)
system(paste('convert -density 600', file.path(scl.str.PDF_FILE), ' ', file.path(scl.str.PNG_FILE)))
system(paste('mv ', scl.str.PNG_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.TEX_FILE, sep = ''))
system(paste('mv ', scl.str.PDF_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.AUX_FILE, sep = ''))
system(paste('rm ', scl.str.LOG_FILE, sep = ''))
}
## Estimate wavelet coefficients
obj.dwt.RESULT <- wavMODWT(vec.flt.dX, wavelet="d8", keep.series=TRUE)
summary(obj.dwt.RESULT)
## Plot wavelet decomposition over time
if (TRUE == TRUE) {
mat.df.PLOT <- data.frame(t = seq(1,scl.int.T)/scl.int.T_DAY,
d1 = as.numeric((obj.dwt.RESULT$data$d1)),
d2 = as.numeric((obj.dwt.RESULT$data$d2)),
d3 = as.numeric((obj.dwt.RESULT$data$d3)),
d4 = as.numeric((obj.dwt.RESULT$data$d4)),
d5 = as.numeric((obj.dwt.RESULT$data$d5)),
d6 = as.numeric((obj.dwt.RESULT$data$d6)),
d7 = as.numeric((obj.dwt.RESULT$data$d7)),
d8 = as.numeric((obj.dwt.RESULT$data$d8)),
d9 = as.numeric((obj.dwt.RESULT$data$d9)),
d10 = as.numeric((obj.dwt.RESULT$data$d10)),
d11 = as.numeric((obj.dwt.RESULT$data$d11)),
d12 = as.numeric((obj.dwt.RESULT$data$d12)),
d13 = as.numeric((obj.dwt.RESULT$data$d13)),
d14 = as.numeric((obj.dwt.RESULT$data$d14))
)
mat.df.PLOT <- melt(mat.df.PLOT, c("t"))
names(mat.df.PLOT) <- c("t", "variable", "value")
mat.df.PLOT$variable <- factor(mat.df.PLOT$variable,
levels = c("d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "d11", "d12", "d13", "d14"),
labels = c("$h = 0$", "$h = 1$", "$h = 2$", "$h = 3$", "$h = 4$", "$h = 5$", "$h = 6$", "$h = 7$", "$h = 8$", "$h = 9$", "$h = 10$", "$h = 11$", "$h = 12$", "$h = 13$")
)
theme_set(theme_bw())
scl.str.RAW_FILE <- 'plot--why-use-wavelet-variance--daily-shocks--wavelet-coefficients--25jul2014'
scl.str.TEX_FILE <- paste(scl.str.RAW_FILE,'.tex',sep='')
scl.str.PDF_FILE <- paste(scl.str.RAW_FILE,'.pdf',sep='')
scl.str.PNG_FILE <- paste(scl.str.RAW_FILE,'.png',sep='')
scl.str.AUX_FILE <- paste(scl.str.RAW_FILE,'.aux',sep='')
scl.str.LOG_FILE <- paste(scl.str.RAW_FILE,'.log',sep='')
tikz(file = scl.str.TEX_FILE, height = 6, width = 7, standAlone=TRUE)
obj.gg2.PLOT <- ggplot()
obj.gg2.PLOT <- obj.gg2.PLOT + scale_colour_brewer("Set1")
obj.gg2.PLOT <- obj.gg2.PLOT + geom_path(data = mat.df.PLOT,
aes(x = t,
y = value,
group = variable
),
size = 0.50
)
obj.gg2.PLOT <- obj.gg2.PLOT + xlab("")
obj.gg2.PLOT <- obj.gg2.PLOT + ylab("$\\langle \\theta_{h,i} \\rangle_t$")
obj.gg2.PLOT <- obj.gg2.PLOT + scale_x_continuous(breaks = c(0, 21, 42, 63))
obj.gg2.PLOT <- obj.gg2.PLOT + facet_grid(variable ~ ., scales = "free_y")
obj.gg2.PLOT <- obj.gg2.PLOT + theme(plot.margin = unit(c(1,0.25,-0.75,0.15), "lines"),
plot.title = element_text(vjust = 1.75),
legend.position = "none",
axis.text = element_text(size = 6),
axis.title = element_text(size = 8),
panel.grid.minor = element_blank(),
strip.text = element_text(size = 6)
)
obj.gg2.PLOT <- obj.gg2.PLOT + ggtitle("Wavelet Coefficients")
print(obj.gg2.PLOT)
dev.off()
system(paste('lualatex', file.path(scl.str.TEX_FILE)), ignore.stdout = TRUE)
system(paste('convert -density 600', file.path(scl.str.PDF_FILE), ' ', file.path(scl.str.PNG_FILE)))
system(paste('mv ', scl.str.PNG_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.TEX_FILE, sep = ''))
system(paste('mv ', scl.str.PDF_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.AUX_FILE, sep = ''))
system(paste('rm ', scl.str.LOG_FILE, sep = ''))
}
## Estimate wavelet variance
obj.wvl.WVAR <- wavVar(vec.flt.dX, wavelet = "d8")
vec.flt.WVAR <- summary(obj.wvl.WVAR)$vmat[2,]
obj.wvl.WVAR2 <- wavVar(vec.flt.dX2, wavelet = "d8")
vec.flt.WVAR2 <- summary(obj.wvl.WVAR2)$vmat[2,]
mat.df.WVAR <- data.frame(h = seq(0, length(vec.flt.WVAR)-1),
wvar = vec.flt.WVAR,
wvar2 = vec.flt.WVAR2
)
print(mat.df.WVAR)
print(cbind(seq(0, length(vec.flt.WVAR)-1), mat.df.WVAR$wvar/sum(mat.df.WVAR$wvar)))
summary(obj.dwt.RESULT)
print(var(vec.flt.dX))
print(sum(vec.flt.WVAR))
print(sum(vec.flt.WVAR2))
names(mat.df.WVAR) <- c("h", "1", "2")
mat.df.WVAR <- melt(mat.df.WVAR, c("h"))
mat.df.WVAR$variable <- factor(mat.df.WVAR$variable,
levels = c("1", "2"),
labels = c("Return", "Noise")
)
## Plot time series
if (TRUE == TRUE) {
theme_set(theme_bw())
scl.str.RAW_FILE <- 'plot--why-use-wavelet-variance--daily-shocks--wavelet-variance--25jul2014'
scl.str.TEX_FILE <- paste(scl.str.RAW_FILE,'.tex',sep='')
scl.str.PDF_FILE <- paste(scl.str.RAW_FILE,'.pdf',sep='')
scl.str.PNG_FILE <- paste(scl.str.RAW_FILE,'.png',sep='')
scl.str.AUX_FILE <- paste(scl.str.RAW_FILE,'.aux',sep='')
scl.str.LOG_FILE <- paste(scl.str.RAW_FILE,'.log',sep='')
tikz(file = scl.str.TEX_FILE, height = 2, width = 7, standAlone=TRUE)
obj.gg2.PLOT <- ggplot()
obj.gg2.PLOT <- obj.gg2.PLOT + scale_colour_brewer(palette="Set1")
obj.gg2.PLOT <- obj.gg2.PLOT + geom_vline(xintercept = log(scl.int.T_DAY), size = 1, colour = "black", alpha = 0.50)
obj.gg2.PLOT <- obj.gg2.PLOT + geom_path(data = mat.df.WVAR,
aes(x = h/log(exp(1),2),
y = log(value),
group = variable,
colour = variable
),
size = 0.50
)
obj.gg2.PLOT <- obj.gg2.PLOT + labs(colour = "")
obj.gg2.PLOT <- obj.gg2.PLOT + ylab("$\\log V(h)$")
obj.gg2.PLOT <- obj.gg2.PLOT + xlab("$h$ $[\\log(\\text{min})]$")
## obj.gg2.PLOT <- obj.gg2.PLOT + scale_x_date(limits = c(as.Date("1965-1-1"), as.Date("2012-12-31")), labels = date_format("%Y"))
## obj.gg2.PLOT <- obj.gg2.PLOT + scale_y_continuous(limits = c(1,12), breaks = c(2, 5, 8, 11))
obj.gg2.PLOT <- obj.gg2.PLOT + theme(plot.margin = unit(c(1,0.15,0.15,-0.10), "lines"),
plot.title = element_text(vjust = 1.75),
legend.position = c(0.75,0.90),
legend.background = element_blank(),
axis.title = element_text(size = 10),
axis.text = element_text(size = 6),
panel.grid.minor = element_blank()
)
obj.gg2.PLOT <- obj.gg2.PLOT + ggtitle("Wavelet Variance")
print(obj.gg2.PLOT)
dev.off()
system(paste('lualatex', file.path(scl.str.TEX_FILE)), ignore.stdout = TRUE)
system(paste('convert -density 600', file.path(scl.str.PDF_FILE), ' ', file.path(scl.str.PNG_FILE)))
system(paste('mv ', scl.str.PNG_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.TEX_FILE, sep = ''))
system(paste('mv ', scl.str.PDF_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.AUX_FILE, sep = ''))
system(paste('rm ', scl.str.LOG_FILE, sep = ''))
}
## Estimate spectral density
## spec.ar(vec.flt.X, method = "ols")
obj.stt.PS <- spec.ar(vec.flt.dX, method = "ols", order = 11, plot = FALSE)
mat.df.PS <- data.frame(f = obj.stt.PS$freq,
s = obj.stt.PS$spec
)
obj.stt.PS2 <- spec.ar(vec.flt.dX2, method = "ols", plot = FALSE)
mat.df.PS$s2 <- obj.stt.PS2$spec
print(sum(mat.df.PS$s) * 1/(dim(mat.df.PS)[1] - 1))
print(sum(mat.df.PS$s2) * 1/(dim(mat.df.PS)[1] - 1))
print(var(vec.flt.dX))
names(mat.df.PS) <- c("f", "1", "2")
mat.df.PS <- melt(mat.df.PS, c("f"))
mat.df.PS$variable <- factor(mat.df.PS$variable,
levels = c("1", "2"),
labels = c("Return", "Noise")
)
## Estimate auto-correlation coefficients
vec.int.LAGS <- seq(1, scl.int.T_DAY * 1.20)
scl.int.LAG_MAX <- length(vec.int.LAGS)
mat.df.AC <- data.frame(l = vec.int.LAGS,
c = NA,
c2 = NA
)
vec.int.Y_ROWS <- seq(1, scl.int.T - scl.int.LAG_MAX)
vec.flt.Y <- vec.flt.dX[vec.int.Y_ROWS]
vec.flt.Y2 <- vec.flt.dX2[vec.int.Y_ROWS]
for (l in vec.int.LAGS) {
vec.int.X_ROWS <- seq(1 + l, scl.int.T - scl.int.LAG_MAX + l)
obj.lm.AC <- lm(vec.flt.Y ~ vec.flt.dX[vec.int.X_ROWS])
obj.lm.AC2 <- lm(vec.flt.Y2 ~ vec.flt.dX2[vec.int.X_ROWS])
mat.df.AC[mat.df.AC$l == l, ]$c <- summary(obj.lm.AC)$coef[2,1]
mat.df.AC[mat.df.AC$l == l, ]$c2 <- summary(obj.lm.AC2)$coef[2,1]
}
names(mat.df.AC) <- c("l", "1", "2")
mat.df.AC <- melt(mat.df.AC, c("l"))
mat.df.AC$variable <- factor(mat.df.AC$variable,
levels = c("1", "2"),
labels = c("Return", "Noise")
)
## Plot measures side-by-side
if (TRUE == TRUE) {
obj.vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
theme_set(theme_bw())
scl.str.RAW_FILE <- 'plot--why-use-wavelet-variance--daily-shocks--analysis--25jul2014'
scl.str.TEX_FILE <- paste(scl.str.RAW_FILE,'.tex',sep='')
scl.str.PDF_FILE <- paste(scl.str.RAW_FILE,'.pdf',sep='')
scl.str.PNG_FILE <- paste(scl.str.RAW_FILE,'.png',sep='')
scl.str.AUX_FILE <- paste(scl.str.RAW_FILE,'.aux',sep='')
scl.str.LOG_FILE <- paste(scl.str.RAW_FILE,'.log',sep='')
tikz(file = scl.str.TEX_FILE, height = 2, width = 7, standAlone=TRUE)
## Plot 2
obj.gg2.PLOT2 <- ggplot()
obj.gg2.PLOT2 <- obj.gg2.PLOT2 + scale_colour_brewer(palette="Set1")
obj.gg2.PLOT2 <- obj.gg2.PLOT2 + geom_vline(xintercept = scl.int.T_DAY, size = 1, colour = "black", alpha = 0.50)
obj.gg2.PLOT2 <- obj.gg2.PLOT2 + geom_path(data = mat.df.AC,
aes(x = l,
y = value,
group = variable,
colour = variable
),
size = 0.50
)
obj.gg2.PLOT2 <- obj.gg2.PLOT2 + ylab("$C(\\ell)$")
obj.gg2.PLOT2 <- obj.gg2.PLOT2 + xlab("$\\ell$ $[\\text{min}]$")
## obj.gg2.PLOT2 <- obj.gg2.PLOT2 + scale_x_date(limits = c(as.Date("1965-1-1"), as.Date("2012-12-31")), labels = date_format("%Y"))
## obj.gg2.PLOT2 <- obj.gg2.PLOT2 + scale_y_continuous(limits = c(1,12), breaks = c(2, 5, 8, 11))
obj.gg2.PLOT2 <- obj.gg2.PLOT2 + theme(plot.margin = unit(c(1,0.15,0.15,-0.10), "lines"),
plot.title = element_text(vjust = 1.75),
legend.position = "none",
axis.title = element_text(size = 10),
axis.text = element_text(size = 6),
panel.grid.minor = element_blank()
)
obj.gg2.PLOT2 <- obj.gg2.PLOT2 + ggtitle("Predictive Regression")
## Plot 3
obj.gg2.PLOT3 <- ggplot()
obj.gg2.PLOT3 <- obj.gg2.PLOT3 + scale_colour_brewer(palette="Set1")
obj.gg2.PLOT3 <- obj.gg2.PLOT3 + geom_vline(xintercept = log(scl.int.T_DAY), size = 1, alpha = 0.50, colour = "black")
obj.gg2.PLOT3 <- obj.gg2.PLOT3 + geom_path(data = mat.df.PS,
aes(x = -log(f),
y = log(value),
group = variable,
colour = variable
),
size = 0.50
)
obj.gg2.PLOT3 <- obj.gg2.PLOT3 + ylab("$\\log S(f)$")
obj.gg2.PLOT3 <- obj.gg2.PLOT3 + xlab("$- \\log (f)$ $[\\log(\\text{min})]$")
## obj.gg2.PLOT3 <- obj.gg2.PLOT3 + scale_x_date(limits = c(as.Date("1965-1-1"), as.Date("2012-12-31")), labels = date_format("%Y"))
## obj.gg2.PLOT3 <- obj.gg2.PLOT3 + scale_y_continuous(limits = c(1,12), breaks = c(2, 5, 8, 11))
obj.gg2.PLOT3 <- obj.gg2.PLOT3 + theme(plot.margin = unit(c(1,0.15,0.15,-0.10), "lines"),
plot.title = element_text(vjust = 1.75),
legend.position = "none",
axis.title = element_text(size = 10),
axis.text = element_text(size = 6),
panel.grid.minor = element_blank()
)
obj.gg2.PLOT3 <- obj.gg2.PLOT3 + ggtitle("Spectral Density")
## Plot 4
obj.gg2.PLOT4 <- ggplot()
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + scale_colour_brewer(palette="Set1")
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + geom_vline(xintercept = log(scl.int.T_DAY), size = 1, alpha = 0.50, colour = "black")
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + geom_path(data = mat.df.WVAR,
aes(x = h/log(exp(1),2),
y = log(value),
group = variable,
colour = variable
),
size = 0.50
)
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + labs(colour = "")
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + ylab("$\\log V(h)$")
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + xlab("$h$ $[\\log(\\text{min})]$")
## obj.gg2.PLOT4 <- obj.gg2.PLOT4 + scale_x_date(limits = c(as.Date("1965-1-1"), as.Date("2012-12-31")), labels = date_format("%Y"))
## obj.gg2.PLOT4 <- obj.gg2.PLOT4 + scale_y_continuous(limits = c(1,12), breaks = c(2, 5, 8, 11))
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + theme(plot.margin = unit(c(1,0.15,0.15,-0.10), "lines"),
plot.title = element_text(vjust = 1.75),
legend.position = c(0.25, 0.35),
legend.background = element_blank(),
axis.title = element_text(size = 10),
axis.text = element_text(size = 6),
panel.grid.minor = element_blank()
)
obj.gg2.PLOT4 <- obj.gg2.PLOT4 + ggtitle("Wavelet Variance")
## Print results
pushViewport(viewport(layout = grid.layout(1, 3)))
print(obj.gg2.PLOT2, vp = obj.vplayout(1,1))
print(obj.gg2.PLOT3, vp = obj.vplayout(1,2))
print(obj.gg2.PLOT4, vp = obj.vplayout(1,3))
dev.off()
system(paste('lualatex', file.path(scl.str.TEX_FILE)), ignore.stdout = TRUE)
system(paste('convert -density 600', file.path(scl.str.PDF_FILE), ' ', file.path(scl.str.PNG_FILE)))
system(paste('mv ', scl.str.PNG_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.TEX_FILE, sep = ''))
system(paste('mv ', scl.str.PDF_FILE, ' ', scl.str.FIG_DIR, sep = ''))
system(paste('rm ', scl.str.AUX_FILE, sep = ''))
system(paste('rm ', scl.str.LOG_FILE, sep = ''))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment