#' ---1#' title: "Regime Switching Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' This script demonstrates expressing views on the relative ranking of7#' expected returns based on two methods:8#' 1. R. Almgren and N. Chriss, "Portfolios from Sorts"9#' 2. A. Meucci, "Fully Flexible Views: Theory and Practice"1011#' Load package and data.12library(PortfolioAnalytics)13data(edhec)14R <- edhec[,1:4]15funds <- colnames(R)1617#' Construct initial portfolio with basic constraints.18init.portf <- portfolio.spec(assets=funds)19init.portf <- add.constraint(portfolio=init.portf, type="weight_sum",20min_sum=0.99, max_sum=1.01)21init.portf <- add.constraint(portfolio=init.portf, type="box",22min=0.05, max=0.5)23init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")24init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")2526#' Here we express views on the relative rank of the asset returns.27#' E{ R[,2] < R[,3] < R[,1] < R[,4] }28asset.rank <- c(2, 3, 1, 4)2930#' Use Meucci Fully Flexible Views framework to express views on the relative31#' order of asset returns.32#' Define prior probabilities.33p <- rep(1 / nrow(R), nrow(R))3435#' Express view on the relative ordering of asset returns36m.moments <- meucci.ranking(R, p, asset.rank)3738#' Express views using the method described in Almgren and Chriss,39#' "Portfolios from Sorts".40ac.moments <- list()41ac.moments$mu <- ac.ranking(R, asset.rank)42# Sample estimate for second moment43ac.moments$sigma <- cov(R)4445#' Generate random portfolios for use in the optimization.46rp <- random_portfolios(init.portf, 5000)4748#' Run the optimization using first and second moments estimated from49#' Meucci's Fully Flexible Views framework using the moments we calculated50#' from our view.51opt.meucci <- optimize.portfolio(R,52init.portf,53optimize_method="random",54rp=rp,55trace=TRUE,56momentargs=m.moments)5758#' Run the optimization using first moment estimated based on Almgren and Chriss,59#' "Portfolios from Sorts". The second moment uses the sample estimate.60opt.ac <- optimize.portfolio(R,61init.portf,62optimize_method="random",63rp=rp,64trace=TRUE,65momentargs=ac.moments)6667#' For comparison, run the optimization using sample estimates for first and68#' second moments.69opt.sample <- optimize.portfolio(R,70init.portf,71optimize_method="random",72rp=rp,73trace=TRUE)7475#' Here we plot the optimal weights of each optimization.76chart.Weights(combine.optimizations(list(meucci=opt.meucci,77ac=opt.ac,78sample=opt.sample)),79ylim=c(0,1), plot.type="barplot")8081#' Here we define a custom moment function to estimate moments based on82#' relative ranking views.83#' Asset are ranked according to a momentum or reversal view based on the84#' previous n periods.85moment.ranking <- function(R, n=1, momentum=TRUE, method=c("meucci", "ac")){86# Moment function to estimate moments based on relative ranking of87# expected returns.8889method <- match.arg(method)9091# Use the most recent n periods of returns92tmpR <- apply(tail(R, n), 2, function(x) prod(1 + x) - 1)9394if(momentum){95# Assume that the assets with the highest return will continue to outperform96asset.rank <- order(tmpR)97} else {98# Assume that the assets with the highest return will reverse99asset.rank <- rev(order(tmpR))100}101switch(method,102meucci = {103# Meucci Fully Flexible Views framework104# Prior probabilities105p <- rep(1 / nrow(R), nrow(R))106107# Relative ordering view108moments <- meucci.ranking(R, p, asset.rank)109},110ac = {111# Almgren and Chriss Portfolios from Sorts112moments <- list()113moments$mu <- ac.ranking(R, asset.rank)114# Sample estimate for second moment115moments$sigma <- cov(R)116}117)118return(moments)119}120121#' Here we run out of sample backtests to test the out of sample performance122#' using using the different frameworks to express our views on relative123#' asset return ranking.124opt.bt.meucci <- optimize.portfolio.rebalancing(R, init.portf,125optimize_method="random",126rebalance_on="quarters",127training_period=100,128rp=rp,129momentFUN="moment.ranking",130n=2,131momentum=TRUE,132method="meucci")133134opt.bt.ac <- optimize.portfolio.rebalancing(R, init.portf,135optimize_method="random",136rebalance_on="quarters",137training_period=100,138rp=rp,139momentFUN="moment.ranking",140n=2,141momentum=TRUE,142method="ac")143144opt.bt.sample <- optimize.portfolio.rebalancing(R, init.portf,145optimize_method="random",146rebalance_on="quarters",147training_period=100,148rp=rp)149150#' Compute returns and chart performance summary.151ret.meucci <- Return.portfolio(R, extractWeights(opt.bt.meucci))152ret.ac <- Return.portfolio(R, extractWeights(opt.bt.ac))153ret.sample <- Return.portfolio(R, extractWeights(opt.bt.sample))154ret <- cbind(ret.meucci, ret.ac, ret.sample)155colnames(ret) <- c("meucci.rank", "ac.rank", "sample")156charts.PerformanceSummary(ret, main="Ranking Views Performance")157158159160