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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 12 04:19:01 CEST 2013


Author: rossbennett34
Date: 2013-07-12 04:19:01 +0200 (Fri, 12 Jul 2013)
New Revision: 2551

Modified:
   pkg/PortfolioAnalytics/R/constraint_fn_map.R
   pkg/PortfolioAnalytics/R/random_portfolios.R
Log:
added fn_map to randomize_portfolio_v2 and fixed bug in rp_transform

Modified: pkg/PortfolioAnalytics/R/constraint_fn_map.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-12 01:06:45 UTC (rev 2550)
+++ pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-07-12 02:19:01 UTC (rev 2551)
@@ -381,7 +381,7 @@
   # return w if all constraints are satisfied
   if((sum(w) >= min_sum & sum(w) <= max_sum) & 
        (all(w >= tmp_min) & all(w <= max)) & 
-       (all(!group_fail(weights, groups, cLO, cUP))) &
+       (all(!group_fail(w, groups, cLO, cUP))) &
        (sum(abs(w) > tolerance) <= max_pos)){
     return(w)
   }

Modified: pkg/PortfolioAnalytics/R/random_portfolios.R
===================================================================
--- pkg/PortfolioAnalytics/R/random_portfolios.R	2013-07-12 01:06:45 UTC (rev 2550)
+++ pkg/PortfolioAnalytics/R/random_portfolios.R	2013-07-12 02:19:01 UTC (rev 2551)
@@ -227,14 +227,15 @@
   weight_seq <- as.vector(weight_seq)
   max <- constraints$max
   min <- constraints$min
-  portfolio <- as.vector(seed)
-  rownames(portfolio) <- NULL
+  # initial portfolio
+  iportfolio <- as.vector(seed)
+  rownames(iportfolio) <- NULL
   
   # initialize our loop
   permutations <- 1
   
   # create a temporary portfolio so we don't return a non-feasible portfolio
-  tportfolio <- portfolio
+  tportfolio <- iportfolio
   # first randomly permute each element of the temporary portfolio
   random_index <- sample(1:length(tportfolio), length(tportfolio))
   for (i in 1:length(tportfolio)) {
@@ -286,14 +287,15 @@
     } # end decrease loop
   } # end final walk towards the edges
   
-  portfolio <- tportfolio
+  # final portfolio
+  fportfolio <- fn_map(weights=tportfolio, portfolio=portfolio, relax=FALSE)$weights
   
-  colnames(portfolio) <- colnames(seed)
-  if (sum(portfolio) <= min_sum | sum(tportfolio) >= max_sum){
-    portfolio <- seed
+  colnames(fportfolio) <- colnames(seed)
+  if (sum(fportfolio) <= min_sum | sum(fportfolio) >= max_sum){
+    fportfolio <- seed
     warning("Infeasible portfolio created, defaulting to seed, perhaps increase max_permutations.")
   }
-  if(isTRUE(all.equal(seed,portfolio))) {
+  if(isTRUE(all.equal(seed, fportfolio))) {
     if (sum(seed) >= min_sum & sum(seed) <= max_sum) {
       warning("Unable to generate a feasible portfolio different from seed, perhaps adjust your parameters.")
       return(seed)
@@ -302,7 +304,7 @@
       return(NULL)
     }
   }
-  return(portfolio)
+  return(fportfolio)
 }
 
 #' version 2 generate an arbitary number of constrained random portfolios



More information about the Returnanalytics-commits mailing list