Path: blob/master/inst/tests/test_glpk_minES.R
1433 views
library(PortfolioAnalytics)1library(ROI)2library(ROI.plugin.glpk)3library(Rglpk)4library(testthat)56# Test that ROI.plugin.glpk solutions equal Rglpk solutions7context("Minimum ES Portfolios: PortfolioAnalytics with ROI.plugin.glpk and Rglpk")8910##### Data #####11data(edhec)12R <- edhec[, 1:5]13funds <- colnames(R)1415##### Parameters #####16m <- ncol(R)17n <- nrow(R)18alpha <- 0.051920portf <- portfolio.spec(funds)21portf <- add.constraint(portf, type="full_investment")22portf <- add.constraint(portf, type="box", min=-Inf, max=Inf)23portf <- add.objective(portf, type="risk", name="ES", arguments=list(p=1-alpha))2425# Linear part of objective function26objL <- c(rep(0, m), rep(1 / (alpha * n), n), 1)2728# Constraints matrix29Amat <- cbind(rbind(1, zoo::coredata(R)),30rbind(0, cbind(diag(n), 1)))3132# right hand side of constraints33rhs <- c(1, rep(0, n))3435# direction of inequality of constraints36dir <- c("==", rep(">=", n))3738##### Long Only #####39# Upper and lower bounds (i.e. box constraints)40min_box <- rep(0, m)41max_box <- rep(1, m)4243lb <- c(min_box, rep(0, n), -1)44ub <- c(max_box, rep(Inf, n), 1)4546bnds <- list(lower = list(ind = seq.int(1L, m+n+1), val = lb),47upper = list(ind = seq.int(1L, m+n+1), val = ub))4849# Update box constraints in portfolio50portf$constraints[[2]]$min <- min_box51portf$constraints[[2]]$max <- max_box5253# Solve optimization with Rglpk54opt.glpk <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, bounds=bnds)5556# Solve optimization with PortfolioAnalytics57opt.pa <- optimize.portfolio(R, portf, optimize_method="glpk")58weights <- as.numeric(extractWeights(opt.pa))5960test_that("Long Only: PortfolioAnalytics and Rglpk solution weights are equal", {61expect_that(weights, equals(opt.glpk$solution[1:m]))62})6364test_that("Long Only: PortfolioAnalytics bounds are respected", {65expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())66})6768test_that("Long Only: Rglpk bounds are respected", {69expect_that(all(opt.glpk$solution[1:m] >= min_box) & all(opt.glpk$solution[1:m] <= max_box), is_true())70})7172test_that("Long Only: PortfolioAnalytics and Rglpk solution objective values are equal", {73expect_that(opt.pa$out, equals(opt.glpk$optimum))74})7576##### Box #####77# Upper and lower bounds (i.e. box constraints)78min_box <- rep(0.05, m)79max_box <- rep(0.55, m)8081lb <- c(min_box, rep(0, n), -1)82ub <- c(max_box, rep(Inf, n), 1)8384bnds <- list(lower = list(ind = seq.int(1L, m+n+1), val = lb),85upper = list(ind = seq.int(1L, m+n+1), val = ub))8687# Update box constraints in portfolio88portf$constraints[[2]]$min <- min_box89portf$constraints[[2]]$max <- max_box9091# Solve optimization with Rglpk92opt.glpk <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, bounds=bnds)9394# Solve optimization with PortfolioAnalytics95opt.pa <- optimize.portfolio(R, portf, optimize_method="glpk")96weights <- as.numeric(extractWeights(opt.pa))9798test_that("Box: PortfolioAnalytics and Rglpk solution weights are equal", {99expect_that(weights, equals(opt.glpk$solution[1:m]))100})101102test_that("Box: PortfolioAnalytics bounds are respected", {103expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())104})105106test_that("Box: Rglpk bounds are respected", {107expect_that(all(opt.glpk$solution[1:m] >= min_box) & all(opt.glpk$solution[1:m] <= max_box), is_true())108})109110test_that("Box: PortfolioAnalytics and Rglpk solution objective values are equal", {111expect_that(opt.pa$out, equals(opt.glpk$optimum))112})113114##### Box with Shorting #####115# Upper and lower bounds (i.e. box constraints)116min_box <- rep(-0.05, m)117max_box <- rep(0.55, m)118119lb <- c(min_box, rep(0, n), -1)120ub <- c(max_box, rep(Inf, n), 1)121122bnds <- list(lower = list(ind = seq.int(1L, m+n+1), val = lb),123upper = list(ind = seq.int(1L, m+n+1), val = ub))124125# Update box constraints in portfolio126portf$constraints[[2]]$min <- min_box127portf$constraints[[2]]$max <- max_box128129# Solve optimization with Rglpk130opt.glpk <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, bounds=bnds)131132# Solve optimization with PortfolioAnalytics133opt.pa <- optimize.portfolio(R, portf, optimize_method="glpk")134weights <- as.numeric(extractWeights(opt.pa))135136test_that("Box with Shorting: PortfolioAnalytics and Rglpk solution weights are equal", {137expect_that(weights, equals(opt.glpk$solution[1:m]))138})139140test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {141expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())142})143144test_that("Box with Shorting: Rglpk bounds are respected", {145expect_that(all(opt.glpk$solution[1:m] >= min_box) & all(opt.glpk$solution[1:m] <= max_box), is_true())146})147148test_that("Box with Shorting: PortfolioAnalytics and Rglpk solution objective values are equal", {149expect_that(opt.pa$out, equals(opt.glpk$optimum))150})151152153154155