This file contains hidden or 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
rm(list=ls()) | |
library(plyr) | |
library(ggplot2) | |
library(RColorBrewer) | |
price_data_dir = "C:\\***\\" | |
price_data_filepaths = paste(price_data_dir,list.files(price_data_dir),sep="") | |
price_data_list = lapply(price_data_filepaths,FUN<-function(x) read.csv(x,header=FALSE)) | |
price_data = do.call(rbind,price_data_list) #this gives about 1.5mil transactions during 2010-2011 | |
price_percentiles = quantile(price_data[,2],probs = seq(0.01,0.99,by=0.01)) #we know column 2 contains the prices |
This file contains hidden or 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
library(XML) | |
#use R's inbuilt unzip function, knowing that the required metadata is in docProps/core.xml | |
doc = xmlInternalTreeParse(unzip('test.docx','docProps/core.xml')) | |
#define the namespace | |
ns=c('dc'= 'http://purl.org/dc/elements/1.1/') | |
#extract the author using xpath query | |
author = xmlValue(getNodeSet(doc, '/*/dc:creator', namespaces=ns)[[1]]) |
This file contains hidden or 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
rm(list=ls()) | |
#install the superb quantmod library | |
#we will use it to download the data and compute returns | |
library(quantmod) | |
#install various other libraries for the data manipulation and graphing. | |
library(ggplot2) | |
library(scales) | |
library(RColorBrewer) | |
library(reshape2) | |
library(grid) |
This file contains hidden or 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
#Gold and stock prices in '07-'08: | |
hist.prices = | |
do.call(cbind, | |
lapply(c("GLD","SPY","QQQ"), function(symbol){ | |
symbol.data = get(symbol) #get from enviroment | |
symbol.data.adj = Ad(symbol.data) #extract the adjusted price | |
symbol.data.adj | |
}) | |
) |
This file contains hidden or 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
#Extract the correlation of gold to other assets | |
gold_data = rolling.correlmatrix[((rolling.correlmatrix$Var1=="GLD") * (rolling.correlmatrix$Var2 !="GLD")) == 1,] | |
#plot the evolution of the correlation of gold | |
ggplot(gold_data, aes(y = Var2, x = quarter, fill = Correl))+ | |
geom_tile() + | |
scale_fill_gradientn(colours = mycolvec_2 | |
,limits = c(-1,1))+ | |
facet_grid(.~year)+theme_bw() + | |
theme(panel.grid.major.x = element_blank(), |
This file contains hidden or 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
gold_data = rolling.correlmatrix[((rolling.correlmatrix$Var1=="GLD") * (rolling.correlmatrix$Var2 !="GLD")) == 1,] | |
ggplot(gold_data, aes(y = Var2, x = quarter, fill = Correl))+ | |
geom_tile() + | |
scale_fill_gradientn(colours = mycolvec_2 | |
,limits = c(-1,1))+ | |
facet_grid(.~year)+theme_bw() + | |
theme(panel.grid.major.x = element_blank(), | |
panel.grid.major.y = element_blank(), | |
axis.title.x = element_blank(), |
This file contains hidden or 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
#Personally I find the TLT anti correlation, rather obscures judging | |
#Put it to the right/ | |
PrefOrder = c("SPY","QQQ","EEM","IWM","EFA","IYR","GLD","TLT") | |
rolling.correlmatrix$Var1 = factor(rolling.correlmatrix$Var1, PrefOrder) #careful not to just rename levels here! | |
rolling.correlmatrix$Var2 = factor(rolling.correlmatrix$Var2, PrefOrder) #careful not to just rename levels here! | |
#introduce the purple colouring to the top of the range to better highlight correlation changes. | |
mycolvec_2 = c( | |
rev(brewer.pal(8,"Blues")), | |
colorRampPalette(c( |
This file contains hidden or 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
library(ggplot2) | |
library(scales) | |
library(RColorBrewer) | |
#this is a slightly bespoke scale - basically I wanted a blue to red scale | |
#that gave extra variation over the interesting 0.6-1.0 range. | |
mycolvec_1 = c(rev(brewer.pal(8,"Blues")),rep("white",3),brewer.pal(8,"YlOrRd")) | |
ggplot(rolling.correlmatrix, aes(x = Var1, y = Var2, fill = Correl)) + | |
geom_tile() + | |
scale_fill_gradientn(colours = mycolvec_1 | |
,limits = c(-1,1))+ |
This file contains hidden or 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
#now use Hadley's great plyr library and a simple function to compute the | |
#rolling correlation matrix | |
library(plyr) | |
DataFrameCorOutput<-function(hist.returns){ | |
require(reshape2) | |
correls = melt(cor(as.matrix(na.omit(hist.returns)))) | |
colnames(correls) = c("Var1","Var2","Correl") | |
correls | |
} |
This file contains hidden or 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
#install the superb quantmod library | |
#we will use it to download the data and compute returns | |
library(quantmod) | |
# load historical prices from Yahoo Finance | |
# I use a set that I saw used by systematic investor CREDIT | |
symbols = c('SPY','QQQ','EEM','IWM','EFA','TLT','IYR','GLD') | |
symbols.names = c('S&P 500,Nasdaq 100,Emerging Markets,Russell 2000,EAFE,20 Year | |
Treasury,U.S. Real Estate,Gold') |