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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 31 20:54:23 CEST 2013


Author: rossbennett34
Date: 2013-07-31 20:54:23 +0200 (Wed, 31 Jul 2013)
New Revision: 2686

Added:
   pkg/PortfolioAnalytics/man/update_constraint_v1tov2.Rd
   pkg/PortfolioAnalytics/sandbox/testing_back_compat.R
Modified:
   pkg/PortfolioAnalytics/NAMESPACE
   pkg/PortfolioAnalytics/R/constraints.R
   pkg/PortfolioAnalytics/R/optFUN.R
   pkg/PortfolioAnalytics/R/optimize.portfolio.R
   pkg/PortfolioAnalytics/man/optimize.portfolio_v1.Rd
Log:
Modifying constraints and optimize.portfolio for backwards compatibility. Also added simple testing script using v1_constraint object with optimize.portfolio.

Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE	2013-07-31 17:20:38 UTC (rev 2685)
+++ pkg/PortfolioAnalytics/NAMESPACE	2013-07-31 18:54:23 UTC (rev 2686)
@@ -86,5 +86,6 @@
 export(txfrm_group_constraint)
 export(txfrm_position_limit_constraint)
 export(txfrm_weight_sum_constraint)
+export(update_constraint_v1tov2)
 export(update.constraint)
 export(weight_sum_constraint)

Modified: pkg/PortfolioAnalytics/R/constraints.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraints.R	2013-07-31 17:20:38 UTC (rev 2685)
+++ pkg/PortfolioAnalytics/R/constraints.R	2013-07-31 18:54:23 UTC (rev 2686)
@@ -822,6 +822,47 @@
   return(portfolio)
 }
 
+#' Helper function to update v1_constraint objects to v2 specification in the portfolio object
+#' 
+#' The function takes the constraints and objectives specified in the v1_constraint
+#' object and updates the portfolio object with those constraints and objectives. This
+#' function is used inside optimize.portfolio to maintain backwards compatibility
+#' if the user passes in a v1_constraint object for the constraint arg in
+#' optimize.portfolio.
+#' 
+#' @param portfolio portfolio object passed into optimize.portfolio
+#' @param v1_constraint object of type v1_constraint passed into optimize.portfolio
+#' @return portfolio object containing constraints and objectives from v1_constraint
+#' @author Ross Bennett
+#' @seealso \code{\link{portfolio.spec}}, \code{\link{add.constraint}}
+#' @export
+update_constraint_v1tov2 <- function(portfolio, v1_constraint){
+  if(!is.portfolio(portfolio)) stop("portfolio object must be of class 'portfolio'")
+  if(!inherits(v1_constraint, "v1_constraint")) stop("v1_constraint object must be of class 'v1_constraint'")
+  # Put the assets and weight_seq into slots in portfolio object
+  portfolio$assets <- v1_constraint$assets
+  portfolio$weight_seq <- v1_constraint$weight_seq
+  
+  # The v1_constraint object supported 3 constraint types (weight_sum, box, and group)
+  # Add weight_sum/leverage constraints from v1_constraint to portfolio
+  if(!is.null(v1_constraint$min_sum) & !is.null(v1_constraint$max_sum)){
+    portfolio <- add.constraint(portfolio=portfolio, type='weight_sum', min_sum=v1_constraint$min_sum, max_sum=v1_constraint$max_sum)
+  }
+  # Add box constraints from v1_constraint to portfolio
+  if(!is.null(v1_constraint$min) & !is.null(v1_constraint$max)){
+    portfolio <- add.constraint(portfolio=portfolio, type='box', min=v1_constraint$min, max=v1_constraint$max)
+  }
+  # Add group constraints from v1_constraint to portfolio
+  if(!is.null(v1_constraint$groups) & !is.null(v1_constraint$cLO) & !is.null(v1_constraint$cUP)){
+    portfolio <- add.constraint(portfolio=portfolio, type='group', groups=v1_constraint$groups, group_min=v1_constraint$cLO, group_max=v1_constraint$cUP)
+  }
+  
+  # Put the objectives from v1_constraint into the objectives slot in the portfolio 
+  # object. This overwrites what might already be in portfolio$objectives assuming 
+  # the user is using the v1_constraint object to specify the objectives
+  portfolio$objectives <- v1_constraint$objectives
+  return(portfolio)
+}
 
 # #' constructor for class constraint_ROI
 # #' 

Modified: pkg/PortfolioAnalytics/R/optFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/optFUN.R	2013-07-31 17:20:38 UTC (rev 2685)
+++ pkg/PortfolioAnalytics/R/optFUN.R	2013-07-31 18:54:23 UTC (rev 2686)
@@ -61,7 +61,7 @@
   # Applying box constraints
   bnds <- list(lower=list(ind=seq.int(1L, N), val=as.numeric(constraints$min)),
                upper=list(ind=seq.int(1L, N), val=as.numeric(constraints$max)))
