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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 13 22:55:28 CEST 2013


Author: rossbennett34
Date: 2013-09-13 22:55:28 +0200 (Fri, 13 Sep 2013)
New Revision: 3097

Added:
   pkg/PortfolioAnalytics/man/check_constraints.Rd
   pkg/PortfolioAnalytics/man/rp_grid.Rd
   pkg/PortfolioAnalytics/man/rp_sample.Rd
Modified:
   pkg/PortfolioAnalytics/NAMESPACE
   pkg/PortfolioAnalytics/R/constraints.R
   pkg/PortfolioAnalytics/R/random_portfolios.R
   pkg/PortfolioAnalytics/man/chart.RiskReward.Rd
   pkg/PortfolioAnalytics/man/random_portfolios.Rd
   pkg/PortfolioAnalytics/man/rp_simplex.Rd
Log:
Adding option to eliminate portfolios that do not satisfy constraints. Adding helper function to check if constraints are satisfied. Updating documentation for rp functions.

Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE	2013-09-13 18:34:12 UTC (rev 3096)
+++ pkg/PortfolioAnalytics/NAMESPACE	2013-09-13 20:55:28 UTC (rev 3097)
@@ -61,6 +61,8 @@
 export(return_constraint)
 export(return_objective)
 export(risk_budget_objective)
+export(rp_grid)
+export(rp_sample)
 export(rp_simplex)
 export(rp_transform)
 export(scatterFUN)

Modified: pkg/PortfolioAnalytics/R/constraints.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraints.R	2013-09-13 18:34:12 UTC (rev 3096)
+++ pkg/PortfolioAnalytics/R/constraints.R	2013-09-13 20:55:28 UTC (rev 3097)
@@ -1125,6 +1125,62 @@
   return(portfolio)
 }
 
+#' check if a set of weights satisfies the constraints
+#' 
+#' This function checks if a set of weights satisfies all constraints. This is
+#' used as a helper function for random portfolios created with \code{rp_simplex}
+#' and \code{rp_grid} to eliminate portfolios that do not satisfy the constraints.
+#' 
+#' @param weights vector of weights
+#' @param portfolio object of class 'portfolio'
+#' @return TRUE if all constraints are satisfied, FALSE if any constraint is violated
+#' @author Ross Bennett
+check_constraints <- function(weights, portfolio){
+  
+  # get the constraints to check
+  # We will check leverage, box, group, and position limit constraints
+  constraints <- get_constraints(portfolio)
+  min_sum <- constraints$min_sum
+  max_sum <- constraints$max_sum
+  min <- constraints$min
+  max <- constraints$max
+  groups <- constraints$groups
+  cLO <- constraints$cLO
+  cUP <- constraints$cUP
+  group_pos <- constraints$group_pos
+  div_target <- constraints$div_target
+  turnover_target <- constraints$turnover_target
+  max_pos <- constraints$max_pos
+  max_pos_long <- constraints$max_pos_long
+  max_pos_short <- constraints$max_pos_short
+  tolerance <- .Machine$double.eps^0.5
+  
+  log_vec <- c()
+  # check leverage constraints
+  if(!is.null(min_sum) & !is.null(max_sum)){
+    # TRUE if constraints are satisfied
+    log_vec <- c(log_vec, ((sum(weights) >= min_sum) & (sum(weights) <= max_sum)))
+  }
+  
+  # check box constraints
+  if(!is.null(min) & !is.null(max)){
+    # TRUE if constraints are satisfied
+    log_vec <- c(log_vec, (all(weights >= min) & all(weights <= max)))
+  }
+  
+  # check group constraints
+  if(!is.null(groups) & !is.null(cLO) & !is.null(cUP)){
+    log_vec <- c(log_vec, all(!group_fail(weights, groups, cLO, cUP, group_pos)))
+  }
+  
+  # check position limit constraints
+  if(!is.null(max_pos) | !is.null(max_pos_long) | !is.null(max_pos_short)){
+    log_vec <- c(log_vec, !pos_limit_fail(weights, max_pos, max_pos_long, max_pos_short))
+  }
+  # return TRUE if all constraints are satisfied, FALSE if any constraint is violated
+  return(all(log_vec))
+}
+
 # #' constructor for class constraint_ROI
 # #' 
 # #' @param assets number of assets, or optionally a named vector of assets specifying seed weights

