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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 30 03:38:40 CEST 2014


Author: rossbennett34
Date: 2014-07-30 03:38:39 +0200 (Wed, 30 Jul 2014)
New Revision: 3491

Modified:
   pkg/PortfolioAnalytics/R/optFUN.R
   pkg/PortfolioAnalytics/sandbox/leverageQP.R
Log:
revising leverage QP implementation via ROI. Still not getting same results as quadprog directly

Modified: pkg/PortfolioAnalytics/R/optFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/optFUN.R	2014-07-30 00:15:14 UTC (rev 3490)
+++ pkg/PortfolioAnalytics/R/optFUN.R	2014-07-30 01:38:39 UTC (rev 3491)
@@ -1013,7 +1013,7 @@
   Amat <- c(tmp_means, rep(0, 2*N))
   dir <- "=="
   rhs <- target
-  meq <- N + 1
+  # meq <- N + 1
   
   # separate the weights into w, w+, and w-
   # w - w+ + w- = 0

Modified: pkg/PortfolioAnalytics/sandbox/leverageQP.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/leverageQP.R	2014-07-30 00:15:14 UTC (rev 3490)
+++ pkg/PortfolioAnalytics/sandbox/leverageQP.R	2014-07-30 01:38:39 UTC (rev 3491)
@@ -6,7 +6,7 @@
 library(quadprog)
 
 data(edhec)
-R <- edhec[, 1:10]
+R <- edhec[, 1:4]
 
 N <- ncol(R)
 leverage <- 1.6
@@ -14,6 +14,7 @@
 max_sum <- 1.01
 min_box <- rep(-0.3, N)
 max_box <- rep(1, N)
+lambda <- 1
 
 R0 <- matrix(0, ncol=ncol(R), nrow=nrow(R))
 returns <- cbind(R, R0, R0)
@@ -23,61 +24,78 @@
 # w - w+ + w- = 0
 Amat <- cbind(diag(N), -diag(N), diag(N))
 rhs <- rep(0, N)
+dir <- rep("==", N)
 
 # leverage constraint
 # w+ + w- <= leverage
 Amat <- rbind(Amat, c(rep(0, N), rep(-1, N), rep(-1, N)))
 rhs <- c(rhs, -leverage)
+dir <- c(dir, ">=")
 
 # w+ >= 0
 Amat <- rbind(Amat, cbind(diag(0, N), diag(N), diag(0, N)))
 rhs <- c(rhs, rep(0, N))
+dir <- c(dir, rep(">=", N))
 
 # w- >= 0
 Amat <- rbind(Amat, cbind(diag(0, N), diag(0, N), diag(N)))
 rhs <- c(rhs, rep(0, N))
+dir <- c(dir, rep(">=", N))
 
 # w^T 1 >= min_sum
 Amat <- rbind(Amat, c(rep(1, N), rep(0, N), rep(0, N)))
 rhs <- c(rhs, min_sum)
+dir <- c(dir, ">=")
 
 # w^T 1 <= max_sum
 Amat <- rbind(Amat, c(rep(-1, N), rep(0, N), rep(0, N)))
 rhs <- c(rhs, -max_sum)
+dir <- c(dir, ">=")
 
 # lower box constraints
 Amat <- rbind(Amat, cbind(diag(N), diag(0, N), diag(0, N)))
 rhs <- c(rhs, min_box)
+dir <- c(dir, rep(">=", N))
 
 # upper box constraints
 Amat <- rbind(Amat, cbind(-diag(N), diag(0, N), diag(0, N)))
 rhs <- c(rhs, -max_box)
+dir <- c(dir, rep(">=", N))
 
 sol <- solve.QP(Dmat=V, dvec=rep(0, 3*N), Amat=t(Amat), bvec=rhs, meq=N)
 sol
 
 weights <- sol$solution[1:N]
-weights
+round(weights, 4)
 sum(weights)
 sum(abs(weights)) <= leverage
 
+##### ROI #####
+ROI_objective <- Q_objective(Q=make.positive.definite(2*lambda*V), 
+                             L=rep(0, N*3))
 
-#' This script demonstrates how to solve a constrained portfolio optimization 
-#' problem to minimize standard deviation.
+opt.prob <- OP(objective=ROI_objective, 
+               constraints=L_constraint(L=Amat, dir=dir, rhs=rhs))
 
-#' Load the package and data
-library(PortfolioAnalytics)
-data(edhec)
-R <- edhec[, 1:10]
-funds <- colnames(R)
+roi.result <- ROI_solve(x=opt.prob, solver="quadprog")
+wts <- roi.result$solution[1:N]
+round(wts, 4)
+sum(wts)
+sum(abs(wts)) <= leverage
 
-#' Construct initial portfolio with basic constraints.
-init.portf <- portfolio.spec(assets=funds)
-init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
-init.portf <- add.constraint(portfolio=init.portf, type="box", min=-0.3, max=1)
-init.portf <- add.constraint(portfolio=init.portf, type="leverage_exposure", leverage=1.6)
-init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
+# The quadprog and ROI solution should result in the same solution using the
+# same Amat, dir, and rhs objects
+all.equal(weights, wts)
 
-opt <- optimize.portfolio(R, init.portf, optimize_method="ROI")
-opt$weights
+# Load the package and data
+# funds <- colnames(R)
 
+# Construct initial portfolio with basic constraints.
+# init.portf <- portfolio.spec(assets=funds)
+# init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+# init.portf <- add.constraint(portfolio=init.portf, type="box", min=-0.3, max=1)
+# init.portf <- add.constraint(portfolio=init.portf, type="leverage_exposure", leverage=1.6)
+# init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
+# 
+# opt <- optimize.portfolio(R, init.portf, optimize_method="ROI")
+# round(opt$weights, 4)



More information about the Returnanalytics-commits mailing list