-  
+  print(bnds)
   # set up initial A matrix for leverage constraints
   Amat <- rbind(rep(1, N), rep(1, N))
   dir.vec <- c(">=","<=")

Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R	2013-07-31 17:20:38 UTC (rev 2685)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R	2013-07-31 18:54:23 UTC (rev 2686)
@@ -559,8 +559,14 @@
   
   # Check for constraints and objectives passed in separately outside of the portfolio object
   if(!is.null(constraints)){
-    # Insert the constraints into the portfolio object
-    portfolio <- insert_constraints(portfolio=portfolio, constraints=constraints)
+    if(inherits(constraints, "v1_constraint")){
+      warning("constraint object passed in is a 'v1_constraint' object, updating to v2 specification")
+      portfolio <- update_constraint_v1tov2(portfolio=portfolio, v1_constraint=constraints)
+    }
+    if(!inherits(constraints, "v1_constraint")){
+      # Insert the constraints into the portfolio object
+      portfolio <- insert_constraints(portfolio=portfolio, constraints=constraints)
+    }
   }
   if(!is.null(objectives)){
     # Insert the objectives into the portfolio object

Modified: pkg/PortfolioAnalytics/man/optimize.portfolio_v1.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/optimize.portfolio_v1.Rd	2013-07-31 17:20:38 UTC (rev 2685)
+++ pkg/PortfolioAnalytics/man/optimize.portfolio_v1.Rd	2013-07-31 18:54:23 UTC (rev 2686)
@@ -5,7 +5,7 @@
   optimize.portfolio_v1(R, constraints,
     optimize_method = c("DEoptim", "random", "ROI", "ROI_old", "pso", "GenSA"),
     search_size = 20000, trace = FALSE, ..., rp = NULL,
-    momentFUN = "set.portfolio.moments")
+    momentFUN = "set.portfolio.moments_v1")
 }
 \arguments{
   \item{R}{an xts, vector, matrix, data frame, timeSeries

Added: pkg/PortfolioAnalytics/man/update_constraint_v1tov2.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/update_constraint_v1tov2.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/update_constraint_v1tov2.Rd	2013-07-31 18:54:23 UTC (rev 2686)
@@ -0,0 +1,34 @@
+\name{update_constraint_v1tov2}
+\alias{update_constraint_v1tov2}
+\title{Helper function to update v1_constraint objects to v2 specification in the portfolio object}
+\usage{
+  update_constraint_v1tov2(portfolio, v1_constraint)
+}
+\arguments{
+  \item{portfolio}{portfolio object passed into
+  optimize.portfolio}
+
+  \item{v1_constraint}{object of type v1_constraint passed
+  into optimize.portfolio}
+}
+\value{
+  portfolio object containing constraints and objectives
+  from v1_constraint
+}
+\description{
+  The function takes the constraints and objectives
+  specified in the v1_constraint object and updates the
+  portfolio object with those constraints and objectives.
+  This function is used inside optimize.portfolio to
+  maintain backwards compatibility if the user passes in a
+  v1_constraint object for the constraint arg in
+  optimize.portfolio.
+}
+\author{
+  Ross Bennett
+}
+\seealso{
+  \code{\link{portfolio.spec}},
+  \code{\link{add.constraint}}
+}
+

Added: pkg/PortfolioAnalytics/sandbox/testing_back_compat.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_back_compat.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/testing_back_compat.R	2013-07-31 18:54:23 UTC (rev 2686)
@@ -0,0 +1,35 @@
+library(PortfolioAnalytics)
+library(DEoptim)
+
+data(edhec)
+ret <- edhec[, 1:4]
+funds <- colnames(ret)
+
+# Set up constraint object using v1
+gen.constr <- constraint(assets=funds, min=0, max=0.55, min_sum=0.99, max_sum=1, weight_seq=generatesequence(min=0, max=0.55, by=0.002))
+
+# Add an objective. Note the use of add.objective_v1
+gen.constr <- add.objective_v1(constraints=gen.constr, type="return", name="mean", enabled=TRUE)
+
+# Random
+optrpv1 <- optimize.portfolio_v1(R=ret, constraints=gen.constr, optimize_method="random", search_size=2000)
+optrpv1
+
+# DEoptim
+optdev1 <- optimize.portfolio_v1(R=ret, constraints=gen.constr, optimize_method="DEoptim", search_size=2000)
+optdev1
+
+# When using optimize.portfolio, the user will see that he needs to pass in a
+# portfolio object, so the user will likely just create a portfolio object and
+# then pass in the v1_constraint object
+pspec <- portfolio.spec(assets=funds)
+
+# This uses the new portfolio object and 'v2' of optimize.portfolio. The user 
+# can pass a v1_constraint object in for the constraints arg, but still needs to
+# pass in a portfolio object so that it can be updated with the constraints and
+# objectives from the v1_constraint object
+optrp <- optimize.portfolio(R=ret, portfolio=pspec, constraints=gen.constr, optimize_method="random", search_size=2000)
+optrp
+
+optde <- optimize.portfolio(R=ret, portfolio=pspec, constraints=gen.constr, optimize_method="DEoptim", search_size=2000, traceDE=5)
+optde



More information about the Returnanalytics-commits mailing list