library(PortfolioAnalytics)
library(Rcplex)
library(ROI)
library(ROI.plugin.cplex)
library(testthat)
context("Maximum Quadratic Utility Portfolios: PortfolioAnalytics with ROI.plugin.cplex and Rcplex")
data(edhec)
R <- edhec[, 1:5]
funds <- colnames(R)
m <- ncol(R)
lambda <- 1
portf <- portfolio.spec(funds)
portf <- add.constraint(portf, type="full_investment")
portf <- add.constraint(portf, type="box", min=-Inf, max=Inf)
portf <- add.objective(portf, type="risk", name="var", risk_aversion=lambda)
portf <- add.objective(portf, type="return", name="mean")
objQ <- lambda * 2 * cov(R)
objL <- -colMeans(R)
Amat <- matrix(1, nrow=1, ncol=m)
rhs <- 1
dir <- "E"
lb <- rep(-Inf, m)
ub <- rep(Inf, m)
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
sense=dir, control=list(trace=0))
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
weights <- as.numeric(extractWeights(opt.pa))
test_that("Unconstrained: PortfolioAnalytics and Rcplex solution weights are equal", {
expect_that(weights, equals(opt.rcplex$xopt))
})
test_that("Unconstrained: PortfolioAnalytics and Rcplex solution objective values are equal", {
expect_that(opt.pa$out, equals(opt.rcplex$obj))
})
lb <- rep(0, m)
ub <- rep(1, m)
portf$constraints[[2]]$min <- lb
portf$constraints[[2]]$max <- ub
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
sense=dir, control=list(trace=0))
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
weights <- as.numeric(extractWeights(opt.pa))
test_that("Long Only: PortfolioAnalytics and Rcplex solution weights are equal", {
expect_that(weights, equals(opt.rcplex$xopt))
})
test_that("Long Only: PortfolioAnalytics bounds are respected", {
expect_that(all(weights >= lb) & all(weights <= ub), is_true())
})
test_that("Long Only: Rcplex bounds are respected", {
expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())
})
test_that("Long Only: PortfolioAnalytics and Rcplex solution objective values are equal", {
expect_that(opt.pa$out, equals(opt.rcplex$obj))
})
lb <- rep(0.05, m)
ub <- rep(0.55, m)
portf$constraints[[2]]$min <- lb
portf$constraints[[2]]$max <- ub
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
sense=dir, control=list(trace=0))
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
weights <- as.numeric(extractWeights(opt.pa))
test_that("Box: PortfolioAnalytics and Rcplex solution weights are equal", {
expect_that(weights, equals(opt.rcplex$xopt))
})
test_that("Box: PortfolioAnalytics bounds are respected", {
expect_that(all(weights >= lb) & all(weights <= ub), is_true())
})
test_that("Box: Rcplex bounds are respected", {
expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())
})
test_that("Box: PortfolioAnalytics and Rcplex solution objective values are equal", {
expect_that(opt.pa$out, equals(opt.rcplex$obj))
})
lb <- rep(-0.05, m)
ub <- rep(0.55, m)
portf$constraints[[2]]$min <- lb
portf$constraints[[2]]$max <- ub
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
sense=dir, control=list(trace=0))
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
weights <- as.numeric(extractWeights(opt.pa))
test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution weights are equal", {
expect_that(weights, equals(opt.rcplex$xopt))
})
test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {
expect_that(all(weights >= lb) & all(weights <= ub), is_true())
})
test_that("Box with Shorting: Rcplex bounds are respected", {
expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())
})
test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution objective values are equal", {
expect_that(opt.pa$out, equals(opt.rcplex$obj))
})
Rcplex.close()