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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Sep 16 23:10:19 CEST 2013


Author: rossbennett34
Date: 2013-09-16 23:10:19 +0200 (Mon, 16 Sep 2013)
New Revision: 3123

Modified:
   pkg/PortfolioAnalytics/R/constraint_fn_map.R
   pkg/PortfolioAnalytics/R/constraintsFUN.R
Log:
Removing constrained_group_tmp and txfrm_* functions. I originally wrote these to be used in a mapping function, but these are not being used.

Modified: pkg/PortfolioAnalytics/R/constraint_fn_map.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-09-16 20:10:15 UTC (rev 3122)
+++ pkg/PortfolioAnalytics/R/constraint_fn_map.R	2013-09-16 21:10:19 UTC (rev 3123)
@@ -247,114 +247,8 @@
               max_pos_short=tmp_max_pos_short))
 }
 
-#' Transform weights that violate min or max box constraints
-#' 
-#' This is a helper function called inside constraint_fnMap to transform the weights vector to satisfy box constraints.
-#' 
-#' @param weights vector of weights
-#' @param min vector of minimum asset weights from box constraints
-#' @param max vector of maximum asset weights from box constraints
-#' @author Ross Bennett
-#' @export
-txfrm_box_constraint <- function(weights, min, max) {
-  # 1. Check if any elements of the weights vector violate min or max
-  # 2. If min or max is violated, then set those weights equal to their respective min or max values
-  # The length of the weights vector must be equal to the length of min and max vectors so that an element-by-element comparison is done
-  if(any(weights < min) | any(weights > max)){
-    # get the index of elements in the weights vector that violate min
-    idx.min <- which(weights < min)
-    # set those elements in the weights vector equal to their respective min
-    weights[idx.min] = min[idx.min]
-    # print(weights)
-    # get the index of elements in the weights vector that violate max
-    idx.max <- which(weights > max)
-    # set those elements in the weights vector equal to their respective max
-    weights[idx.max] = max[idx.max]
-    # print(weights)
-    # The transformation will likely change the sum of weights and violate min_sum or max_sum
-    # Should we normalize here by transforming the entire weights vector?
-    # Normalizing by transforming the entire weights may violate min and max, but will get us *close*
-    # if(sum(weights) < min_sum) weights <- min_sum / sum(weights) * weights
-    # if(sum(weights) > max_sum) weights <- max_sum / sum(weights) * weights
-  }
-  return(weights)
-}
 
-#' Transform weights that violate group constraints
-#' 
-#' This is a helper function called inside constraint_fnMap to transform the weights vector to satisfy group constraints.
-#' 
-#' @param weights vector of weights
-#' @param groups vector of groups
-#' @param cLO vector of minimum group weights from group constraints
-#' @param cUP vector of maximum group weights from group constraints
-#' @author Ross Bennett
-#' @export
-txfrm_group_constraint <- function(weights, groups, cLO, cUP){
-  n.groups <- length(groups)
-  k <- 1
-  l <- 0
-  for(i in 1:n.groups){
-    j <- groups[i]
-    tmp.w <- weights[k:(l+j)]
-    # normalize weights for a given group that sum to less than specified group min
-    grp.min <- cLO[i]
-    if(sum(tmp.w) < grp.min) {
-      weights[k:(l+j)] <- (grp.min / sum(tmp.w)) * tmp.w
-    }
-    # normalize weights for a given group that sum to greater than specified group max
-    grp.max <- cUP[i]
-    if(sum(tmp.w) > grp.max) {
-      weights[k:(l+j)] <- (grp.max / sum(tmp.w)) * tmp.w
-    }
-    k <- k + j
-    l <- k - 1
-  }
-  # Normalizing the weights inside the groups changes the sum of the weights.
-  # Should normalizing the sum of weights take place here or somewhere else?
-  # Re-normalizing the weights will get us *close* to satisfying the group constraints.
-  # Maybe then add a penalty in constrained objective for violation of group constraints?
-  return(weights)
-}
 
