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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Dec 15 23:58:27 CET 2013


Author: rossbennett34
Date: 2013-12-15 23:58:27 +0100 (Sun, 15 Dec 2013)
New Revision: 3277

Modified:
   pkg/PortfolioAnalytics/demo/backwards_compat.R
   pkg/PortfolioAnalytics/demo/demo_efficient_frontier.R
   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_return_target.R
   pkg/PortfolioAnalytics/demo/demo_risk_budgets.R
   pkg/PortfolioAnalytics/demo/demo_weight_concentration.R
Log:
Minor modifications to some demos for consistency and easier testing

Modified: pkg/PortfolioAnalytics/demo/backwards_compat.R
===================================================================
--- pkg/PortfolioAnalytics/demo/backwards_compat.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/backwards_compat.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -8,7 +8,8 @@
 funds <- colnames(ret)
 
 # Set up constraint object using v1 specification
-gen.constr <- constraint(assets=funds, min=0, max=0.55, min_sum=0.99, max_sum=1, weight_seq=generatesequence(min=0, max=0.55, by=0.002))
+gen.constr <- constraint(assets=funds, min=0, max=0.55, min_sum=0.99, max_sum=1.01, 
+                         weight_seq=generatesequence(min=0, max=0.55, by=0.002))
 class(gen.constr)
 
 # Add an objective to the gen.constr object
@@ -19,17 +20,16 @@
 # and will update to the v2 specification using a portfolio object with 
 # constraints and objectives from the v1_constraint object.
 
-# Random
+# Random Portfolios
 optrpv1 <- optimize.portfolio(R=ret, constraints=gen.constr, optimize_method="random", search_size=2000)
-print(optrpv1$portfolio)
-print(optrpv1)
+optrpv1
 
 # DEoptim
 optdev1 <- optimize.portfolio(R=ret, constraints=gen.constr, optimize_method="DEoptim", search_size=2000)
-print(optdev1)
+optdev1
 
 # ROI
 optroiv1 <- optimize.portfolio(R=ret, constraints=gen.constr, optimize_method="ROI")
-print(optroiv1)
+optroiv1
 
 

Modified: pkg/PortfolioAnalytics/demo/demo_efficient_frontier.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_efficient_frontier.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_efficient_frontier.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -40,7 +40,7 @@
 
 # mean-var efficient frontier
 meanvar.ef <- create.EfficientFrontier(R=R, portfolio=meanvar.portf, type="mean-StdDev")
-print(meanvar.ef)
+meanvar.ef
 summary(meanvar.ef, digits=2)
 meanvar.ef$frontier
 
@@ -107,15 +107,17 @@
 # the optimize.portfolio object, it is best to extractEfficientFrontier as shown
 # below
 ef <- extractEfficientFrontier(object=opt_meanvar, match.col="StdDev", n.portfolios=15)
-print(ef)
+ef
 summary(ef, digits=5)
 chart.Weights.EF(ef, match.col="StdDev", colorset=bluemono)
 chart.Weights.EF(ef, match.col="StdDev", colorset=bluemono, by.groups=TRUE)
 
 # mean-etl efficient frontier
 meanetl.ef <- create.EfficientFrontier(R=R, portfolio=meanetl.portf, type="mean-ES")
-print(meanetl.ef)
+meanetl.ef
 summary(meanetl.ef)
+meanetl.ef$frontier
+
 chart.EfficientFrontier(meanetl.ef, match.col="ES", main="mean-ETL Efficient Frontier", type="l", col="blue", RAR.text="STARR")
 chart.Weights.EF(meanetl.ef, colorset=bluemono, match.col="ES")
 chart.Weights.EF(meanetl.ef, by.groups=TRUE, colorset=bluemono, match.col="ES")

Modified: pkg/PortfolioAnalytics/demo/demo_group_constraints.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_group_constraints.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_group_constraints.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -9,23 +9,23 @@
 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")
+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  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)
+init.portf <- add.constraint(portfolio=init.portf, 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))
+init.portf
 
 
 # Add an objective to minimize portfolio standard deviation
-pspec <- add.objective(portfolio=pspec, type="risk", name="StdDev")
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
 
 # The examples here use the obective to minimize standard deviation, but any
 # supported objective can also be used.
@@ -34,25 +34,25 @@
 # 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)
+minStdDev.ROI <- optimize.portfolio(R=R, portfolio=init.portf, optimize_method="ROI")
+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
+init.portf$constraints[[1]]$min_sum=0.99
+init.portf$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)
+minStdDev.RP <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                   optimize_method="random", search_size=2000)
+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)
+minStdDev.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
+                                   optimize_method="DEoptim", search_size=2000)
+minStdDev.DE
 extractGroups(minStdDev.DE)

