[Returnanalytics-commits] r3394 - pkg/PortfolioAnalytics/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue May 27 06:51:58 CEST 2014


Author: rossbennett34
Date: 2014-05-27 06:51:58 +0200 (Tue, 27 May 2014)
New Revision: 3394

Modified:
   pkg/PortfolioAnalytics/R/moment.functions.R
   pkg/PortfolioAnalytics/R/optimize.portfolio.R
Log:
Adding checks so that higher moments are not calculated when solving ES problem with LP solver

Modified: pkg/PortfolioAnalytics/R/moment.functions.R
===================================================================
--- pkg/PortfolioAnalytics/R/moment.functions.R	2014-05-26 17:08:48 UTC (rev 3393)
+++ pkg/PortfolioAnalytics/R/moment.functions.R	2014-05-27 04:51:58 UTC (rev 3394)
@@ -220,10 +220,15 @@
              ETL=,
              mETL=,
              ES = {
-               if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
-               if(is.null(momentargs$sigma)) momentargs$sigma = cov(R)
-               if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics:::M3.MM(R)
-               if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics:::M4.MM(R)
+               # We don't want to calculate these moments if we have an ES 
+               # objective and are solving as an LP problem.
+               if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
+               if(!ROI){
+                 if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
+                 if(is.null(momentargs$sigma)) momentargs$sigma = cov(R)
+                 if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics:::M3.MM(R)
+                 if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics:::M4.MM(R)
+               }
              }
       ) # end switch on objectives    
     }    

Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R	2014-05-26 17:08:48 UTC (rev 3393)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R	2014-05-27 04:51:58 UTC (rev 3394)
@@ -532,9 +532,22 @@
   if(!is.function(momentFUN)){
     momentFUN <- match.fun(momentFUN)
   }
+  
+  # **
+  # When an ES/ETL/CVaR problem is being solved by a linear solver, the higher
+  # moments do not need to be calculated. The moments are very compute 
+  # intensive and slow down the optimization problem.
+  
   # match the args for momentFUN
   .formals <- formals(momentFUN)
   .formals <- modify.args(formals=.formals, arglist=NULL, ..., dots=FALSE)
+  # ** pass ROI=TRUE to set.portfolio.moments so the moments are not calculated
+  if(optimize_method %in% c("ROI", "quadprog", "glpk", "symphony", "ipop", "cplex")){
+    obj_names <- unlist(lapply(portfolio$objectives, function(x) x$name))
+    if(any(obj_names %in% c("CVaR", "ES", "ETL"))){
+      .formals <- modify.args(formals=.formals, arglist=list(ROI=TRUE), dots=TRUE)
+    }
+  }
   if("R" %in% names(.formals)) .formals <- modify.args(formals=.formals, arglist=NULL, R=R, dots=FALSE)
   if("portfolio" %in% names(.formals)) .formals <- modify.args(formals=.formals, arglist=NULL, portfolio=portfolio, dots=FALSE)
   .formals$... <- NULL



More information about the Returnanalytics-commits mailing list