Modified: pkg/PortfolioAnalytics/R/random_portfolios.R
===================================================================
--- pkg/PortfolioAnalytics/R/random_portfolios.R	2013-09-13 18:34:12 UTC (rev 3096)
+++ pkg/PortfolioAnalytics/R/random_portfolios.R	2013-09-13 20:55:28 UTC (rev 3097)
@@ -328,10 +328,11 @@
 #'   \item{simplex: }{The 'simplex' method to generate random portfolios is 
 #'   based on a paper by W. T. Shaw. The simplex method is useful to generate 
 #'   random portfolios with the full investment constraint, where the sum of the 
-#'   weights is equal to 1, and min box constraints. All other constraints such 
-#'   as group and position limit constraints will be handled by elimination. If 
-#'   the constraints are very restrictive, this may result in very few feasible 
-#'   portfolios remaining.}
+#'   weights is equal to 1, and min box constraints. Values for \code{min_sum} 
+#'   and \code{max_sum} of the leverage constraint will be ignored, the sum of 
+#'   weights will equal 1. All other constraints such as group and position 
+#'   limit constraints will be handled by elimination. If the constraints are 
+#'   very restrictive, this may result in very few feasible portfolios remaining.}
 #'   \item{grid: }{The 'grid' method to generate random portfolios is based on
 #'   the \code(gridSearch} function in package 'NMOF'. The grid search method 
 #'   only satisfies the \code{min} and \code{max} box constraints. The 
@@ -341,17 +342,21 @@
 #'   penalized in \code{constrained_objective}.}
 #' }
 #' 
+#' The constraint types checked are leverage, box, group, and position limit. Any
+#' portfolio that does not satisfy all these constraints will be eliminated.
+#' 
 #' @param portfolio an object of type "portfolio" specifying the constraints for the optimization, see \code{\link{constraint}}
 #' @param permutations integer: number of unique constrained random portfolios to generate
 #' @param \dots any other passthru parameters
 #' @param rp_method method to generate random portfolios
+#' @param eliminate TRUE/FALSE, eliminate portfolios that do not satisfy constraints
 #' @return matrix of random portfolio weights
 #' @seealso \code{\link{portfolio.spec}}, \code{\link{objective}}, \code{\link{randomize_portfolio_v2}}
 #' @author Peter Carl, Brian G. Peterson, (based on an idea by Pat Burns)
 #' @aliases random_portfolios
 #' @rdname random_portfolios
 #' @export
-random_portfolios_v2 <- function( portfolio, permutations=100, rp_method="sample", ...){
+random_portfolios_v2 <- function( portfolio, permutations=100, rp_method="sample", eliminate=TRUE, ...){
   if(hasArg(p)) p=match.call(expand.dots=TRUE)$p else p=0:5
   if(hasArg(normalize)) normalize=match.call(expand.dots=TRUE)$normalize else normalize=TRUE
   switch(rp_method,
@@ -362,6 +367,15 @@
          grid = {rp <- rp_grid(portfolio, permutations, normalize, ...)
          }
   )
+  if(eliminate){
+    # eliminate portfolios that do not satisfy constraints
+    stopifnot("package:foreach" %in% search() || require("foreach",quietly = TRUE))
+    check <- foreach(i=1:nrow(rp), .combine=c) %dopar% {
+      # check_constraint returns TRUE if all constraints are satisfied
+      check_constraints(weights=rp[i,], portfolio=portfolio)
+    }
+    rp <- rp[which(check==TRUE),]
+  }
   return(rp)
 }
 
@@ -417,7 +431,8 @@
 #' @details
 #' The simplex method is useful to generate random portfolios with the full
 #' investment constraint where the sum of the weights is equal to 1 and min 
-#' box constraints. All other constraints such as group and position limit 
+#' box constraints. Values for min_sum and max_sum will be ignored, the sum 
+#' of weights will equal 1. All other constraints such as group and position limit 
 #' constraints will be handled by elimination. If the constraints are very 
 #' restrictive, this may result in very few feasible portfolios remaining. 
 #' 
@@ -449,6 +464,7 @@
   
   # get the constraints
   # the simplex method for generating random portfolios requires that the sum of weights is equal to 1
+  # ignore the min_sum and max_sum constraints
   constraints <- get_constraints(portfolio)
   L <- constraints$min
   
@@ -570,6 +586,7 @@
       tmp
     }
     out <- do.call(rbind, out)
