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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 22 02:26:32 CEST 2013


Author: rossbennett34
Date: 2013-06-22 02:26:31 +0200 (Sat, 22 Jun 2013)
New Revision: 2397

Added:
   pkg/PortfolioAnalytics/R/objectiveFUN.R
Log:
adding a turnover objective to objectiveFUN.R. Note objectiveFUN.R is a new file for functions used as objectives.

Added: pkg/PortfolioAnalytics/R/objectiveFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/objectiveFUN.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/R/objectiveFUN.R	2013-06-22 00:26:31 UTC (rev 2397)
@@ -0,0 +1,19 @@
+
+#' Calculates turnover given two vectors of weights.
+#' This is used as an objective function and is called when the user adds an objective of type turnover with \code{\link{add.objective}}
+#' @param weights vector of weights from optimization
+#' @param wts.init vector of initial weights used to calculate turnover from
+#' @author Ross Bennett
+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")
+  
+  # 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)
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list