[Returnanalytics-commits] r3246 - pkg/PortfolioAnalytics/demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 8 04:06:30 CET 2013


Author: rossbennett34
Date: 2013-11-08 04:06:29 +0100 (Fri, 08 Nov 2013)
New Revision: 3246

Added:
   pkg/PortfolioAnalytics/demo/demo_group_constraints.R
   pkg/PortfolioAnalytics/demo/demo_leverage_exposure_constraint.R
   pkg/PortfolioAnalytics/demo/demo_max_STARR.R
   pkg/PortfolioAnalytics/demo/demo_max_Sharpe.R
   pkg/PortfolioAnalytics/demo/demo_max_quadratic_utility.R
   pkg/PortfolioAnalytics/demo/demo_max_return.R
   pkg/PortfolioAnalytics/demo/demo_min_StdDev.R
   pkg/PortfolioAnalytics/demo/demo_min_expected_shortfall.R
   pkg/PortfolioAnalytics/demo/demo_risk_budgets.R
Modified:
   pkg/PortfolioAnalytics/demo/00Index
Log:
Adding demos for specific objectives and constraints.

Modified: pkg/PortfolioAnalytics/demo/00Index
===================================================================
--- pkg/PortfolioAnalytics/demo/00Index	2013-11-05 21:58:22 UTC (rev 3245)
+++ pkg/PortfolioAnalytics/demo/00Index	2013-11-08 03:06:29 UTC (rev 3246)
@@ -15,4 +15,13 @@
 demo_random_portfolios Demonstrate examples from script.workshop2012.R using random portfolios.
 demo_proportional_cost Demonstrate how to use proportional transaction cost constraint.
 demo_return_target Demonstrate how to specify a target return as a constraint or objective.
+demo_group_constraints Demonstrate using group constraints.
+demo_leverage_exposure_constraint Demonstrate using the leverage exposure constraint to put a constraint on overall portfolio leverage exposure.
+demo_max_STARR Demonstrate maximizing STARR as an objective using ROI, DEoptim, and random solvers.
+demo_max_Sharpe Demonstrate maximizing sharpe ratio as an objective using ROI, DEoptim, and random solvers.
+demo_max_quadratic_utility Demonstrate solving maximum quadratic utility objective with ROI solver.
+demo_max_return Demonstrate objective to maximize portfolio mean return.
+demo_min_StdDev Demonstrate objective to minimize portfolio standard deviation.
+demo_min_expected_shortfall Demonstrate objective to minimize expected shortfall.
+demo_risk_budgets Demonstrate using risk budget objectives.
 

Added: pkg/PortfolioAnalytics/demo/demo_group_constraints.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_group_constraints.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_group_constraints.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,58 @@
+
+# Examples of solving optimization problems with group constraints
+
+library(PortfolioAnalytics)
+
+data(edhec)
+R <- edhec[, 1:5]
+colnames(R) <- c("CA", "CTAG", "DS", "EM", "EQM")
+funds <- colnames(R)
+
+# Set up portfolio with objectives and constraints
+pspec <- portfolio.spec(assets=funds)
+pspec <- add.constraint(portfolio=pspec, type="full_investment")
+pspec <- add.constraint(portfolio=pspec, type="long_only")
+# Add  group constraints such that assets 1, 3, and 5 are in a group called
+# GroupA and assets 2 and 4 are in a group called Group B. The sum of the 
+# weights in GroupA must be between 0.05 and 0.7. The sum of the weights in
+# GroupB must be between 0.15 and 0.5.
+pspec <- add.constraint(portfolio=pspec, type="group",
+                        groups=list(groupA=c(1, 3, 5),
+                                    groupB=c(2, 4)),
+                        group_min=c(0.05, 0.15),
+                        group_max=c(0.7, 0.5))
+print(pspec)
+
+
+# Add an objective to minimize portfolio standard deviation
+pspec <- add.objective(portfolio=pspec, type="risk", name="StdDev")
+
+# The examples here use the obective to minimize standard deviation, but any
+# supported objective can also be used.
+
+# Minimizing standard deviation can be formulated as a quadratic programming 
+# problem and solved very quickly using optimize_method="ROI". Although "StdDev"
+# was specified as an objective, the quadratic programming problem uses the 
+# variance-covariance matrix in the objective function.
+minStdDev.ROI <- optimize.portfolio(R=R, portfolio=pspec, optimize_method="ROI")
+print(minStdDev.ROI)
+extractGroups(minStdDev.ROI)
+
+# The leverage constraints should be relaxed slightly for random portfolios 
+# and DEoptim
+pspec$constraints[[1]]$min_sum=0.99
+pspec$constraints[[1]]$max_sum=1.01
+
+# Solve with random portfolios
+# By construction, the random portfolios will be generated to satisfy the
+# group constraint.
+minStdDev.RP <- optimize.portfolio(R=R, portfolio=pspec, 
+                                   optimize_method="random", search_size=2500)
+print(minStdDev.RP)
+extractGroups(minStdDev.RP)
+
+# Solve with DEoptim
+minStdDev.DE <- optimize.portfolio(R=R, portfolio=pspec, 
+                                   optimize_method="DEoptim", search_size=2500)
+print(minStdDev.DE)
+extractGroups(minStdDev.DE)

