Portfolio Optimization in R, Part 4
Want to share your content on Rbloggers? click here if you have a blog, or here if you don't.
This post will conclude the portfolio optimization series. In this post, we will construct a trading strategy based on portfolio optimization and test the results against the CAPM market portfolio as well as another strategy.
It is worth reiterating:
Nothing I am about to say should be taken as advice for investing. These results are based on prior observed returns and the future rarely mimics the past. These techniques can give helpful insight on how you can better allocate a portfolio. It should not be used as the sole investment decision. Speak with a qualified professional if you are looking for advice.
Building on the work of Markowitz, Treynor, Sharpe, et. al. developed the Capital Asset Pricing Model — CAPM. They shared the Nobel Prize with Markowitz in 1990 for this work. CAPM is a general equilibrium model. The model assumes that market prices reflect all available information and give the “fair” value of a security. Based on that, the market portfolio can be shown to be market capitalization weighted portfolio. Market capitalization (or market cap) is defined as the share price times the number of shares outstanding — the total value of the equity of a company. A company’s weight is its market cap divided by the total market cap of all securities.
Capitalization weighting has become the standard for indexes and index funds. The S&P500 being what most people consider the standard “market portfolio.” We will test our portfolio optimization strategy against a capitalization weighted strategy.
Now there are problems with CAPM. There are lots of ways to poke holes in it. One way is to say that prices are not exactly fair value, but instead mean revert to fair value. In this case, when a price is above fair value, the market cap weighted portfolio will overweight the overpriced security. When it mean reverts, the cap weight portfolio will under perform because of the overweighting.
This position was famously put forward by Robert Arnott. I highly recommend his book,The Fundamental Index: A Better Way to Invest. He argues that any portfolio strategy that breaks the correlation with price will outperform the capitalization index over time. He created a new index which he puts forward in the book. Another would be to simply equal weight each security (S&P actually publishes this index). Because of that, we will also test our strategy against an equal weight strategy.
Here is our portfolio optimization strategy:
 At the beginning of each quarter, take the previous quarterly returns and calculate the market portfolio.
 Use this portfolio for the current quarter.
 At the start of the next quarter, go back to #1.
 Require at least 3 stocks in our portfolio.
 No shorting.
 Use 2% as the risk free rate.
 For the first quarter and if the optimization fails, use an equal weight portfolio.
marketPortfolio = function(merged, rf, returnNames, weightNames,graph=FALSE, points=500, maxWeight=.334, Debug=FALSE)
 Add a Debug option to print outputs if we request them
 Make the function fault tolerant. Sometimes a feasible solution is not possible and the function needs to detect this and handle it accordingly.
 Cap the max return we search over to 50%. This is a sanity check as large values can cause other weird behavior.
 Likewise, put a floor on the min return at .005%.
 If the max return is <0, then simply find the minimum variance portfolio that achieves that value.
 Add a maxWeight option that lets us cap the weight on any 1 security.
CapWeight Portfolio

EqualWeight Portfolio

Portfolio Optimization


Annualized Return

0.0393

0.0128

0.0069

Annualized Std Dev

0.2530

0.2242

0.1785

Annualized Sharpe (Rf=0%)

0.1554

0.0570

0.0387

Rbloggers.com offers daily email 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/datascience job.
Want to share your content on Rbloggers? click here if you have a blog, or here if you don't.