Path: blob/master/demo/demo_min_expected_shortfall.R
1433 views
#' ---1#' title: "Minimum Expected Shortfall Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' This script demonstrates how to solve a constrained portfolio optimization7#' problem to minimize expected shortfall (ES). The objective can also be8#' specified as "CVaR" or "ETL".910#' Load the package and data11library(PortfolioAnalytics)12data(edhec)13R <- edhec[, 1:10]14funds <- colnames(R)1516#' Construct initial portfolio with basic constraints17init.portf <- portfolio.spec(assets=funds)18init.portf <- add.constraint(portfolio=init.portf, type="full_investment")19init.portf <- add.constraint(portfolio=init.portf, type="long_only")20#' Add objective to minimize expected shortfall with a confidence level of 0.9.21init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",22arguments=list(p=0.9))23print(init.portf)2425#' Minimizing expected shortfall can be formulated as a linear programming26#' problem and solved very quickly using optimize_method="ROI". The linear27#' programming problem is formulated to minimize sample ES.28minES.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf,29optimize_method="ROI",30trace=TRUE)31print(minES.lo.ROI)3233plot(minES.lo.ROI, risk.col="ES", return.col="mean",34main="Long Only Minimize Expected Shortfall")3536#' It is more practical to impose box constraints on the weights of assets.37#' Update the second constraint element with box constraints.38init.portf <- add.constraint(portfolio=init.portf, type="box",39min=0.05, max=0.3, indexnum=2)4041minES.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf,42optimize_method="ROI",43trace=TRUE)44print(minES.box.ROI)4546chart.Weights(minES.box.ROI, main="Minimize ES with Box Constraints")4748#' Although the minimum ES objective can be solved quickly and accurately49#' with optimize_method="ROI", it is also possible to solve this optimization50#' problem using other solvers such as random portfolios or DEoptim. These51#' solvers have the added flexibility of using different methods to calculate52#' ES (e.g. gaussian, modified, or historical). The default is to calculate53#' modified ES.5455#' For random portfolios and DEoptim, the leverage constraints should be56#' relaxed slightly.57init.portf$constraints[[1]]$min_sum=0.9958init.portf$constraints[[1]]$max_sum=1.015960#' Add mean as an objective with multiplier=0. The multiplier=0 argument means61#' that it will not be used in the objective function, but will be calculated62# 'for each portfolio so that we can plot the optimal portfolio in63#' mean-ES space.64init.portf <- add.objective(portfolio=init.portf, type="return",65name="mean", multiplier=0)6667#' First run the optimization with a wider bound on the box constraints that68#' also allows shorting. Then use more restrictive box constraints. This is69#' useful to visualize impact of the constraints on the feasible space.7071#' Create a new portfolio called 'port1' by using init.portf and modify the72#' box constraints.73port1 <- add.constraint(portfolio=init.portf, type="box",74min=-0.3, max=0.8, indexnum=2)7576minES.box1.RP <- optimize.portfolio(R=R, portfolio=port1,77optimize_method="random",78search_size=2000,79trace=TRUE)80print(minES.box1.RP)81plot(minES.box1.RP, risk.col="ES", return.col="mean")8283#' Create a new portfolio called 'port2' by using init.portf and modify the84#' box constraints85port2 <- add.constraint(portfolio=init.portf, type="box",86min=0.05, max=0.3, indexnum=2)8788# Use random portfolios to run the optimization.89minES.box2.RP <- optimize.portfolio(R=R, portfolio=port2,90optimize_method="random",91search_size=2000,92trace=TRUE)93print(minES.box2.RP)94plot(minES.box2.RP, risk.col="ES", return.col="mean")9596# Use DEoptim to run the optimization.97minES.box.DE <- optimize.portfolio(R=R, portfolio=init.portf,98optimize_method="DEoptim",99search_size=2000,100trace=TRUE)101print(minES.box.DE)102plot(minES.box.DE, risk.col="ES", return.col="mean")103104105