Live Rolling Correlation Plot

Open source is amazing! I cannot even start to imagine the amount of work invested in R, in firefox browser (Mozilla), or Rstudio IDE, all of which are used extensively around the globe, free. Not free as in: free sample till you decide to upgrade, or: sure it’s free, just watch this one minute commercial every time you need to use it,  but free, as in: we think it might make your life better, enjoy. Warms the heart, in direct opposite to the fabulous fabs out there, that instead of contributing to a better, safer society, set it back and get paid for it (see appendix). Character is also normally distributed I guess.

Jeroen C.L. Ooms, (and friends probably) started OpenCPU, an R based implementation of Open Computing, transparent and reproducible. I took a close look and decided to try it out. I will briefly show what you can do with it.

In financial markets, there is empirical evidence for higher correlation between sectors when markets weaken, lower correlation when markets are on the up. It is part of a wider phenomena called Financial Contagion. I have created a graph of rolling average correlation between sectors (sectors ETF were used as proxies). I check nine sectors, “utilities”, “financial” etc., calculate correlation between each pair, and average them. This give a measure for the “closeness” between the sectors. Rolling means I use only the  most recent two weeks at each point in time for the calculation. What we should be able to see is that when market is going up, average correlation declines or the reverse. Another option is that the measure has some predictive power to it, but I don’t believe it is so, in any case we can use it as an extra tool to decide if we are bears or bulls (or pigeons.. for neutral). What’s OpenCPU has to do with it?

OpenCPU allows you is to upload your own function which is rendered on the server side and outputs to your browser. It is still in diapers, so it’s possible that the link will not work at some point, I uploaded it in their “temp” directory. The way the function is built is that you should be able to view the correlation in real time, as every click will execute the function anew. Check it out!, it produces a pdf file with the figure that was generated as described. You can also get other formats, click on me!. The nice thing about it is that next time you are interested, the pdf file is updated since the click calls the function again, so the graph is “alive” in that sense.
Code and appendix below, thanks for reading.

Appendix:
Comments:
1. When I use “Yahoo” to get the quotes, about 40% of the time something goes wrong, google as source is more stable. Yet also not 100%, you might get: “Error: cannot open URL ‘http://finance.google.com/finance/historical?q=XLF&startdate=Feb+19,+2011&enddate=Feb+19,+2012&output=csv'”, just refresh and try again.
2. OpenCPU has nice interface to upload your work, if you made it this far, you don’t need my explanation about how to use it.
3. The aggregation site Rbloggers managed by Tal Galili is also a form of free service, it is on a smaller scale, not firefox browser yet, but it’s on the move.

* Fabulous Fabs:

Related Books:

Programming Collective Intelligence

Data Analysis with Open Source Tools

Leave a Reply

Your email address will not be published. Required fields are marked *