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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 13 14:33:44 CEST 2012


Author: hezkyvaron
Date: 2012-07-13 14:33:44 +0200 (Fri, 13 Jul 2012)
New Revision: 2156

Modified:
   pkg/PortfolioAnalytics/R/optimize.portfolio.R
   pkg/PortfolioAnalytics/man/optimize.portfolio.Rd
   pkg/PortfolioAnalytics/sandbox/testing_ROI.R
Log:
- finished fixing some bugs in optimize_method="ROI_new"
- Added two ROI_new sample problems in the testing_ROI script (there is still work to be done in this)
- Still needed:  comple documentation of PSO and ROI_new

Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R	2012-07-13 10:25:48 UTC (rev 2155)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R	2012-07-13 12:33:44 UTC (rev 2156)
@@ -259,8 +259,8 @@
     # This takes in a regular constraint object and extracts the desired business objectives
     # and converts them to matrix form to be inputed into a closed form solver
     # Applying box constraints
-    bnds <- list(lower = list(ind = seq.int(1L, N), val = rep(constraints$min, N)),
-                 upper = list(ind = seq.int(1L, N), val = rep(constraints$max, N)))
+    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)))
     # retrive the objectives to minimize, these should either be "var" and/or "mean"
     # we can eight miniminze variance or maximize quiadratic utility (we will be minimizing the neg. quad. utility)
     objectives <- do.call(cbind, sapply(constraints$objectives, "[", "name"))
@@ -268,22 +268,23 @@
     for(i in 1:length(objectives)) moments[[i]]<- eval(as.symbol(objectives[i]))(R)
     names(moments) <- objectives
     plugin <- ifelse(any(objectives=="var"), "quadprog", "glpk")
+    target.return <- do.call(cbind,sapply(init.constr$objectives, "[", "target"))
+    lambda <- do.call(cbind,sapply(init.constr$objectives, "[", "risk_aversion"))
+    if(is.null(lambda)) lambda <- 1    
     if(plugin == "quadprog") ROI_objective <- ROI:::Q_objective(Q=2*lambda*moments$var, L=-moments$mean)
     if(plugin == "glpk") ROI_objective <- ROI:::L_objective(L=-moments$mean)
-    if(!hasArg(constraints$lambda)) constraints$lambda <- 1
-    target.return <- do.call(cbind,sapply(init.constr$objectives, "[", "target"))
     Amat <- rbind(rep(1, N), rep(1, N))
     dir.vec <- c(">=","<=")
     rhs.vec <- c(constraints$min_sum, constraints$max_sum)
     if(!is.null(target.return)) {
       Amat <- rbind(Amat, moments$mean)
-      dir.vec <- cbind(dir.vec, "=="))
+      dir.vec <- cbind(dir.vec, "==")
       rhs.vec <- cbind(rhs.vec, target.return)
     }
     q.prob <- ROI:::OP(objective=ROI_objective, 
                        constraints=L_constraint(L=Amat, dir=dir.vec, rhs=rhs.vec),
                        bounds=bnds)
-    roi.results <- ROI:::ROI_solve(x=q.prob, solver=plugin)
+    roi.result <- ROI:::ROI_solve(x=q.prob, solver=plugin)
     weights <- roi.result$solution
     names(weights) <- colnames(R)
     out$weights <- weights

Modified: pkg/PortfolioAnalytics/man/optimize.portfolio.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/optimize.portfolio.Rd	2012-07-13 10:25:48 UTC (rev 2155)
+++ pkg/PortfolioAnalytics/man/optimize.portfolio.Rd	2012-07-13 12:33:44 UTC (rev 2156)
@@ -3,7 +3,7 @@
 \title{wrapper for constrained optimization of portfolios}
 \usage{
   optimize.portfolio(R, constraints,
-    optimize_method = c("DEoptim", "random", "ROI"),
+    optimize_method = c("DEoptim", "random", "ROI", "ROI_new", "pso"),
     search_size = 20000, trace = FALSE, ..., rp = NULL,
     momentFUN = "set.portfolio.moments")
 }
@@ -16,9 +16,12 @@
   \code{\link{constraint}}, if using closed for solver,
   need to pass a \code{\link{constraint_ROI}} object.}
 
-  \item{optimize_method}{one of "DEoptim", "random", "ROI".
-  For using ROI, need to use a constraint_ROI object in
-  constraints.}
+  \item{optimize_method}{one of "DEoptim", "random",
+  "ROI","ROI_new", "pso".  For using \code{ROI}, need to
+  use a constraint_ROI object in constraints. For using
+  \code{ROI_new}, pass standard \code{constratint} object
+  in \code{constraints} argument.  Presently, ROI has
+  plugins for \code{quadprog}}
 
   \item{search_size}{integer, how many portfolios to test,
   default 20,000}

