Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/inst/tests/test_roi_max_ret_milp.R
1433 views
1
2
# maximum return with position limit constraints
3
library(testthat)
4
library(ROI)
5
library(ROI.plugin.glpk)
6
library(Rglpk)
7
library(PerformanceAnalytics)
8
9
data(edhec)
10
R <- edhec[, 1:5]
11
m <- ncol(R)
12
13
constraints <- list()
14
constraints$min_sum <- 0.99
15
constraints$max_sum <- 1.01
16
constraints$min <- rep(0.2, m)
17
constraints$max <- rep(1, m)
18
constraints$max_pos <- 3
19
20
moments <- list()
21
moments$mu <- colMeans(R)
22
moments$mean <- colMeans(R)
23
24
target <- NA
25
26
max_pos <- constraints$max_pos
27
min_pos <- 2
28
29
# Number of assets
30
N <- ncol(R)
31
32
# Upper and lower bounds on weights
33
LB <- as.numeric(constraints$min)
34
UB <- as.numeric(constraints$max)
35
36
##### ROI #####
37
38
# Check for target return
39
if(!is.na(target)){
40
# We have a target
41
targetcon <- rbind(c(moments$mean, rep(0, N)),
42
c(-moments$mean, rep(0, N)))
43
targetdir <- c("<=", "==")
44
targetrhs <- c(Inf, -target)
45
} else {
46
# No target specified, just maximize
47
targetcon <- NULL
48
targetdir <- NULL
49
targetrhs <- NULL
50
}
51
52
# weight_sum constraint
53
Amat <- rbind(c(rep(1, N), rep(0, N)),
54
c(rep(1, N), rep(0, N)))
55
56
# Target return constraint
57
Amat <- rbind(Amat, targetcon)
58
59
# Bounds and position limit constraints
60
Amat <- rbind(Amat, cbind(-diag(N), diag(LB)))
61
Amat <- rbind(Amat, cbind(diag(N), -diag(UB)))
62
Amat <- rbind(Amat, c(rep(0, N), rep(-1, N)))
63
Amat <- rbind(Amat, c(rep(0, N), rep(1, N)))
64
65
dir <- c("<=", ">=", targetdir, rep("<=", 2*N), "<=", "<=")
66
rhs <- c(1, 1, targetrhs, rep(0, 2*N), -min_pos, max_pos)
67
68
# Only seems to work if I do not specify bounds
69
# bnds <- V_bound(li=seq.int(1L, 2*N), lb=c(as.numeric(constraints$min), rep(0, N)),
70
# ui=seq.int(1L, 2*N), ub=c(as.numeric(constraints$max), rep(Inf, N)))
71
bnds <- NULL
72
73
# Set up the types vector with continuous and binary variables
74
types <- c(rep("C", N), rep("B", N))
75
76
# Set up the linear objective to maximize mean return
77
ROI_objective <- L_objective(L=c(-moments$mean, rep(0, N)))
78
79
# Set up the optimization problem and solve
80
opt.prob <- OP(objective=ROI_objective,
81
constraints=L_constraint(L=Amat, dir=dir, rhs=rhs),
82
bounds=bnds, types=types)
83
roi.result <- ROI_solve(x=opt.prob, solver="glpk")
84
85
##### Rglpk #####
86
87
objL <- c(-moments$mean, rep(0, N))
88
89
result <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, bounds=bnds, types=types)
90
91
context("Test Rglpk_solve_LP and ROI_solve for maximum return with cardinality constraints")
92
93
test_that("Objective values are equal", {
94
expect_equal(roi.result$objval, result$optimum)
95
})
96
97
test_that("Solutions (optimal weights) are equal", {
98
expect_equal(roi.result$solution[1:m], result$solution[1:m])
99
})
100