(This article was first published on

**Adventures in Statistical Computing**, and kindly contributed to R-bloggers)So where did we mess up? In the calculation of returns for the market cap weighted portfolio and the portfolio optimization portfolio, we simply took the starting weights (W

_{0}) 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.

- Recalculate the weight at each time point assuming a starting weight. Multiply those weights to each day’s returns to produce the series
- Assume a starting monetary value of the portfolio (1 is convenient), and apply the return series to the position values. At each period, calculate the daily 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 the beginning (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 with time. Your brokerage account works according to #2.

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

How do we calculate w

_{i,,t }given w_{i,t-1}? We apply the returns to the weights and re-standardize the numbers.Because we want to reuse this methodology for cap weighted and optimized portfolios, we should just create 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 place of 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 capitalization weighted portfolio performs much better than previously calculated. This is expected. The Portfolio Optimization portfolio also does better and has a slight advantage over the cap weight portfolio in the Sharpe Ratio.

The correlation chart is:

The cap weight portfolio and the equal weight 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 optimized portfolio 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 highly correlated. The cap weight pulls away after the 2009 bottom. The optimized portfolio shows less volatility than the cap weight portfolio after the bottom. It has a steady march up while the cap weighted portfolio whips up and down.

To

**leave a comment**for the author, please follow the link and comment on his blog:**Adventures in Statistical Computing**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...