Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/inst/tests/test_roi_min_etl_milp.R
1433 views
1
2
library(testthat)
3
library(ROI)
4
library(ROI.plugin.glpk)
5
library(Rglpk)
6
library(PerformanceAnalytics)
7
8
data(edhec)
9
R <- edhec[, 1:5]
10
m <- ncol(R)
11
12
constraints <- list()
13
constraints$min_sum <- 0.99
14
constraints$max_sum <- 1.01
15
constraints$min <- rep(0, m)
16
constraints$max <- rep(1, m)
17
constraints$max_pos <- 3
18
19
moments <- list()
20
moments$mean <- colMeans(R)
21
22
target <- NA
23
alpha <- 0.05
24
25
##### Rglpk #####
26
# Number of rows
27
n <- nrow(R)
28
29
# Number of columns
30
m <- ncol(R)
31
32
max_sum <- constraints$max_sum
33
min_sum <- constraints$min_sum
34
LB <- constraints$min
35
UB <- constraints$max
36
max_pos <- constraints$max_pos
37
min_pos <- 1
38
moments_mean <- as.numeric(moments$mean)
39
40
# A benchmark can be specified in the parma package.
41
# Leave this in and set to 0 for now
42
benchmark <- 0
43
44
# Check for target return
45
if(!is.na(target)){
46
# We have a target
47
targetcon <- c(moments_mean, rep(0, n+2))
48
targetdir <- "=="
49
targetrhs <- target
50
} else {
51
# No target specified, just maximize
52
targetcon <- NULL
53
targetdir <- NULL
54
targetrhs <- NULL
55
}
56
57
# Set up initial A matrix
58
tmpAmat <- cbind(-coredata(R),
59
matrix(-1, nrow=n, ncol=1),
60
-diag(n),
61
matrix(benchmark, nrow=n, ncol=1))
62
63
# Add leverage constraints to matrix
64
tmpAmat <- rbind(tmpAmat, rbind(c(rep(1, m), rep(0, n+2)),
65
c(rep(1, m), rep(0, n+2))))
66
67
# Add target return to matrix
68
tmpAmat <- rbind(tmpAmat, as.numeric(targetcon))
69
70
# This step just adds m rows to the matrix to accept box constraints in the next step
71
tmpAmat <- cbind(tmpAmat, matrix(0, ncol=m, nrow=dim(tmpAmat)[1]))
72
73
# Add lower bound box constraints
74
tmpAmat <- rbind(tmpAmat, cbind(-diag(m), matrix(0, ncol=n+2, nrow=m), diag(LB)))
75
76
# Add upper bound box constraints
77
tmpAmat <- rbind(tmpAmat, cbind(diag(m), matrix(0, ncol=n+2, nrow=m), diag(-UB)))
78
79
# Add row for max_pos cardinality constraints
80
tmpAmat <- rbind(tmpAmat, cbind(matrix(0, ncol=m + n + 2, nrow=1), matrix(-1, ncol=m, nrow=1)))
81
tmpAmat <- rbind(tmpAmat, cbind(matrix(0, ncol=m + n + 2, nrow=1), matrix(1, ncol=m, nrow=1)))
82
83
# Set up the rhs vector
84
rhs <- c( rep(0, n), min_sum, max_sum, targetrhs, rep(0, 2*m), -min_pos, max_pos)
85
86
# Set up the dir vector
87
dir <- c( rep("<=", n), ">=", "<=", targetdir, rep("<=", 2*m), "<=", "<=")
88
89
# Linear objective vector
90
objL <- c( rep(0, m), 1, rep(1/n, n) / alpha, 0, rep(0, m))
91
92
# Set up the types vector with continuous and binary variables
93
types <- c( rep("C", m), "C", rep("C", n), "C", rep("B", m))
94
95
bounds <- list( lower = list( ind = 1L:(m + n + 2 + m), val = c(LB, -1, rep(0, n), 1, rep(0, m)) ),
96
upper = list( ind = 1L:(m + n + 2 + m), val = c( UB, 1, rep(Inf, n), 1 , rep(1, m)) ) )
97
98
99
result <- Rglpk_solve_LP(obj=objL, mat=tmpAmat, dir=dir, rhs=rhs, types=types, bounds=bounds)
100
101
##### ROI #####
102
bnds <- V_bound( li = 1L:(m + n + 2 + m), lb = c(LB, -1, rep(0, n), 1, rep(0, m)),
103
ui = 1L:(m + n + 2 + m), ub = c( UB, 1, rep(Inf, n), 1 , rep(1, m)))
104
105
ROI_objective <- L_objective(c( rep(0, m), 1, rep(1/n, n) / alpha, 0, rep(0, m)))
106
107
opt.prob <- OP(objective=ROI_objective,
108
constraints=L_constraint(L=tmpAmat, dir=dir, rhs=rhs),
109
bounds=bnds, types=types)
110
roi.result <- ROI_solve(x=opt.prob, solver="glpk")
111
112
context("Test Rglpk_solve_LP and ROI_solve for minimum ES with cardinality constraint")
113
114
test_that("Objective values are equal", {
115
expect_equal(roi.result$objval, result$optimum)
116
})
117
118
test_that("Solutions (optimal weights) are equal", {
119
expect_equal(roi.result$solution[1:m], result$solution[1:m])
120
})
121
122