Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_cvxrPortfolioAnalytics.R
1433 views
1
## Running Times for the following code with an Intel(R)
2
## Core(TM) i7-10750H Processor. Unless listed as follows,
3
## sections take less than 10 seconds:
4
## Section 9.1: 3 min and 40 seconds
5
## Section 9.2: 4 min and 50 seconds
6
## Section 11.1: 1 min and 10 seconds
7
## Section 12: 1 min and 16 seconds
8
9
10
## Section 2
11
12
library(PortfolioAnalytics)
13
library(CVXR)
14
library(data.table)
15
library(xts)
16
library(PCRA)
17
18
19
## SECTION 2.3
20
21
data(edhec)
22
class(edhec)
23
ret_edhec <- tail(edhec, 60) # Extract the last 5 years
24
range(index(edhec)) # Start and end dates of `edhec`
25
range(index(ret_edhec)) # Start and end dates of ret_edhec
26
# names(edhec) # Names of `edhec` long, so use shorter names
27
colnames(ret_edhec) <- c("CA", "CTAG", "DS", "EM", "EMN", "ED", "FIA", "GM", "LSE", "MA", "RV", "SS", "FF")
28
print(head(ret_edhec, 5))
29
30
tsPlotMP(ret_edhec, layout = c(2, 7))
31
32
33
## SECTION 3.1
34
35
# Create portfolio object
36
fund_edhec <- colnames(ret_edhec)
37
pspec_maxret <- portfolio.spec(assets = fund_edhec)
38
# Add constraints to the portfolio object
39
pspec_maxret <- add.constraint(pspec_maxret, type = "full_investment")
40
pspec_maxret <- add.constraint(portfolio = pspec_maxret, type = "box",
41
min = rep(0.02, 13),
42
max = c(rep(0.15, 8), rep(0.1, 5)))
43
# Add objective to the portfolio object
44
pspec_maxret <- add.objective(portfolio = pspec_maxret,
45
type = "return", name = "mean")
46
pspec_maxret
47
48
49
## SECTION 3.2
50
51
# Run the optimization with default solver
52
opt_maxret <- optimize.portfolio(R = ret_edhec, portfolio = pspec_maxret, optimize_method = "CVXR")
53
opt_maxret
54
opt_maxret$solver
55
56
# Run the optimization with a different solver
57
opt_maxret_glpk <- optimize.portfolio(R = ret_edhec, portfolio = pspec_maxret, optimize_method = c("CVXR", "GLPK"))
58
opt_maxret_glpk$solver
59
60
class(opt_maxret)
61
62
names(opt_maxret)
63
64
opt_maxret$weights
65
66
opt.outputMvo(opt_maxret, ret_edhec, digits =3)
67
68
69
## SECTION 3.3
70
71
bt_maxret <- optimize.portfolio.rebalancing(R = ret_edhec, portfolio = pspec_maxret,
72
optimize_method = "CVXR",
73
rebalance_on = "quarters", training_period = 36)
74
75
names(bt_maxret)
76
77
names(bt_maxret$opt_rebalancing)
78
79
80
## SECTION 4.1
81
82
# Create portfolio object
83
pspec_gmv <- portfolio.spec(assets = fund_edhec)
84
# Add full-investment constraint
85
pspec_gmv <- add.constraint(pspec_gmv, type = "full_investment")
86
# Add objective of minimizing variance
87
pspec_gmv <- add.objective(portfolio = pspec_gmv, type = "risk", name = "var")
88
89
opt_gmv <- optimize.portfolio(ret_edhec, pspec_gmv, optimize_method = "CVXR")
90
opt.outputMvo(opt_gmv, ret_edhec, digits =3)
91
92
93
## SECTION 4.2
94
95
# portfolio object
96
pspec_gmv <- add.constraint(pspec_gmv, type = "long_only")
97
pspec_gmvGroup <- add.constraint(pspec_gmv, type = "group",
98
groups = list(groupA=1,
99
groupB=c(2:12),
100
groupC=13),
101
group_min = c(0, 0.05, 0.05),
102
group_max = c(0.4, 0.8, 0.5))
103
pspec_gmvGroup <- add.constraint(pspec_gmvGroup, type = "return", return_target = 0.003)
104
pspec_gmvGroup
105
106
# optimization
107
opt_gmvGroup <- optimize.portfolio(ret_edhec, pspec_gmvGroup, optimize_method = "CVXR")
108
opt.outputMvo(opt_gmvGroup, ret_edhec, digits =3)
109
110
opt_gmvGroup_ecos <- optimize.portfolio(ret_edhec, pspec_gmvGroup, optimize_method = c("CVXR", "ECOS"))
111
opt.outputMvo(opt_gmvGroup_ecos, ret_edhec, digits =3)
112
113
opt_gmvGroup$solver
114
opt_gmvGroup_ecos$solver
115
116
117
## SECTION 5.1
118
119
pspec_qu <- portfolio.spec(assets = fund_edhec)
120
pspec_qu <- add.constraint(pspec_qu, type = "full_investment")
121
pspec_qu <- add.constraint(pspec_qu, type = "long_only")
122
# Add objectives
123
pspec_qu <- add.objective(portfolio = pspec_qu, type = "return", name = "mean")
124
pspec_qu <- add.objective(portfolio = pspec_qu, type = "risk", name = "var",
125
risk_aversion = 20)
126
127
128
## SECTION 5.2
129
130
opt_qu <- optimize.portfolio(ret_edhec, pspec_qu, optimize_method = "CVXR")
131
opt.outputMvo(opt_qu, ret_edhec, digits =3)
132
133
134
## SECTION 6.1
135
136
pspec_es <- portfolio.spec(assets = fund_edhec)
137
pspec_es <- add.constraint(pspec_es, type = "full_investment")
138
pspec_es <- add.constraint(pspec_es, type = "long_only")
139
# Add objective of minimizing ES by using the default gamma
140
pspec_gmes <- add.objective(portfolio = pspec_es, type = "risk", name = "ES") # Uses default tail probability 0.05
141
# Add objective of minimizing ES by using the specific gamma=0.1
142
pspec_gmes_1 <- add.objective(portfolio = pspec_es, type = "risk", name = "ES", arguments = list(p=0.1))
143
144
145
## SECTION 6.2
146
147
# GMES with default gamma=0.05
148
opt_gmes <- optimize.portfolio(ret_edhec, pspec_gmes, optimize_method = "CVXR")
149
opt_gmes
150
# GMES with specific gamma=0.1
151
opt_gmes_1 <- optimize.portfolio(ret_edhec, pspec_gmes_1, optimize_method = "CVXR")
152
opt_gmes_1
153
154
155
## SECTION 7.1
156
157
pspec_csm <- portfolio.spec(assets = fund_edhec)
158
pspec_csm <- add.constraint(pspec_csm, type = "full_investment")
159
pspec_csm <- add.constraint(pspec_csm, type = "long_only")
160
# Add objective of minimizing CSM
161
pspec_mcsm <- add.objective(portfolio = pspec_csm, type = "risk", name = "CSM",
162
arguments = list(p=0.05))
163
164
165
## SECTION 7.2
166
167
opt_mcsm <- optimize.portfolio(ret_edhec, pspec_mcsm, optimize_method = "CVXR")
168
opt_mcsm
169
170
171
## SECTION 8.1
172
173
# Create portfolio object
174
pspec_sr <- portfolio.spec(assets = fund_edhec)
175
## Add constraints of maximizing Sharpe Ratio
176
pspec_sr <- add.constraint(pspec_sr, type = "full_investment")
177
pspec_sr <- add.constraint(pspec_sr, type = "long_only")
178
## Add objectives of maximizing Sharpe Ratio
179
pspec_sr <- add.objective(pspec_sr, type = "return", name = "mean")
180
pspec_sr <- add.objective(pspec_sr, type = "risk", name = "var")
181
182
# Optimization
183
optimize.portfolio(ret_edhec, pspec_sr, optimize_method = "CVXR", maxSR = TRUE)
184
185
186
## SECTION 8.2
187
188
# Create portfolio object
189
pspec_ESratio <- portfolio.spec(assets = fund_edhec)
190
## Add constraints of maximizing return per unit ES
191
pspec_ESratio <- add.constraint(pspec_ESratio, type = "full_investment")
192
pspec_ESratio <- add.constraint(pspec_ESratio, type = "long_only")
193
## Add objectives of maximizing return per unit ES
194
pspec_ESratio <- add.objective(pspec_ESratio, type = "return", name = "mean")
195
pspec_ESratio <- add.objective(pspec_ESratio, type = "risk", name = "ES", arguments = list(p=0.05))
196
197
# Optimization
198
optimize.portfolio(ret_edhec, pspec_ESratio, optimize_method = "CVXR", ESratio = TRUE)
199
200
201
## SECTION 8.3
202
203
# Create portfolio object
204
pspec_CSMratio <- portfolio.spec(assets = fund_edhec)
205
## Add constraints of maximizing return per unit CSM
206
pspec_CSMratio <- add.constraint(pspec_CSMratio, type = "full_investment")
207
pspec_CSMratio <- add.constraint(pspec_CSMratio, type = "long_only")
208
## Add objectives of maximizing return per unit CSM
209
pspec_CSMratio <- add.objective(pspec_CSMratio, type = "return", name = "mean")
210
pspec_CSMratio <- add.objective(pspec_CSMratio, type = "risk", name = "CSM",
211
arguments = list(p=0.05))
212
213
# Optimization
214
optimize.portfolio(ret_edhec, pspec_CSMratio, optimize_method = "CVXR", CSMratio = TRUE)
215
216
217
## Section 9
218
219
# Get daily returns of the 30 smallcap stocks
220
library(PCRA)
221
library(PortfolioAnalytics)
222
library(xts)
223
stocksCRSPdaily <- getPCRAData(dataset = "stocksCRSPdaily")
224
225
smallcapTS <- selectCRSPandSPGMI(
226
periodicity = "daily",
227
stockItems = c("Date", "TickerLast", "CapGroupLast", "Return"),
228
factorItems = NULL,
229
subsetType = "CapGroupLast",
230
subsetValues = "SmallCap",
231
outputType = "xts")
232
233
# find top 30 small cap stocks based on the market capitalization
234
smallcapDT <- factorsSPGMI[CapGroupLast == "SmallCap"]
235
scSize <- smallcapDT[, mean(LogMktCap), by = "TickerLast"]
236
names(scSize)[2] <- "Size"
237
scSize <- scSize[order(scSize$Size, decreasing = TRUE),]
238
sc30largest <- scSize[,TickerLast][1:30]
239
240
# daily return of top 30 stocks
241
retD_CRSP <- smallcapTS[ , sc30largest]
242
243
names(retD_CRSP)
244
245
# monthly return of top 30 stocks needed for monthly rebalancing
246
ep <- endpoints(retD_CRSP, on= "months", k=1)
247
prod1 <- function(x){apply(x+1, 2, prod)}
248
retM_CRSP <- period.apply(retD_CRSP, INDEX = ep, FUN = prod1) - 1
249
250
251
## SECTION 9.1
252
253
# Generate GMV, GMES and GMCSM portfolios
254
pspec_sc <- portfolio.spec(assets = sc30largest)
255
pspec_sc <- add.constraint(pspec_sc, type = "full_investment")
256
pspec_sc <- add.constraint(pspec_sc, type = "long_only")
257
258
pspec_GMV <- add.objective(pspec_sc, type = "risk", name = "var")
259
pspec_GMES <- add.objective(pspec_sc, type = "risk", name = "ES")
260
pspec_GMCSM <- add.objective(pspec_sc, type = "risk", name = "CSM")
261
262
# Optimize Portfolio at Monthly Rebalancing and 500-Day Training
263
bt.GMV <- optimize.portfolio.rebalancing(retD_CRSP, pspec_GMV,
264
optimize_method = "CVXR",
265
rebalance_on = "months",
266
rolling_window = 500)
267
bt.ES <- optimize.portfolio.rebalancing(retD_CRSP, pspec_GMES,
268
optimize_method = "CVXR",
269
rebalance_on = "months",
270
rolling_window = 500)
271
bt.CSM <- optimize.portfolio.rebalancing(retD_CRSP, pspec_GMCSM,
272
optimize_method = "CVXR",
273
rebalance_on = "months",
274
rolling_window = 500)
275
276
# Extract time series of portfolio weights
277
wts.GMV <- extractWeights(bt.GMV)
278
wts.GMV <- wts.GMV[complete.cases(wts.GMV),]
279
280
wts.ES <- extractWeights(bt.ES)
281
wts.ES <- wts.ES[complete.cases(wts.ES),]
282
283
wts.CSM <- extractWeights(bt.CSM)
284
wts.CSM <- wts.CSM[complete.cases(wts.CSM),]
285
286
# Compute cumulative returns of three portfolios
287
GMV <- Return.rebalancing(retM_CRSP, wts.GMV)
288
ES <- Return.rebalancing(retM_CRSP, wts.ES)
289
CSM <- Return.rebalancing(retM_CRSP, wts.CSM)
290
291
# Combine GMV, ES and CSM portfolio cumulative returns
292
ret.comb <- na.omit(merge(GMV, ES, CSM, all=F))
293
names(ret.comb) <- c("GMV", "GMES", "GMCSM")
294
295
backtest.plot(ret.comb, colorSet = c("black", "darkblue", "darkgreen"), ltySet = c(3, 2, 1))
296
297
298
## SECTION 9.2
299
300
# Generate Sr, ESr and CSMr portfolios
301
pspec_sc_ratio <- add.objective(pspec_sc, type = "return", name = "mean")
302
pspec_Sr <- add.objective(pspec_sc_ratio, type = "risk", name = "var")
303
pspec_ESr <- add.objective(pspec_sc_ratio, type = "risk", name = "ES")
304
pspec_CSMr <- add.objective(pspec_sc_ratio, type = "risk", name = "CSM")
305
306
# Optimize Portfolio at Monthly Rebalancing and 500-Day Training
307
bt.Sr <- optimize.portfolio.rebalancing(retD_CRSP, pspec_Sr, maxSR = TRUE,
308
optimize_method = "CVXR",
309
rebalance_on = "months",
310
rolling_window = 500)
311
bt.ESr <- optimize.portfolio.rebalancing(retD_CRSP, pspec_ESr,
312
optimize_method = "CVXR",
313
rebalance_on = "months",
314
rolling_window = 500)
315
bt.CSMr <- optimize.portfolio.rebalancing(retD_CRSP, pspec_CSMr,
316
optimize_method = "CVXR",
317
rebalance_on = "months",
318
rolling_window = 500)
319
320
# Extract time series of portfolio weights
321
wts.Sr <- extractWeights(bt.Sr)
322
wts.Sr <- wts.Sr[complete.cases(wts.Sr),]
323
324
wts.ESr <- extractWeights(bt.ESr)
325
wts.ESr <- wts.ESr[complete.cases(wts.ESr),]
326
327
wts.CSMr <- extractWeights(bt.CSMr)
328
wts.CSMr <- wts.CSMr[complete.cases(wts.CSMr),]
329
330
# Compute cumulative returns of three portfolios
331
Sr <- Return.rebalancing(retM_CRSP, wts.Sr, rebalance_on = "months")
332
ESr <- Return.rebalancing(retM_CRSP, wts.ESr, rebalance_on = "months")
333
CSMr <- Return.rebalancing(retM_CRSP, wts.CSMr, rebalance_on = "months")
334
335
# Combine Sr, ESr and CSMr portfolio cumulative returns
336
ret.comb.ratios <- na.omit(merge(Sr, ESr, CSMr, all=F))
337
names(ret.comb.ratios) <- c("Sharpe ratio", "ES ratio", "CSM ratio")
338
339
backtest.plot(ret.comb.ratios, colorSet = c("black", "darkblue", "darkgreen"), ltySet = c(3, 2, 1))
340
341
342
## SECTION 10
343
344
# monthly return of top 30 stocks in last 5 years
345
ep <- endpoints(retD_CRSP, on= "months", k=1)
346
prod1 <- function(x){apply(x+1, 2, prod)}
347
retM_CRSP <- period.apply(retD_CRSP, INDEX = ep, FUN = prod1) - 1
348
retM_CRSP_5 <- tail(retM_CRSP, 60)
349
350
tsPlotMP(retM_CRSP_5, layout = c(2,15), yname = "RETURNS",
351
stripText.cex = 0.7, axis.cex = 0.7)
352
353
354
## SECTION 10.1
355
356
# mean-var efficient frontier
357
pspec_sc <- portfolio.spec(names(retM_CRSP_5))
358
pspec_sc <- add.constraint(pspec_sc, type = "full_investment")
359
pspec_sc <- add.constraint(pspec_sc, type = "long_only")
360
361
meanvar.ef <- create.EfficientFrontier(R = retM_CRSP_5,
362
portfolio = pspec_sc, type = "mean-StdDev")
363
364
chart.EfficientFrontier(meanvar.ef, match.col = "StdDev", type = "l",
365
chart.assets = FALSE, main = NULL,
366
RAR.text = "Max Sharpe ratio", pch = 1)
367
368
meanvar.ef$frontier[, 1:2]
369
370
sr <- meanvar.ef$frontier[, 1]/meanvar.ef$frontier[, 2]
371
maximumSR <- max(sr)
372
meanMaxSR <- meanvar.ef$frontier[, 1][sr == max(sr)]
373
stdevMaxSR <- meanvar.ef$frontier[, 2][sr == max(sr)]
374
dat <- (round(c(maximumSR, meanMaxSR, stdevMaxSR), 3))
375
dat <- data.frame(dat)
376
names(dat) <- NULL
377
row.names(dat) <- c("maximum SR", "maxSRport Mean", "maxSRport Stdev")
378
dat
379
380
# Mean-StdDev Efficient Frontier
381
pspec_MV <- add.objective(pspec_sc, type = "risk", name = "var")
382
pspec_MV <- add.objective(portfolio = pspec_MV, type = "return", name = "mean")
383
opt_MV <- optimize.portfolio(retM_CRSP_5, pspec_MV, optimize_method = "CVXR", maxSR = TRUE)
384
opt.outputMvo(opt_MV, retM_CRSP_5, annualize = FALSE, digits = 3)
385
386
387
pspec_sc_init <- portfolio.spec(assets = sc30largest)
388
pspec_sc_init <- add.constraint(pspec_sc_init, type = "full_investment")
389
390
# Portfolio with long-only constraints
391
pspec_sc_lo <- add.constraint(portfolio = pspec_sc_init, type = "long_only")
392
393
# Portfolio with long-only box constraints
394
pspec_sc_lobox <- add.constraint(portfolio = pspec_sc_init, type = "box",
395
min = 0.02, max = 0.1)
396
397
# Portfolio with long-short box constraints
398
pspec_sc_lsbox <- add.constraint(portfolio = pspec_sc_init, type = "box",
399
min = -0.1, max = 0.1)
400
401
# Combine the portfolios into a list
402
portf_list <- combine.portfolios(list(pspec_sc_lo, pspec_sc_lobox, pspec_sc_lsbox))
403
404
# Plot the efficient frontier overlay of the portfolios with varying constraints
405
legend_labels <- c("Long Only", "Long Only Box (0.02,0.1)", "Long Short Box (-0.01,0.1)")
406
chart.EfficientFrontierOverlay(R = retM_CRSP_5, portfolio_list = portf_list,
407
type = "mean-StdDev", match.col = "StdDev",
408
legend.loc = "bottomright", chart.assets = FALSE,
409
legend.labels = legend_labels, cex.legend = 1,
410
labels.assets = FALSE, lwd = c(3,3,3),
411
col = c("black", "dark red", "dark green"),
412
main = NULL,
413
xlim = c(0.03, 0.11), ylim = c(0.005, 0.035))
414
415
416
## SECTION 10.2
417
418
# Mean-ES Efficient Frontier
419
meanes.ef <- create.EfficientFrontier(R = retM_CRSP_5, portfolio = pspec_sc, type = "mean-ES")
420
chart.EfficientFrontier(meanes.ef, match.col = "ES", type = "l",
421
chart.assets = FALSE, main = NULL,
422
RAR.text = "Max ES ratio", pch = 1)
423
424
legend_labels <- c("Long Only ES (p=0.05)",
425
"Long Only Box ES (p=0.05)", "Long Short Box ES (p=0.05)")
426
chart.EfficientFrontierOverlay(R = retM_CRSP_5, portfolio_list = portf_list,
427
type = "mean-ES", match.col = "ES",
428
legend.loc = "bottomright", chart.assets = FALSE,
429
legend.labels = legend_labels, cex.legend = 1,
430
labels.assets = FALSE, lwd = c(3,3,3),
431
col = c("black", "dark red", "dark green"),
432
main = NULL,
433
xlim = c(0.03, 0.17), ylim = c(0.005, 0.035))
434
435
# Create long-only ES portfolios with different tail probabilities
436
ES_05 <- add.objective(portfolio = pspec_sc_lo, type = "risk", name = "ES",
437
arguments = list(p=0.05))
438
439
ES_10 <- add.objective(portfolio = pspec_sc_lo, type = "risk", name = "ES",
440
arguments = list(p=0.1))
441
442
ES_15 <- add.objective(portfolio = pspec_sc_lo, type = "risk", name = "ES",
443
arguments = list(p=0.15))
444
445
# Combine the portfolios into a list
446
portf_ES_list <- combine.portfolios(list(ES_05, ES_10, ES_15))
447
448
# Plot the efficient frontier overlay of the portfolios with varying tail probabilities
449
legend_ES_labels <- c("ES (p=0.05)", "ES (p=0.1)", "ES (p=0.15)")
450
chart.EfficientFrontierOverlay(R = retM_CRSP_5, portfolio_list = portf_ES_list,
451
type = "mean-ES", match.col = "ES",
452
legend.loc = "bottomright", chart.assets = FALSE,
453
legend.labels = legend_ES_labels, cex.legend = 1,
454
labels.assets = FALSE, lwd = c(3,3,3),
455
col = c("black", "dark red", "dark green"),
456
main = NULL,
457
xlim = c(0.035, 0.165), ylim = c(0.005, 0.03))
458
459
460
## SECTION 10.3
461
462
# Mean-CSM Efficient Frontier
463
meancsm.ef <- create.EfficientFrontier(R = retM_CRSP_5, portfolio = pspec_sc,
464
type = "mean-CSM")
465
chart.EfficientFrontier(meancsm.ef, match.col = "CSM", type = "l",
466
chart.assets = FALSE, main = NULL,
467
RAR.text = "Max CSM ratio", pch = 1)
468
469
470
## SECTION 11.1
471
472
args(chart.EfficientFrontierCompare)
473
474
# Compare StdDev of minStd and minES portfolios with guideline
475
chart.EfficientFrontierCompare(R = retM_CRSP_5, portfolio = pspec_sc, risk_type = "StdDev", match.col = c("StdDev", "ES"), lwd = c(2, 2), xlim = c(0.0,0.14), ylim = c(0.005,0.032))
476
477
# Compare ES of minStd and minES portfolios with guideline
478
chart.EfficientFrontierCompare(R = retM_CRSP_5, portfolio = pspec_sc,
479
risk_type = "ES", match.col = c("ES", "StdDev"), lwd = c(2, 2),
480
xlim = c(0.0,0.14), ylim = c(0.005,0.032))
481
482
# Compare ES of minStd, minES and minCSM portfolios without guideline
483
chart.EfficientFrontierCompare(R = retM_CRSP_5, portfolio = pspec_sc,
484
risk_type = "ES",match.col = c("StdDev", "ES", "CSM"),
485
guideline = FALSE, col = c("darkred","darkgreen","blue"),
486
lty = c("dotted", "solid", "dashed"), lwd = c(1, 1.5, 1.5))
487
488
489
## SECTION 11.2
490
491
minStdDev_port <- add.objective(pspec_sc, type = "risk", name = "StdDev")
492
minStdDev_opt <- optimize.portfolio(retM_CRSP_5, minStdDev_port,
493
optimize_method = "CVXR")
494
minStdDev_w <- minStdDev_opt$weight
495
496
minES_port <- add.objective(pspec_sc, type = "risk", name = "ES")
497
minES_opt <- optimize.portfolio(retM_CRSP_5, minES_port,
498
optimize_method = "CVXR")
499
minES_w <- minES_opt$weight
500
501
# Extract risk StdDev portfolio
502
extract_risk(retM_CRSP_5, minStdDev_w)
503
504
# Extract risk ES portfolio
505
extract_risk(retM_CRSP_5, minES_w)
506
507
508
minStdDev_opt_covRob <-optimize.portfolio(retM_CRSP_5,minStdDev_port,
509
optimize_method =
510
"CVXR", momentFUN = 'custom.covRob.Mcd')
511
extract_risk(retM_CRSP_5, minStdDev_w, moment_setting =
512
minStdDev_opt_covRob$moment_values)
513
514
515
## SECTION 12
516
517
# Compare mean-var frontiers with classic and robust
518
# covariance matrix estimators.
519
sampleCov = meanvar.efficient.frontier(pspec_sc, retM_CRSP_5, optimize_method = 'CVXR')
520
robustCov = meanvar.efficient.frontier(pspec_sc, retM_CRSP_5, optimize_method = 'CVXR', momentFUN = 'custom.covRob.TSGS')
521
plotFrontiers(retM_CRSP_5, frontiers=list(sampleCov, robustCov), risk='StdDev')
522
523
524