Path: blob/master/demo/multi_layer_optimization.R
1433 views
#' ---1#' title: "Multi Layer Optimization Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' Demonstrate multi layer portfolio optimization7#' The top level (i.e. layer) optimization problem is to minimize modified ES8#' with equal component contribution to modified ES of the two portfolios in9#' the lower layer.10#'11#' The sub portfolios consist of different assets and different objectives12#' relative to each other. The out of sample returns for each sub portfolio13#' are calculated based on their respective constraints, objectives, and14#' optimization parameters. The out of sample returns are then used as the15#' returns input for the top level optimization.1617#' Load package and data.18library(PortfolioAnalytics)19data(edhec)20R <- edhec[, 1:10]21funds <- colnames(R)2223#' The first sub-portfolio, portf1, will contain assets 1:5 of edhec24#' with an objective to minimize standard deviation.25portf1 <- portfolio.spec(assets=funds[1:5])26portf1 <- add.constraint(portfolio=portf1, type="weight_sum",27min_sum=0.99, max_sum=1.01)28portf1 <- add.constraint(portfolio=portf1, type="long_only")29portf1 <- add.objective(portfolio=portf1, type="risk", name="StdDev")3031#' The second sub-portfolio, portf2, will contain assets 6:10 of edhec32#' with an objective to minimize expected shortfall.33portf2 <- portfolio.spec(assets=funds[6:10])34# portf2 <- portfolio.spec(assets=5)35portf2 <- add.constraint(portfolio=portf2, type="weight_sum",36min_sum=0.99, max_sum=1.01)37portf2 <- add.constraint(portfolio=portf2, type="long_only")38portf2 <- add.objective(portfolio=portf2, type="risk", name="ES",39arguments=list(p=0.9))4041#' portf1 and portf2 have the same constraints so they can used the same42#' set of random portfolios.43set.seed(123)44rp <- random_portfolios(portf2, 2000)454647#' The 'top level' portfolio has objectives for equal contribution to risk48#' where modified ES is the risk measure.49portf <- portfolio.spec(assets=paste("proxy",1:2, sep="."))50portf <- add.constraint(portfolio=portf, type="weight_sum",51min_sum=0.99, max_sum=1.01)52portf <- add.constraint(portfolio=portf, type="long_only")53portf <- add.objective(portfolio=portf, type="risk", name="ES",54arguments=list(p=0.9))55portf <- add.objective(portfolio=portf, type="risk_budget", name="ES",56arguments=list(p=0.9), min_concentration=TRUE)5758#' Specify a mult-layer portfolio.59mult.portf <- mult.portfolio.spec(portf)6061#' Add portf1 as a sub portfolio with optimization parameters specific to62#' running optimize.portfolio.rebalancing with portf1.63mult.portf <- add.sub.portfolio(mult.portf, portf1, rp=rp,64optimize_method="random",65rebalance_on="quarters",66training_period=136)6768#' Add portf2 as a sub portfolio with optimization parameters specific to69#' running optimize.portfolio.rebalancing with portf2.70mult.portf <- add.sub.portfolio(mult.portf, portf2, rp=rp,71optimize_method="random",72rebalance_on="months",73training_period=136,74trailing_periods=48)7576#' Generate random portfolios for the top layer optimization77set.seed(123)78rp.top <- random_portfolios(portf, 1000)7980#' Run the multi layer optimization81opt.mult <- optimize.portfolio(R, mult.portf,82optimize_method="random",83trace=TRUE, rp=rp.top)84opt.mult85868788