Portfolio Construction with R

Preview

Constructing a portfolio means allocating your money between few chosen assets. The simplest thing you can do is evenly split your money between few chosen assets. Simple as it is, good research shows it is just fine, and even better than other more sophisticated methods (for example Optimal Versus Naive Diversification: How Inefficient is the 1/N). However, there is also good research that declares the opposite (for example Large Dynamic Covariance Matrices) so go figure.

Anyway, this post shows a few of the most common to build a portfolio. We will discuss portfolios which are optimized for:

  • Equal Risk Contribution
  • Global Minimum Variance
  • Minimum Tail-Dependence
  • Most Diversified
  • Equal weights

We will optimize based on half the sample and see out-of-sample results in the second half. Simply speaking, how those portfolios have performed.

Portfolio-construction, few options

Before we go to the data, first a quick review of the portfolio-construction options at our disposal:

  • Equal Risk Contribution
  • This means that each asset would contribute the same degree of volatility to the portfolio. So if the volatility of asset A is 3x, and the volatility of asset B is 1x, then we need to hold three parts of asset B for every part of asset A (here for exposition purposes we assume that only A and B exist and zero correlation between them).

  • Global Minimum Variance
  • Still reading? you know this one. Moving on.

  • Minimum Tail-Dependence
  • I already wrote about tail-dependence in the context of correlation structure and in the context of Extreme Value Theory. I paste from the R help page of the Minimum Tail-Dependence function :

    “Akin to the optimisation of a global minimum-variance portfolio, the minimum tail dependent portfolio is determined by replacing the variance-covariance matrix with the matrix of the lower tail dependence coefficients”.

    So instead of targeting portfolio’s volatility as the thing to minimize, we input another matrix, the matrix of tail dependence (diagonal is “dismissed” as 1). This matrix is much more noisy to estimate than the covariance\correlation matrix (which is already quite noisy mind you). The reason is explained in the previous correlation structure post.

    Using a tail-dependence matrix can dictate much different portfolio than that of the global minimum, due to the correlation structure I spoke about before.

  • Most Diversified
  • First time for me to hear about this actually. Learning new things is one of the leading drivers for blogging you see. And also why a “quick short post” in my mind turns out to be as lengthy as this one. Indeed, sharing those random thoughts with you doesn’t help brevity, but we are friends so it’s ok.

    Look at this ratio: \frac{W ^ T \Sigma }{ \sqrt{ W ^ T \Sigma W }}. In words, it is a weighted average of the individual volatilities divided by the volatility of the portfolio. The nominator is at least as large as the denominator, so the minimum of this ratio is 1, and it is achieved if you have only a single asset.
    If you have say two assets which are independent of each other and have the same volatility then the ratio is quickly computed as:

        \[\frac{.5 \sigma + .5 \sigma}{ \sqrt{ .5^2\sigma^2 +  .5^2\sigma^2} } =  \frac{\sigma}{\sqrt {.5} \sigma }= \frac{1}{\sqrt {.5} } = \sqrt {2},\]

    and this can be generalized to N assets. You want this ratio to be as large as possible, which would mean that your portfolio has much lower volatility compared with the individual constituents. See here for a good Most Diversified portfolio reference.

    Before we continue just bear in mind that the term diversification is not an exact term or a formal one. When we write correlation we can recall the formula for (linear) correlation between two variables. But the definition of diversification is context-dependent, so the above represents a particular definition in this specific context. Another word other than diversification could easily be chosen.

Package Frapo in R

The large number of portfolio optimization packages can be overwhelming. Just google “Portfolio Construction with R” and see what comes. Enter Bernhard Pfaff. I met him during the 2016’s R in Finance excellent conference where he gave a talk about portfolio selection with multiple criteria objectives. He is the maintainer of the FRAPO package which I will be using in this tutorial. The FRAPO package is efficient (read: C++ engine) and friendly, as you will see from the very few lines of code below.

We start by pulling data on the three tickers: 'SPY','TLT', 'IEF'. Those are ETF’s servicing us as proxies for stocks (US) long term bonds (US) and short term bonds (US) respectively. We will use weekly returns and split the data in halves. The first half is used for portfolio optimization, and the second half is used to examine how those portfolio have performed. No rebalancing, no slippage or trading costs incorporated.

Now and build them following portfolios using the code below:

And those are the resulting portfolio weights:
Different portfolio construction
In the chart above we can see that:
Global Minimum Variance needs short term bonds (IEF) the most. Equal Risk Contribution does not have much larger weight compared with the other two which is somewhat surprising. Most Diversified takes the “two ends”; Stock and Bonds, while IEF which is more like cash is left out. Finally Minimum Tail-Dependence looks a bit like the Equal Risk Contribution, which tells me that the tail dependence (as estimated here) is not very different than the volatility. Or perhaps more accurately, the relative considerations don’t change when we input tail-dependence matrix instead of the correlation matrix).

Now lets see how those portfolios performed on the second half of our sample.

Distribution of daily returns per year

out of sample portfolio performance
Looks like Equal Risk Contribution, Minimum Tail-Dependence and Equal weights are not very far apart. Most Diversified and Global Minimum Variance differ. The latter has only lukewarm performance while the Most Diversified is doing very well. On the whole, the main driver of results here is how much money you hold in the IEF ETF (which is close to cash in a way). The more short term bond you have (IEF), the less return you make. Of course, the Global Minimum Variance is doing exactly what is supposed to be doing. That is, looking at the recent downturn it has the smallest draw-down.

Finally, we can compute the mean over the variance of returns (not annualized, just on weekly frequency).

Distribution of daily returns per year

Sharpe Ratio out of sample
Apart from the Global Minimum Variance, I would say you simply get paid for the volatility you are willing to endure. At least looking at weekly returns, there is not an awesome difference between the portfolios.

What will happen if we use say 100 other US equity tickers? I suspect the differences are even less pronounced. So it only matters when you are talking big money, or maybe if you have less correlation between the different portfolio components, I think.

If you enjoyed this post and you like those kind of comparisons have a look at this book: Global Asset Allocation: A Survey of the World’s Top Asset Allocation Strategies You can buy it by paying roughly the price of a double espresso, or you can simply register here and get a kindle version after a few days (by doing this you agree to review the book).

Appendix

Some requested code

8 comments on “Portfolio Construction with R”

  1. Thanks for this great analysis. Very informative…would you please mind sharing the rest of the code on the out of sample covering the charts?

    Regards,

      1. If you had a “like” button I would have hit that instead of commenting. I particularly enjoyed the work you din on the various types of volatility some years back (Parkinson, Garman-Klass etc, though my fav is Yang-Zhang but you did not talk about that). Good stuff, please keep it up. Frank

Leave a Reply

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