Path: blob/master/sandbox/test_cplex_gmv.R
1433 views
library(PortfolioAnalytics)1library(Rcplex)2library(ROI)3library(ROI.plugin.cplex)4library(testthat)56# Test that PortfolioAnalytics with ROI.plugin.cplex solutions equal Rcplex solutions7context("GMV 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=-Inf, max=Inf)22portf <- add.objective(portf, type="risk", name="var")2324# Quadratic part of objective function25objQ <- 2 * cov(R)2627# Linear part of objective function28objL <- rep(0, m)2930# Constraints matrix31Amat <- matrix(1, nrow=1, ncol=m)3233# right hand side of constraints34rhs <- 13536# direction of inequality of constraints37dir <- "E"3839##### Unconstrained #####40# Upper and lower bounds (i.e. box constraints)41# Rcplex bounds42lb <- rep(-Inf, m)43ub <- rep(Inf, m)4445# Solve optimization with Rcplex46opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,47sense=dir, control=list(trace=0))4849# Solve optimization with PortfolioAnalytics50opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")51weights <- as.numeric(extractWeights(opt.pa))5253test_that("Unconstrained: PortfolioAnalytics and Rcplex solution weights are equal", {54expect_that(weights, equals(opt.rcplex$xopt))55})5657test_that("Unconstrained: PortfolioAnalytics and Rcplex solution objective values are equal", {58expect_that(opt.pa$out, equals(opt.rcplex$obj))59})6061##### Long Only #####62# Upper and lower bounds (i.e. box constraints)63# Rcplex bounds64lb <- rep(0, m)65ub <- rep(1, m)6667# Update box constraints in portfolio68portf$constraints[[2]]$min <- lb69portf$constraints[[2]]$max <- ub7071# Solve optimization with Rcplex72opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,73sense=dir, control=list(trace=0))7475# Solve optimization with PortfolioAnalytics76opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")77weights <- as.numeric(extractWeights(opt.pa))7879test_that("Long Only: PortfolioAnalytics and Rcplex solution weights are equal", {80expect_that(weights, equals(opt.rcplex$xopt))81})8283test_that("Long Only: PortfolioAnalytics bounds are respected", {84expect_that(all(weights >= lb) & all(weights <= ub), is_true())85})8687test_that("Long Only: Rcplex bounds are respected", {88expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())89})9091test_that("Long Only: PortfolioAnalytics and Rcplex solution objective values are equal", {92expect_that(opt.pa$out, equals(opt.rcplex$obj))93})9495##### Box #####96# Upper and lower bounds (i.e. box constraints)97# Rcplex bounds98lb <- rep(0.05, m)99ub <- rep(0.55, m)100101# Update box constraints in portfolio102portf$constraints[[2]]$min <- lb103portf$constraints[[2]]$max <- ub104105# Solve optimization with Rcplex106opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,107sense=dir, control=list(trace=0))108109# Solve optimization with PortfolioAnalytics110opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")111weights <- as.numeric(extractWeights(opt.pa))112113114test_that("Box: PortfolioAnalytics and Rcplex solution weights are equal", {115expect_that(weights, equals(opt.rcplex$xopt))116})117118test_that("Box: PortfolioAnalytics bounds are respected", {119expect_that(all(weights >= lb) & all(weights <= ub), is_true())120})121122test_that("Box: Rcplex bounds are respected", {123expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())124})125126test_that("Box: PortfolioAnalytics and Rcplex solution objective values are equal", {127expect_that(opt.pa$out, equals(opt.rcplex$obj))128})129130##### Box with Shorting #####131# Upper and lower bounds (i.e. box constraints)132# Rcplex bounds133lb <- rep(-0.05, m)134ub <- rep(0.55, m)135136# Update box constraints in portfolio137portf$constraints[[2]]$min <- lb138portf$constraints[[2]]$max <- ub139140# Solve optimization with Rcplex141opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,142sense=dir, control=list(trace=0))143144# Solve optimization with PortfolioAnalytics145opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")146weights <- as.numeric(extractWeights(opt.pa))147148149test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution weights are equal", {150expect_that(weights, equals(opt.rcplex$xopt))151})152153test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {154expect_that(all(weights >= lb) & all(weights <= ub), is_true())155})156157test_that("Box with Shorting: Rcplex bounds are respected", {158expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())159})160161test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution objective values are equal", {162expect_that(opt.pa$out, equals(opt.rcplex$obj))163})164165Rcplex.close()166167168