Path: blob/master/inst/tests/test_glpk_maxMean.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("Maximum Mean Return Portfolios: PortfolioAnalytics with ROI.plugin.glpk and Rglpk")8910##### Data #####11data(edhec)12R <- edhec[, 1:5]13funds <- colnames(R)1415##### Parameters #####16m <- ncol(R)1718portf <- portfolio.spec(funds)19portf <- add.constraint(portf, type="full_investment")20portf <- add.constraint(portf, type="box", min=-Inf, max=Inf)21portf <- add.objective(portf, type="return", name="mean")2223# Linear part of objective function24objL <- -colMeans(R)2526# Constraints matrix27Amat <- matrix(1, nrow=1, ncol=m)2829# right hand side of constraints30rhs <- 13132# direction of inequality of constraints33dir <- "=="3435##### Long Only #####36# Upper and lower bounds (i.e. box constraints)37lb <- rep(0, m)38ub <- rep(1, m)3940bnds <- list(lower = list(ind = seq.int(1L, m), val = lb),41upper = list(ind = seq.int(1L, m), val = ub))4243# Update box constraints in portfolio44portf$constraints[[2]]$min <- lb45portf$constraints[[2]]$max <- ub4647# Solve optimization with Rglpk48opt.glpk <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, bounds=bnds)4950# Solve optimization with PortfolioAnalytics51opt.pa <- optimize.portfolio(R, portf, optimize_method="glpk")52weights <- as.numeric(extractWeights(opt.pa))5354test_that("Long Only: PortfolioAnalytics and Rglpk solution weights are equal", {55expect_that(weights, equals(opt.glpk$solution[1:m]))56})5758test_that("Long Only: PortfolioAnalytics bounds are respected", {59expect_that(all(weights >= lb) & all(weights <= ub), is_true())60})6162test_that("Long Only: Rglpk bounds are respected", {63expect_that(all(opt.glpk$solution[1:m] >= lb) & all(opt.glpk$solution[1:m] <= ub), is_true())64})6566test_that("Long Only: PortfolioAnalytics and Rglpk solution objective values are equal", {67expect_that(opt.pa$out, equals(opt.glpk$optimum))68})6970##### Box #####71# Upper and lower bounds (i.e. box constraints)72lb <- rep(0.05, m)73ub <- rep(0.55, m)7475bnds <- list(lower = list(ind = seq.int(1L, m), val = lb),76upper = list(ind = seq.int(1L, m), val = ub))7778# Update box constraints in portfolio79portf$constraints[[2]]$min <- lb80portf$constraints[[2]]$max <- ub8182# Solve optimization with Rglpk83opt.glpk <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, bounds=bnds)8485# Solve optimization with PortfolioAnalytics86opt.pa <- optimize.portfolio(R, portf, optimize_method="glpk")87weights <- as.numeric(extractWeights(opt.pa))8889test_that("Box: PortfolioAnalytics and Rglpk solution weights are equal", {90expect_that(weights, equals(opt.glpk$solution[1:m]))91})9293test_that("Box: PortfolioAnalytics bounds are respected", {94expect_that(all(weights >= lb) & all(weights <= ub), is_true())95})9697test_that("Box: Rglpk bounds are respected", {98expect_that(all(opt.glpk$solution[1:m] >= lb) & all(opt.glpk$solution[1:m] <= ub), is_true())99})100101test_that("Box: PortfolioAnalytics and Rglpk solution objective values are equal", {102expect_that(opt.pa$out, equals(opt.glpk$optimum))103})104105##### Box with Shorting #####106# Upper and lower bounds (i.e. box constraints)107lb <- rep(-0.05, m)108ub <- rep(0.55, m)109110bnds <- list(lower = list(ind = seq.int(1L, m), val = lb),111upper = list(ind = seq.int(1L, m), val = ub))112113# Update box constraints in portfolio114portf$constraints[[2]]$min <- lb115portf$constraints[[2]]$max <- ub116117# Solve optimization with Rglpk118opt.glpk <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, bounds=bnds)119120# Solve optimization with PortfolioAnalytics121opt.pa <- optimize.portfolio(R, portf, optimize_method="glpk")122weights <- as.numeric(extractWeights(opt.pa))123124test_that("Box with Shorting: PortfolioAnalytics and Rglpk solution weights are equal", {125expect_that(weights, equals(opt.glpk$solution[1:m]))126})127128test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {129expect_that(all(weights >= lb) & all(weights <= ub), is_true())130})131132test_that("Box with Shorting: Rglpk bounds are respected", {133expect_that(all(opt.glpk$solution[1:m] >= lb) & all(opt.glpk$solution[1:m] <= ub), is_true())134})135136test_that("Box with Shorting: PortfolioAnalytics and Rglpk solution objective values are equal", {137expect_that(opt.pa$out, equals(opt.glpk$optimum))138})139140141142143