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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 13 20:37:54 CET 2013


Author: rossbennett34
Date: 2013-11-13 20:37:54 +0100 (Wed, 13 Nov 2013)
New Revision: 3251

Modified:
   pkg/PortfolioAnalytics/R/constraint_fn_map.R
Log:
Modified rp_transform in inner while loops to only subset once per loop and use sample.int

Modified: pkg/PortfolioAnalytics/R/constraint_fn_map.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-11-13 19:06:02 UTC (rev 3250)
+++ pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-11-13 19:37:54 UTC (rev 3251)
@@ -404,13 +404,17 @@
       # randomly permute and increase a random portfolio element
       cur_index <- random_index[i]
       cur_val <- tmp_w[cur_index]
-      if (length(weight_seq[(weight_seq >= cur_val) & (weight_seq <= max[cur_index])]) > 1) {
+      tmp_seq <- weight_seq[(weight_seq >= cur_val) & (weight_seq <= max[cur_index])]
+      n_tmp_seq <- length(tmp_seq)
+      if (n_tmp_seq > 1) {
         # randomly sample an element from weight_seq that is greater than cur_val and less than max
-        tmp_w[cur_index] <- sample(weight_seq[(weight_seq >= cur_val) & (weight_seq <= max[cur_index])], 1)
+        # tmp_w[cur_index] <- sample(weight_seq[(weight_seq >= cur_val) & (weight_seq <= max[cur_index])], 1)
+        tmp_w[cur_index] <- tmp_seq[sample.int(n=n_tmp_seq, size=1L, replace=FALSE, prob=NULL)]
         # print(paste("new val:",tmp_w[cur_index]))
       } else {
-        if (length(weight_seq[(weight_seq >= cur_val) & (weight_seq <= max[cur_index])]) == 1) {
-          tmp_w[cur_index] <- weight_seq[(weight_seq >= cur_val) & (weight_seq <= max[cur_index])]
+        if (n_tmp_seq == 1) {
+          # tmp_w[cur_index] <- weight_seq[(weight_seq >= cur_val) & (weight_seq <= max[cur_index])]
+          tmp_w[cur_index] <- tmp_seq
         }
       }
       i=i+1 # increment our counter
@@ -423,12 +427,16 @@
       # randomly permute and decrease a random portfolio element
       cur_index <- random_index[i]
       cur_val <- tmp_w[cur_index]
-      if (length(weight_seq[(weight_seq <= cur_val) & (weight_seq >= tmp_min[cur_index])] ) > 1) {
+      tmp_seq <- weight_seq[(weight_seq <= cur_val) & (weight_seq >= tmp_min[cur_index])]
+      n_tmp_seq <- length(tmp_seq)
+      if (n_tmp_seq > 1) {
         # randomly sample an element from weight_seq that is less than cur_val and greater than tmp_min
-        tmp_w[cur_index] <- sample(weight_seq[(weight_seq <= cur_val) & (weight_seq >= tmp_min[cur_index])] , 1)
+        # tmp_w[cur_index] <- sample(weight_seq[(weight_seq <= cur_val) & (weight_seq >= tmp_min[cur_index])] , 1)
+        tmp_w[cur_index] <- tmp_seq[sample.int(n=n_tmp_seq, size=1L, replace=FALSE, prob=NULL)]
       } else {
-        if (length(weight_seq[(weight_seq <= cur_val) & (weight_seq >= tmp_min[cur_index])] ) == 1) {
-          tmp_w[cur_index] <- weight_seq[(weight_seq <= cur_val) & (weight_seq >= tmp_min[cur_index])]
+        if (n_tmp_seq == 1) {
+          # tmp_w[cur_index] <- weight_seq[(weight_seq <= cur_val) & (weight_seq >= tmp_min[cur_index])]
+          tmp_w[cur_index] <- tmp_seq
         }
       }
       i=i+1 # increment our counter



More information about the Returnanalytics-commits mailing list