+    out <- na.omit(out)
   }
   if(normalize) return(out) else return(rp)
 }

Modified: pkg/PortfolioAnalytics/man/chart.RiskReward.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.RiskReward.Rd	2013-09-13 18:34:12 UTC (rev 3096)
+++ pkg/PortfolioAnalytics/man/chart.RiskReward.Rd	2013-09-13 20:55:28 UTC (rev 3097)
@@ -8,8 +8,6 @@
 \alias{chart.RiskReward.optimize.portfolio.ROI}
 \title{classic risk reward scatter}
 \usage{
-  chart.RiskReward(object, ...)
-  
   \method{chart.RiskReward}{optimize.portfolio.DEoptim} (object, ..., neighbors = NULL, return.col = "mean",
     risk.col = "ES", chart.assets = FALSE,
     element.color = "darkgray", cex.axis = 0.8,
@@ -35,6 +33,8 @@
     element.color = "darkgray", cex.axis = 0.8,
     ylim = NULL, xlim = NULL, rp = FALSE)
 
+  chart.RiskReward(object, ...)
+
   \method{chart.RiskReward}{opt.list} (object, ...,
     risk.col = "ES", return.col = "mean", main = "",
     ylim = NULL, xlim = NULL, labels.assets = TRUE,

Added: pkg/PortfolioAnalytics/man/check_constraints.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/check_constraints.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/check_constraints.Rd	2013-09-13 20:55:28 UTC (rev 3097)
@@ -0,0 +1,26 @@
+\name{check_constraints}
+\alias{check_constraints}
+\title{check if a set of weights satisfies the constraints}
+\usage{
+  check_constraints(weights, portfolio)
+}
+\arguments{
+  \item{weights}{vector of weights}
+
+  \item{portfolio}{object of class 'portfolio'}
+}
+\value{
+  TRUE if all constraints are satisfied, FALSE if any
+  constraint is violated
+}
+\description{
+  This function checks if a set of weights satisfies all
+  constraints. This is used as a helper function for random
+  portfolios created with \code{rp_simplex} and
+  \code{rp_grid} to eliminate portfolios that do not
+  satisfy the constraints.
+}
+\author{
+  Ross Bennett
+}
+

Modified: pkg/PortfolioAnalytics/man/random_portfolios.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/random_portfolios.Rd	2013-09-13 18:34:12 UTC (rev 3096)
+++ pkg/PortfolioAnalytics/man/random_portfolios.Rd	2013-09-13 20:55:28 UTC (rev 3097)
@@ -3,7 +3,8 @@
 \alias{random_portfolios_v2}
 \title{version 2 generate an arbitary number of constrained random portfolios}
 \usage{
-  random_portfolios_v2(portfolio, permutations = 100, ...)
+  random_portfolios_v2(portfolio, permutations = 100,
+    rp_method = "sample", eliminate = TRUE, ...)
 }
 \arguments{
   \item{portfolio}{an object of type "portfolio" specifying
@@ -14,6 +15,11 @@
   random portfolios to generate}
 
   \item{\dots}{any other passthru parameters}
+
+  \item{rp_method}{method to generate random portfolios}
+
+  \item{eliminate}{TRUE/FALSE, eliminate portfolios that do
+  not satisfy constraints}
 }
 \value{
   matrix of random portfolio weights
@@ -23,6 +29,38 @@
   generate an arbitrary number of constrained random
   portfolios.
 }
+\details{
+  Random portfolios can be generate using one of three
+  methods. \itemize{ \item{sample: }{The 'sample' method to
+  generate random portfolios is based on an idea pioneerd
+  by Pat Burns. This is the most flexible method and can
+  generate portfolios to satisfy leverage, box, group, and
+  position limit constraints.} \item{simplex: }{The
+  'simplex' method to generate random portfolios is based
+  on a paper by W. T. Shaw. The simplex method is useful to
+  generate random portfolios with the full investment
+  constraint, where the sum of the weights is equal to 1,
+  and min box constraints. Values for \code{min_sum} and
+  \code{max_sum} of the leverage constraint will be
+  ignored, the sum of weights will equal 1. All other
+  constraints such as group and position limit constraints
+  will be handled by elimination. If the constraints are
+  very restrictive, this may result in very few feasible
+  portfolios remaining.} \item{grid: }{The 'grid' method to
+  generate random portfolios is based on the
+  \code(gridSearch} function in package 'NMOF'. The grid
+  search method only satisfies the \code{min} and
+  \code{max} box constraints. The \code{min_sum} and
+  \code{max_sum} leverage constraints will likely be
+  violated and the weights in the random portfolios should
+  be normalized.  Normalization may cause the box
+  constraints to be violated and will be penalized in
+  \code{constrained_objective}.} }
+
+  The constraint types checked are leverage, box, group,
+  and position limit. Any portfolio that does not satisfy
+  all these constraints will be eliminated.
+}
 \author{
   Peter Carl, Brian G. Peterson, (based on an idea by Pat
   Burns)

Added: pkg/PortfolioAnalytics/man/rp_grid.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/rp_grid.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/rp_grid.Rd	2013-09-13 20:55:28 UTC (rev 3097)
@@ -0,0 +1,40 @@
+\name{rp_grid}
+\alias{rp_grid}
+\title{Generate random portfolios based on grid search method}
+\usage{
+  rp_grid(portfolio, permutations = 2000, normalize = TRUE,
+    ...)
+}
+\arguments{
+  \item{portfolio}{}
+
+  \item{permutations}{}
+
+  \item{normalize}{TRUE/FALSE}
+
+  \item{\dots}{any passthru parameters. Currently ignored}
+}
+\value{
+  matrix of random portfolio weights
+}
+\description{
+  This function generates random portfolios based on the
+  \code{gridSearch} function from the 'NMOF' package.
+}
+\details{
+  The number of levels is calculated based on permutations
+  and number of assets. The number of levels must be an
+  integer and may not result in the exact number of
+  permutations. We round up to the nearest integer for the
+  levels so the number of portfolios generated will be
+  greater than or equal to permutations.
+
+  The grid search method only satisfies the \code{min} and
+  \code{max} box constraints. The \code{min_sum} and
+  \code{max_sum} leverage constraints will likely be
+  violated and the weights in the random portfolios should
+  be normalized. Normalization may cause the box
+  constraints to be violated and will be penalized in
+  \code{constrained_objective}.
+}
+

Added: pkg/PortfolioAnalytics/man/rp_sample.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/rp_sample.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/rp_sample.Rd	2013-09-13 20:55:28 UTC (rev 3097)
@@ -0,0 +1,30 @@
+\name{rp_sample}
+\alias{rp_sample}
+\title{Generate random portfolios using the sample method}
+\usage{
+  rp_sample(portfolio, permutations, ...)
+}
+\arguments{
+  \item{portfolio}{an object of type "portfolio" specifying
+  the constraints for the optimization, see
+  \code{\link{portfolio.spec}}}
+
+  \item{permutations}{integer: number of unique constrained
+  random portfolios to generate}
+
+  \item{\dots}{any other passthru parameters}
+}
+\value{
+  a matrix of random portfolio weights
+}
+\description{
+  This function generates random portfolios based on an
+  idea by Pat Burns.
+}
+\details{
+  The 'sample' method to generate random portfolios is
+  based on an idea pioneerd by Pat Burns. This is the most
+  flexible method and can generate portfolios to satisfy
+  leverage, box, group, and position limit constraints.
+}
+

Modified: pkg/PortfolioAnalytics/man/rp_simplex.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/rp_simplex.Rd	2013-09-13 18:34:12 UTC (rev 3096)
+++ pkg/PortfolioAnalytics/man/rp_simplex.Rd	2013-09-13 20:55:28 UTC (rev 3097)
@@ -17,7 +17,7 @@
   \item{\dots}{any other passthru parameters}
 }
 \value{
-  a matrix of random portfolios
+  a matrix of random portfolio weights
 }
 \description{
   This function generates random portfolios based on the
@@ -26,9 +26,10 @@
 \details{
   The simplex method is useful to generate random
   portfolios with the full investment constraint where the
-  sum of the weights is equal to 1 and min and max box
-  constraints. All other constraints such as group and
-  position limit constraints will be handled by
+  sum of the weights is equal to 1 and min box constraints.
+  Values for min_sum and max_sum will be ignored, the sum
+  of weights will equal 1. All other constraints such as
+  group and position limit constraints will be handled by
   elimination. If the constraints are very restrictive,
   this may result in very few feasible portfolios
   remaining.



More information about the Returnanalytics-commits mailing list