[Returnanalytics-commits] r2585 - in pkg/PortfolioAnalytics: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 17 05:31:11 CEST 2013


Author: rossbennett34
Date: 2013-07-17 05:31:11 +0200 (Wed, 17 Jul 2013)
New Revision: 2585

Modified:
   pkg/PortfolioAnalytics/R/constraints.R
   pkg/PortfolioAnalytics/man/group_constraint.Rd
Log:
adding support for position limits per group to group_constraint constructor

Modified: pkg/PortfolioAnalytics/R/constraints.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraints.R	2013-07-17 03:15:21 UTC (rev 2584)
+++ pkg/PortfolioAnalytics/R/constraints.R	2013-07-17 03:31:11 UTC (rev 2585)
@@ -382,6 +382,7 @@
 #' @param group_labels character vector to label the groups (e.g. size, asset class, style, etc.)
 #' @param group_min numeric or vector specifying minimum weight group constraints
 #' @param group_max numeric or vector specifying minimum weight group constraints
+#' @param group_pos vector specifying the number of non-zero weights per group
 #' @param enabled TRUE/FALSE
 #' @param \dots any other passthru parameters to specify group constraints
 #' @author Ross Bennett
@@ -399,7 +400,7 @@
 #'                         group_min=c(0.15, 0.25),
 #'                         group_max=c(0.65, 0.55))
 #' @export
-group_constraint <- function(type, assets, groups, group_labels=NULL, group_min, group_max, enabled=TRUE, ...) {
+group_constraint <- function(type, assets, groups, group_labels=NULL, group_min, group_max, group_pos=NULL, enabled=TRUE, ...) {
   nassets <- length(assets)
   ngroups <- length(groups)
   
@@ -428,11 +429,24 @@
   
   if(length(group_labels) != length(groups)) stop("length of group_labels must be equal to the length of groups")
   
+  # Construct group_pos vector
+  if(!is.null(group_pos)){
+    # Check the length of the group_pos vector
+    if(length(group_poss) != length(groups)) stop("length of group_pos must be equal to the length of groups")
+    # Check for negative values in group_pos
+    if(any(group_pos < 0)) stop("all elements of group_pos must be positive")
+    # Elements of group_pos cannot be greater than groups
+    if(any(group_pos > groups)){
+      group_pos <- pmin(group_pos, groups)
+    }
+  }
+  
   Constraint <- constraint_v2(type, enabled=enabled, constrclass="group_constraint", ...)
   Constraint$groups <- groups
   Constraint$group_labels <- group_labels
   Constraint$cLO <- group_min
   Constraint$cUP <- group_max
+  Constraint$group_pos <- group_pos
   return(Constraint)
 }
 

Modified: pkg/PortfolioAnalytics/man/group_constraint.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/group_constraint.Rd	2013-07-17 03:15:21 UTC (rev 2584)
+++ pkg/PortfolioAnalytics/man/group_constraint.Rd	2013-07-17 03:31:11 UTC (rev 2585)
@@ -4,7 +4,7 @@
 \usage{
   group_constraint(type, assets, groups,
     group_labels = NULL, group_min, group_max,
-    enabled = TRUE, ...)
+    group_pos = NULL, enabled = TRUE, ...)
 }
 \arguments{
   \item{type}{character type of the constraint}
@@ -23,6 +23,9 @@
   \item{group_max}{numeric or vector specifying minimum
   weight group constraints}
 
+  \item{group_pos}{vector specifying the number of non-zero
+  weights per group}
+
   \item{enabled}{TRUE/FALSE}
 
   \item{\dots}{any other passthru parameters to specify



More information about the Returnanalytics-commits mailing list