Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/test_cplex_maxMean.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("Maximum Mean Return 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=0, max=1)
23
portf <- add.objective(portf, type="return", name="mean")
24
25
# Quadratic part of objective function
26
objQ <- NULL
27
28
# Linear part of objective function
29
objL <- -colMeans(R)
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
41
##### Long Only #####
42
# Upper and lower bounds (i.e. box constraints)
43
# Rcplex bounds
44
lb <- rep(0, m)
45
ub <- rep(1, m)
46
47
# Solve optimization with Rcplex
48
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
49
sense=dir, control=list(trace=0))
50
51
# Solve optimization with PortfolioAnalytics
52
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
53
weights <- as.numeric(extractWeights(opt.pa))
54
55
test_that("Long Only: PortfolioAnalytics and Rcplex solution weights are equal", {
56
expect_that(weights, equals(opt.rcplex$xopt))
57
})
58
59
test_that("Long Only: PortfolioAnalytics bounds are respected", {
60
expect_that(all(weights >= lb) & all(weights <= ub), is_true())
61
})
62
63
test_that("Long Only: Rcplex bounds are respected", {
64
expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())
65
})
66
67
test_that("Long Only: PortfolioAnalytics and Rcplex solution objective values are equal", {
68
expect_that(opt.pa$out, equals(opt.rcplex$obj))
69
})
70
71
##### Box #####
72
# Upper and lower bounds (i.e. box constraints)
73
# Rcplex bounds
74
lb <- rep(0.05, m)
75
ub <- rep(0.55, m)
76
77
# Update box constraints in portfolio
78
portf$constraints[[2]]$min <- lb
79
portf$constraints[[2]]$max <- ub
80
81
# Solve optimization with Rcplex
82
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
83
sense=dir, control=list(trace=0))
84
85
# Solve optimization with PortfolioAnalytics
86
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
87
weights <- as.numeric(extractWeights(opt.pa))
88
89
test_that("Box: PortfolioAnalytics and Rcplex solution weights are equal", {
90
expect_that(weights, equals(opt.rcplex$xopt))
91
})
92
93
test_that("Box: PortfolioAnalytics bounds are respected", {
94
expect_that(all(weights >= lb) & all(weights <= ub), is_true())
95
})
96
97
test_that("Box: Rcplex bounds are respected", {
98
expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())
99
})
100
101
test_that("Box: PortfolioAnalytics and Rcplex solution objective values are equal", {
102
expect_that(opt.pa$out, equals(opt.rcplex$obj))
103
})
104
105
##### Box with Shorting #####
106
# Upper and lower bounds (i.e. box constraints)
107
# Rcplex bounds
108
lb <- rep(-0.05, m)
109
ub <- rep(0.55, m)
110
111
# Update box constraints in portfolio
112
portf$constraints[[2]]$min <- lb
113
portf$constraints[[2]]$max <- ub
114
115
# Solve optimization with Rcplex
116
opt.rcplex <- Rcplex(cvec=objL, Amat=Amat, bvec=rhs, Qmat=objQ, lb=lb, ub=ub,
117
sense=dir, control=list(trace=0))
118
119
# Solve optimization with PortfolioAnalytics
120
opt.pa <- optimize.portfolio(R, portf, optimize_method="cplex")
121
weights <- as.numeric(extractWeights(opt.pa))
122
123
test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution weights are equal", {
124
expect_that(weights, equals(opt.rcplex$xopt))
125
})
126
127
test_that("Box with Shorting: PortfolioAnalytics bounds are respected", {
128
expect_that(all(weights >= lb) & all(weights <= ub), is_true())
129
})
130
131
test_that("Box with Shorting: Rcplex bounds are respected", {
132
expect_that(all(opt.rcplex$xopt >= lb) & all(opt.rcplex$xopt <= ub), is_true())
133
})
134
135
test_that("Box with Shorting: PortfolioAnalytics and Rcplex solution objective values are equal", {
136
expect_that(opt.pa$out, equals(opt.rcplex$obj))
137
})
138
139
Rcplex.close()
140
141
142