Path: blob/master/inst/tests/test_roi_min_etl_milp.R
1433 views
1library(testthat)2library(ROI)3library(ROI.plugin.glpk)4library(Rglpk)5library(PerformanceAnalytics)67data(edhec)8R <- edhec[, 1:5]9m <- ncol(R)1011constraints <- list()12constraints$min_sum <- 0.9913constraints$max_sum <- 1.0114constraints$min <- rep(0, m)15constraints$max <- rep(1, m)16constraints$max_pos <- 31718moments <- list()19moments$mean <- colMeans(R)2021target <- NA22alpha <- 0.052324##### Rglpk #####25# Number of rows26n <- nrow(R)2728# Number of columns29m <- ncol(R)3031max_sum <- constraints$max_sum32min_sum <- constraints$min_sum33LB <- constraints$min34UB <- constraints$max35max_pos <- constraints$max_pos36min_pos <- 137moments_mean <- as.numeric(moments$mean)3839# A benchmark can be specified in the parma package.40# Leave this in and set to 0 for now41benchmark <- 04243# Check for target return44if(!is.na(target)){45# We have a target46targetcon <- c(moments_mean, rep(0, n+2))47targetdir <- "=="48targetrhs <- target49} else {50# No target specified, just maximize51targetcon <- NULL52targetdir <- NULL53targetrhs <- NULL54}5556# Set up initial A matrix57tmpAmat <- cbind(-coredata(R),58matrix(-1, nrow=n, ncol=1),59-diag(n),60matrix(benchmark, nrow=n, ncol=1))6162# Add leverage constraints to matrix63tmpAmat <- rbind(tmpAmat, rbind(c(rep(1, m), rep(0, n+2)),64c(rep(1, m), rep(0, n+2))))6566# Add target return to matrix67tmpAmat <- rbind(tmpAmat, as.numeric(targetcon))6869# This step just adds m rows to the matrix to accept box constraints in the next step70tmpAmat <- cbind(tmpAmat, matrix(0, ncol=m, nrow=dim(tmpAmat)[1]))7172# Add lower bound box constraints73tmpAmat <- rbind(tmpAmat, cbind(-diag(m), matrix(0, ncol=n+2, nrow=m), diag(LB)))7475# Add upper bound box constraints76tmpAmat <- rbind(tmpAmat, cbind(diag(m), matrix(0, ncol=n+2, nrow=m), diag(-UB)))7778# Add row for max_pos cardinality constraints79tmpAmat <- rbind(tmpAmat, cbind(matrix(0, ncol=m + n + 2, nrow=1), matrix(-1, ncol=m, nrow=1)))80tmpAmat <- rbind(tmpAmat, cbind(matrix(0, ncol=m + n + 2, nrow=1), matrix(1, ncol=m, nrow=1)))8182# Set up the rhs vector83rhs <- c( rep(0, n), min_sum, max_sum, targetrhs, rep(0, 2*m), -min_pos, max_pos)8485# Set up the dir vector86dir <- c( rep("<=", n), ">=", "<=", targetdir, rep("<=", 2*m), "<=", "<=")8788# Linear objective vector89objL <- c( rep(0, m), 1, rep(1/n, n) / alpha, 0, rep(0, m))9091# Set up the types vector with continuous and binary variables92types <- c( rep("C", m), "C", rep("C", n), "C", rep("B", m))9394bounds <- list( lower = list( ind = 1L:(m + n + 2 + m), val = c(LB, -1, rep(0, n), 1, rep(0, m)) ),95upper = list( ind = 1L:(m + n + 2 + m), val = c( UB, 1, rep(Inf, n), 1 , rep(1, m)) ) )969798result <- Rglpk_solve_LP(obj=objL, mat=tmpAmat, dir=dir, rhs=rhs, types=types, bounds=bounds)99100##### ROI #####101bnds <- V_bound( li = 1L:(m + n + 2 + m), lb = c(LB, -1, rep(0, n), 1, rep(0, m)),102ui = 1L:(m + n + 2 + m), ub = c( UB, 1, rep(Inf, n), 1 , rep(1, m)))103104ROI_objective <- L_objective(c( rep(0, m), 1, rep(1/n, n) / alpha, 0, rep(0, m)))105106opt.prob <- OP(objective=ROI_objective,107constraints=L_constraint(L=tmpAmat, dir=dir, rhs=rhs),108bounds=bnds, types=types)109roi.result <- ROI_solve(x=opt.prob, solver="glpk")110111context("Test Rglpk_solve_LP and ROI_solve for minimum ES with cardinality constraint")112113test_that("Objective values are equal", {114expect_equal(roi.result$objval, result$optimum)115})116117test_that("Solutions (optimal weights) are equal", {118expect_equal(roi.result$solution[1:m], result$solution[1:m])119})120121122