Modified: pkg/PortfolioAnalytics/demo/demo_leverage_exposure_constraint.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_leverage_exposure_constraint.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_leverage_exposure_constraint.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -4,7 +4,7 @@
 library(PortfolioAnalytics)
 
 data(edhec)
-R <- edhec[, 1:5]
+R <- edhec[, 1:10]
 funds <- colnames(R)
 
 # Set up an initial portfolio object with basic constraints
@@ -30,8 +30,8 @@
 # Run optimization
 dollar.neutral.opt <- optimize.portfolio(R=R, portfolio=dollar.neutral.portf, 
                                          optimize_method="DEoptim",
-                                         search_size=2500)
-print(dollar.neutral.opt)
+                                         search_size=2000)
+dollar.neutral.opt
 
 # Leveraged portfolio with max 1.6:1 leverage constraint
 leveraged.portf <- init.portf
@@ -46,7 +46,6 @@
 # Run optimization
 leveraged.opt <- optimize.portfolio(R=R, portfolio=leveraged.portf, 
                                          optimize_method="DEoptim",
-                                         search_size=2500)
-print(leveraged.opt)
+                                         search_size=2000)
+leveraged.opt
 
-

Modified: pkg/PortfolioAnalytics/demo/demo_max_STARR.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_STARR.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_max_STARR.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -15,20 +15,20 @@
 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)
+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
+# The default action if "mean" and "ES" are specified as objectives with
+# optimize_method="ROI" is to maximize STARR. 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)
+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
@@ -45,17 +45,17 @@
 # Use random portfolios
 maxSTARR.lo.RP <- optimize.portfolio(R=R, portfolio=init.portf, 
                                   optimize_method="random",
-                                  search_size=5000,
+                                  search_size=2000,
                                   trace=TRUE)
-print(maxSTARR.lo.RP)
+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)
+                                     optimize_method="DEoptim",
+                                     search_size=2000,
+                                     trace=TRUE)
+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))

Modified: pkg/PortfolioAnalytics/demo/demo_max_Sharpe.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_Sharpe.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_max_Sharpe.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -13,9 +13,9 @@
 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)
+init.portf
 
-# Maximizing Sharpe Ratio can be formulated as a quardratic programming 
+# Maximizing Sharpe Ratio 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.
@@ -27,7 +27,7 @@
 maxSR.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
                                    optimize_method="ROI", 
                                    maxSR=TRUE, trace=TRUE)
-print(maxSR.lo.ROI)
+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
@@ -45,7 +45,7 @@
                                   optimize_method="random",
                                   search_size=2000,
                                   trace=TRUE)
-print(maxSR.lo.RP)
+maxSR.lo.RP
 chart.RiskReward(maxSR.lo.RP, risk.col="StdDev", return.col="mean")
 
 # Use DEoptim
@@ -53,6 +53,6 @@
                                   optimize_method="DEoptim",
                                   search_size=2000,
                                   trace=TRUE)
-print(maxSR.lo.DE)
+maxSR.lo.DE
 chart.RiskReward(maxSR.lo.DE, risk.col="StdDev", return.col="mean")
 

Modified: pkg/PortfolioAnalytics/demo/demo_max_quadratic_utility.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_quadratic_utility.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_max_quadratic_utility.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -16,7 +16,7 @@
 # is penalized
 init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev",
                             risk_aversion=4)
-print(init.portf)
+init.portf
 
 # Maximizing quadratic utility can be formulated as a quardratic programming 
 # problem and solved very quickly using optimize_method="ROI". Although "StdDev"
@@ -24,7 +24,7 @@
 # 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)
+maxQU.lo.ROI
 
 plot(maxQU.lo.ROI, risk.col="StdDev", main=expression("Long Only Max Quadratic Utility" ~ lambda ~"=0.25"))
 
@@ -34,7 +34,7 @@
 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)
+maxQU.maxret.ROI
 
 plot(maxQU.maxret.ROI, risk.col="StdDev", main=expression("Long Only Max Quadratic Utility" ~ lambda ~"= 1e-6"))
 
@@ -43,7 +43,7 @@
 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)
+maxQU.minvol.ROI
 
 plot(maxQU.minvol.ROI, risk.col="StdDev", main=expression("Long Only Max Quadratic Utility" ~ lambda ~"= 1e6"))
 

Modified: pkg/PortfolioAnalytics/demo/demo_max_return.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_max_return.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_max_return.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -12,7 +12,7 @@
 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)
