[Returnanalytics-commits] r3425 - in pkg/PortfolioAnalytics: R demo sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 18 19:26:43 CEST 2014


Author: rossbennett34
Date: 2014-06-18 19:26:42 +0200 (Wed, 18 Jun 2014)
New Revision: 3425

Added:
   pkg/PortfolioAnalytics/demo/multi_layer_optimization.R
   pkg/PortfolioAnalytics/sandbox/mult_layer_script.R
Removed:
   pkg/PortfolioAnalytics/sandbox/multi_layer_script.R
Modified:
   pkg/PortfolioAnalytics/R/mult.layer.portfolio.R
   pkg/PortfolioAnalytics/demo/00Index
Log:
Adding better multi layer script to sandbox. Adding demo. Improving checks in function to compute the proxy returns for sub portfolios.

Modified: pkg/PortfolioAnalytics/R/mult.layer.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/mult.layer.portfolio.R	2014-06-17 20:13:06 UTC (rev 3424)
+++ pkg/PortfolioAnalytics/R/mult.layer.portfolio.R	2014-06-18 17:26:42 UTC (rev 3425)
@@ -126,6 +126,9 @@
   }
   
   n.sub.portfolios <- length(mult.portfolio$sub.portfolios)
+  if(n.sub.portfolios <= 1) stop("Must have more than 1 sub portfolio")
+  
+  # Initialize list to store the returns for each sub portfolio
   ret <- vector("list", n.sub.portfolios)
   
   # Loop through the sub portfolios and call optimize.portfolio.rebalancing
@@ -142,21 +145,21 @@
       stop("R object of returns not subset correctly. Make sure the names of 
            the assets in the sub portfolio match the column names of the R object") 
     }
-    # This needs to support 
+    # This needs to support anything in ... that could be passed to optimize.portfolio
     .formals <- formals(optimize.portfolio.rebalancing)
     .formals <- PortfolioAnalytics:::modify.args(formals=.formals, arglist=NULL, R=R, dots=TRUE)
     .formals <- PortfolioAnalytics:::modify.args(formals=.formals, arglist=tmp, dots=TRUE)
     .formals$... <- NULL
     #print(.formals)
     opt <- try(do.call(optimize.portfolio.rebalancing, .formals), silent=TRUE)
-    if(inherits(opt, "try-error")) { 
-      message(paste("optimize.portfolio.rebalancing for sub portfolio", i, "generated an error or warning:", opt))
-      next()  
-    } 
-    ret.tmp <- Return.rebalancing(R.tmp, extractWeights(opt))
-    colnames(ret.tmp) <- paste("proxy", i, sep=".")
-    ret[[i]] <- ret.tmp
-    #print(ret[[i]])
+    if(!inherits(opt, "try-error")) {
+      ret.tmp <- Return.rebalancing(R.tmp, extractWeights(opt))
+      colnames(ret.tmp) <- paste("proxy", i, sep=".")
+      ret[[i]] <- ret.tmp
+      #print(ret[[i]])
+    } else {
+      stop(paste("optimize.portfolio.rebalancing for sub portfolio", i, "generated an error or warning:", opt))
+    }
   }
   proxy.ret <- na.omit(do.call(cbind, ret))
   return(proxy.ret)

Modified: pkg/PortfolioAnalytics/demo/00Index
===================================================================
--- pkg/PortfolioAnalytics/demo/00Index	2014-06-17 20:13:06 UTC (rev 3424)
+++ pkg/PortfolioAnalytics/demo/00Index	2014-06-18 17:26:42 UTC (rev 3425)
@@ -30,3 +30,4 @@
 multiple_portfolio_optimization Demonstrate passing a list of portfolios to optimize.portfolio and optimize.portfolio.rebalancing
 regime_switching       Demonstrate optimization with support for regime switching to switch portfolios based on the regime.
 higher_moments_boudt     Demonstrate using a statistical factor model to estimate moments based on work by Kris Boudt.
+multi_layer_optimization    Demonstrate multi layer optimization of optimization problem with two layers and two sub portfolios in the lower layer.

