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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Oct 14 23:07:18 CEST 2013


Author: rossbennett34
Date: 2013-10-14 23:07:18 +0200 (Mon, 14 Oct 2013)
New Revision: 3221

Modified:
   pkg/PortfolioAnalytics/R/optFUN.R
Log:
Modifying turnover constraint gmv optimization

Modified: pkg/PortfolioAnalytics/R/optFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/optFUN.R	2013-10-14 19:36:43 UTC (rev 3220)
+++ pkg/PortfolioAnalytics/R/optFUN.R	2013-10-14 21:07:18 UTC (rev 3221)
@@ -497,13 +497,6 @@
   # initial weights for solver
   if(is.null(init_weights)) init_weights <- rep(1/ N, N)
   
-  # Amat for initial weights
-  # Amat <- cbind(diag(N), matrix(0, nrow=N, ncol=N*2))
-  Amat <- cbind(diag(N), -1*diag(N), diag(N))
-  rhs <- init_weights
-  dir <- rep("==", N)
-  meq <- 4
-  
   # check for a target return constraint
   if(!is.na(target)) {
     # If var is the only objective specified, then moments$mean won't be calculated
@@ -511,28 +504,23 @@
       tmp_means <- colMeans(R)
     } else {
       tmp_means <- moments$mean
+      target <- 0
     }
-    Amat <- rbind(Amat, c(tmp_means, rep(0, 2*N)))
-    dir <- c(dir, "==")
-    rhs <- c(rhs, target)
-    meq <- 5
+  } else {
+    tmp_means <- moments$mean
+    target <- 0
   }
+  Amat <- c(tmp_means, rep(0, 2*N))
+  dir <- "=="
+  rhs <- target
+  meq <- N + 1
   
-  # Amat for full investment constraint
-  Amat <- rbind(Amat, rbind(c(rep(1, N), rep(0,2*N)), c(rep(-1, N), rep(0,2*N))))
-  rhs <- c(rhs, constraints$min_sum, -constraints$max_sum)
-  dir <- c(dir, ">=", ">=")
+  # Amat for initial weights
+  # Amat <- cbind(diag(N), matrix(0, nrow=N, ncol=N*2))
+  Amat <- rbind(Amat, cbind(diag(N), -1*diag(N), diag(N)))
+  rhs <- c(rhs, init_weights)
+  dir <- c(dir, rep("==", N))
   
-  # Amat for lower box constraints
-  Amat <- rbind(Amat, cbind(diag(N), diag(0, N), diag(0, N)))
-  rhs <- c(rhs, constraints$min)
-  dir <- c(dir, rep(">=", N))
-  
-  # Amat for upper box constraints
-  Amat <- rbind(Amat, cbind(-diag(N), diag(0, N), diag(0, N)))
-  rhs <- c(rhs, -constraints$max)
-  dir <- c(dir, rep(">=", N))
-  
   # Amat for turnover constraints
   Amat <- rbind(Amat, c(rep(0, N), rep(-1, N), rep(-1, N)))
   rhs <- c(rhs, -constraints$turnover_target)
@@ -548,6 +536,21 @@
   rhs <- c(rhs, rep(0, N))
   dir <- c(dir, rep(">=", N))
   
+  # Amat for full investment constraint
+  Amat <- rbind(Amat, rbind(c(rep(1, N), rep(0,2*N)), c(rep(-1, N), rep(0,2*N))))
+  rhs <- c(rhs, constraints$min_sum, -constraints$max_sum)
+  dir <- c(dir, ">=", ">=")
+  
+  # Amat for lower box constraints
+  Amat <- rbind(Amat, cbind(diag(N), diag(0, N), diag(0, N)))
+  rhs <- c(rhs, constraints$min)
+  dir <- c(dir, rep(">=", N))
+  
+  # Amat for upper box constraints
+  Amat <- rbind(Amat, cbind(-diag(N), diag(0, N), diag(0, N)))
+  rhs <- c(rhs, -constraints$max)
+  dir <- c(dir, rep(">=", N))
+  
   # include group constraints
   if(try(!is.null(constraints$groups), silent=TRUE)){
     n.groups <- length(constraints$groups)
@@ -575,7 +578,7 @@
   }
   
   d <- rep(-moments$mean, 3)
-  
+  # print(Amat)
   stopifnot("package:corpcor" %in% search() || require("corpcor",quietly = TRUE))
   stopifnot("package:quadprog" %in% search() || require("quadprog",quietly = TRUE))
   qp.result <- try(solve.QP(Dmat=make.positive.definite(2*lambda*V), 
@@ -583,6 +586,7 @@
   if(inherits(qp.result, "try-error")) stop("No solution found, consider adjusting constraints.")
   
   wts <- qp.result$solution
+  # print(round(wts,4))
   wts.final <- wts[(1:N)]
   # wts.buy <- wts[(1+N):(2*N)]
   # wts.sell <- wts[(2*N+1):(3*N)]



More information about the Returnanalytics-commits mailing list