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