Monthly Archives: September 2012

FinancialInstrument Moves to CRAN

I thought I would break up the posts about GSOC (no, I’m not done yet – there are a few more to do) with a quick note about FinancialInstrument.

The FinancialInstrument package provides a construct for defining and storing meta-data for tradable contracts (referred to as instruments, e.g., stocks, futures, options, etc.). The package can be used to create any asset class and derivatives, so it is required for packages like blotter and quantstrat.

FinancialInstrument was originally conceived as blotter was being written. Blotter provides portfolio accounting functionality, accumulating transactions into positions, then into portfolios and an account. Blotter, of course, needs to know something about the instrument being traded.

FinancialInstrument is used to hold the meta-data about an instrument that blotter uses to calculate the notional value of positions and the resulting P&L. FinancialInstrument, however, has plenty of utility beyond portfolio accounting, such as pre-trade pricing, risk management, etc., and was carved out so that others might take advantage of its functionality. Brian Peterson did the heavy lifting there, constructing FinancialInstrument as a meta-data container based on a data design we developed for a portfolio management system years ago.

Utility packages like this are generally thankless work, although incredibly useful and powerful for (potentialy several) end applications. They quietly do a bunch of heavy lifting that allows the user interface to be simpler, more powerful and more flexible than they otherwise might be, and allow the developer to focus on the specific application rather than re-inventing already-existing but trapped functionality.

Thankfully, Garrett See has found both the time and motivation to take what was a useful but unfinished package and help Brian carry it across the finish line into CRAN. Garrett also added a great deal of functionality around managing the .instrument namespace, such as ls_instruments() and many other ls_* and rm_* functions. Those ls_* functions get names of instruments of a particular type or denominated in a given currency (or currencies), while rm_* functions remove instruments. Similarly, a series of update_* functions help update instruments from various sources, such as Yahoo!.

At this point, FinancialInstrument has a lot of functionality. Let’s take a closer look…
Continue reading


Conversion of Meucci’s MatLab Code

You might remember a second proposal I put forward for this summer’s Google Summer of Code (GSoC). This project was ambitious, looking to convert a subset of Attillio Meucci’s MatLab code to R. Thankfully, Brian Peterson took the lead mentor position for this particular project.

Coincidently, the day before GSoC started we received a very generous contribution of ported code from Ram Ahluwalia at Wingfoot Capital. He even provided a csv file that detailed the forty-some functions and mapped them to Meucci’s original paper and script file.

In response, we re-scoped the project to focus on the code contribution and shape it into a stand-alone package that would mirror Attilio’s code and match the results he displays in his papers and publications. Manan proceeded through Ram’s code, testing and organizing the code into a package named ‘Meucci’ that is now available on r-forge.

Ram had converted several of Meucci’s interesting MATLAB scripts to R, such as robust Bayesian portfolio optimization from his script “Meucci_RobustBayesian,” and entropy pooling for blending views on scenarios with a prior scenario-probability distribution covered in his paper, “Fully Flexible Views: Theory and Practice.” Other topics included detecting outliers using the minimum volume ellipsoid from the script for “S_HighBreakdownMVE.m,” and his marginal copula algorithm.

Manan was also able to extend the code. He added code around Meucci’s “Managing Diversification” article published in June 2009 in Risk Magazine, and code from his “Review of Statistical Arbitrage, Cointegration, and Multivariate Ornstein-Uhlenbeck“.

This has all been factored into an installable package on r-forge that contains twenty-some exported functions, a number of utility functions, six demos, and some degree of documentation for most of them. There is still much to do here, but this package should provide a good foundation for future work. All of Meucci’s original MATLAB source is available on; browsing through the extensive material he makes available will give you a sense of how ambitious we are…

A very big thanks goes to Ram Ahluwalia for the significant code contribution, and congratulations to Manan Shaw on a successful GSoC 2012.

