Portfolio Optimization in R, Part 4 Redeux

[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.


So where did we mess up? In the calculation of returns for the market cap weighted portfolio andthe portfolio optimization portfolio, we simply took the starting weights (W0)and multiplied them by the relevant series of returns.

resEqual = as.matrix(returns) %*% t(ret)

and

subRes = as.matrix(subRes) %*% t(ret)

To correct this, we have 2 options.
  1. Recalculate the weight at each time pointassuming a starting weight.  Multiplythose weights to each day’s returns to produce the series
  2. Assume a starting monetary value of theportfolio (1 is convenient), and apply the return series to the positionvalues.  At each period, calculate thedaily portfolio return.

1 does not equal 2.  Why?Remember that we are using log returns. 

#1 is a weighted arithmetic average. 
#2 is the log of a sum of functions. 

If we say that prices are final and weights are initial –that is that we observe the price at the end of time t and the weight at thebeginning (as a function of prior period prices), then we can rewrite #2 as


The literature uses #1 because it makes the math easier.  The numbers are approximately equal because log(1+r)~= r (or exp(r) ~= 1+r, if you prefer).  However, they are different and the difference can compound withtime.  Your brokerage account worksaccording to #2. 

We will use #1 as that is the convention.  If I was analyzing something for real, and Iwas given a log returns data set to use, I would use #2.  #1 may be more compact and make the mathnice.  #2 will more closely reflect youraccount balance.

How do we calculate wi,,t given wi,t-1?  We apply the returns to the weights andre-standardize the numbers.

Because we want to reusethis methodology for cap weighted and optimized portfolios, we should justcreate a function that will take a series of returns and a vector of weights,and give back the return series.

reweight = function(returns,startWeight){
      n= nrow(returns)
     
      lastWeight= as.vector(startWeight)
     
      outReturn= data.frame()
     
      for(i in seq(1,n)){
            rts= as.vector(exp(returns[i,]))
            w= lastWeight * rts
            sumW= sum(w)
            w= w/sumW
           
            r= as.matrix(returns[i,]) %*% w
           
            lastWeight= w
           
            outReturn= rbind(outReturn,r)
      }
     
      return(outReturn)
     
}

Substituting the function call in placeof the lines quoted above, the new Annualized Returns table looks like this:

CapWeight Portfolio
EqualWeight Portfolio
Portfolio Optimization
Annualized Return
0.0258
0.0128
0.0282
Annualized Std Dev
0.2199
0.2242
0.1775
Annualized Sharpe (Rf=0%)
0.1174
0.0570
0.1590

Here we can see the capitalizationweighted portfolio performs much better than previously calculated.  This is expected.  The Portfolio Optimization portfolio alsodoes better and has a slight advantage over the cap weight portfolio in theSharpe Ratio.

The correlation chart is:

The cap weight portfolio and the equalweight portfolio are nearly perfectly correlated.  The optimized portfolio is less correlated,but still has a high degree of correlation. 

NOTE: we can put a Beta on the optimizedportfolio of 0.70 (.87 * .1775 / .2199) using the calculations above.

The cumulative return graph is:

This really just shows us what we already know.  The cap and equal weights are highlycorrelated.  The cap weight pulls awayafter the 2009 bottom.  The optimizedportfolio shows less volatility than the cap weight portfolio after the bottom.  It has a steady march up while the cap weightedportfolio whips up and down.

To leave a comment for the author, please follow the link and comment on their blog: Adventures in Statistical Computing.

R-bloggers.com 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)