Added: pkg/PortfolioAnalytics/demo/multi_layer_optimization.R
===================================================================
--- pkg/PortfolioAnalytics/demo/multi_layer_optimization.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/multi_layer_optimization.R	2014-06-18 17:26:42 UTC (rev 3425)
@@ -0,0 +1,79 @@
+
+# Demonstrate multi layer portfolio optimization
+# The top level (i.e. layer) optimization problem is to minimize modified ES
+# with equal component contribution to modified ES of the two portfolios in 
+# the lower layer.
+# The sub portfolios consist of different assets and different objectives
+# relative to each other. The out of sample returns for each sub portfolio
+# are calculated based on their respective constraints, objectives, and
+# optimization parameters. The out of sample returns are then used as the
+# returns input for the top level optimization.
+
+library(PortfolioAnalytics)
+
+data(edhec)
+R <- edhec[, 1:10]
+funds <- colnames(R)
+
+# The first sub-portfolio, portf1, will contain assets 1:5 of edhec
+# with an objective to minimize standard deviation.
+portf1 <- portfolio.spec(assets=funds[1:5])
+portf1 <- add.constraint(portfolio=portf1, type="weight_sum", 
+                         min_sum=0.99, max_sum=1.01)
+portf1 <- add.constraint(portfolio=portf1, type="long_only")
+portf1 <- add.objective(portfolio=portf1, type="risk", name="StdDev")
+
+# The second sub-portfolio, portf2, will contain assets 6:10 of edhec
+# with an objective to minimize expected shortfall.
+portf2 <- portfolio.spec(assets=funds[6:10])
+# portf2 <- portfolio.spec(assets=5)
+portf2 <- add.constraint(portfolio=portf2, type="weight_sum", 
+                         min_sum=0.99, max_sum=1.01)
+portf2 <- add.constraint(portfolio=portf2, type="long_only")
+portf2 <- add.objective(portfolio=portf2, type="risk", name="ES",
+                            arguments=list(p=0.9))
+
+# portf1 and portf2 have the same constraints so they can used the same
+# set of random portfolios
+set.seed(123)
+rp <- random_portfolios(portf2, 2000)
+
+
+# The 'top level' portfolio has objectives for equal contribution to risk
+# where modified ES is the risk measure
+portf <- portfolio.spec(assets=paste("proxy",1:2, sep="."))
+portf <- add.constraint(portfolio=portf, type="weight_sum", 
+                        min_sum=0.99, max_sum=1.01)
+portf <- add.constraint(portfolio=portf, type="long_only")
+portf <- add.objective(portfolio=portf, type="risk", name="ES",
+                        arguments=list(p=0.9))
+portf <- add.objective(portfolio=portf, type="risk_budget", name="ES",
+                       arguments=list(p=0.9), min_concentration=TRUE)
+
+# Specify a mult-layer portfolio
+mult.portf <- mult.portfolio.spec(portf)
+
+# Add portf1 as a sub portfolio with optimization parameters specific to 
+# running optimize.portfolio.rebalancing with portf1
+mult.portf <- add.sub.portfolio(mult.portf, portf1, rp=rp, 
+                                optimize_method="random",
+                                rebalance_on="quarters", 
+                                training_period=136)
+
+# Add portf2 as a sub portfolio with optimization parameters specific to 
+# running optimize.portfolio.rebalancing with portf2
+mult.portf <- add.sub.portfolio(mult.portf, portf2, rp=rp,
+                                optimize_method="random",
+                                rebalance_on="months", 
+                                training_period=136, 
+                                trailing_periods=48)
+
+# Generate random portfolios for the top layer optimization
+set.seed(123)
+rp.top <- random_portfolios(portf, 1000)
+
+# Run the multi layer optimization
+opt.mult <- optimize.portfolio(R, mult.portf,
+                               optimize_method="random", 
+                               trace=TRUE, rp=rp.top)
+