Added: pkg/PortfolioAnalytics/demo/demo_leverage_exposure_constraint.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_leverage_exposure_constraint.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_leverage_exposure_constraint.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,52 @@
+
+# Examples for solving optimization problems with a leverage exposure constraint
+
+library(PortfolioAnalytics)
+
+data(edhec)
+R <- edhec[, 1:5]
+funds <- colnames(R)
+
+# Set up an initial portfolio object with basic constraints
+init.portf <- portfolio.spec(assets=funds)
+
+# Add an objective to maximize mean return per unit expected shortfall
+init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES")
+
+# The leverage_exposure constraint type is supported for random, DEoptim, pso,
+# and GenSA solvers. The following examples use DEoptim for solving the
+# optimization problem.
+
+# Dollar neutral portfolio with max 2:1 leverage constraint
+dollar.neutral.portf <- init.portf
+dollar.neutral.portf <- add.constraint(portfolio=dollar.neutral.portf, 
+                                       type="weight_sum", 
+                                       min_sum=-0.01, max_sum=0.01)
+dollar.neutral.portf <- add.constraint(portfolio=dollar.neutral.portf, 
+                                       type="box", min=-0.5, max=0.5)
+dollar.neutral.portf <- add.constraint(portfolio=dollar.neutral.portf, 
+                                       type="leverage_exposure", leverage=2)
+# Run optimization
+dollar.neutral.opt <- optimize.portfolio(R=R, portfolio=dollar.neutral.portf, 
+                                         optimize_method="DEoptim",
+                                         search_size=2500)
+print(dollar.neutral.opt)
+
+# Leveraged portfolio with max 1.6:1 leverage constraint
+leveraged.portf <- init.portf
+leveraged.portf <- add.constraint(portfolio=leveraged.portf, 
+                                       type="weight_sum", 
+                                       min_sum=0.99, max_sum=1.01)
+leveraged.portf <- add.constraint(portfolio=leveraged.portf, 
+                                       type="box", min=-0.3, max=0.8)
+leveraged.portf <- add.constraint(portfolio=leveraged.portf, 
+                                       type="leverage_exposure", leverage=1.6)
+
+# Run optimization
+leveraged.opt <- optimize.portfolio(R=R, portfolio=leveraged.portf, 
+                                         optimize_method="DEoptim",
+                                         search_size=2500)
+print(leveraged.opt)
+
+

