Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/test_cplex_minES.R
1433 views
1
library(PortfolioAnalytics)
2
library(Rcplex)
3
library(ROI)
4
library(ROI.plugin.cplex)
5
library(testthat)
6
7
# Test that ROI.plugin.cplex solutions equal Rcplex solutions
8
context("Minimum ES Portfolios: PortfolioAnalytics with ROI.plugin.cplex and Rcplex")
9
10
# args(Rcplex)
11
# ?Rcplex
12
13
##### Data #####
14
data(edhec)
15
R <- edhec[, 1:5]
16
funds <- colnames(R)
17
18
##### Parameters #####
19
m <- ncol(R)
20
n <- nrow(R)
21
alpha <- 0.05
22
23
portf <- portfolio.spec(funds)
24
portf <- add.constraint(portf, type="full_investment")
25
portf <- add.constraint(portf, type="box", min=-Inf, max=Inf)
26
portf <- add.objective(portf, type="risk", name="ES", arguments=list(p=1-alpha))
27
28
# Quadratic part of objective function
29
objQ <- NULL
30
31
# Linear part of objective function
32
objL <- c(rep(0, m), rep(1 / (alpha * n), n), 1)
33
34
# Constraints matrix
35
Amat <- cbind(rbind(1, zoo::coredata(R)),
36
rbind(0, cbind(diag(n), 1)))
37
38
# right hand side of constraints
39
rhs <- c(1, rep(0, n))
40
41
# direction of inequality of constraints
42
dir <- c("E", rep("G", n))
43
44
##### Unconstrained #####
45
# Upper and lower bounds (i.e. box constraints)
46
# Rcplex bounds
47
min_box <- rep(-Inf, m)
48
max_box <- rep(Inf, m)
49
50
lb <- c(min_box, rep(0, n), -1)
51
ub <- c(max_box, rep(Inf, n), 1)
52
53
54
# Solve optimization with Rcplex
55
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
56
sense=dir, control=list(trace=0))
57
58
# Solve optimization with PortfolioAnalytics
59
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
60
weights <- as.numeric(extractWeights(opt.pa))
61
62
63
test_that("Unconstrained: PortfolioAnalytics and Rcplex solution weights are equal", {
64
expect_that(weights, equals(opt.rcplex$xopt[1:m]))
65
})
66
67
test_that("Unconstrained: PortfolioAnalytics and Rcplex solution objective values are equal", {
68
expect_that(opt.pa$out, equals(opt.rcplex$obj))
69
})
70
71
72
##### Long Only #####
73
# Upper and lower bounds (i.e. box constraints)
74
# Rcplex bounds
75
min_box <- rep(0, m)
76
max_box <- rep(1, m)
77
78
lb <- c(min_box, rep(0, n), -1)
79
ub <- c(max_box, rep(Inf, n), 1)
80
81
# Update box constraints in portfolio
82
portf$constraints[[2]]$min <- min_box
83
portf$constraints[[2]]$max <- max_box
84
85
# Solve optimization with Rcplex
86
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
87
sense=dir, control=list(trace=0))
88
89
# Solve optimization with PortfolioAnalytics
90
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
91
weights <- as.numeric(extractWeights(opt.pa))
92
93
test_that("Long Only: PortfolioAnalytics and Rcplex solution weights are equal", {
94
expect_that(weights, equals(opt.rcplex$xopt[1:m]))
95
})
96
97
test_that("Long Only: PortfolioAnalytics bounds are respected", {
98
expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())
99
})
100
101
test_that("Long Only: Rcplex bounds are respected", {
102
expect_that(all(opt.rcplex$xopt[1:m] >= min_box) & all(opt.rcplex$xopt[1:m] <= max_box), is_true())
103
})
104
105
test_that("Long Only: PortfolioAnalytics and Rcplex solution objective values are equal", {
106
expect_that(opt.pa$out, equals(opt.rcplex$obj))
107
})
108
109
##### Box #####
110
# Upper and lower bounds (i.e. box constraints)
111
# Rcplex bounds
112
min_box <- rep(0.05, m)
113
max_box <- rep(0.55, m)
114
115
lb <- c(min_box, rep(0, n), -1)
116
ub <- c(max_box, rep(Inf, n), 1)
117
118
# Update box constraints in portfolio
119
portf$constraints[[2]]$min <- min_box
120
portf$constraints[[2]]$max <- max_box
121
122
# Solve optimization with Rcplex
123
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
124
sense=dir, control=list(trace=0))
125
126
# Solve optimization with PortfolioAnalytics
127
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
128
weights <- as.numeric(extractWeights(opt.pa))
129
130
test_that("Box: PortfolioAnalytics and Rcplex solution weights are equal", {
131
expect_that(weights, equals(opt.rcplex$xopt[1:m]))
132
})
133
134
test_that("Box: PortfolioAnalytics bounds are respected", {
135
expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())
136
})
137
138
test_that("Box: Rcplex bounds are respected", {
139
expect_that(all(opt.rcplex$xopt[1:m] >= min_box) & all(opt.rcplex$xopt[1:m] <= max_box), is_true())
140
})
141
142
test_that("Box: PortfolioAnalytics and Rcplex solution objective values are equal", {
143
expect_that(opt.pa$out, equals(opt.rcplex$obj))
144
})
145
146
##### Box with Shorting #####
147
# Upper and lower bounds (i.e. box constraints)
148
# Rcplex bounds
149
min_box <- rep(-0.05, m)
150
max_box <- rep(0.55, m)
151
152
lb <- c(min_box, rep(0, n), -1)
153
ub <- c(max_box, rep(Inf, n), 1)
154
155
# Update box constraints in portfolio
156
portf$constraints[[2]]$min <- min_box
157
portf$constraints[[2]]$max <- max_box
158
159
# Solve optimization with Rcplex
160
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
161
sense=dir, control=list(trace=0))
162
163
# Solve optimization with PortfolioAnalytics
164
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
165
weights <- as.numeric(extractWeights(opt.pa))
166
167
test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution weights are equal", {
168
expect_that(weights, equals(opt.rcplex$xopt[1:m]))
169
})
170
171
test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {
172
expect_that(all(weights >= min_box) & all(weights <= max_box), is_true())
173
})
174
175
test_that("Box with Shorting: Rcplex bounds are respected", {
176
expect_that(all(opt.rcplex$xopt[1:m] >= min_box) & all(opt.rcplex$xopt[1:m] <= max_box), is_true())
177
})
178
179
test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution objective values are equal", {
180
expect_that(opt.pa$out, equals(opt.rcplex$obj))
181
})
182
183
Rcplex.close()
184
185
186