+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
@@ -20,7 +20,7 @@
 # highest mean return.
 maxret.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
                                     optimize_method="ROI", trace=TRUE)
-print(maxret.lo.ROI)
+maxret.lo.ROI
 
 chart.Weights(maxret.lo.ROI, main="Long Only Maximize Return")
 
@@ -31,7 +31,7 @@
 
 maxret.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf, 
                                      optimize_method="ROI", trace=TRUE)
-print(maxret.box.ROI)
+maxret.box.ROI
 
 chart.Weights(maxret.box.ROI, main="Box Maximize Return")
 
@@ -61,9 +61,9 @@
 
 maxret.box1.RP <- optimize.portfolio(R=R, portfolio=port1, 
                                     optimize_method="random", 
-                                    search_size=5000, 
+                                    search_size=2000, 
                                     trace=TRUE)
-print(maxret.box1.RP)
+maxret.box1.RP
 ploy(maxret.box1.RP, risk.col="StdDev")
 
 # create a new portfolio called 'port2' by using init.portf and modify the 
@@ -73,15 +73,15 @@
 
 maxret.box2.RP <- optimize.portfolio(R=R, portfolio=port2, 
                                     optimize_method="random", 
-                                    search_size=5000, 
+                                    search_size=2000, 
                                     trace=TRUE)
-print(maxret.box2.RP)
+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, 
+                                    search_size=2000, 
                                     trace=TRUE)
-print(maxret.box.DE)
+maxret.box.DE
 plot(maxret.box.DE, risk.col="StdDev", return.col="mean")

Modified: pkg/PortfolioAnalytics/demo/demo_min_StdDev.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_min_StdDev.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_min_StdDev.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -63,7 +63,7 @@
 
 minStdDev.box1.RP <- optimize.portfolio(R=R, portfolio=port1, 
                                         optimize_method="random", 
-                                        search_size=5000, 
+                                        search_size=2000, 
                                         trace=TRUE)
 print(minStdDev.box1.RP)
 ploy(minStdDev.box1.RP, risk.col="StdDev")
@@ -75,7 +75,7 @@
 
 minStdDev.box2.RP <- optimize.portfolio(R=R, portfolio=port2, 
                                         optimize_method="random", 
-                                        search_size=5000, 
+                                        search_size=2000, 
                                         trace=TRUE)
 print(minStdDev.box2.RP)
 plot(minStdDev.box2.RP, risk.col="StdDev")
@@ -83,7 +83,7 @@
 # Now solve the problem with DEoptim
 minStdDev.box.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
                                        optimize_method="DEoptim", 
-                                       search_size=5000, 
+                                       search_size=2000, 
                                        trace=TRUE)
 print(minStdDev.box.DE)
 plot(minStdDev.box.DE, risk.col="StdDev", return.col="mean")

Modified: pkg/PortfolioAnalytics/demo/demo_min_expected_shortfall.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_min_expected_shortfall.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_min_expected_shortfall.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -13,7 +13,7 @@
 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.
+# 0.9.
 init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",
                             arguments=list(p=0.9))
 print(init.portf)
@@ -71,7 +71,7 @@
 
 minES.box1.RP <- optimize.portfolio(R=R, portfolio=port1, 
                                     optimize_method="random", 
-                                    search_size=5000, 
+                                    search_size=2000, 
                                     trace=TRUE)
 print(minES.box1.RP)
 plot(minES.box1.RP, risk.col="ES", return.col="mean")
@@ -83,7 +83,7 @@
 
 minES.box2.RP <- optimize.portfolio(R=R, portfolio=port2, 
                                     optimize_method="random", 
-                                    search_size=5000, 
+                                    search_size=2000, 
                                     trace=TRUE)
 print(minES.box2.RP)
 plot(minES.box2.RP, risk.col="ES", return.col="mean")
@@ -91,7 +91,7 @@
 # Now solve the problem with DEoptim
 minES.box.DE <- optimize.portfolio(R=R, portfolio=init.portf, 
                                    optimize_method="DEoptim", 
-                                   search_size=5000, 
+                                   search_size=2000, 
                                    trace=TRUE)
 print(minES.box.DE)
 plot(minES.box.DE, risk.col="ES", return.col="mean")

Modified: pkg/PortfolioAnalytics/demo/demo_return_target.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_return_target.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_return_target.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -6,45 +6,49 @@
 data(edhec)
 ret <- edhec[, 1:4]
 
