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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 13 20:34:12 CEST 2013


Author: rossbennett34
Date: 2013-09-13 20:34:12 +0200 (Fri, 13 Sep 2013)
New Revision: 3096

Added:
   pkg/PortfolioAnalytics/sandbox/rp_method_comparison.R
Modified:
   pkg/PortfolioAnalytics/R/random_portfolios.R
   pkg/PortfolioAnalytics/man/chart.Weights.Rd
Log:
Adding sample, simplex, and grid methods to random_portfolios. Added script in sandbox to demo 3 different methods for random portfolios.

Modified: pkg/PortfolioAnalytics/R/random_portfolios.R
===================================================================
--- pkg/PortfolioAnalytics/R/random_portfolios.R	2013-09-13 17:23:08 UTC (rev 3095)
+++ pkg/PortfolioAnalytics/R/random_portfolios.R	2013-09-13 18:34:12 UTC (rev 3096)
@@ -318,17 +318,76 @@
 #' repeatedly calls \code{\link{randomize_portfolio}} to 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. 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}.}
+#' }
+#' 
 #' @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 \dots any other passthru parameters
+#' @param rp_method method to generate random portfolios
 #' @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, ...)
-{ # 
+random_portfolios_v2 <- function( portfolio, permutations=100, rp_method="sample", ...){
+  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,
+         sample = {rp <- rp_sample(portfolio, permutations, ...)
+                   },
+         simplex = {rp <- rp_simplex(portfolio, permutations, p, ...)
+                    },
+         grid = {rp <- rp_grid(portfolio, permutations, normalize, ...)
+         }
+  )
+  return(rp)
+}
+
+# Alias randomize_portfolio_v2 to randomize_portfolio
+#' @export
+randomize_portfolio <- randomize_portfolio_v2
+
+# Alias random_portfolios_v2 to random_portfolios
+#' @export
+random_portfolios <- random_portfolios_v2
+
+#' Generate random portfolios using the sample method
+#' 
+#' 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.
+#' @param portfolio an object of type "portfolio" specifying the constraints for the optimization, see \code{\link{portfolio.spec}}
+#' @param permutations integer: number of unique constrained random portfolios to generate
+#' @param \dots any other passthru parameters
+#' @return a matrix of random portfolio weights
+#' @export
+rp_sample <- function(portfolio, permutations, ...){
   # this function generates a series of portfolios that are a "random walk" from the current portfolio
   seed <- portfolio$assets
   result <- matrix(nrow=permutations, ncol=length(seed))
@@ -350,14 +409,6 @@
   return(result)
 }
 
-# Alias randomize_portfolio_v2 to randomize_portfolio
-#' @export
-randomize_portfolio <- randomize_portfolio_v2
-
-# Alias random_portfolios_v2 to random_portfolios
-#' @export
-random_portfolios <- random_portfolios_v2
-
 #' Generate random portfolios using the simplex method
 #' 
 #' This function generates random portfolios based on the method outlined in the
@@ -365,8 +416,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 and
-#' max box constraints. All other constraints such as group and position limit 
+#' 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. 
 #' 
@@ -389,7 +440,7 @@
 #' @param permutations integer: number of unique constrained random portfolios to generate
 #' @param p scalar or vector for FEV biasing
 #' @param \dots any other passthru parameters
-#' @return a matrix of random portfolios
+#' @return a matrix of random portfolio weights
 #' @export
 rp_simplex <- function(portfolio, permutations, p=0:5, ...){
   # get the assets from the portfolio
@@ -441,7 +492,7 @@
 #' @param permutations
 #' @param normalize TRUE/FALSE
 #' @param \dots any passthru parameters. Currently ignored
-#' @return matrix of random portfolios
+#' @return matrix of random portfolio weights
 #' @export
 rp_grid <- function(portfolio, permutations=2000, normalize=TRUE, ...){
   
@@ -514,7 +565,7 @@
     }
     
     stopifnot("package:foreach" %in% search() || require("foreach",quietly = TRUE))
-    out <- foreach(1=1:nrow(rp)) %dopar% {
+    out <- foreach(i=1:nrow(rp)) %dopar% {
       tmp <- normalize_weights(weights=rp[i,])
       tmp
     }

Modified: pkg/PortfolioAnalytics/man/chart.Weights.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Weights.Rd	2013-09-13 17:23:08 UTC (rev 3095)
+++ pkg/PortfolioAnalytics/man/chart.Weights.Rd	2013-09-13 18:34:12 UTC (rev 3096)
@@ -9,10 +9,6 @@
 \alias{chart.Weights.optimize.portfolio.RP}
 \title{boxplot of the weights of the optimal portfolios}
 \usage{
-    chart.Weights(object, neighbors = NULL, ...,
-    main = "Weights", las = 3, xlab = NULL, cex.lab = 1,
-    element.color = "darkgray", cex.axis = 0.8)
-    
   \method{chart.Weights}{optimize.portfolio.DEoptim} (object, neighbors = NULL, ..., main = "Weights",
     las = 3, xlab = NULL, cex.lab = 1,
     element.color = "darkgray", cex.axis = 0.8,
@@ -46,6 +42,10 @@
     legend.loc = "topright", cex.legend = 0.8,
     plot.type = "line")
 
+  chart.Weights(object, neighbors = NULL, ...,
+    main = "Weights", las = 3, xlab = NULL, cex.lab = 1,
+    element.color = "darkgray", cex.axis = 0.8)
+
   \method{chart.Weights}{opt.list} (object,
     neighbors = NULL, ..., main = "Weights", las = 3,
     xlab = NULL, cex.lab = 1, element.color = "darkgray",

Added: pkg/PortfolioAnalytics/sandbox/rp_method_comparison.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/rp_method_comparison.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/rp_method_comparison.R	2013-09-13 18:34:12 UTC (rev 3096)
@@ -0,0 +1,34 @@
+library(PortfolioAnalytics)
+
+data(edhec)
+R <- edhec[, 1:4]
+
+# set up simple portfolio with leverage and box constraints 
+pspec <- portfolio.spec(assets=colnames(R))
+pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01)
+pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1)
+
+# generate random portfolios using the 3 methods
+rp1 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='sample')
+rp2 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='simplex') 
+rp3 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='grid')
+
+# show feasible portfolios in mean-StdDev space
+tmp1.mean <- apply(rp1, 1, function(x) mean(R %*% x))
+tmp1.StdDev <- apply(rp1, 1, function(x) StdDev(R=R, weights=x))
+tmp2.mean <- apply(rp2, 1, function(x) mean(R %*% x))
+tmp2.StdDev <- apply(rp2, 1, function(x) StdDev(R=R, weights=x))
+tmp3.mean <- apply(rp3, 1, function(x) mean(R %*% x))
+tmp3.StdDev <- apply(rp3, 1, function(x) StdDev(R=R, weights=x))
+
+# plot feasible portfolios 
+plot(x=tmp1.StdDev, y=tmp1.mean, col="gray", main="Random Portfolio Methods")
+points(x=tmp2.StdDev, y=tmp2.mean, col="red", pch=2)
+points(x=tmp3.StdDev, y=tmp3.mean, col="lightgreen", pch=5)
+legend("bottomright", legend=c("sample", "simplex", "grid"), col=c("gray", "red", "lightgreen"),
+       pch=c(1, 2, 5), bty="n")
+
+# sample has pretty even coverage of feasible space
+# simplex is concentrated around the assets
+# grid is 'pushed'/concentrated to the interior due to normalization
+# This could be a really good example with Shiny for an interactive example



More information about the Returnanalytics-commits mailing list