#' ---1#' title: "Minimum Standard Deviation Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' This script demonstrates how to solve a constrained portfolio optimization7#' problem to minimize standard deviation.89#' Load the package and data10library(PortfolioAnalytics)11data(edhec)12R <- edhec[, 1:10]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="risk", name="StdDev")20print(init.portf)2122#' Minimizing standard deviation can be formulated as a quadratic programming23#' problem and solved very quickly using optimize_method="ROI". Although "StdDev"24#' was specified as an objective, the quadratic programming problem uses the25#' variance-covariance matrix in the objective function.26minStdDev.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf,27optimize_method="ROI",28trace=TRUE)29print(minStdDev.lo.ROI)3031plot(minStdDev.lo.ROI, risk.col="StdDev", main="Long Only Minimize Portfolio StdDev")3233#' It is more practical to impose box constraints on the weights of assets.34#' Update the second constraint element with box constraints.35init.portf <- add.constraint(portfolio=init.portf, type="box",36min=0.05, max=0.3, indexnum=2)3738minStdDev.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf,39optimize_method="ROI",40trace=TRUE)41print(minStdDev.box.ROI)4243chart.Weights(minStdDev.box.ROI, main="Minimize StdDev with Box Constraints")4445#' Although the maximum return objective can be solved quickly and accurately46#' with optimize_method="ROI", it is also possible to solve this optimization47#' problem using other solvers such as random portfolios or DEoptim.4849#' For random portfolios, the leverage constraints should be relaxed slightly.50init.portf$constraints[[1]]$min_sum=0.9951init.portf$constraints[[1]]$max_sum=1.015253#' Add mean as an object with multiplier=0. The multiplier=0 argument means54#' that it will not be used in the objective function, but will be calculated55#' for each portfolio so that we can plot the optimal portfolio in56#' mean-StdDev space.57init.portf <- add.objective(portfolio=init.portf, type="return",58name="mean", multiplier=0)5960#' First run the optimization with a wider bound on the box constraints that61#' also allows shorting. Then use more restrictive box constraints. This is62#' useful to visualize impact of the constraints on the feasible space6364#' Create a new portfolio called 'port1' by using init.portf and modify the65#' box constraints.66port1 <- add.constraint(portfolio=init.portf, type="box",67min=-0.3, max=0.8, indexnum=2)6869minStdDev.box1.RP <- optimize.portfolio(R=R, portfolio=port1,70optimize_method="random",71search_size=2000,72trace=TRUE)73print(minStdDev.box1.RP)74plot(minStdDev.box1.RP, risk.col="StdDev")7576#' Create a new portfolio called 'port2' by using init.portf and modify the77#' box constraints.78port2 <- add.constraint(portfolio=init.portf, type="box",79min=0.05, max=0.3, indexnum=2)8081#' Run the optimization with random portfolios.82minStdDev.box2.RP <- optimize.portfolio(R=R, portfolio=port2,83optimize_method="random",84search_size=2000,85trace=TRUE)86print(minStdDev.box2.RP)87plot(minStdDev.box2.RP, risk.col="StdDev")8889#' Run the optimization with DEoptim.90minStdDev.box.DE <- optimize.portfolio(R=R, portfolio=init.portf,91optimize_method="DEoptim",92search_size=2000,93trace=TRUE)94print(minStdDev.box.DE)95plot(minStdDev.box.DE, risk.col="StdDev", return.col="mean")969798