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