-# set up portfolio specification object target in the return object
-pspec1 <- portfolio.spec(assets=colnames(ret))
-pspec1 <- add.constraint(portfolio=pspec1, type="leverage", min_sum=0.99, max_sum=1.01)
-pspec1 <- add.constraint(portfolio=pspec1, type="box")
-pspec1 <- add.objective(portfolio=pspec1, type="return", name="mean", target=0.007)
+# Create an initial portfolio object
+init.portf <- portfolio.spec(assets=colnames(ret))
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
 
-# run optimization using ROI with pspec1
-opt1 <- optimize.portfolio(R=ret, portfolio=pspec1, optimize_method="ROI")
-opt1
-summary(opt1)
-wts1 <- extractWeights(opt1)
+# Add mean return objective with target return
+ret.obj.portf <- add.objective(portfolio=init.portf, type="return", 
+                         name="mean", target=0.007)
 
-# set up portfolio specification object target with the return constraint
-pspec2 <- portfolio.spec(assets=colnames(ret))
-pspec2 <- add.constraint(portfolio=pspec2, type="leverage", min_sum=0.99, max_sum=1.01)
-pspec2 <- add.constraint(portfolio=pspec2, type="box")
-pspec2 <- add.constraint(portfolio=pspec2, type="return", return_target=0.007)
-pspec2 <- add.objective(portfolio=pspec2, type="return", name="mean")
+# Add return target constraint
+ret.constr.portf <- add.constraint(portfolio=init.portf, type="return", return_target=0.007)
+ret.constr.portf <- add.objective(portfolio=ret.constr.portf, type="return", name="mean")
 
-# run optimization using ROI with pspec2
-opt2 <- optimize.portfolio(R=ret, portfolio=pspec2, optimize_method="ROI")
-opt2
-summary(opt2)
-wts2 <- extractWeights(opt2)
 
-# run optimization with DEoptim using pspec1
+# Run optimization using ROI with target return as an objective
+ret.obj.opt <- optimize.portfolio(R=ret, portfolio=ret.obj.portf, optimize_method="ROI")
+ret.obj.opt
+
+# Run optimization using ROI with target return as a constraint
+ret.constr.opt <- optimize.portfolio(R=ret, portfolio=ret.constr.portf, optimize_method="ROI")
+ret.constr.opt
+
+# Relaxe constraints for the sum of weights for DEoptim and random portfolios
+ret.obj.portf$constraints[[1]]$min_sum <- 0.99
+ret.obj.portf$constraints[[1]]$max_sum <- 1.01
+
+ret.constr.portf$constraints[[1]]$min_sum <- 0.99
+ret.constr.portf$constraints[[1]]$max_sum <- 1.01
+
+# run optimization with DEoptim using ret.obj.portf
 set.seed(123)
-opt_de1 <- optimize.portfolio(R=ret, portfolio=pspec1, optimize_method="DEoptim", search_size=4000, traceDE=5)
-opt_de1
+opt.obj.de <- optimize.portfolio(R=ret, portfolio=ret.obj.portf, optimize_method="DEoptim", search_size=2000, traceDE=5)
+opt.obj.de
 
-# run optimization with DEoptim using pspec2
+# run optimization with DEoptim using ret.constr.portf
 set.seed(123)
-opt_de2 <- optimize.portfolio(R=ret, portfolio=pspec2, optimize_method="DEoptim", search_size=4000, traceDE=5)
-opt_de2
+opt.constr.de <- optimize.portfolio(R=ret, portfolio=ret.constr.portf, optimize_method="DEoptim", search_size=2000, traceDE=5)
+opt.constr.de
 
-# run optimizations with random portfolios using pspec1
-opt_rp1 <- optimize.portfolio(R=ret, portfolio=pspec1, optimize_method="random", search_size=4000)
-opt_rp1
+# run optimizations with random portfolios using ret.obj.portf
+opt.obj.rp <- optimize.portfolio(R=ret, portfolio=ret.obj.portf, optimize_method="random", search_size=2000)
+opt.obj.rp
 
-# run optimizations with random portfolios using pspec2
-opt_rp2 <- optimize.portfolio(R=ret, portfolio=pspec2, optimize_method="random", search_size=4000)
-opt_rp2
+# run optimizations with random portfolios using ret.constr.portf
+opt.constr.rp <- optimize.portfolio(R=ret, portfolio=ret.constr.portf, optimize_method="random", search_size=2000)
+opt.constr.rp

Modified: pkg/PortfolioAnalytics/demo/demo_risk_budgets.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_risk_budgets.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_risk_budgets.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -22,8 +22,8 @@
 # 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
