Site icon R-bloggers

Portfolio Optimization in R, Part 4

[This article was first published on Adventures in Statistical Computing, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? 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:

  1. At the beginning of each quarter, take the previous quarterly returns and calculate the market portfolio.
  2. Use this portfolio for the current quarter.
  3. At the start of the next quarter, go back to #1.
  4. Require at least 3 stocks in our portfolio.
  5. No shorting.
  6. Use 2% as the risk free rate.
  7. For the first quarter and if the optimization fails, use an equal weight portfolio.
This strategy will tend to outperform when prices are trending quarter over quarter.  I.E. if last quarter’s returns and volatility accurately predict this quarter’s values.  We are not taking trading costs into account.  The 2% rate is static, and we should technically use the 3 month treasury rate for each quarter beginning.  That’s why this is just an illustration.

To start, we need to modify the marketPortfolio() function we previously created.  You can find it here.  The new function signature is
marketPortfolio = function(merged, rf, returnNames, weightNames,graph=FALSEpoints=500maxWeight=.334Debug=FALSE)
The improvements we have made are:
  1. Add a Debug option to print outputs if we request them
  2. Make the function fault tolerant.  Sometimes a feasible solution is not possible and the function needs to detect this and handle it accordingly.
  3. Cap the max return we search over to 50%.  This is a sanity check as large values can cause other weird behavior.
  4. Likewise, put a floor on the min return at .005%.
  5. If the max return is <0, then simply find the minimum variance portfolio that achieves that value.
  6. Add a maxWeight option that lets us cap the weight on any 1 security.
The universe of stocks we will consider is 28 of the Dow components (for some reason Yahoo! Finance only gave me 28 instead of 30).  I pre-downloaded the returns and stored them in a .rda file.  You can get it here.  I calculated the starting market cap weights of each stock and stored them in a .csv file.  Get it here.

#read the saved returns< o:p>
load(“D:\\Workspace\\PortfolioOptimization\\returns.rda”)< o:p>
returns = results< o:p>
rm(results)< o:p>
stocks = colnames(returns)< o:p>

#get the capitalization weights< o:p>
stockWgt = read.table(“D:\\Workspace\\PortfolioOptimization\\stocks.csv”,< o:p>
                          header=TRUE,sep=“,”)[,“Weight”]< o:p>

#calculate the CapWeight portfolio returns< o:p>
results = as.matrix(returns) %*% stockWgt< o:p>
colnames(results) = c(“CapWeight Portfolio”)< o:p>

results = as.timeSeries(results)< o:p>

#calculate the EqualWeight portfolio Returns< o:p>
ret = t(as.matrix(rep(1/length(stocks),length(stocks))))< o:p>
resEqual = as.matrix(returns) %*% t(ret)< o:p>
colnames(resEqual) = “EqualWeight Portfolio”< o:p>

#combing the results into 1 timeSeries object< o:p>
results = cbind(results,resEqual)< o:p>

#grab the dates of the returns< o:p>
dates = time(returns)< o:p>
dates = dates@Data< o:p>

#get the Quarters for the dates< o:p>
qtrs = quarters(dates)< o:p>
qtrs = cbind(dates,qtrs)< o:p>

keep = NULL< o:p>
lastQtr = “n”< o:p>

#go through the dates and quaters and keep only the date with < o:p>
#the first day of the quarter< o:p>
for (i in seq(1,nrow(qtrs))){< o:p>
      if (qtrs[i,2] == lastQtr){< o:p>
            if (i == nrow(qtrs)){< o:p>
                  keep = c(keep,1)< o:p>
            } else {< o:p>
                  keep = c(keep,0)< o:p>
            }< o:p>
      }else {< o:p>
            keep = c(keep,1)< o:p>
      }< o:p>
      < o:p>
      lastQtr = qtrs[i,2]< o:p>

}< o:p>
qtrs = cbind(qtrs,keep)< o:p>

#get the indices of the first days of the quarter< o:p>
indx = which(qtrs[,3] == 1)< o:p>

#for the first quarter, use the equal weights< o:p>
res = as.matrix(returns[indx[1]:(indx[2]1),]) %*% t(ret)< o:p>

#loop throug the quarters, calculate the market portfolio< o:p>
#based on the previous quarter’s data and then calculate< o:p>
#the returns for the current quarter< o:p>
for (i in seq(2,length(indx)1)){< o:p>
      print(“Running “)< o:p>
      print(i)< o:p>
      < o:p>
      #get subset of last quarter stock returns< o:p>
      subset = returns[indx[i1]:(indx[i]1),]< o:p>
      s = start(subset)< o:p>
      e = end(subset)< o:p>
      print(“Fitting for:”)< o:p>
      print(s)< o:p>
      print(e)< o:p>
      < o:p>
      #calculate market portfolio< o:p>
      mp = marketPortfolio(subset,.02,stocks,stocks,< o:p>
                           graph=TRUE,< o:p>
                           points=500,< o:p>
                           Debug=FALSE)< o:p>
      < o:p>
      #if optimization failed, use equal weights, else get < o:p>
      #the weights from the market portfolio < o:p>
      if (is.null(mp)){< o:p>
            ret = t(as.matrix(rep(1/length(stocks),length(stocks))))< o:p>
      } else {< o:p>
            ret = as.matrix(mp[,stocks])< o:p>
      }< o:p>
      < o:p>
      #subset for the current quarter< o:p>
      subRes = returns[indx[i]:(indx[i+1]1),]< o:p>
      s = start(subRes)< o:p>
      e = end(subRes)< o:p>
      print(“Calculating Returns for:”)< o:p>
      print(s)< o:p>
      print(e)< o:p>
      < o:p>
      #calculate current quarter returns for market < o:p>
      #portfolio and add to return series< o:p>
      subRes = as.matrix(subRes) %*% t(ret)< o:p>
      res = rbind(res,subRes)< o:p>
}< o:p>

#loop does not calculate return for the last day in the series< o:p>
subRes = returns[nrow(returns),]< o:p>
subRes = as.matrix(subRes) %*% t(ret)< o:p>
res = rbind(res,subRes)< o:p>

#add the porfoliot optimized strategy to the others< o:p>
colnames(res) = “Portfolio Optimization”< o:p>
res = as.timeSeries(res)< o:p>

results = cbind(results,res)< o:p>

#calculate annualized return statistics< o:p>
table.AnnualizedReturns(results)< o:p>

#calculate and chart the correlations< o:p>
png(“d:\\Workspace\\PortfolioOptimization\\mpCorr.png”)< o:p>
chart.Correlation(results,histogram=TRUE,pch=“+”)< o:p>;< o:p>

#calculate and chart the cumlative returns< o:p>
png(“d:\\Workspace\\PortfolioOptimization\\mpReturns.png”)< o:p>
chart.CumReturns(results,< o:p>
            main=“Total Returns CapWeight vs PortOpt”,< o:p>
            legend.loc=“topleft”)< o:p>

Our Annualized Return Table:

CapWeight Portfolio< o:p>
EqualWeight Portfolio< o:p>
Portfolio Optimization< o:p>
Annualized Return< o:p>
-0.0393< o:p>
0.0128< o:p>
0.0069< o:p>
Annualized Std Dev< o:p>
0.2530< o:p>
0.2242< o:p>
0.1785< o:p>
Annualized Sharpe (Rf=0%)< o:p>
-0.1554< o:p>
0.0570< o:p>
0.0387< o:p>

Our portfolio optimization portfolio outperforms the Cap Weight, but under performs the Equal Weight.  Not surprising if you believe Mr. Arnott as we did not break the correlation with price.

Here is the chart of correlations:

We’ve created a portfolio that is correlated with the “market” cap weight portfolio, but not nearly so as the equal weight portfolio.  The degree of correlation of the equal weight and the cap weight is interesting.  

Here is the time series of returns charted:

Interestingly, our portfolio in green has trended sharply up since the market bottom in March of 2009.  It strongly outperformed the Cap Weight portfolio.

To leave a comment for the author, please follow the link and comment on their blog: Adventures in Statistical Computing. offers daily e-mail 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/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.