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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 17 05:15:22 CEST 2013


Author: rossbennett34
Date: 2013-07-17 05:15:21 +0200 (Wed, 17 Jul 2013)
New Revision: 2584

Modified:
   pkg/PortfolioAnalytics/R/constraint_fn_map.R
   pkg/PortfolioAnalytics/man/group_fail.Rd
Log:
modifying group_fail to check for number of non-zero weights per group

Modified: pkg/PortfolioAnalytics/R/constraint_fn_map.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-17 01:37:21 UTC (rev 2583)
+++ pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-17 03:15:21 UTC (rev 2584)
@@ -465,13 +465,19 @@
 #' @param groups vector specifying the groups of the assets
 #' @param cLO numeric or vector specifying minimum weight group constraints
 #' @param cUP numeric or vector specifying minimum weight group constraints
+#' @param group_pos vector specifying the number of non-zero weights per group
 #' @return logical vector: TRUE if group constraints are violated for a given group
 #' @author Ross Bennett
 #' @export
-group_fail <- function(weights, groups, cLO, cUP){
+group_fail <- function(weights, groups, cLO, cUP, group_pos=NULL){
   # return FALSE if groups, cLO, or cUP is NULL
   if(is.null(groups) | is.null(cLO) | is.null(cUP)) return(FALSE)
   
+  # group_pos sets a limit on the number of non-zero weights by group
+  # Set equal to groups if NULL
+  if(is.null(group_pos)) group_pos <- groups
+  tolerance <- .Machine$double.eps^0.5
+  
   n.groups <- length(groups)
   group_fail <- vector(mode="logical", length=n.groups)
   k <- 1
@@ -481,8 +487,9 @@
     tmp.w <- weights[k:(l+j)]
     grp.min <- cLO[i]
     grp.max <- cUP[i]
+    grp.pos <- group_pos[i]
     # return TRUE if grp.min or grp.max is violated
-    group_fail[i] <- ( sum(tmp.w) < grp.min | sum(tmp.w) > grp.max )
+    group_fail[i] <- ( sum(tmp.w) < grp.min | sum(tmp.w) > grp.max (sum(abs(tmp.w) > tolerance) > grp.pos))
     k <- k + j
     l <- k - 1
   }

Modified: pkg/PortfolioAnalytics/man/group_fail.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/group_fail.Rd	2013-07-17 01:37:21 UTC (rev 2583)
+++ pkg/PortfolioAnalytics/man/group_fail.Rd	2013-07-17 03:15:21 UTC (rev 2584)
@@ -2,7 +2,7 @@
 \alias{group_fail}
 \title{Test if group constraints have been violated}
 \usage{
-  group_fail(weights, groups, cLO, cUP)
+  group_fail(weights, groups, cLO, cUP, group_pos = NULL)
 }
 \arguments{
   \item{weights}{weights vector to test}
@@ -14,6 +14,9 @@
 
   \item{cUP}{numeric or vector specifying minimum weight
   group constraints}
+
+  \item{group_pos}{vector specifying the number of non-zero
+  weights per group}
 }
 \value{
   logical vector: TRUE if group constraints are violated



More information about the Returnanalytics-commits mailing list