+# 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))
@@ -31,24 +31,26 @@
 # Use DEoptim for optimization
 rbES.DE <- optimize.portfolio(R=R, portfolio=rbES.portf, 
                               optimize_method="DEoptim", 
-                              search_size=5000, trace=TRUE)
-print(rbES.DE)
+                              search_size=2000, trace=TRUE)
+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))
+                            name="ES", min_concentration=TRUE, 
+                            arguments=list(p=0.9, clean="boudt"),
+                            multiplier=10)
 
 # 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,
+# R.clean <- Return.clean(R=R, method="boudt")
+eqES.RP <- optimize.portfolio(R=R, portfolio=eqES.portf,
                               optimize_method="random",
-                              search_size=2500, trace=TRUE)
+                              search_size=2000, trace=TRUE)
 
-print(eqES.RP)
+eqES.RP
 plot(eqES.RP)
 chart.RiskBudget(eqES.RP, risk.type="pct_contrib")
 
@@ -60,11 +62,8 @@
 # Use DEoptim for optimization
 rbStdDev.DE <- optimize.portfolio(R=R, portfolio=rbStdDev.portf,
                                   optimize_method="DEoptim",
-                                  search_size=5000, trace=TRUE)
+                                  search_size=2000, trace=TRUE)
 
-print(eqES.RP)
-plot(eqES.RP)
-chart.RiskBudget(eqES.RP, risk.type="pct_contrib")
-
-
-
+rbStdDev.DE
+plot(rbStdDev.DE, risk.col="StdDev", xlim=c(0, 0.035), ylim=c(0, 0.01))
+chart.RiskBudget(rbStdDev.DE, risk.type="pct_contrib")

Modified: pkg/PortfolioAnalytics/demo/demo_weight_concentration.R
===================================================================
--- pkg/PortfolioAnalytics/demo/demo_weight_concentration.R	2013-12-15 04:35:02 UTC (rev 3276)
+++ pkg/PortfolioAnalytics/demo/demo_weight_concentration.R	2013-12-15 22:58:27 UTC (rev 3277)
@@ -12,21 +12,21 @@
 
 
 # Create initial portfolio object with category_labels
-init <- portfolio.spec(assets=funds, category_labels=cap_labels)
+init.portf <- portfolio.spec(assets=funds, category_labels=cap_labels)
 # Add some weight constraints
-init <- add.constraint(portfolio=init, type="full_investment")
-init <- add.constraint(portfolio=init, type="long_only")
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+init.portf <- add.constraint(portfolio=init.portf, type="long_only")
 # Add objective to minimize variance
-init <- add.objective(portfolio=init, type="risk", name="var")
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="var")
 
 # Run the optimization with var as the only objective
-opt1 <- optimize.portfolio(R=R, portfolio=init, optimize_method="ROI", trace=TRUE)
+opt1 <- optimize.portfolio(R=R, portfolio=init.portf, optimize_method="ROI", trace=TRUE)
 opt1
 
 # Add the weight_concentration objective
 # Set the conc_aversion values to 0 so that we should get the same value as min var
-conc <- add.objective(portfolio=init, type="weight_concentration", name="HHI", 
-                       conc_aversion=0, conc_groups=init$category_labels)
+conc.portf <- add.objective(portfolio=init.portf, type="weight_concentration", name="HHI", 
+                            conc_aversion=0, conc_groups=init.portf$category_labels)
 
 opt2 <- optimize.portfolio(R=R, portfolio=conc, optimize_method="ROI", trace=TRUE)
 opt2
@@ -34,8 +34,8 @@
 chart.Weights(opt2)
 
 # Now change the conc_aversion values
-conc$objectives[[2]]$conc_aversion <- c(0.03, 0.03, 0.06, 0.02)
-opt3 <- optimize.portfolio(R=R, portfolio=conc, optimize_method="ROI", trace=TRUE)
+conc.portf$objectives[[2]]$conc_aversion <- c(0.03, 0.03, 0.06, 0.02)
+opt3 <- optimize.portfolio(R=R, portfolio=conc.portf, optimize_method="ROI", trace=TRUE)
 opt3
 
 chart.Weights(opt3)
@@ -45,7 +45,7 @@
 chart.GroupWeights(opt3, grouping="category", plot.type="barplot", col=bluemono)
 
 # If all the conc_aversion values are very high, this should result in an equal weight portfolio
-conc$objectives[[2]]$conc_aversion <- rep(1e6, 4)
-opt4 <- optimize.portfolio(R=R, portfolio=conc, optimize_method="ROI", trace=TRUE)
+conc.portf$objectives[[2]]$conc_aversion <- rep(1e6, 4)
+opt4 <- optimize.portfolio(R=R, portfolio=conc.portf, optimize_method="ROI", trace=TRUE)
 opt4
 chart.Weights(opt4)



More information about the Returnanalytics-commits mailing list