Added: pkg/PortfolioAnalytics/sandbox/mult_layer_script.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/mult_layer_script.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/mult_layer_script.R	2014-06-18 17:26:42 UTC (rev 3425)
@@ -0,0 +1,96 @@
+
+data(edhec)
+R <- edhec[, 1:10]
+funds <- colnames(R)
+
+# The first sub-portfolio, portf1, will contain assets 1:5 of edhec
+# with an objective to minimize standard deviation.
+portf1 <- portfolio.spec(assets=funds[1:5])
+portf1 <- add.constraint(portfolio=portf1, type="weight_sum", 
+                         min_sum=0.99, max_sum=1.01)
+portf1 <- add.constraint(portfolio=portf1, type="long_only")
+portf1 <- add.objective(portfolio=portf1, type="risk", name="StdDev")
+
+# The second sub-portfolio, portf2, will contain assets 6:10 of edhec
+# with an objective to minimize expected shortfall.
+portf2 <- portfolio.spec(assets=funds[6:10])
+# portf2 <- portfolio.spec(assets=5)
+portf2 <- add.constraint(portfolio=portf2, type="weight_sum", 
+                         min_sum=0.99, max_sum=1.01)
+portf2 <- add.constraint(portfolio=portf2, type="long_only")
+portf2 <- add.objective(portfolio=portf2, type="risk", name="ES",
+                            arguments=list(p=0.9))
+
+# portf1 and portf2 have the same constraints so they can used the same
+# set of random portfolios
+set.seed(123)
+rp <- random_portfolios(portf2, 2000)
+
+
+# The 'top level' portfolio has objectives for equal contribution to risk
+# where modified ES is the risk measure
+portf <- portfolio.spec(assets=paste("proxy",1:2, sep="."))
+portf <- add.constraint(portfolio=portf, type="weight_sum", 
+                        min_sum=0.99, max_sum=1.01)
+portf <- add.constraint(portfolio=portf, type="long_only")
+portf <- add.objective(portfolio=portf, type="risk", name="ES",
+                        arguments=list(p=0.9))
+portf <- add.objective(portfolio=portf, type="risk_budget", name="ES",
+                       arguments=list(p=0.9), min_concentration=TRUE)
+
+# Specify a mult-layer portfolio
+mult.portf <- mult.portfolio.spec(portf)
+
+# Add portf1 as a sub portfolio with optimization parameters specific to 
+# running optimize.portfolio.rebalancing with portf1
+mult.portf <- add.sub.portfolio(mult.portf, portf1, rp=rp, 
+                                optimize_method="random",
+                                rebalance_on="quarters", 
+                                training_period=136)
+
+# Add portf2 as a sub portfolio with optimization parameters specific to 
+# running optimize.portfolio.rebalancing with portf2
+mult.portf <- add.sub.portfolio(mult.portf, portf2, rp=rp,
+                                optimize_method="random",
+                                rebalance_on="months", 
+                                training_period=136, 
+                                trailing_periods=48)
+
+# Compute the out of sample backtesting returns for each sub portfolio
+proxy.ret <- PortfolioAnalytics:::proxy.mult.portfolio(R, mult.portf)
+
+# Verify that proxy.mult.portfolio is computing returns correctly
+opt1 <- optimize.portfolio.rebalancing(R[,1:5], 
+                                       portf1, 
+                                       optimize_method="random",
+                                       rp=rp,
+                                       rebalance_on="quarters", 
+                                       training_period=136)
+ret1 <- summary(opt1)$portfolio_returns
+
+opt2 <- optimize.portfolio.rebalancing(R[,6:10], 
+                                       portf2, 
+                                       optimize_method="random",
+                                       rp=rp,
+                                       rebalance_on="months", 
+                                       training_period=136,
+                                       trailing_periods=48)
+ret2 <- summary(opt2)$portfolio_returns
+ret <- na.omit(cbind(ret1, ret2))
+
+all.equal(ret, proxy.ret, check.attributes=FALSE)
+
+# Verify that multi layer optimization is done correctly in optimize.portfolio
+set.seed(123)
+rp.top <- random_portfolios(portf, 1000)
+
+opt <- optimize.portfolio(proxy.ret, portf, 
+                          optimize_method="random", 
+                          trace=TRUE, rp=rp.top)
+
+opt.mult <- optimize.portfolio(R, mult.portf,
+                               optimize_method="random", 
+                               trace=TRUE, rp=rp.top)
+
+all.equal(extractObjectiveMeasures(opt), extractObjectiveMeasures(opt.mult))
+all.equal(extractWeights(opt), extractWeights(opt.mult))

