All That Glitters
[This article was first published on Joe's Data Diner, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
“The law itself follows Gold” Sextus Propertius.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
But what about stocks, bonds and real estate? Do they follow Gold too? Using the correlation data from my previous post, The Financial Crisis on Tape Part I, this question is easy to investigate. Indeed, it’s possible to see the correlations in last week’s graph:
where GLD is a Gold tracking fund. Fortunately, the excellent ggplot2 package means that in two slightly chunky lines we can create a bespoke heat-map to answer this question and we don’t have to strain our eyes finding them in the graph above!
SPY: S&P 500
QQQ: Nasdaq 100
EEM: Emerging Markets
IWM: Russel 2000
EFA: EAFE (Europe, Australasia and Far East)
TLT: 20 Year Treasury
IYR: U.S. Real Estate
Here are the two lines used to create it (using data obtained in The Financial Crisis on Tape Part I):
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
#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(), | |
panel.grid.major.y = element_blank(), | |
axis.title.x = element_blank(), | |
axis.title.y = element_blank(), | |
axis.text.x=element_text(angle=90)) |
In quieter times it seems that Gold is reasonably un-correlated to the other sectors, but in times of stress, strong correlations appear as expected. The really interesting point is the strong negative correlation seen in Q3 2008. Let’s take a look at the price time series of the S&P, Nasdaq and Gold during ’07-’08:
In terms of the units, these are the prices of representative funds not the assets themselves (see the links to Yahoo Finance which I’ve provided above). As the prices are similar and fit nicely I’ve not used any scaling. The negative correlation is probably related to Gold’s safe haven status. As stocks started dropping, a strong bid for Gold supported its price. This can result in a negative correlation. The graph was created using the code below.
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
#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 | |
}) | |
) | |
hist.prices = hist.prices[index(hist.prices) > as.Date("2007-01-01"),] #start '07 | |
hist.prices = hist.prices[index(hist.prices) < as.Date("2009-01-01"),] #end '08 | |
hist.prices = data.frame(hist.prices, date = index(hist.prices)) | |
hist.prices = melt(hist.prices,id.var="date") #get into form for ggplot2 | |
hist.prices$variable = gsub(".Adjusted","",hist.prices$variable) #tidy variable names | |
#use ggplot2 to plot a simple time-series of the prices | |
ggplot(hist.prices,aes(x=date,y=value,colour=variable))+geom_line()+theme_bw() + | |
scale_y_continuous("Price")+ | |
scale_colour_brewer("",palette="Set1")+ | |
theme(axis.title.x = element_blank()) |
To leave a comment for the author, please follow the link and comment on their blog: Joe's Data Diner.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.