-#' Transform weights that violate weight_sum constraints
-#' 
-#' This is a helper function called inside constraint_fnMap to transform the weights vector to satisfy weight_sum constraints.
-#' 
-#' @param weights vector of weights
-#' @param min_sum minimum sum of asset weights
-#' @param max_sum maximum sum of asset weights
-#' @author Ross Bennett
-#' @export
-txfrm_weight_sum_constraint <- function(weights, min_sum, max_sum){
-  # normalize to max_sum
-  if(sum(weights) > max_sum) { weights <- (max_sum / sum(weights)) * weights }
-  # normalize to min_sum
-  if(sum(weights) < min_sum) { weights <- (min_sum / sum(weights)) * weights }
-  return(weights)
-}
-
-#' Transform weights for position_limit constraints
-#' 
-#' This is a helper function called inside constraint_fnMap to transform the weights vector to satisfy position_limit constraints.
-#' This function sets the minimum nassets-max_pos assets equal to 0 such that the max_pos number of assets will have non-zero weights.
-#' 
-#' @param weights vector of weights
-#' @param max_pos maximum position of assets with non_zero weights
-#' @param nassets number of assets
-#' @param tolerance tolerance for non-zero weights
-#' @author Ross Bennett
-#' @export
-txfrm_position_limit_constraint <- function(weights, max_pos, nassets, tolerance=.Machine$double.eps^0.5){
-  # account for weights that are very small (less than .Machine$double.eps^0.5) and are basically zero
-  # check if max_pos is violated
-  if(sum(abs(weights) > tolerance) > max_pos){
-    # set the minimum nassets-max_pos weights equal to 0
-    weights[head(order(weights), nassets - max_pos)] <- 0
-  }
-  return(weights)
-}
-
 #' Transform a weights vector to satisfy leverage, box, group, and position_limit constraints using logic from \code{randomize_portfolio}
 #' 
 #' This function uses a block of code from \code{\link{randomize_portfolio}} 

Modified: pkg/PortfolioAnalytics/R/constraintsFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraintsFUN.R	2013-09-16 20:10:15 UTC (rev 3122)
+++ pkg/PortfolioAnalytics/R/constraintsFUN.R	2013-09-16 21:10:19 UTC (rev 3123)
@@ -1,67 +1,3 @@
-#' Generic function to impose group constraints on a vector of weights
-#' 
-#' This function gets group subsets of the weights vector and checks if the sum
-#' of the weights in that group violates the minimum or maximum value. If the 
-#' sum of weights in a given group violates its maximum or minimum value, the 
-#' group of weights is normalized to be equal to the minimum or maximum value.
-#' This group normalization causes the sum of weights to change. The weights
-#' vector is then normalized so that the min_sum and max_sum constraints are 
-#' satisfied. This "re-normalization" of the weights vector may then cause the
-#' group constraints to not be satisfied.
-#' 
-#' Group constraints are implemented in ROI solvers, but this function could
-#' be used in constrained_objective for random portfolios, DEoptim, pso, or 
-#' gensa solvers.
-#' 
-#' @param groups vector to group assets
-#' @param cLO vector of group weight minimums
-#' @param cUP vector of group weight maximums
-#' @param weights vector of weights
-#' @param min_sum minimum sum of weights
-#' @param max_sum maximum sum of weights
-#' @param normalize TRUE/FALSE to normalize the weights vector to satisfy the min_sum and max_sum constraints
-#' 
-#' @author Ross Bennett
-#' @export
-constrained_group_tmp <- function(groups, cLO, cUP, weights, min_sum, max_sum, normalize=TRUE){
-  # Modify the args later to accept a portfolio or constraint object
-  n.groups <- length(groups)
-  
-  k <- 1
-  l <- 0
-  for(i in 1:n.groups){
-    j <- groups[i]
-    tmp.w <- weights[k:(l+j)]
-    # normalize weights for a given group that sum to less than specified group min
-    grp.min <- cLO[i]
-    if(sum(tmp.w) < grp.min) {
-      weights[k:(l+j)] <- (grp.min / sum(tmp.w)) * tmp.w
-    }
-    # normalize weights for a given group that sum to greater than specified group max
-    grp.max <- cUP[i]
-    if(sum(tmp.w) > grp.max) {
-      weights[k:(l+j)] <- (grp.max / sum(tmp.w)) * tmp.w
-    }
-    # cat(sum(tmp.w), "\t", cLO[i], "\n")
-    # cat(k, " ", l+j, "\n")
-    k <- k + j
-    l <- k - 1
-  }
-  # Normalizing the weights inside the groups changes the sum of the weights.
-  # Should normalizing the sum of weights take place here or somewhere else?
-  
-  if(normalize){
-    # max_sum and min_sum normalization borrowed from constrained_objective
-    # Normalize to max_sum
-    if(sum(weights) > max_sum) { weights <- (max_sum / sum(weights)) * weights }
-    # Normalize to min_sum
-    if(sum(weights) < min_sum) { weights <- (min_sum / sum(weights)) * weights }
-  }
-  # "Re-normalizing" the weights causes some of the group constraints to
-  # be violated. Can this be addressed later with a penalty term for violating
-  # the group constraints? Or another way?
-  return(weights)
-}
 
 #' Function to compute diversification as a constraint
 #' 



More information about the Returnanalytics-commits mailing list