Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/testing_ROI_Martin.R
1433 views
1
# Testing for replicating professor Martin's examples
2
# The numbered examples corresopond to 1. theory review weights constrained mvo v5.pdf
3
4
# data = crsp.short.Rdata
5
# returns = midcap.ts[, 1:10]
6
7
rm(list=ls())
8
9
# Load packages
10
library(PortfolioAnalytics)
11
library(ROI)
12
library(ROI.plugin.glpk)
13
library(ROI.plugin.quadprog)
14
15
# Use edhec data set from PerformanceAnalytics for reproducing if user does not
16
# have the crsp.short.Rdata data
17
# data(edhec)
18
# returns <- edhec[, 1:10]
19
20
# Use crsp.short.Rdata from Prof Martin
21
# data file should be in working directory or specify path
22
# Can we include this as a data set in the PortfolioAnalytics package?
23
load("/Users/rossbennett/Desktop/Testing/crsp.short.Rdata")
24
25
returns <- midcap.ts[, 1:10]
26
funds <- colnames(returns)
27
28
29
##### Example 1.1: Global Minimum Variance (GMV) Portfolio #####
30
31
# GMV portfolio using new interface
32
pspec <- portfolio.spec(assets=funds)
33
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
34
pspec <- add.constraint(portfolio=pspec, type="box", min=-Inf, max=Inf, enabled=TRUE)
35
pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
36
37
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
38
39
# Optimal weights
40
round(opt$weights, 3)
41
42
# Portfolio standard deviation
43
sqrt(opt$out)
44
45
##### Example 1.2: Long Only GMV Portfolio #####
46
47
# GMV long only portfolio using new interface
48
pspec <- portfolio.spec(assets=funds)
49
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
50
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
51
pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
52
53
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
54
55
# Optimal weights
56
round(opt$weights, 3)
57
58
# Portfolio standard deviation
59
sqrt(opt$out)
60
61
##### Example 1.3: GMV Box Constraints #####
62
63
# GMV box constraints portfolio using new interface
64
pspec <- portfolio.spec(assets=funds)
65
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
66
pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
67
pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
68
69
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
70
71
# Optimal weights
72
round(opt$weights, 3)
73
74
# Portfolio standard deviation
75
sqrt(opt$out)
76
77
##### Example 1.4: GMV long only with Group Constraints #####
78
# Combine returns from different market cap groups
79
returns.cap <- cbind(microcap.ts[, 1:2],
80
smallcap.ts[, 1:2],
81
midcap.ts[, 1:2],
82
largecap.ts[, 1:2])
83
84
funds.cap <- colnames(returns.cap)
85
86
# GMV group constraints portfolio using new interface
87
pspec <- portfolio.spec(assets=funds.cap)
88
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
89
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
90
pspec <- add.constraint(portfolio=pspec, type="group", enabled=TRUE,
91
groups=c(2, 2, 2, 2),
92
group_min=c(0.1, 0.15, 0, 0),
93
group_max=c(0.25, .35, 0.35, 0.45),
94
group_labels=c("MICRO", "SMALL", "MID", "LARGE"))
95
pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
96
97
opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")
98
99
# Optimal weights
100
round(opt$weights, 3)
101
102
# Get the group weights
103
# This is something I will work to include in the summary.optimize.portfolio.ROI
104
groups <- pspec$constraints[[3]]$groups
105
group_labels <- pspec$constraints[[3]]$group_labels
106
n.groups <- length(groups)
107
group_weights <- rep(0, n.groups)
108
k <- 1
109
l <- 0
110
for(i in 1:n.groups){
111
j <- groups[i]
112
group_weights[i] <- sum(opt$weights[k:(l+j)])
113
k <- k + j
114
l <- k - 1
115
}
116
names(group_weights) <- group_labels
117
group_weights
118
119
# Portfolio standard deviation
120
sqrt(opt$out)
121
122
# In the previous examples, we were solving global minimum variance with optmize_method="ROI".
123
# The solve.QP plugin is selected automatically by optimize.portfolio when "var" is the objective
124
125
##### Example 1.6: Maximize mean-return with box constraints #####
126
127
# Maximize mean return with box constraints portfolio using new interface
128
pspec <- portfolio.spec(assets=funds)
129
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
130
pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
131
pspec <- add.objective(portfolio=pspec, type="return", name="mean", enabled=TRUE)
132
133
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
134
135
# Optimal weights
136
round(opt$weights, 3)
137
138
# Portfolio standard deviation
139
sqrt(opt$out)
140
141
##### Example 1.7 Maximize mean-return Long Only with Group Constraints #####
142
143
# GMV group constraints portfolio using new interface
144
pspec <- portfolio.spec(assets=funds.cap)
145
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
146
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
147
pspec <- add.constraint(portfolio=pspec, type="group", enabled=TRUE,
148
groups=c(2, 2, 2, 2),
149
group_min=c(0.1, 0.15, 0, 0),
150
group_max=c(0.25, .35, 0.35, 0.45),
151
group_labels=c("MICRO", "SMALL", "MID", "LARGE"))
152
pspec <- add.objective(portfolio=pspec, type="return", name="mean", enabled=TRUE)
153
154
opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")
155
156
# Optimal weights
157
round(opt$weights, 3)
158
159
# Get the group weights
160
# This is something I will work to include in the summary.optimize.portfolio.ROI
161
groups <- pspec$constraints[[3]]$groups
162
group_labels <- pspec$constraints[[3]]$group_labels
163
group_weights <- rep(0, n.groups)
164
n.groups <- length(groups)
165
k <- 1
166
l <- 0
167
for(i in 1:n.groups){
168
j <- groups[i]
169
group_weights[i] <- sum(opt$weights[k:(l+j)])
170
k <- k + j
171
l <- k - 1
172
}
173
names(group_weights) <- group_labels
174
group_weights
175
176
# Portfolio standard deviation
177
sqrt(opt$out)
178
179
# Check results for quadratic utility with manual code
180
p <- ncol(returns)
181
V <- var(returns)
182
mu <- colMeans(returns)
183
lambda <- 20
184
min_wt <- 0
185
max_wt <- 1
186
187
# parameters for solve.QP
188
A <- cbind(rep(1, p), diag(p), -diag(p))
189
b <- c(1, rep(min_wt, p), rep(-max_wt, p))
190
d <- mu
191
res <- quadprog:::solve.QP(Dmat=2*lambda*V, dvec=d, Amat=A, bvec=b, meq=1)
192
wts2 <- round(res$solution, 4)
193
names(wts2) <- colnames(returns)
194
wts2
195
196
# Note that target mean return CANNOT be specified as a constraint currently
197
# It is specified as a target in the return objective
198
# Can do quadratic utility optimization with target return
199
200
##### Example X: Mean Variance Optimization (MVO) with target mean return constraint #####
201
202
# MVO with target mean return
203
pspec <- portfolio.spec(assets=funds)
204
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
205
pspec <- add.constraint(portfolio=pspec, type="box", min=-Inf, max=Inf, enabled=TRUE)
206
pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
207
pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
208
209
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
210
211
# Optimal weights
212
round(opt$weights, 3)
213
214
# Portfolio return
215
t(opt$weights) %*% colMeans(returns)
216
217
218
##### Example X: Mean Variance Optimization (MVO) with target mean return and long only constraints #####
219
220
# MVO with long only and target mean return
221
pspec <- portfolio.spec(assets=funds)
222
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
223
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
224
pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
225
pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
226
227
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
228
229
# Optimal weights
230
round(opt$weights, 3)
231
232
# Portfolio return
233
t(opt$weights) %*% colMeans(returns)
234
235
##### Example X: Mean Variance Optimization (MVO) with target mean return and box constraints #####
236
237
# MVO with box constraints and target mean return
238
pspec <- portfolio.spec(assets=funds)
239
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
240
pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
241
pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
242
pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
243
244
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
245
246
# Optimal weights
247
round(opt$weights, 3)
248
249
# Portfolio return
250
t(opt$weights) %*% colMeans(returns)
251
252
##### Example X: ETL Long Only #####
253
254
pspec <- portfolio.spec(assets=funds)
255
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
256
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
257
# This can be specified with ETL, ES, or CVaR for name
258
pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
259
260
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
261
262
# Optimal weights
263
round(opt$weights, 3)
264
265
##### Example X: ETL with box constraints #####
266
267
pspec <- portfolio.spec(assets=funds)
268
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
269
pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
270
# This can be specified with ETL, ES, or CVaR for name
271
pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
272
273
opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
274
275
# Optimal weights
276
round(opt$weights, 3)
277
278
##### Example X: ETL long only with group constraints #####
279
280
# GMV group constraints portfolio using new interface
281
pspec <- portfolio.spec(assets=funds.cap)
282
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
283
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
284
pspec <- add.constraint(portfolio=pspec, type="group", enabled=TRUE,
285
groups=c(2, 2, 2, 2),
286
group_min=c(0.1, 0.15, 0, 0),
287
group_max=c(0.25, .35, 0.35, 0.45),
288
group_labels=c("MICRO", "SMALL", "MID", "LARGE"))
289
pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
290
291
opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")
292
293
# Optimal weights
294
round(opt$weights, 3)
295
296
# Get the group weights
297
# This is something I will work to include in the summary.optimize.portfolio.ROI
298
groups <- pspec$constraints[[3]]$groups
299
group_labels <- pspec$constraints[[3]]$group_labels
300
group_weights <- rep(0, n.groups)
301
n.groups <- length(groups)
302
k <- 1
303
l <- 0
304
for(i in 1:n.groups){
305
j <- groups[i]
306
group_weights[i] <- sum(opt$weights[k:(l+j)])
307
k <- k + j
308
l <- k - 1
309
}
310
names(group_weights) <- group_labels
311
group_weights
312
313
314