Monday, April 30, 2012

French Global Factors

I have said it already in multiple posts, but Kenneth French’s data library is one of the most generous and powerful contributions to the financial community.  To build on Systematic Investor’s series on factors, I thought I should run some basic analysis on the Global Factors maintained by Kenneth French.  I cannot imagine how long this would take without the data library and the incredible set of R packages available.

From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio

R code from GIST:

Friday, April 27, 2012

Real Time Structural Break

Yesterday as I played with bfast I kept thinking “Yes, but this is all in hindsight.  How can I potentially use this in a system?”  Fortunately, one of the fine authors very generously commented on my post Structural Breaks (Bull or Bear?):

Jan Verbesselt Apr 27, 2012 02:01 AM

Nice application! you can also detect seasonal breaks. also check some new near real-time disturbance detection functionality using bfastmonitor()
cheers, Jan”

And away I went on an unexpected but very pleasant journey into bfastmonitor.  Please see the following paper for all the details.

Jan Verbesselt, Achim Zeileis, Martin Herold (2011). Near Real-Time Disturbance
  Detection in Terrestrial Ecosystems Using Satellite Image Time Series: Drought
  Detection in Somalia. Working Paper 2011-18. Working Papers in Economics and
  Statistics, Research Platform Empirical and Experimental Economics, Universitaet
  Innsbruck. URL

Doing a walk-forward test seemed like the best method of testing and illustration, so I chose the excruciating and incredibly volatile period from late 2008 to early 2009 as our example.  Amazingly, it picked with no optimizing or manual intervention March 2009 as the breakpoint. Of course, we would not know this until the end of March, but picking real-time with only a month lag is unbelievable to me.  Please try it out, and let me know your results.  Of course, I already have the 30 year bond bull in mind as a next trial.

Thanks to Yihui Xie who resurfaced again (see posts on knitr) with his animation package, which I used to create a good old-fashioned animated GIF.  I wish I had time to play more with the prettier and more robust options offered by the package.


R code from GIST:

Thursday, April 26, 2012

Monday, April 23, 2012

Drawdown Look at Frontier of Assets and Systems

In Efficient Frontier of Funds and Allocation Systems, I had hoped to start exploring how a frontier can potentially be created with only one asset, or how an even more efficient frontier could be created with assets and also systems on those assets.  I am obsessed with drawdown, so of course I need to extend that post with a look at drawdown to satisfy this obsession. If you have not read the original post, please read it before this, since I will only show the additional graph created.

From TimelyPortfolio

R code from GIST:

Wednesday, April 18, 2012

Efficient Frontier of Funds and Allocation Systems

I did a very basic experiment in Efficient Frontier of Buy-Hold and Tactical System where I determined the efficient frontier of the S&P 500 with itself transformed by a Mebane Faber 10-month moving average tactical allocation.

The result was interesting, but I did not pursue further.  Now with some inspiration and tools by Systematic Investor, I thought I would extend the post. This time around we will use both the Vanguard U.S. Total Bond Market (VBMFX) and Vanguard U.S. S&P 500 (VFINX) combined with both portfolios determined by tactical methods (moving average, RSI, and omega) and those funds transformed individually by the same tactical methods.  I will as always warn you that this is not advice and large losses are almost guaranteed.  Also, I would like to note that I have checked the 10-month moving average every way possible (even manually in Excel), and it has been incredible on the VFINX since 1990.  Prior to 1990, results were good but nowhere near as amazing.  If I messed up, please let me know.

From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio

R code from GIST:

knitr Performance Report-Attempt 2

Over the years I have changed my learning process from reading thoroughly first before proceeding to reading minimally and then applying immediately.  I very quickly see the gaps in my knowledge.  This method is far more painful but seems to quicken progress up the learning curve.  You will definitely see the process in its entirety with this series on latex, knitr, and performance reporting.  For those who are still hesitating, jump right in with me and let me know your result.

As I experimented with Attempt 2, I could see from others sample .Rnw files that LyX can serve as the IDE for Sweave/knitr documents, and I thought this would be a more pleasant route.  However it was not, and I eventually reverted back to old-school manual coding.  This post from R, Ruby, and Finance talks about TeXnic Center as an IDE.  Maybe I will try it for attempt 3. I am still a long way from a useable result (any bets on how many attempts it takes?), but the report is definitely an improvement

In Attempt 2, I used the echo=FALSE option to not output the R code on the final pdf report.  However, the code is still all there for public view within the .Rnw file.

I just remembered that ttrTests offers a Latex output option. Just what I need--another distraction. Look for that in future posts.

R code from GIST:

Friday, April 13, 2012

knitr Performance Report-Attempt 1

I get very excited about new R packages, but rarely is my excitement so fulfilled as with knitr.  Even with no skill, I have already been able to adapt the example Yihui Xie provides in his knitr Graphics Manual into a crude first version of a performance report that I could actually show clients and prospects.  Although this is far from production quality, two days of experimentation already has me to a level that assures me of the wonderful potential of combining the existing amazing R packages with knitr.  Before knitr, I do not believe I could have accomplished even this rough first draft.

If you have not played with MiKTeX before, you will need to use the Package Manager to install the xcolor and tufte-handout templates for this example to work properly.  MiKTeX is installed automatically with LyX as discussed in yesterday’s post Latex Allergy Cured by knitr.  If xcolor causes problems, then just change your repository to another repository.


knitr weaved with a little PerformanceAnalytics, lattice, and latticeExtra provides this first draft.  As always, thanks so much to the brilliant contributors that have made this possible.

R code in GIST:

Wednesday, April 11, 2012

The Making of…R Fell on Alabama

Slightly off topic but if you are as easily distracted by amazing technology as I am then you might enjoy my short (all made on my Iphone 4s) “The Making of…R Fell on Alabama.”  Clearly I am not accustomed to doing this type of video.

Latex Allergy Cured by knitr

I have always known that at some point I would have to succumb to the power of Latex, but Latex has been uncharacteristically intimidating to me.  I finally found the remedy to my Latex allergy with the amazing and fantastic knitr package from Yihui Xie.  With very minimal effort, I ran my first experiment and now am extremely excited to incorporate it in production-quality performance reports (I plan to document the steps to get there in future posts).

For those starting from scratch on Windows, I think the easiest method to get up and running is to install LyX, which will also install MikTex.  If these are successfully installed, then you should be ready to experiment with knitr in R.

I will use knitr’s stich function, which is clearly not designed for the robust production use of knitr, but makes for a very easy first test.  stitch will open a very short script, apply a template, and generate a Sweave style .Rnw (can be changed).  knit2pdf converts the .Rnw file into a pdf, and with a couple lines of code you get a remarkable result.



R code from GIST (unbelievably only 7 lines):

Monday, April 9, 2012

Piggybacking and Hopefully Publicizing R Experts

I was inspired by the Revolution Analytics blog post on the d3.js style calendar heat map that Paul Bleicher from Humedica developed in R.  In an effort to publicize such fine work, I wanted to put a slightly different spin on it to visualize a system’s time in the market.  The system is ridiculously basic (not investment advice and will lose money), but the visualization is very helpful.

From TimelyPortfolio

To continue with the theme, I would like to continue to highlight some fine R work from  Although his work is far more sophisticated than this, I thought I would use his plota function to plot the German Dax (used in this example) with RSI below.

From TimelyPortfolio

Thanks so much to the amazing R coders who provided the code and functions for this post.

R code from GIST: