[Returnanalytics-commits] r2527 - in pkg/PortfolioAnalytics: R sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 9 04:53:42 CEST 2013


Author: rossbennett34
Date: 2013-07-09 04:53:42 +0200 (Tue, 09 Jul 2013)
New Revision: 2527

Modified:
   pkg/PortfolioAnalytics/R/constraint_fn_map.R
   pkg/PortfolioAnalytics/sandbox/testing_fn_map.R
Log:
adding functionality to relax position limit constraints in fn_map

Modified: pkg/PortfolioAnalytics/R/constraint_fn_map.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-09 00:35:39 UTC (rev 2526)
+++ pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-09 02:53:42 UTC (rev 2527)
@@ -71,6 +71,7 @@
   tmp_max <- max
   tmp_cLO <- cLO
   tmp_cUP <- cUP
+  tmp_max_pos <- max_pos
   
   # step 2: check that the vector of weights satisfies the constraints, 
   # transform weights if constraint is violated
@@ -85,16 +86,9 @@
       if(inherits(tmp_weights, "try-error")){
         # Default to initial weights
         tmp_weights <- weights
-        # Other actions to consider
-        # relax constraints (rp_transform checks all constraints together so we may not know which constraint is too restrictive)
-        # different normalization method
-        # return initial weights and penalize?
-      }
-      # print("leverage constraint violated, transforming weights.")
-      # print(tmp_weights)
-      # tmp_weights <- txfrm_weight_sum_constraint(tmp_weights, min_sum, max_sum)
-    }
-  }
+      } # end try-error recovery
+    } # end check for leverage constraint violation
+  } # end check for NULL arguments
   
   # check box constraints
   if(!is.null(tmp_min) & !is.null(tmp_max)){
@@ -178,23 +172,27 @@
   } # end check for NULL arguments
   
   # check position_limit constraints
-  if(!is.null(max_pos)){
-    if(!(sum(abs(tmp_weights) > tolerance) <= max_pos)){
+  if(!is.null(tmp_max_pos)){
+    if(!(sum(abs(tmp_weights) > tolerance) <= tmp_max_pos)){
       # Try to transform only considering leverage, box, group, and position_limit constraints
-      tmp_weights <- try(rp_transform(tmp_weights, min_sum, max_sum, tmp_min, tmp_max, groups, tmp_cLO, tmp_cUP, max_pos, 500), silent=TRUE)
+      tmp_weights <- try(rp_transform(tmp_weights, min_sum, max_sum, tmp_min, tmp_max, groups, tmp_cLO, tmp_cUP, tmp_max_pos, 500), silent=TRUE)
       if(inherits(tmp_weights, "try-error")){
         # Default to initial weights
         tmp_weights <- weights
-        # Other actions to consider
-        # relax constraints (rp_transform checks all constraints together so we may not know which constraint is too restrictive)
-        # different normalization method
-        # return initial weights and penalize?
-      }
-      # print("position_limit constraint violated, transforming weights.")
-      # print(tmp_weights)
-      # tmp_weights <- txfrm_position_limit_constraint(tmp_weights, max_pos, nassets)
-    }
-  }
+        if(relax){
+          i <- 1
+          while((sum(abs(tmp_weights) > tolerance) > tmp_max_pos) & (tmp_max_pos <= nassets) & (i <= 5)){
+            # increment tmp_max_pos by 1
+            tmp_max_pos <- tmp_max_pos + 1
+            # Now try the transformation again
+            tmp_weights <- try(rp_transform(tmp_weights, min_sum, max_sum, tmp_min, tmp_max, groups, tmp_cLO, tmp_cUP, tmp_max_pos, 500), silent=TRUE)
+            if(inherits(tmp_weights, "try-error")) tmp_weights <- weights
+            i <- i + 1
+          }
+        } # end if(relax) statement
+      } # end try-error recovery
+    } # end check for position limit constraint violation
+  } # end check for NULL arguments
   
   # check diversification constraint
   if(!is.null(div_target)){
@@ -218,7 +216,13 @@
     }
   }
   names(tmp_weights) <- names(weights)
-  return(list(weights=tmp_weights, min=tmp_min, max=tmp_max, cLO=tmp_cLO, cUP=tmp_cUP, out=out))
+  return(list(weights=tmp_weights, 
+              min=tmp_min, 
+              max=tmp_max, 
+              cLO=tmp_cLO, 
+              cUP=tmp_cUP, 
+              max_pos=tmp_max_pos, 
+              out=out))
 }
 
 #' Transform weights that violate min or max box constraints

Modified: pkg/PortfolioAnalytics/sandbox/testing_fn_map.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_fn_map.R	2013-07-09 00:35:39 UTC (rev 2526)
+++ pkg/PortfolioAnalytics/sandbox/testing_fn_map.R	2013-07-09 02:53:42 UTC (rev 2527)
@@ -75,3 +75,19 @@
 # group constraints needs to be relaxed
 # note how cLO and cUP have been changed
 fn_map(weights, pspec, TRUE)
+
+##### relaxing position limits constraints #####
+pspec <- portfolio.spec(assets=funds)
+
+pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)
+pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.4, enabled=T)
+# Make position limit constraint too restrictive
+pspec <- add.constraint(portfolio=pspec, type="position_limit", max_pos=2, enabled=T)
+
+# weights satisfy leverage and box constraints, but not group
+weights <- c(0.4, 0.05, 0.15, 0.4)
+
+# position limit constraint needs to be relaxed
+# note how max_pos has been increased to 3
+fn_map(weights, pspec, TRUE)
+



More information about the Returnanalytics-commits mailing list