Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/inst/tests/test_roi_gmv_toc.R
1433 views
1
2
library(testthat)
3
library(ROI)
4
library(ROI.plugin.quadprog)
5
library(quadprog)
6
library(corpcor)
7
library(PerformanceAnalytics)
8
9
data(edhec)
10
R <- edhec[, 1:5]
11
m <- ncol(R)
12
13
constraints <- list()
14
constraints$min_sum <- 1
15
constraints$max_sum <- 1
16
constraints$min <- rep(0, m)
17
constraints$max <- rep(1, m)
18
constraints$turnover_target <- 5
19
20
moments <- list()
21
moments$mean <- colMeans(R)
22
23
lambda <- 1
24
target <- NA
25
26
# Modify the returns matrix. This is done because there are 3 sets of
27
# variables 1) w.initial, 2) w.buy, and 3) w.sell
28
R0 <- matrix(0, ncol=ncol(R), nrow=nrow(R))
29
returns <- cbind(R, R0, R0)
30
V <- cov(returns)
31
32
# number of assets
33
N <- ncol(R)
34
35
# initial weights for solver
36
init_weights <- rep(1/ N, N)
37
38
# check for a target return constraint
39
if(!is.na(target)) {
40
# If var is the only objective specified, then moments$mean won't be calculated
41
if(all(moments$mean==0)){
42
tmp_means <- colMeans(R)
43
} else {
44
tmp_means <- moments$mean
45
}
46
} else {
47
tmp_means <- rep(0, N)
48
target <- 0
49
}
50
Amat <- c(tmp_means, rep(0, 2*N))
51
dir <- "=="
52
rhs <- target
53
meq <- N + 1
54
55
# Amat for initial weights
56
# Amat <- cbind(diag(N), matrix(0, nrow=N, ncol=N*2))
57
Amat <- rbind(Amat, cbind(diag(N), -1*diag(N), diag(N)))
58
rhs <- c(rhs, init_weights)
59
dir <- c(dir, rep("==", N))
60
61
# Amat for turnover constraints
62
Amat <- rbind(Amat, c(rep(0, N), rep(-1, N), rep(-1, N)))
63
rhs <- c(rhs, -constraints$turnover_target)
64
dir <- c(dir, ">=")
65
66
# Amat for positive weights
67
Amat <- rbind(Amat, cbind(matrix(0, nrow=N, ncol=N), diag(N), matrix(0, nrow=N, ncol=N)))
68
rhs <- c(rhs, rep(0, N))
69
dir <- c(dir, rep(">=", N))
70
71
# Amat for negative weights
72
Amat <- rbind(Amat, cbind(matrix(0, nrow=N, ncol=2*N), diag(N)))
73
rhs <- c(rhs, rep(0, N))
74
dir <- c(dir, rep(">=", N))
75
76
# Amat for full investment constraint
77
Amat <- rbind(Amat, rbind(c(rep(1, N), rep(0,2*N)), c(rep(-1, N), rep(0,2*N))))
78
rhs <- c(rhs, constraints$min_sum, -constraints$max_sum)
79
dir <- c(dir, ">=", ">=")
80
81
# Amat for lower box constraints
82
Amat <- rbind(Amat, cbind(diag(N), diag(0, N), diag(0, N)))
83
rhs <- c(rhs, constraints$min)
84
dir <- c(dir, rep(">=", N))
85
86
# Amat for upper box constraints
87
Amat <- rbind(Amat, cbind(-diag(N), diag(0, N), diag(0, N)))
88
rhs <- c(rhs, -constraints$max)
89
dir <- c(dir, rep(">=", N))
90
91
d <- rep(tmp_means, 3)
92
93
Amat <- Amat[!is.infinite(rhs), ]
94
rhs <- rhs[!is.infinite(rhs)]
95
96
result <- solve.QP(Dmat=make.positive.definite(2*lambda*V),
97
dvec=d, Amat=t(Amat), bvec=rhs, meq=meq)
98
result
99
wts <- result$solution
100
wts.final <- wts[(1:N)]
101
102
##### ROI #####
103
ROI_objective <- Q_objective(Q=make.positive.definite(2*lambda*V),
104
L=rep(-tmp_means, 3))
105
106
opt.prob <- OP(objective=ROI_objective,
107
constraints=L_constraint(L=Amat, dir=dir, rhs=rhs))
108
109
roi.result <- ROI_solve(x=opt.prob, solver="quadprog")
110
print.default(roi.result)
111
weights <- result$solution[(1:N)]
112
113
context("Test solve.QP and ROI_solve for gmv with turnover constraint")
114
115
test_that("Objective values are equal", {
116
expect_equal(roi.result$objval, result$value)
117
})
118
119
test_that("Solutions (optimal weights) are equal", {
120
expect_equal(roi.result$solution[1:m], result$solution[1:m])
121
})
122
123
124