Path: blob/master/sandbox/test_cplex_minES.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("Minimum ES Portfolios: PortfolioAnalytics with ROI.plugin.cplex and Rcplex")89# args(Rcplex)10# ?Rcplex1112##### Data #####13data(edhec)14R <- edhec[, 1:5]15funds <- colnames(R)1617##### Parameters #####18m <- ncol(R)19n <- nrow(R)20alpha <- 0.052122portf <- portfolio.spec(funds)23portf <- add.constraint(portf, type="full_investment")24portf <- add.constraint(portf, type="box", min=-Inf, max=Inf)25portf <- add.objective(portf, type="risk", name="ES", arguments=list(p=1-alpha))2627# Quadratic part of objective function28objQ <- NULL2930# Linear part of objective function31objL <- c(rep(0, m), rep(1 / (alpha * n), n), 1)3233# Constraints matrix34Amat <- cbind(rbind(1, zoo::coredata(R)),35rbind(0, cbind(diag(n), 1)))3637# right hand side of constraints38rhs <- c(1, rep(0, n))3940# direction of inequality of constraints41dir <- c("E", rep("G", n))4243##### Unconstrained #####44# Upper and lower bounds (i.e. box constraints)45# Rcplex bounds46min_box <- rep(-Inf, m)47max_box <- rep(Inf, m)4849lb <- c(min_box, rep(0, n), -1)50ub <- c(max_box, rep(Inf, n), 1)515253# Solve optimization with Rcplex54opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,55sense=dir, control=list(trace=0))5657# Solve optimization with PortfolioAnalytics58opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")59weights <- as.numeric(extractWeights(opt.pa))606162test_that("Unconstrained: PortfolioAnalytics and Rcplex solution weights are equal", {63expect_that(weights, equals(opt.rcplex$xopt[1:m]))64})6566test_that("Unconstrained: PortfolioAnalytics and Rcplex solution objective values are equal", {67expect_that(opt.pa$out, equals(opt.rcplex$obj))68})697071##### Long Only #####72# Upper and lower bounds (i.e. box constraints)73# Rcplex bounds74min_box <- rep(0, m)75max_box <- rep(1, m)7677lb <- c(min_box, rep(0, n), -1)78ub <- c(max_box, rep(Inf, n), 1)7980# Update box constraints in portfolio81portf$constraints[[2]]$min <- min_box82portf$constraints[[2]]$max <- max_box8384# Solve optimization with Rcplex85opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,86sense=dir, control=list(trace=0))8788# Solve optimization with PortfolioAnalytics89opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")90weights <- as.numeric(extractWeights(opt.pa))9192test_that("Long Only: PortfolioAnalytics and Rcplex solution weights are equal", {93expect_that(weights, equals(opt.rcplex$xopt[1:m]))94})9596test_that("Long Only: PortfolioAnalytics bounds are respected", {97expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())98})99100test_that("Long Only: Rcplex bounds are respected", {101expect_that(all(opt.rcplex$xopt[1:m] >= min_box) & all(opt.rcplex$xopt[1:m] <= max_box), is_true())102})103104test_that("Long Only: PortfolioAnalytics and Rcplex solution objective values are equal", {105expect_that(opt.pa$out, equals(opt.rcplex$obj))106})107108##### Box #####109# Upper and lower bounds (i.e. box constraints)110# Rcplex bounds111min_box <- rep(0.05, m)112max_box <- rep(0.55, m)113114lb <- c(min_box, rep(0, n), -1)115ub <- c(max_box, rep(Inf, n), 1)116117# Update box constraints in portfolio118portf$constraints[[2]]$min <- min_box119portf$constraints[[2]]$max <- max_box120121# Solve optimization with Rcplex122opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,123sense=dir, control=list(trace=0))124125# Solve optimization with PortfolioAnalytics126opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")127weights <- as.numeric(extractWeights(opt.pa))128129test_that("Box: PortfolioAnalytics and Rcplex solution weights are equal", {130expect_that(weights, equals(opt.rcplex$xopt[1:m]))131})132133test_that("Box: PortfolioAnalytics bounds are respected", {134expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())135})136137test_that("Box: Rcplex bounds are respected", {138expect_that(all(opt.rcplex$xopt[1:m] >= min_box) & all(opt.rcplex$xopt[1:m] <= max_box), is_true())139})140141test_that("Box: PortfolioAnalytics and Rcplex solution objective values are equal", {142expect_that(opt.pa$out, equals(opt.rcplex$obj))143})144145##### Box with Shorting #####146# Upper and lower bounds (i.e. box constraints)147# Rcplex bounds148min_box <- rep(-0.05, m)149max_box <- rep(0.55, m)150151lb <- c(min_box, rep(0, n), -1)152ub <- c(max_box, rep(Inf, n), 1)153154# Update box constraints in portfolio155portf$constraints[[2]]$min <- min_box156portf$constraints[[2]]$max <- max_box157158# Solve optimization with Rcplex159opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,160sense=dir, control=list(trace=0))161162# Solve optimization with PortfolioAnalytics163opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")164weights <- as.numeric(extractWeights(opt.pa))165166test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution weights are equal", {167expect_that(weights, equals(opt.rcplex$xopt[1:m]))168})169170test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {171expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())172})173174test_that("Box with Shorting: Rcplex bounds are respected", {175expect_that(all(opt.rcplex$xopt[1:m] >= min_box) & all(opt.rcplex$xopt[1:m] <= max_box), is_true())176})177178test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution objective values are equal", {179expect_that(opt.pa$out, equals(opt.rcplex$obj))180})181182Rcplex.close()183184185186