Added: pkg/PortfolioAnalytics/demo/demo_max_STARR.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_STARR.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_max_STARR.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,61 @@
+# demo/max_STARR.R
+
+library(PortfolioAnalytics)
+
+# Examples of solving optimization problems to maximize mean return per unit ES
+
+data(edhec)
+R <- edhec[, 1:8]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
+init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",
+                            arguments=list(p=0.925))
+print(init.portf)
+
+# Maximizing STARR Ratio can be formulated as a linear programming 
+# problem and solved very quickly using optimize_method="ROI". 
+
+# The default action if "mean" and "StdDev" are specified as objectives with
+# optimize_method="ROI" is to maximize quadratic utility. If we want to use
+# both mean and ES in the objective function, but only minimize ES, we need to 
+# pass in maxSTARR=FALSE to optimize.portfolio.
+
+maxSTARR.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                      optimize_method="ROI",
+                                      trace=TRUE)
+print(maxSTARR.lo.ROI)
+
+# Although the maximum STARR Ratio objective can be solved quickly and accurately
+# with optimize_method="ROI", it is also possible to solve this optimization
+# problem using other solvers such as random portfolios or DEoptim. These
+# solvers have the added flexibility of using different methods to calculate
+# the Sharpe Ratio (e.g. we could specify annualized measures of risk and 
+# return or use modified, guassian, or historical ES).
+
+# For random portfolios and DEoptim, the leverage constraints should be 
+# relaxed slightly.
+init.portf$constraints[[1]]$min_sum=0.99
+init.portf$constraints[[1]]$max_sum=1.01
+
+# Use random portfolios
+maxSTARR.lo.RP <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                  optimize_method="random",
+                                  search_size=5000,
+                                  trace=TRUE)
+print(maxSTARR.lo.RP)
+
+chart.RiskReward(maxSTARR.lo.RP, risk.col="ES", return.col="mean")
+
+# Use DEoptim
+maxSTARR.lo.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                  optimize_method="DEoptim",
+                                  search_size=5000,
+                                  trace=TRUE)
+print(maxSTARR.lo.DE)
+chart.RiskReward(maxSTARR.lo.DE, risk.col="ES", return.col="mean",
+                 xlim=c(0.01, 0.08), ylim=c(0.004,0.008))

Added: pkg/PortfolioAnalytics/demo/demo_max_Sharpe.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_Sharpe.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_max_Sharpe.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,58 @@
+
+library(PortfolioAnalytics)
+
+# Examples of solving optimization problems to maximize mean return per unit StdDev
+
+data(edhec)
+R <- edhec[, 1:8]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
+init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
+print(init.portf)
+
+# Maximizing Sharpe Ratio can be formulated as a quardratic programming 
+# problem and solved very quickly using optimize_method="ROI". Although "StdDev"
+# was specified as an objective, the quadratic programming problem uses the 
+# variance-covariance matrix in the objective function.
+
+# The default action if "mean" and "StdDev" are specified as objectives with
+# optimize_method="ROI" is to maximize quadratic utility. If we want to maximize
+# Sharpe Ratio, we need to pass in maxSR=TRUE to optimize.portfolio.
+
+maxSR.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                   optimize_method="ROI", 
+                                   maxSR=TRUE, trace=TRUE)
+print(maxSR.lo.ROI)
+
+# Although the maximum Sharpe Ratio objective can be solved quickly and accurately
+# with optimize_method="ROI", it is also possible to solve this optimization
+# problem using other solvers such as random portfolios or DEoptim. These
+# solvers have the added flexibility of using different methods to calculate
+# the Sharpe Ratio (e.g. we could specify annualized measures of risk and return).
+
+# For random portfolios and DEoptim, the leverage constraints should be 
+# relaxed slightly.
+init.portf$constraints[[1]]$min_sum=0.99
+init.portf$constraints[[1]]$max_sum=1.01
+
+# Use random portfolios
+maxSR.lo.RP <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                  optimize_method="random",
+                                  search_size=2000,
+                                  trace=TRUE)
+print(maxSR.lo.RP)
+chart.RiskReward(maxSR.lo.RP, risk.col="StdDev", return.col="mean")
+
+# Use DEoptim
+maxSR.lo.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                  optimize_method="DEoptim",
+                                  search_size=2000,
+                                  trace=TRUE)
+print(maxSR.lo.DE)
+chart.RiskReward(maxSR.lo.DE, risk.col="StdDev", return.col="mean")
+

