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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 26 05:17:30 CEST 2013


Author: rossbennett34
Date: 2013-06-26 05:17:29 +0200 (Wed, 26 Jun 2013)
New Revision: 2439

Modified:
   pkg/PortfolioAnalytics/R/objectiveFUN.R
Log:
fixing turnover function

Modified: pkg/PortfolioAnalytics/R/objectiveFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/objectiveFUN.R	2013-06-26 03:04:27 UTC (rev 2438)
+++ pkg/PortfolioAnalytics/R/objectiveFUN.R	2013-06-26 03:17:29 UTC (rev 2439)
@@ -7,13 +7,15 @@
 turnover <- function(weights, wts.init=NULL) {
   # turnover function from https://r-forge.r-project.org/scm/viewvc.php/pkg/PortfolioAnalytics/sandbox/script.workshop2012.R?view=markup&root=returnanalytics
   
-  # Check that weights and wts.init are the same length
-  if(length(weights) != length(wts.init)) stop("weights and wts.init are not the same length")
+  N <- length(weights)
   
   # If wts.init is not given, then assume a vector of equal weights
   if(is.null(wts.init)) {
-    N <- length(weights)
     wts.init <- rep(1/N, N)
   }
-  return(sum(abs(wts.init-weights))/N)
+  
+  # Check that weights and wts.init are the same length
+  if(length(weights) != length(wts.init)) stop("weights and wts.init are not the same length")
+  
+  return(sum(abs(wts.init - weights)) / N)
 }
\ No newline at end of file



More information about the Returnanalytics-commits mailing list