By the way, I probably haven’t said enough about Brian Peterson’s involvement in the GSoC this summer. Beyond being the lead mentor for this project and co-mentoring others, Brian was one of the representatives of the R Project overall. His work in leading and organizing the broader R Project effort this summer took a great deal of time, but resulted in a project list that was enthusiastically received by students and mentors alike. Many thanks to him as well for all his efforts this summer.

New Attribution Functions for PortfolioAnalytics

Another Google Summer of Code (GSoC) project this summer focused on creating functions for doing returns-based performance attribution. I’ve always been a little puzzled about why this functionality wasn’t covered already, but I think that most analysts do this kind of work in Excel. That, of course, has its own perils. But beyond the workflow issues, there have been a number of methodological refinements through time that I’m guessing most analysts don’t take advantage of. Furthermore, FactSet, Morningstar, and other vendors provide attribution functionality embedded within their reporting functions.

I’m of the opinion that R is a natural place for this kind of functionality to reside, and portfolio attribution has long been on the list of additions to the PortfolioAnalytics package. The calculations are relatively straight forward, but the mechanics of specifying and managing a portfolio hierarchy can be inconvenient. They require an attention to calculation order and other details, as well.

The mentors for this project included David Cariño, who is a Research Fellow at Russell Investments. He is also a co-author of the book that was used as a reference[1] for the project, and he teaches a course on this topic at UW. Doug Martin, who is a Professor of Statistics, Adjunct Professor of Finance and Director of Computational Finance at the University of Washington, and former Chairman of the Department of Statistics, was also a mentor.

Again, student interest in the project was strong. The mentors for GSoC decided that Andrii Babii, a student at Toulouse School of Economics, provided the strongest proposal and example code.

Working from Christopherson, Carino and Ferson (2009), Bacon (2008), and several other sources, Andrii proceeded to construct functions for calculating contribution and linking returns through time both arithmetically and geometrically. He supports a variety of methods for multi-period linking including Carino, Menchero, Davies-Laker, Frongello, and GRAP.

Using his functions, arithmetic effects can be displayed either as suggested in Brinson, Hood and Beebower (1986), or as in Brinson and Fachler (1985). Priority can be switched around from top-down to bottom-up, as well. Currency attribution is included with arithmetic effects handled according to Ankrim and Hensel (1992).

Andrii established a data format for specifying a portfolio hierarchy, which allows him to provide multi-level attribution. This is usually discussed within the Brinson model as “asset-country-sector,” although the function is written generally. It then returns the total multi-period effects and attribution effects at each level. He provides functions for weight aggregation to accomplish this, which I think will also be generally useful in PortfolioAnalytics.

Finally, Andrii also constructed functions for Fixed Income attribution and delta adjusting option returns for attribution.

All of these attribution-related functions will be moved into the PortfolioAnalytics package proper, shortly. I think you will find that all of these functions are well documented and come with good examples.

But, wait, there’s more! Andrii also knocked out a function for estimating market timing attribution according to either the Treynor-Mazuy or Merton-Henriksson models, called MarketTiming. That function is likely to end up in PerformanceAnalytics, since it is estimated using multiple regression and doesn’t require portfolio weights.

He also provided an AcctReturns function that extends the blotter package. This function takes the account data calculated from transactions and prices, along with external cash flows (such as contributions or withdrawals) to calculate time-weighted returns or linked modifed Dietz returns. This will be a welcome addition to blotter.

Congratulations to Andrii on a very successful GSoC 2012! This is a substantial contribution to PortfolioAnalytics, and is one that I think will see a great deal of use through time. I’m also looking forward to seeing where Andrii’s interests take him, and I hope they result in more contributions. Thanks also go to the mentors, David and Doug, and also to Google to making the summer possible.

[1] Jon A. Christopherson, David R. Carino, Wayne E. Ferson “Portfolio Performance Measurement and Benchmarking”, 2009. 466p. – (Wiley finance series)