Path: blob/master/demo/multiple_portfolio_optimization.R
1433 views
1library(PortfolioAnalytics)23# Examples of passing a list portfolio objects to optimize.portfolio and4# optimize.portfolio.rebalancing56data(edhec)7R <- edhec[, 1:4]8funds <- colnames(R)910# Construct initial portfolio11init.portf <- portfolio.spec(assets=funds)12init.portf <- add.constraint(portfolio=init.portf, type="weight_sum",13min_sum=0.99, max_sum=1.01)14init.portf <- add.constraint(portfolio=init.portf, type="long_only")1516# Minimize portfolio standard deviation17minSD.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")1819# Maximize mean return per unit portfolio standard deviation20meanSD.portf <- add.objective(portfolio=minSD.portf, type="return", name="mean")2122# Minimize expected shortfall23minES.portf <- add.objective(portfolio=init.portf, type="risk", name="ES")2425# Maximize mean return per unit portfolio expected shortfall26meanES.portf <- add.objective(portfolio=minES.portf, type="return", name="mean")2728# Combine the portfolios29mult.portf <- combine.portfolios(list(minSD.portf, meanSD.portf, minES.portf, meanES.portf))30mult.portf3132# run the optimization for mult.portf33mult.opt <- optimize.portfolio(R, mult.portf, optimize_method="random",34search_size=2000, trace=TRUE, message = TRUE)3536class(mult.opt)37mult.opt3839# This combines the weights for each portfolio optimized40extractWeights(mult.opt)4142# This combines the objective measures for each portfolio43extractObjectiveMeasures(mult.opt)4445# For N portfolios, this returns a list of length N with the stats46# for each portfolio47opt.xtract <- extractStats(mult.opt)4849# Run the rebalancing optimization for mult.portf50mult.opt.rebal <- optimize.portfolio.rebalancing(R, mult.portf,51optimize_method="random",52search_size=2000,53trace=TRUE,54message=TRUE,55rebalance_on="quarters",56training_period=140)5758class(mult.opt.rebal)59mult.opt.rebal6061# For N portfolios, this returns a list of length N with the optimal weights62# at each rebalancing date63extractWeights(mult.opt.rebal)6465# For N portfolios, this returns a list of length N with the objective measures66# at each rebalancing date67extractObjectiveMeasures(mult.opt.rebal)6869# For N portfolios, this returns a list of length N with the stats70# for each portfolio71opt.rebal.xtract <- extractStats(mult.opt.rebal)7273747576