Modified: pkg/PortfolioAnalytics/sandbox/testing_ROI.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_ROI.R	2012-07-13 10:25:48 UTC (rev 2155)
+++ pkg/PortfolioAnalytics/sandbox/testing_ROI.R	2012-07-13 12:33:44 UTC (rev 2156)
@@ -83,13 +83,13 @@
 #
 funds <- names(edhec)
 mu.port <- 0.002
-Amat <- cbind(rep(1,n.assets), mu.vec)
-mean.var.prob <- OP(objective=Q_objective(Q=-2*cov.mat, L=mu.vec), 
-                    constraints=L_constraint(L=t(Amat),
+Amat <- rbind(rep(1,n.assets), mu.vec)
+lambda <- 1
+mean.var.prob <- OP(objective=Q_objective(Q=2*lambda*cov.mat, L=-mu.vec), 
+                    constraints=L_constraint(L=Amat,
                                              dir=c("==","=="),
                                              rhs=c(1,mu.port)),
-                    bounds=bnds,
-                    maximum=TRUE)
+                    bounds=bnds)
 mean.var.constr <- constraint_ROI(assets=funds, op.problem=mean.var.prob, solver="quadprog")
 wts <- ROI_solve(x=mean.var.prob, solver="quadprog")$solution
 mean.var.solution <- optimize.portfolio(edhec, mean.var.constr, "ROI")
@@ -114,42 +114,43 @@
 # Some constraints are of type nonlinear.
 
 
-# =====================
-# Mean-variance:  Maximize quadratic utility
-#
-funds <- names(edhec)
-mu.port <- 0.002
-Amat <- cbind(rep(1,n.assets), mu.vec)
-mean.var.prob <- OP(objective=Q_objective(Q=-2*cov.mat, L=mu.vec), 
-                    constraints=L_constraint(L=t(Amat),
-                                             dir=c("==","=="),
-                                             rhs=c(1,mu.port)),
-                    bounds=bnds,
-                    maximum=TRUE)
-mean.var.constr <- constraint_ROI(assets=funds, op.problem=mean.var.prob, solver="quadprog")
-wts <- ROI_solve(x=mean.var.prob, solver="quadprog")$solution
-mean.var.solution <- optimize.portfolio(edhec, mean.var.constr, "ROI")
 
-
 # ========================================================
 # Mean-variance:  Maximize quadratic utility --- dollar neutral
 #
 funds <- names(edhec)
 mu.port <- 0.002
 Amat <- cbind(rep(1,n.assets), mu.vec)
-dollar.neutral.prob <- OP(objective=Q_objective(Q=-2*cov.mat, L=mu.vec), 
+dollar.neutral.prob <- OP(objective=Q_objective(Q=2*cov.mat, L=-mu.vec), 
                           constraints=L_constraint(L=t(Amat),
                                                    dir=c("==","=="),
                                                    rhs=c(0,mu.port)),
-                          bounds=bnds,
-                          maximum=TRUE)
+                          bounds=bnds)
 dollar.neutral.constr <- constraint_ROI(assets=funds, op.problem=dollar.neutral.prob, solver="quadprog")
 wts <- ROI_solve(x=dollar.neutral.prob, solver="quadprog")$solution
 dollar.neutral.solution <- optimize.portfolio(edhec, dollar.neutral.constr, "ROI")
+paste(funds, dollar.neutral.solution$weights)
 
+# using integrated ROI
+mean.var <- constraint(assets = colnames(edhec), min = -Inf, max = Inf, min_sum=0, max_sum=0, risk_aversion=1)
+mean.var <- add.objective(constraints=mean.var, type="return", name="mean", enabled=TRUE, multiplier=0, target=mu.port)
+mean.var <- add.objective(constraints=mean.var, type="risk", name="var", enabled=TRUE, multiplier=0)
+solution <- optimize.portfolio(edhec, mean.var, "ROI_new")
+paste(names(edhec),solution$weights)
 
 
 # =====================
+# Maximize return given box constraints
+#
+# A set of box constraints used to initialize portfolios
+init.constr <- constraint(assets = colnames(edhec), min = .05, max = .3, min_sum=1, max_sum=1)
+init.constr <- add.objective(constraints=init.constr, type="return", name="mean", enabled=TRUE, multiplier=0)
+test <- optimize.portfolio(edhec, init.constr, "ROI_new")
+
+
+
+
+# =====================
 # Mean-variance:  Maximize return, with constraint on variance
 # setting varaince to be the average variance between the edhec funds.
 #



More information about the Returnanalytics-commits mailing list