[Returnanalytics-commits] r3341 - pkg/PerformanceAnalytics/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 26 01:28:23 CET 2014


Author: braverock
Date: 2014-02-26 01:28:22 +0100 (Wed, 26 Feb 2014)
New Revision: 3341

Modified:
   pkg/PerformanceAnalytics/R/Return.portfolio.R
Log:
- revert calculation for geometric=TRUE in Return.portfolio to r2170, we'll improve this with a broader refactoring


Modified: pkg/PerformanceAnalytics/R/Return.portfolio.R
===================================================================
--- pkg/PerformanceAnalytics/R/Return.portfolio.R	2014-02-26 00:20:00 UTC (rev 3340)
+++ pkg/PerformanceAnalytics/R/Return.portfolio.R	2014-02-26 00:28:22 UTC (rev 3341)
@@ -180,16 +180,17 @@
       for (col in colnames(weights)){
           wealthindex.weighted[,col]=weights[,col]*wealthindex.assets[,col]
       }
-      wealthindex=reclass(apply(wealthindex.weighted,1,sum), R)
-      result = wealthindex
-      result[2:length(result)] = result[2:length(result)] /
-        lag(result)[2:length(result)] - 1
-      #result[1] = result[1] - 1
-      result[1] = result[1] / sum(abs(weights[1,])) -1 #divide by the sum of the first weighting vector to account for possible leverage
-	  w = matrix(rep(NA), ncol(wealthindex.assets) * nrow(wealthindex.assets), ncol = ncol(wealthindex.assets), nrow = nrow(wealthindex.assets))
-      w[1, ] = weights
-      w[2:length(wealthindex), ] = (wealthindex.weighted / rep(wealthindex, ncol(wealthindex.weighted)))[1:(length(wealthindex) - 1), ]
-      weightedreturns = R[, colnames(weights)] * w
+      wealthindex=apply(wealthindex.weighted,1,sum)
+
+      # weighted cumulative returns
+      weightedcumcont=t(apply (wealthindex.assets,1, function(x,weights){ as.vector((x-1)* weights)},weights=weights))
+      weightedreturns=diff(rbind(0,weightedcumcont)) # compound returns
+      colnames(weightedreturns)=colnames(wealthindex.assets)
+      if (!wealth.index){
+        result=as.matrix(apply(weightedreturns,1,sum),ncol=1)
+      } else {
+        wealthindex=matrix(cumprod(1 + as.matrix(apply(weightedreturns,1, sum), ncol = 1)),ncol=1)
+      }
     }
 
 



More information about the Returnanalytics-commits mailing list