Created
June 24, 2016 16:04
-
-
Save alexchinco/3d1c15b514cba4c1a707cb5910616838 to your computer and use it in GitHub Desktop.
Create figure for blog post: "Investor Holdings, Naïve Beliefs, and Artificial Supply Constraints"
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
########################################################################### | |
########################################################################### | |
## @purpose: Plot supply constraint imposed by investors purchases. | |
## ------------------------------------------------------------------------ | |
## @author: Alex Chinco | |
## @date: 11-JUN-2016 | |
########################################################################### | |
########################################################################### | |
########################################################################### | |
########################################################################### | |
## @section: Prep workspace | |
########################################################################### | |
########################################################################### | |
options(width=200, digits=6, digits.secs=6) | |
rm(list=ls()) | |
library(foreign) | |
library(grid) | |
library(plyr) | |
library(ggplot2) | |
library(reshape) | |
library(xtable) | |
library(Cairo) | |
library(sparkTable) | |
library(vars) | |
library(scales) | |
library(foreach) | |
library(doMC) | |
registerDoMC(8) | |
library(tikzDevice) | |
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{pxfonts}\n", | |
"\\usepackage{bm}\n", | |
"\\usepackage{xfrac}\n" | |
) | |
) | |
setTikzDefaults(overwrite = FALSE) | |
print(options('tikzLatexPackages')) | |
scl.str.DAT_DIR <- "~/Dropbox/research/trading_volume_and_supply/data/" | |
scl.str.FIG_DIR <- "~/Dropbox/research/trading_volume_and_supply/figures/" | |
set.seed(12356) | |
## ######################################################################### | |
## ######################################################################### | |
## @section: Define global parameters | |
## ######################################################################### | |
## ######################################################################### | |
scl.flt.GAM_O_O <- 120/121 | |
scl.flt.GAM_O_FS <- 1/121 | |
scl.flt.GAM_I_I <- 6/7 | |
scl.flt.GAM_I_FS <- 1/7 | |
scl.flt.GAM_FS_FS <- 12/13 | |
scl.flt.GAM_FS_O <- 2/39 | |
scl.flt.GAM_FS_I <- 1/39 | |
## ######################################################################### | |
## ######################################################################### | |
## @section: Compute constraint values | |
## ######################################################################### | |
## ######################################################################### | |
mat.df.PLOT <- data.frame(h = rep(seq(1, 30), 3), variable = sort(rep(c(0.02, 0.04, 0.08), 30)), value = NA) | |
mat.df.PLOT$value <- (scl.flt.GAM_I_I^mat.df.PLOT$h * mat.df.PLOT$variable)/(1 - scl.flt.GAM_O_O^mat.df.PLOT$h * (0.90 - mat.df.PLOT$variable)) | |
mat.df.PLOT$variable <- as.character(mat.df.PLOT$variable) | |
mat.df.PLOT$variable <- paste("$\\textit{investor} = ", mat.df.PLOT$variable, "$", sep = "") | |
mat.df.PLOT$variable <- factor(mat.df.PLOT$variable) | |
mat.df.PLOT$variable <- factor(mat.df.PLOT$variable, levels = rev(levels(mat.df.PLOT$variable))) | |
## ######################################################################### | |
## ######################################################################### | |
## @section: Plot results | |
## ######################################################################### | |
## ######################################################################### | |
theme_set(theme_bw()) | |
scl.str.RAW_FILE <- "plot--investors-and-supply-constraints--11jun2016" | |
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(data = mat.df.PLOT) | |
obj.gg2.PLOT <- obj.gg2.PLOT + scale_colour_brewer(palette = "Set1") | |
obj.gg2.PLOT <- obj.gg2.PLOT + geom_hline(yintercept = 0, | |
size = 1.00, | |
alpha = 0.75 | |
) | |
obj.gg2.PLOT <- obj.gg2.PLOT + geom_path(aes(x = h, | |
y = value, | |
group = variable, | |
colour = variable, | |
linetype = variable | |
), | |
size = 2.00, | |
alpha = 0.75 | |
) | |
obj.gg2.PLOT <- obj.gg2.PLOT + xlab("$h$, in months") | |
obj.gg2.PLOT <- obj.gg2.PLOT + ylab("$\\textit{constraint}_h$") | |
obj.gg2.PLOT <- obj.gg2.PLOT + coord_cartesian(xlim = c(1, 30), ylim = c(0, 0.40)) | |
obj.gg2.PLOT <- obj.gg2.PLOT + scale_x_continuous(breaks = c(1,2,3,6,9,12,18,24,30)) | |
obj.gg2.PLOT <- obj.gg2.PLOT + scale_y_continuous(breaks = c(0.00, 0.10, 0.20, 0.30, 0.40)) | |
obj.gg2.PLOT <- obj.gg2.PLOT + labs(colour = "", linetype = "") | |
obj.gg2.PLOT <- obj.gg2.PLOT + theme(plot.margin = unit(c(1,0.15,0.15,0.15), "lines"), | |
axis.text = element_text(size = 10), | |
axis.title = element_text(size = 10), | |
plot.title = element_text(vjust = 1.75), | |
panel.grid.minor = element_blank(), | |
legend.position = c(0.75, 0.65), | |
legend.background = element_blank() | |
) | |
obj.gg2.PLOT <- obj.gg2.PLOT + ggtitle("Supply Constraint Imposed by Investors") | |
print(obj.gg2.PLOT) | |
dev.off() | |
system(paste('lualatex', file.path(scl.str.TEX_FILE)), ignore.stdout = TRUE) | |
system(paste('mv ', scl.str.PDF_FILE, ' ', scl.str.FIG_DIR, sep = '')) | |
system(paste('rm ', scl.str.TEX_FILE, 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