Added: pkg/PortfolioAnalytics/demo/demo_max_quadratic_utility.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_quadratic_utility.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_max_quadratic_utility.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,49 @@
+
+library(PortfolioAnalytics)
+
+# Examples of solving optimization problems to maximize quadratic utility
+
+data(edhec)
+R <- edhec[, 1:10]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
+init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+# Here we can set the risk_aversion parameter to control how much risk
+# is penalized
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev",
+                            risk_aversion=4)
+print(init.portf)
+
+# Maximizing quadratic utility can be formulated as a quardratic programming 
+# problem and solved very quickly using optimize_method="ROI". Although "StdDev"
+# was specified as an objective, the quadratic programming problem uses the 
+# variance-covariance matrix in the objective function.
+maxQU.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                    optimize_method="ROI", trace=TRUE)
+print(maxQU.lo.ROI)
+
+plot(maxQU.lo.ROI, risk.col="StdDev", main=expression("Long Only Max Quadratic Utility" ~ lambda ~"=0.25"))
+
+# A risk aversion parameter that is very small, will effectively make the term
+# that penalizes risk zero and approximates the maximum return. Note that the
+# risk_aversion parameter must be non-zero
+init.portf$objectives[[2]]$risk_aversion <- 1e-6
+
+maxQU.maxret.ROI <- optimize.portfolio(R=R, portfolio=init.portf, optimize_method="ROI", trace=TRUE)
+print(maxQU.maxret.ROI)
+
+plot(maxQU.maxret.ROI, risk.col="StdDev", main=expression("Long Only Max Quadratic Utility" ~ lambda ~"= 1e-6"))
+
+# A risk aversion parameter that is very large will heavily penalize the risk 
+# term in the objective function and approximates the minimum variance portfolio.
+init.portf$objectives[[2]]$risk_aversion <- 1e6
+
+maxQU.minvol.ROI <- optimize.portfolio(R=R, portfolio=init.portf, optimize_method="ROI", trace=TRUE)
+print(maxQU.minvol.ROI)
+
+plot(maxQU.minvol.ROI, risk.col="StdDev", main=expression("Long Only Max Quadratic Utility" ~ lambda ~"= 1e6"))
+

Added: pkg/PortfolioAnalytics/demo/demo_max_return.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_return.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_max_return.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,87 @@
+
+library(PortfolioAnalytics)
+
+# Examples of solving optimization problems to maximize mean return
+
+data(edhec)
+R <- edhec[, 1:10]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
+init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+print(init.portf)
+
+# Maximizing return can be formulated as a linear programming problem and
+# solved very quickly using optimize_method="ROI". We are using long_only
+# constraints so it is expected that allocation is to the portfolio with the
+# highest mean return.
+maxret.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                    optimize_method="ROI", trace=TRUE)
+print(maxret.lo.ROI)
+
+chart.Weights(maxret.lo.ROI, main="Long Only Maximize Return")
+
+# It is more practical to impose box constraints on the weights of assets.
+# Update the second constraint element with box constraints
+init.portf <- add.constraint(portfolio=init.portf, type="box", 
+                             min=0.05, max=0.3, indexnum=2)
+
+maxret.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                     optimize_method="ROI", trace=TRUE)
+print(maxret.box.ROI)
+
+chart.Weights(maxret.box.ROI, main="Box Maximize Return")
+
+# Although the maximum return objective can be solved quickly and accurately
+# with optimize_method="ROI", it is also possible to solve this optimization
+# problem using other solvers such as random portfolios or DEoptim.
+
+# For random portfolios, the leverage constraints should be relaxed slightly.
+init.portf$constraints[[1]]$min_sum=0.99
+init.portf$constraints[[1]]$max_sum=1.01
+
+# Add StdDev as an object with multiplier=0. The multiplier=0 argument means 
+# that it will not be used in the objective function, but will be calculated
+# for each portfolio so that we can plot the optimal portfolio in 
+# mean-StdDev space.
+init.portf <- add.objective(portfolio=init.portf, type="risk", 
+                            name="StdDev", multiplier=0)
+
+# First run the optimization with a wider bound on the box constraints that 
+# also allows shorting. Then use more restrictive box constraints. This is 
+# useful to visualize impact of the constraints on the feasible space
+
+# create a new portfolio called 'port1' by using init.portf and modify the
+# box constraints
+port1 <- add.constraint(portfolio=init.portf, type="box", 
+                             min=-0.3, max=0.8, indexnum=2)
+
+maxret.box1.RP <- optimize.portfolio(R=R, portfolio=port1, 
+                                    optimize_method="random", 
+                                    search_size=5000, 
+                                    trace=TRUE)
+print(maxret.box1.RP)
+ploy(maxret.box1.RP, risk.col="StdDev")
+
+# create a new portfolio called 'port2' by using init.portf and modify the 
+# box constraints
+port2 <- add.constraint(portfolio=init.portf, type="box", 
+                             min=0.05, max=0.3, indexnum=2)
+
+maxret.box2.RP <- optimize.portfolio(R=R, portfolio=port2, 
+                                    optimize_method="random", 
+                                    search_size=5000, 
+                                    trace=TRUE)
+print(maxret.box2.RP)
+plot(maxret.box2.RP, risk.col="StdDev")
+
+# Now solve the problem with DEoptim
+maxret.box.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                    optimize_method="DEoptim", 
+                                    search_size=5000, 
+                                    trace=TRUE)
+print(maxret.box.DE)
+plot(maxret.box.DE, risk.col="StdDev", return.col="mean")

