Path: blob/master/sandbox/test_cplex_maxMean.R
1433 views
library(PortfolioAnalytics)1library(Rcplex)2library(ROI)3library(ROI.plugin.cplex)4library(testthat)56# Test that ROI.plugin.cplex solutions equal Rcplex solutions7context("Maximum Mean Return Portfolios: PortfolioAnalytics with ROI.plugin.cplex and Rcplex")89# args(Rcplex)10# ?Rcplex1112##### Data #####13data(edhec)14R <- edhec[, 1:5]15funds <- colnames(R)16m <- ncol(R)1718##### Parameters #####19portf <- portfolio.spec(funds)20portf <- add.constraint(portf, type="full_investment")21portf <- add.constraint(portf, type="box", min=0, max=1)22portf <- add.objective(portf, type="return", name="mean")2324# Quadratic part of objective function25objQ <- NULL2627# Linear part of objective function28objL <- -colMeans(R)2930# Constraints matrix31Amat <- matrix(1, nrow=1, ncol=m)3233# right hand side of constraints34rhs <- 13536# direction of inequality of constraints37dir <- "E"383940##### Long Only #####41# Upper and lower bounds (i.e. box constraints)42# Rcplex bounds43lb <- rep(0, m)44ub <- rep(1, m)4546# Solve optimization with Rcplex47opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,48sense=dir, control=list(trace=0))4950# Solve optimization with PortfolioAnalytics51opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")52weights <- as.numeric(extractWeights(opt.pa))5354test_that("Long Only: PortfolioAnalytics and Rcplex solution weights are equal", {55expect_that(weights, equals(opt.rcplex$xopt))56})5758test_that("Long Only: PortfolioAnalytics bounds are respected", {59expect_that(all(weights >= lb) & all(weights <= ub), is_true())60})6162test_that("Long Only: Rcplex bounds are respected", {63expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())64})6566test_that("Long Only: PortfolioAnalytics and Rcplex solution objective values are equal", {67expect_that(opt.pa$out, equals(opt.rcplex$obj))68})6970##### Box #####71# Upper and lower bounds (i.e. box constraints)72# Rcplex bounds73lb <- rep(0.05, m)74ub <- rep(0.55, m)7576# Update box constraints in portfolio77portf$constraints[[2]]$min <- lb78portf$constraints[[2]]$max <- ub7980# Solve optimization with Rcplex81opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,82sense=dir, control=list(trace=0))8384# Solve optimization with PortfolioAnalytics85opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")86weights <- as.numeric(extractWeights(opt.pa))8788test_that("Box: PortfolioAnalytics and Rcplex solution weights are equal", {89expect_that(weights, equals(opt.rcplex$xopt))90})9192test_that("Box: PortfolioAnalytics bounds are respected", {93expect_that(all(weights >= lb) & all(weights <= ub), is_true())94})9596test_that("Box: Rcplex bounds are respected", {97expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())98})99100test_that("Box: PortfolioAnalytics and Rcplex solution objective values are equal", {101expect_that(opt.pa$out, equals(opt.rcplex$obj))102})103104##### Box with Shorting #####105# Upper and lower bounds (i.e. box constraints)106# Rcplex bounds107lb <- rep(-0.05, m)108ub <- rep(0.55, m)109110# Update box constraints in portfolio111portf$constraints[[2]]$min <- lb112portf$constraints[[2]]$max <- ub113114# Solve optimization with Rcplex115opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,116sense=dir, control=list(trace=0))117118# Solve optimization with PortfolioAnalytics119opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")120weights <- as.numeric(extractWeights(opt.pa))121122test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution weights are equal", {123expect_that(weights, equals(opt.rcplex$xopt))124})125126test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {127expect_that(all(weights >= lb) & all(weights <= ub), is_true())128})129130test_that("Box with Shorting: Rcplex bounds are respected", {131expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())132})133134test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution objective values are equal", {135expect_that(opt.pa$out, equals(opt.rcplex$obj))136})137138Rcplex.close()139140141142