Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/leverageQP.R
1433 views
1
2
# leverage constrained minimum variance QP
3
4
library(PortfolioAnalytics)
5
library(corpcor)
6
library(quadprog)
7
8
data(edhec)
9
R <- edhec[, 1:4]
10
11
N <- ncol(R)
12
leverage <- 1.6
13
min_sum <- 0.99
14
max_sum <- 1.01
15
min_box <- rep(-0.3, N)
16
max_box <- rep(1, N)
17
lambda <- 1
18
19
R0 <- matrix(0, ncol=ncol(R), nrow=nrow(R))
20
returns <- cbind(R, R0, R0)
21
V <- corpcor::make.positive.definite(cov(returns))
22
23
# separate the weights into w, w+, and w-
24
# w - w+ + w- = 0
25
Amat <- cbind(diag(N), -diag(N), diag(N))
26
rhs <- rep(0, N)
27
dir <- rep("==", N)
28
29
# leverage constraint
30
# w+ + w- <= leverage
31
Amat <- rbind(Amat, c(rep(0, N), rep(-1, N), rep(-1, N)))
32
rhs <- c(rhs, -leverage)
33
dir <- c(dir, ">=")
34
35
# w+ >= 0
36
Amat <- rbind(Amat, cbind(diag(0, N), diag(N), diag(0, N)))
37
rhs <- c(rhs, rep(0, N))
38
dir <- c(dir, rep(">=", N))
39
40
# w- >= 0
41
Amat <- rbind(Amat, cbind(diag(0, N), diag(0, N), diag(N)))
42
rhs <- c(rhs, rep(0, N))
43
dir <- c(dir, rep(">=", N))
44
45
# w^T 1 >= min_sum
46
Amat <- rbind(Amat, c(rep(1, N), rep(0, N), rep(0, N)))
47
rhs <- c(rhs, min_sum)
48
dir <- c(dir, ">=")
49
50
# w^T 1 <= max_sum
51
Amat <- rbind(Amat, c(rep(-1, N), rep(0, N), rep(0, N)))
52
rhs <- c(rhs, -max_sum)
53
dir <- c(dir, ">=")
54
55
# lower box constraints
56
Amat <- rbind(Amat, cbind(diag(N), diag(0, N), diag(0, N)))
57
rhs <- c(rhs, min_box)
58
dir <- c(dir, rep(">=", N))
59
60
# upper box constraints
61
Amat <- rbind(Amat, cbind(-diag(N), diag(0, N), diag(0, N)))
62
rhs <- c(rhs, -max_box)
63
dir <- c(dir, rep(">=", N))
64
65
sol <- solve.QP(Dmat=V, dvec=rep(0, 3*N), Amat=t(Amat), bvec=rhs, meq=N)
66
sol
67
68
weights <- sol$solution[1:N]
69
round(weights, 4)
70
sum(weights)
71
sum(abs(weights)) <= leverage
72
73
##### ROI #####
74
ROI_objective <- Q_objective(Q=make.positive.definite(2*lambda*V),
75
L=rep(0, N*3))
76
77
opt.prob <- OP(objective=ROI_objective,
78
constraints=L_constraint(L=Amat, dir=dir, rhs=rhs))
79
80
roi.result <- ROI_solve(x=opt.prob, solver="quadprog")
81
wts <- roi.result$solution[1:N]
82
round(wts, 4)
83
sum(wts)
84
sum(abs(wts)) <= leverage
85
86
# The quadprog and ROI solution should result in the same solution using the
87
# same Amat, dir, and rhs objects
88
all.equal(weights, wts)
89
90
# Load the package and data
91
# funds <- colnames(R)
92
93
# Construct initial portfolio with basic constraints.
94
# init.portf <- portfolio.spec(assets=funds)
95
# init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
96
# init.portf <- add.constraint(portfolio=init.portf, type="box", min=-0.3, max=1)
97
# init.portf <- add.constraint(portfolio=init.portf, type="leverage_exposure", leverage=1.6)
98
# init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
99
#
100
# opt <- optimize.portfolio(R, init.portf, optimize_method="ROI")
101
# round(opt$weights, 4)
102
103