Added: pkg/PortfolioAnalytics/demo/demo_min_StdDev.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_min_StdDev.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_min_StdDev.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,89 @@
+
+library(PortfolioAnalytics)
+
+# Examples of solving optimization problems to minimize portfolio standard deviation
+
+data(edhec)
+R <- edhec[, 1:10]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
+print(init.portf)
+
+# Minimizing standard deviation can be formulated as a quadratic programming 
+# problem and solved very quickly using optimize_method="ROI". Although "StdDev"
+# was specified as an objective, the quadratic programming problem uses the 
+# variance-covariance matrix in the objective function.
+minStdDev.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                       optimize_method="ROI", 
+                                       trace=TRUE)
+print(minStdDev.lo.ROI)
+
+plot(minStdDev.lo.ROI, risk.col="StdDev", main="Long Only Minimize Portfolio StdDev")
+
+# It is more practical to impose box constraints on the weights of assets.
+# Update the second constraint element with box constraints.
+init.portf <- add.constraint(portfolio=init.portf, type="box", 
+                             min=0.05, max=0.3, indexnum=2)
+
+minStdDev.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                        optimize_method="ROI", 
+                                        trace=TRUE)
+print(minStdDev.box.ROI)
+
+chart.Weights(minStdDev.box.ROI, main="Minimize StdDev with Box Constraints")
+
+# Although the maximum return objective can be solved quickly and accurately
+# with optimize_method="ROI", it is also possible to solve this optimization
+# problem using other solvers such as random portfolios or DEoptim.
+
+# For random portfolios, the leverage constraints should be relaxed slightly.
+init.portf$constraints[[1]]$min_sum=0.99
+init.portf$constraints[[1]]$max_sum=1.01
+
+# Add mean as an object with multiplier=0. The multiplier=0 argument means 
+# that it will not be used in the objective function, but will be calculated
+# for each portfolio so that we can plot the optimal portfolio in 
+# mean-StdDev space.
+init.portf <- add.objective(portfolio=init.portf, type="return", 
+                            name="mean", multiplier=0)
+
+# First run the optimization with a wider bound on the box constraints that 
+# also allows shorting. Then use more restrictive box constraints. This is 
+# useful to visualize impact of the constraints on the feasible space
+
+# create a new portfolio called 'port1' by using init.portf and modify the
+# box constraints
+port1 <- add.constraint(portfolio=init.portf, type="box", 
+                        min=-0.3, max=0.8, indexnum=2)
+
+minStdDev.box1.RP <- optimize.portfolio(R=R, portfolio=port1, 
+                                        optimize_method="random", 
+                                        search_size=5000, 
+                                        trace=TRUE)
+print(minStdDev.box1.RP)
+ploy(minStdDev.box1.RP, risk.col="StdDev")
+
+# create a new portfolio called 'port2' by using init.portf and modify the 
+# box constraints
+port2 <- add.constraint(portfolio=init.portf, type="box", 
+                        min=0.05, max=0.3, indexnum=2)
+
+minStdDev.box2.RP <- optimize.portfolio(R=R, portfolio=port2, 
+                                        optimize_method="random", 
+                                        search_size=5000, 
+                                        trace=TRUE)
+print(minStdDev.box2.RP)
+plot(minStdDev.box2.RP, risk.col="StdDev")
+
+# Now solve the problem with DEoptim
+minStdDev.box.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                       optimize_method="DEoptim", 
+                                       search_size=5000, 
+                                       trace=TRUE)
+print(minStdDev.box.DE)
+plot(minStdDev.box.DE, risk.col="StdDev", return.col="mean")

