#' ---1#' title: "Maximizing Modified Sharpe Ratio Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' This script demonstrates how to solve a constrained portfolio optimization7#' problem to maximize modified Sharpe Ratio using ES as the risk measure.89#' Load the package and data10library(PortfolioAnalytics)11data(edhec)12R <- edhec[, 1:8]13funds <- colnames(R)1415#' Construct initial portfolio with basic constraints.16init.portf <- portfolio.spec(assets=funds)17init.portf <- add.constraint(portfolio=init.portf, type="full_investment")18init.portf <- add.constraint(portfolio=init.portf, type="long_only")19init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")20init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",21arguments=list(p=0.925))22init.portf2324#' Maximizing STARR Ratio can be formulated as a linear programming25#' problem and solved very quickly using optimize_method="ROI".2627#' The default action if "mean" and "ES" are specified as objectives with28#' optimize_method="ROI" is to maximize STARR. If we want to use29#' both mean and ES in the objective function, but only minimize ES, we need to30#' pass in maxSTARR=FALSE to optimize.portfolio.3132maxSTARR.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf,33optimize_method="ROI",34trace=TRUE)35maxSTARR.lo.ROI3637#' Although the maximum STARR Ratio objective can be solved quickly and accurately38#' with optimize_method="ROI", it is also possible to solve this optimization39#' problem using other solvers such as random portfolios or DEoptim. These40#' solvers have the added flexibility of using different methods to calculate41#' the Sharpe Ratio (e.g. we could specify annualized measures of risk and42#' return or use modified, guassian, or historical ES).4344#' For random portfolios and DEoptim, the leverage constraints should be45#' relaxed slightly.46init.portf$constraints[[1]]$min_sum=0.9947init.portf$constraints[[1]]$max_sum=1.014849# Use random portfolios to run the optimization.50maxSTARR.lo.RP <- optimize.portfolio(R=R, portfolio=init.portf,51optimize_method="random",52search_size=2000,53trace=TRUE)54maxSTARR.lo.RP5556chart.RiskReward(maxSTARR.lo.RP, risk.col="ES", return.col="mean")5758# Use DEoptim to run the optimization.59maxSTARR.lo.DE <- optimize.portfolio(R=R, portfolio=init.portf,60optimize_method="DEoptim",61search_size=2000,62trace=TRUE)63maxSTARR.lo.DE64chart.RiskReward(maxSTARR.lo.DE, risk.col="ES", return.col="mean",65xlim=c(0.01, 0.08), ylim=c(0.004,0.008))666768