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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 16 22:10:08 CET 2013


Author: rossbennett34
Date: 2013-11-16 22:10:07 +0100 (Sat, 16 Nov 2013)
New Revision: 3265

Modified:
   pkg/PortfolioAnalytics/R/constrained_objective.R
   pkg/PortfolioAnalytics/R/objectiveFUN.R
Log:
Calculating mean return more efficiently using weights and moments

Modified: pkg/PortfolioAnalytics/R/constrained_objective.R
===================================================================
--- pkg/PortfolioAnalytics/R/constrained_objective.R	2013-11-15 20:28:47 UTC (rev 3264)
+++ pkg/PortfolioAnalytics/R/constrained_objective.R	2013-11-16 21:10:07 UTC (rev 3265)
@@ -474,7 +474,7 @@
   # penalize weights that violate return target constraint
   if(!is.null(constraints$return_target)){
     return_target <- constraints$return_target
-    mean_return <- mean(R %*% w)
+    mean_return <- port.mean(weights=w, mu=moments$mu)
     mult <- 1
     out = out + penalty * mult * abs(mean_return - return_target)
   } # End return constraint penalty
@@ -559,8 +559,12 @@
         switch(objective$name,
                mean =,
                median = {
+                 fun = match.fun(port.mean)
+                 # would it be better to do crossprod(w, moments$mu)?
+                 # tmp_args$x <- ( R %*% w ) #do the multivariate mean/median with Kroneker product
+               },
+               median = {
                  fun = match.fun(objective$name)
-                 # would it be better to do crossprod(w, moments$mu)?
                  tmp_args$x <- ( R %*% w ) #do the multivariate mean/median with Kroneker product
                },
                sd =,
@@ -596,6 +600,7 @@
         if(is.function(fun)){
           .formals <- formals(fun)
           # Add the moments from the nargs object
+          # nargs contains the moments, these are being evaluated
           .formals <- modify.args(formals=.formals, arglist=nargs, dots=TRUE)
           # Add anything from tmp_args
           .formals <- modify.args(formals=.formals, arglist=tmp_args, dots=TRUE)

Modified: pkg/PortfolioAnalytics/R/objectiveFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/objectiveFUN.R	2013-11-15 20:28:47 UTC (rev 3264)
+++ pkg/PortfolioAnalytics/R/objectiveFUN.R	2013-11-16 21:10:07 UTC (rev 3265)
@@ -63,3 +63,9 @@
     return(hhi)
   }
 }
+
+# portfolio mean return
+port.mean <- function(weights, mu){
+  # t(weights) %*% moments$mu
+  as.numeric(crossprod(weights, mu))
+}



More information about the Returnanalytics-commits mailing list