Added: pkg/PortfolioAnalytics/demo/demo_min_expected_shortfall.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_min_expected_shortfall.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_min_expected_shortfall.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,97 @@
+
+library(PortfolioAnalytics)
+
+# Examples of solving optimization problems to minimize expected shortfall (ES)
+# The objective can also be specified as "CVaR" and "ETL".
+
+data(edhec)
+R <- edhec[, 1:10]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
+# Add objective to minimize expected shortfall with a confidence level of
+# 0.95.
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",
+                            arguments=list(p=0.9))
+print(init.portf)
+
+# Minimizing expected shortfall can be formulated as a linear programming 
+# problem and solved very quickly using optimize_method="ROI". The linear
+# programming problem is formulated to minimize sample ES.
+minES.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                   optimize_method="ROI", 
+                                   trace=TRUE)
+print(minES.lo.ROI)
+
+plot(minES.lo.ROI, risk.col="ES", return.col="mean", 
+     main="Long Only Minimize Expected Shortfall")
+
+# It is more practical to impose box constraints on the weights of assets.
+# Update the second constraint element with box constraints.
+init.portf <- add.constraint(portfolio=init.portf, type="box", 
+                             min=0.05, max=0.3, indexnum=2)
+
+minES.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                    optimize_method="ROI", 
+                                    trace=TRUE)
+print(minES.box.ROI)
+
+chart.Weights(minES.box.ROI, main="Minimize ES with Box Constraints")
+
+# Although the minimum ES objective can be solved quickly and accurately
+# with optimize_method="ROI", it is also possible to solve this optimization
+# problem using other solvers such as random portfolios or DEoptim. These
+# solvers have the added flexibility of using different methods to calculate
+# ES (e.g. gaussian, modified, or historical). The default is to calculate
+# modified ES.
+
+# For random portfolios and DEoptim, the leverage constraints should be 
+# relaxed slightly.
+init.portf$constraints[[1]]$min_sum=0.99
+init.portf$constraints[[1]]$max_sum=1.01
+
+# Add mean as an objective with multiplier=0. The multiplier=0 argument means 
+# that it will not be used in the objective function, but will be calculated
+# for each portfolio so that we can plot the optimal portfolio in 
+# mean-ES space.
+init.portf <- add.objective(portfolio=init.portf, type="return", 
+                            name="mean", multiplier=0)
+
+# First run the optimization with a wider bound on the box constraints that 
+# also allows shorting. Then use more restrictive box constraints. This is 
+# useful to visualize impact of the constraints on the feasible space
+
+# create a new portfolio called 'port1' by using init.portf and modify the
+# box constraints
+port1 <- add.constraint(portfolio=init.portf, type="box", 
+                        min=-0.3, max=0.8, indexnum=2)
+
+minES.box1.RP <- optimize.portfolio(R=R, portfolio=port1, 
+                                    optimize_method="random", 
+                                    search_size=5000, 
+                                    trace=TRUE)
+print(minES.box1.RP)
+plot(minES.box1.RP, risk.col="ES", return.col="mean")
+
+# create a new portfolio called 'port2' by using init.portf and modify the 
+# box constraints
+port2 <- add.constraint(portfolio=init.portf, type="box", 
+                        min=0.05, max=0.3, indexnum=2)
+
+minES.box2.RP <- optimize.portfolio(R=R, portfolio=port2, 
+                                    optimize_method="random", 
+                                    search_size=5000, 
+                                    trace=TRUE)
+print(minES.box2.RP)
+plot(minES.box2.RP, risk.col="ES", return.col="mean")
+
+# Now solve the problem with DEoptim
+minES.box.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                   optimize_method="DEoptim", 
+                                   search_size=5000, 
+                                   trace=TRUE)
+print(minES.box.DE)
+plot(minES.box.DE, risk.col="ES", return.col="mean")