Deleted: pkg/PortfolioAnalytics/sandbox/multi_layer_script.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/multi_layer_script.R	2014-06-17 20:13:06 UTC (rev 3424)
+++ pkg/PortfolioAnalytics/sandbox/multi_layer_script.R	2014-06-18 17:26:42 UTC (rev 3425)
@@ -1,91 +0,0 @@
-library(PortfolioAnalytics)
-
-# Script for multilayer optimization
-
-# We need to support the different arguments/parameters for 
-# optimize.portfolio.rebalancing for each sub-portfolio
-# * R
-# * optimize_method
-# * search_size
-# * trace
-# * ...
-# * rp
-# * rebalance_on
-# * training_period
-# * trailings_period
-
-# The returns need to have the same periodicity
-
-# Each sub-portfolio may have a different rebalancing frequency, training, and
-# trailing parameters, as well as optimization method
-
-data(edhec)
-R <- edhec[, 1:10]
-funds <- colnames(R)
-
-# The first sub-portfolio, portf1, will contain assets 1:5 of the edhec
-# with an objective to minimize standard deviation.
-portf1 <- portfolio.spec(assets=funds[1:5])
-portf1 <- add.constraint(portfolio=portf1, type="full_investment")
-portf1 <- add.constraint(portfolio=portf1, type="long_only")
-portf1 <- add.objective(portfolio=portf1, type="risk", name="StdDev")
-
-# The second sub-portfolio, portf2, will contain assets 6:10 of the edhec
-# with an objective to minimize expected shortfall.
-portf2 <- portfolio.spec(assets=funds[6:10])
-portf2 <- add.constraint(portfolio=portf2, type="full_investment")
-portf2 <- add.constraint(portfolio=portf2, type="long_only")
-portf2 <- add.objective(portfolio=portf2, type="risk", name="ES",
-                            arguments=list(p=0.9))
-
-# Run optimize.portfolio.rebalancing for each sub-portfolio to get proxy 
-# returns 
-proxy1 <- optimize.portfolio.rebalancing(R[,1:5], 
-                                         portf1, 
-                                         optimize_method="ROI", 
-                                         rebalance_on="quarters", 
-                                         training_period=60)
-proxy1
-proxy1.ret <- summary(proxy1)$portfolio_returns
-
-proxy2 <- optimize.portfolio.rebalancing(R[,6:10], 
-                                         portf2, 
-                                         optimize_method="ROI", 
-                                         rebalance_on="quarters", 
-                                         training_period=48)
-proxy2
-proxy2.ret <- summary(proxy2)$portfolio_returns
-
-# A different training period was used so the returns do not exactly align
-ret <- cbind(proxy1.ret, proxy2.ret)
-head(ret, 14)
-
-# Get rid of the NAs
-ret <- na.omit(ret)
-colnames(ret) <- c("proxy1", "proxy2")
-head(ret)
-
-# Construct portfolio for the top level optimization of the proxy portfolios
-portf <- portfolio.spec(assets=colnames(ret))
-portf <- add.constraint(portfolio=portf, type="weight_sum", min_sum=0.99, max_sum=1.01)
-portf <- add.constraint(portfolio=portf, type="long_only")
-portf <- add.objective(portfolio=portf, type="risk", name="ES",
-                        arguments=list(p=0.9))
-portf <- add.objective(portfolio=portf, type="risk_budget", name="ES",
-                       arguments=list(p=0.9), min_concentration=TRUE)
-
-opt <- optimize.portfolio(ret, portf, 
-                          optimize_method="random", 
-                          search_size=4000, 
-                          trace=TRUE)
-opt
-
-opt.bt <- optimize.portfolio.rebalancing(ret, portf, 
-                                      optimize_method="random", 
-                                      search_size=4000, 
-                                      trace=TRUE,
-                                      rebalance_on="months",
-                                      training_period=48)
-opt.bt
-opt.ret <- summary(opt.bt)$portfolio_returns
-charts.PerformanceSummary(cbind(opt.ret, ret))



More information about the Returnanalytics-commits mailing list