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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu May 29 01:31:44 CEST 2014


Author: rossbennett34
Date: 2014-05-29 01:31:44 +0200 (Thu, 29 May 2014)
New Revision: 3402

Modified:
   pkg/PortfolioAnalytics/R/optimize.portfolio.R
   pkg/PortfolioAnalytics/R/random_portfolios.R
   pkg/PortfolioAnalytics/demo/regime_switching.R
Log:
Adding function to generate superset of random portfolios based on a regime.portfolios object for using in optimize.portfolio.rebalancing. Cleaning up regime switching demo.

Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R	2014-05-28 19:54:24 UTC (rev 3401)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R	2014-05-28 23:31:44 UTC (rev 3402)
@@ -1441,12 +1441,22 @@
   #store the call for later
   call <- match.call()
   if(optimize_method=="random"){
+    # get any rp related arguments passed in through dots
+    if(hasArg(rp_method)) rp_method=match.call(expand.dots=TRUE)$rp_method else rp_method="sample"
+    if(hasArg(eliminate)) eliminate=match.call(expand.dots=TRUE)$eliminate else eliminate=TRUE
+    if(hasArg(fev)) fev=match.call(expand.dots=TRUE)$fev else fev=0:5
+    
     #' call random_portfolios() with constraints and search_size to create matrix of portfolios
     if(is.null(rp))
-      rp<-random_portfolios(portfolio=portfolio, permutations=search_size)
+      if(inherits(portfolio, "regime.portfolios")){
+        rp <- rp.regime.portfolios(regime=portfolio, permutations=search_size, rp_method=rp_method, eliminate=eliminate, fev=fev)
+      } else {
+        rp <- random_portfolios(portfolio=portfolio, permutations=search_size, rp_method=rp_method, eliminate=eliminate, fev=fev)
+      }
   } else {
-    rp=NULL
-  }    
+    rp = NULL
+  }
+  print(dim(rp))
   
   if(is.null(training_period)) {if(nrow(R)<36) training_period=nrow(R) else training_period=36}
   if (is.null(trailing_periods)){

Modified: pkg/PortfolioAnalytics/R/random_portfolios.R
===================================================================
--- pkg/PortfolioAnalytics/R/random_portfolios.R	2014-05-28 19:54:24 UTC (rev 3401)
+++ pkg/PortfolioAnalytics/R/random_portfolios.R	2014-05-28 23:31:44 UTC (rev 3402)
@@ -608,6 +608,21 @@
   if(normalize) return(out) else return(rp)
 }
 
+# function to generate a set of random portfolios for each portfolio and return the superset
+# this is primarily for use in optimize.portfolio.rebalancing
+rp.regime.portfolios <- function(regime, permutations=100, rp_method="sample", eliminate=TRUE, ...){
+  if(!inherits(regime, "regime.portfolios")) stop("regime must be an object of class 'regime.portfolios'")
+  portf <- regime$portfolio.list
+  nportf <- length(portf)
+  rp.list <- vector("list", nportf)
+  for(i in 1:nportf){
+    rp.list[[i]] <- random_portfolios(portf[[i]], permutations=permutations, rp_method=rp_method, eliminate=eliminate, ...=...)
+  }
+  # rbind the list of matrices together and remove any duplicates
+  out <- unique(do.call("rbind", rp.list))
+  return(out)
+}
+
 # EXAMPLE: start_t<- Sys.time(); x=random_walk_portfolios(rep(1/5,5), generatesequence(min=0.01, max=0.30, by=0.01), max_permutations=500, permutations=5000, min_sum=.99, max_sum=1.01); end_t<-Sys.time(); end_t-start_t;
 # > nrow(unique(x))
 # [1] 4906

Modified: pkg/PortfolioAnalytics/demo/regime_switching.R
===================================================================
--- pkg/PortfolioAnalytics/demo/regime_switching.R	2014-05-28 19:54:24 UTC (rev 3401)
+++ pkg/PortfolioAnalytics/demo/regime_switching.R	2014-05-28 23:31:44 UTC (rev 3402)
@@ -48,16 +48,9 @@
 opt2
 opt2$regime
 
-# For optimize_method="random", which portfolio do we use and how do we 
-# generate random portfolios
-# - prompt the user to generate random portfolios?
-# - use the first portfolio?
-# - specify which portfolio?
-# This is important because the constraints may be very different
-# This only impacts optimize.portfolio.rebalancing because we do not know
-# the portfolio specification at the time we generate the random portfolios
+# Run optimization with rebalancing for regime switching
 opt.rebal <- optimize.portfolio.rebalancing(R, regime.port,
-                                            optimize_method="DEoptim", 
+                                            optimize_method="random", 
                                             rebalance_on="quarters", 
                                             training_period=130,
                                             search_size=2000, 



More information about the Returnanalytics-commits mailing list