Added: pkg/PortfolioAnalytics/demo/demo_risk_budgets.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_risk_budgets.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/demo_risk_budgets.R	2013-11-08 03:06:29 UTC (rev 3246)
@@ -0,0 +1,70 @@
+
+library(PortfolioAnalytics)
+
+# Examples of solving optimization problems using risk budget objectives
+
+data(edhec)
+R <- edhec[, 1:8]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="leverage", 
+                             min_sum=0.99, max_sum=1.01)
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
+
+# Portfolio optimization problems with risk budget objectives must be solved
+# with DEoptim, random portfolios, pso, or GenSA.
+
+# Risk budget objectives can be used to place limits on component contribution
+# to risk or for equal risk contribution portfolios. Note that there are 
+# potentially many portfolios that satisfy component ES risk limits so we need
+# to have a "sub" objective such as maximizing return, minimizing ES, 
+# minimizing StdDev, etc.
+
+# Add objective to maximize mean with limit on component ES risk contribution
+# The max_prisk controls the maximum percentage contribution to risk
+rbES.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+rbES.portf <- add.objective(portfolio=rbES.portf, type="risk_budget", name="ES",
+                            max_prisk=0.4, arguments=list(p=0.92))
+
+# Use DEoptim for optimization
+rbES.DE <- optimize.portfolio(R=R, portfolio=rbES.portf, 
+                              optimize_method="DEoptim", 
+                              search_size=5000, trace=TRUE)
+print(rbES.DE)
+plot(rbES.DE, xlim=c(0, 0.08), ylim=c(0, 0.01))
+chart.RiskBudget(rbES.DE, risk.type="pct_contrib")
+
+# Add objective to maximize mean return with equal ES risk contribution
+eqES.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+eqES.portf <- add.objective(portfolio=eqES.portf, type="risk_budget", 
+                            name="ES", min_concentration=TRUE, arguments=list(p=0.9))
+
+# Use random portfolios for optimization
+# Use cleaned returns
+R.clean <- Return.clean(R=R, method="boudt")
+eqES.RP <- optimize.portfolio(R=R.clean, portfolio=eqES.portf,
+                              optimize_method="random",
+                              search_size=2500, trace=TRUE)
+
+print(eqES.RP)
+plot(eqES.RP)
+chart.RiskBudget(eqES.RP, risk.type="pct_contrib")
+
+# Add objective to maximize mean return with limits on StdDev risk contribution
+rbStdDev.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
+rbStdDev.portf <- add.objective(portfolio=rbStdDev.portf, type="risk_budget",
+                                name="StdDev", max_prisk=0.25)
+
+# Use DEoptim for optimization
+rbStdDev.DE <- optimize.portfolio(R=R, portfolio=rbStdDev.portf,
+                                  optimize_method="DEoptim",
+                                  search_size=5000, trace=TRUE)
+
+print(eqES.RP)
+plot(eqES.RP)
+chart.RiskBudget(eqES.RP, risk.type="pct_contrib")
+
+
+



More information about the Returnanalytics-commits mailing list