Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/moment.functions.R
1433 views
1
###############################################################################
2
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
3
#
4
# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
5
#
6
# This library is distributed under the terms of the GNU Public License (GPL)
7
# for full details see the file COPYING
8
#
9
# $Id$
10
#
11
###############################################################################
12
13
#' compute comoments for use by lower level optimization functions when the conditional covariance matrix is a CCC GARCH model
14
#'
15
#' it first estimates the conditional GARCH variances, then filters out the
16
#' time-varying volatility and estimates the higher order comoments on the innovations
17
#' rescaled such that their unconditional covariance matrix is the conditional covariance matrix forecast
18
#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns
19
#' @param momentargs list containing arguments to be passed down to lower level functions, default NULL
20
#' @param \dots any other passthru parameters
21
#' @export
22
CCCgarch.MM = function(R, momentargs = NULL , ... )
23
{
24
stopifnot("package:fGarch" %in% search() || requireNamespace("fGarch",quietly=TRUE))
25
if (!hasArg(momentargs) | is.null(momentargs))
26
momentargs <- list()
27
cAssets = ncol(R)
28
T = nrow(R)
29
if (!hasArg(mu)){
30
mu = apply(R, 2, "mean")
31
}else{ mu = match.call(expand.dots = TRUE)$mu }
32
R = R - matrix( rep(mu,T) , nrow = T , byrow = TRUE )
33
momentargs$mu = mu
34
S = nextS = c();
35
for( i in 1:cAssets ){
36
gout = fGarch::garchFit(formula ~ garch(1,1), data = R[,i],include.mean = F, cond.dist="QMLE", trace = FALSE )
37
if( as.vector(gout@fit$coef["alpha1"]) < 0.01 ){
38
sigmat = rep( sd( as.vector(R[,i])), length(R[,i]) ); nextSt = sd( as.vector(R[,i]))
39
}else{
40
sigmat = gout@sigma.t; nextSt = fGarch::predict(gout)[1,3]
41
}
42
S = cbind( S , sigmat); nextS = c(nextS,nextSt)
43
}
44
U = R/S; #filtered out time-varying volatility
45
if (!hasArg(clean)){
46
clean = match.call(expand.dots = TRUE)$clean
47
}else{ clean = NULL }
48
if(!is.null(clean)){
49
cleanU <- try(Return.clean(U, method = clean))
50
if (!inherits(cleanU, "try-error")) { U = cleanU }
51
}
52
Rcor = cor(U)
53
D = diag( nextS ,ncol=cAssets )
54
momentargs$sigma = D%*%Rcor%*%D
55
# set volatility of all U to last observation, such that cov(rescaled U)=sigma
56
uncS = sqrt(diag( cov(U) ))
57
U = U*matrix( rep(nextS/uncS,T ) , ncol = cAssets , byrow = T )
58
momentargs$m3 = PerformanceAnalytics::M3.MM(U)
59
momentargs$m4 = PerformanceAnalytics::M4.MM(U)
60
return(momentargs)
61
}
62
63
#' set portfolio moments for use by lower level optimization functions
64
#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns
65
#' @param constraints an object of type "constraints" specifying the constraints for the optimization, see \code{\link{constraint}}
66
#' @param momentargs list containing arguments to be passed down to lower level functions, default NULL
67
#' @param \dots any other passthru parameters
68
set.portfolio.moments_v1 <- function(R, constraints, momentargs=NULL,...){
69
70
if(!hasArg(momentargs) | is.null(momentargs)) momentargs<-list()
71
if(is.null(constraints$objectives)) {
72
warning("no objectives specified in constraints")
73
} else {
74
75
lcl <- grep('garch', constraints)
76
if (!identical(lcl, integer(0))) {
77
for (objective in constraints[lcl]) {
78
objective = unlist(objective)
79
if( is.null( objective$garch ) ) next
80
if (objective$garch){
81
if (is.null(momentargs$mu)|is.null(momentargs$sigma)|is.null(momentargs$m3)|is.null(momentargs$m4))
82
{
83
momentargs = CCCgarch.MM(R,clean=objective$arguments.clean,...)
84
}
85
}
86
}
87
}
88
89
90
lcl<-grep('clean',constraints)
91
if(!identical(lcl,integer(0))) {
92
for (objective in constraints[lcl]){
93
objective = unlist(objective)
94
#if(!is.null(objective$arguments$clean)) {
95
if (!is.null(objective$arguments.clean)){
96
if (is.null(momentargs$mu)|is.null(momentargs$sigma)|is.null(momentargs$m3)|is.null(momentargs$m4))
97
{
98
# cleanR<-try(Return.clean(R,method=objective$arguments$clean))
99
cleanR <- try(Return.clean(R, method = objective$arguments.clean,...))
100
if(!inherits(cleanR,"try-error")) {
101
momentargs$mu = matrix( as.vector(apply(cleanR,2,'mean')),ncol=1);
102
momentargs$sigma = cov(cleanR);
103
momentargs$m3 = PerformanceAnalytics::M3.MM(cleanR)
104
momentargs$m4 = PerformanceAnalytics::M4.MM(cleanR)
105
#' FIXME NOTE: this isn't perfect as it overwrites the moments for all objectives, not just one with clean='boudt'
106
}
107
}
108
}
109
}
110
}
111
for (objective in constraints$objectives){
112
switch(objective$name,
113
sd =,
114
StdDev = {
115
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1);
116
if(is.null(momentargs$sigma)) momentargs$sigma = cov(R, use='pairwise.complete.obs')
117
},
118
var =,
119
mVaR =,
120
VaR = {
121
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
122
if(is.null(momentargs$sigma)) momentargs$sigma = cov(R)
123
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
124
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
125
},
126
es =,
127
mES =,
128
CVaR =,
129
cVaR =,
130
ES = {
131
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
132
if(is.null(momentargs$sigma)) momentargs$sigma = cov(R)
133
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
134
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
135
}
136
) # end switch on objectives
137
}
138
}
139
return(momentargs)
140
}
141
142
#' Portfolio Moments
143
#'
144
#' Set portfolio moments for use by lower level optimization functions. Currently
145
#' three methods for setting the moments are available
146
#'
147
#' \describe{
148
#' \item{sample: }{sample estimates are used for the moments}
149
#' \item{boudt: }{estimate the second, third, and fourth moments using a
150
#' statistical factor model based on the work of Kris Boudt.}
151
#' See \code{\link{statistical.factor.model}}
152
#' \item{black_litterman: }{estimate the first and second moments using the
153
#' Black Litterman Formula. See \code{\link{black.litterman}}}.
154
#' }
155
#'
156
#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns
157
#' @param portfolio an object of type "portfolio" specifying the constraints and objectives for the optimization, see \code{\link{portfolio.spec}}
158
#' @param momentargs list containing arguments to be passed down to lower level functions, default NULL
159
#' @param method the method used to estimate portfolio moments. Valid choices include "sample", "boudt", and "black_litterman".
160
#' @param \dots any other passthru parameters
161
#' @aliases set.portfolio.moments set.portfolio.moments_v2
162
#' @rdname set.portfolio.moments
163
#' @export set.portfolio.moments
164
set.portfolio.moments <- set.portfolio.moments_v2 <- function(R,
165
portfolio,
166
momentargs=NULL,
167
method=c("sample", "boudt", "black_litterman", "meucci"),
168
...){
169
170
if(!hasArg(momentargs) | is.null(momentargs)) momentargs <- list()
171
if(is.null(portfolio$objectives)) {
172
warning("no objectives specified in portfolio")
173
} else {
174
method <- match.arg(method)
175
176
# If any of the objectives have clean as an argument, we fit the factor
177
# model and Black Litterman model with cleaned returns.
178
clean <- unlist(lapply(portfolio$objectives, function(x) x$arguments$clean))
179
if(!is.null(clean)){
180
if(length(unique(clean)) > 1){
181
warning(paste("Multiple methods detected for cleaning returns, default to use clean =", clean[1]))
182
}
183
cleanR <- Return.clean(R, method=clean[1])
184
cleaned <- TRUE
185
} else {
186
cleaned <- FALSE
187
}
188
189
if(cleaned){
190
tmpR <- cleanR
191
} else {
192
tmpR <- R
193
}
194
195
# Fit model based on method
196
switch(method,
197
boudt = {
198
if(hasArg(k)) k=match.call(expand.dots=TRUE)$k else k=1
199
fit <- statistical.factor.model(R=tmpR, k=k)
200
},
201
black_litterman = {
202
if(hasArg(P)) P=match.call(expand.dots=TRUE)$P else P=matrix(rep(1, ncol(R)), nrow=1)
203
if(hasArg(Mu)) Mu=match.call(expand.dots=TRUE)$Mu else Mu=NULL
204
if(hasArg(Sigma)) Sigma=match.call(expand.dots=TRUE)$Sigma else Sigma=NULL
205
if(hasArg(Views)) Views=match.call(expand.dots=TRUE)$Views else Views=NULL
206
B <- black.litterman(R=tmpR, P=P, Mu=Mu, Sigma=Sigma, Views=Views)
207
},
208
meucci = {
209
if(hasArg(posterior_p)) posterior_p=match.call(expand.dots=TRUE)$posterior_p else posterior_p=rep(1 / nrow(R), nrow(R))
210
meucci.model <- meucci.moments(R=tmpR, posterior_p=posterior_p)
211
}
212
) # end switch for fitting models based on method
213
214
lcl <- grep('garch', portfolio)
215
if (!identical(lcl, integer(0))) {
216
for (objective in portfolio[lcl]) {
217
objective = unlist(objective)
218
if( is.null( objective$garch ) ) next
219
if (objective$garch){
220
if (is.null(momentargs$mu)|is.null(momentargs$sigma)|is.null(momentargs$m3)|is.null(momentargs$m4))
221
{
222
momentargs = CCCgarch.MM(R,clean=objective$arguments.clean,...)
223
}
224
}
225
}
226
}
227
228
for (objective in portfolio$objectives){
229
# The returns should already have been cleaned if any objective has
230
# arguments=list(clean=*). One drawback is if different cleaning
231
# methods are being used for different objectives, only the first
232
# method for cleaning is used. This is mor efficient and avoids "re"-cleaning.
233
# Not sure that anyone would want to use different cleaning methods anyway.
234
# Another thing is that we don't recalculate the moments. So if a moment
235
# is set with un-cleaned returns then the next objective may have
236
# clean="boudt", but the cleaned returns are not used for that moment.
237
# I think this is more consisent with how the objectives are specified
238
# rather than overwriting all moments, but I am open to other ideas or
239
# suggestions.
240
if(!is.null(objective$arguments$clean)){
241
tmpR <- cleanR
242
} else {
243
tmpR <- R
244
}
245
switch(objective$name,
246
mean = {
247
switch(method,
248
sample =,
249
boudt = {
250
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean', na.rm=TRUE)), ncol=1)
251
},
252
black_litterman = {
253
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
254
},
255
meucci = {
256
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
257
}
258
) # end nested switch on method
259
}, # end switch on mean
260
var =,
261
sd =,
262
StdDev = {
263
switch(method,
264
sample = {
265
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean', na.rm=TRUE)), ncol=1);
266
if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR, use='pairwise.complete.obs')
267
},
268
boudt = {
269
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean', na.rm=TRUE)), ncol=1);
270
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
271
},
272
black_litterman = {
273
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
274
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
275
},
276
meucci = {
277
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
278
if(is.null(momentargs$sigma)) momentargs$sigma = meucci.model$sigma
279
}
280
) # end nested switch on method
281
}, # end switch on var, sd, StdDev
282
mVaR =,
283
VaR = ,
284
CSM = {
285
switch(method,
286
sample = {
287
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
288
if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR)
289
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
290
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
291
},
292
boudt = {
293
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
294
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
295
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
296
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
297
},
298
black_litterman = {
299
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
300
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
301
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
302
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
303
},
304
meucci = {
305
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
306
if(is.null(momentargs$sigma)) momentargs$sigma = meucci.model$sigma
307
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
308
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
309
}
310
) # end nested switch on method
311
}, # end switch on mVaR, VaR
312
es =,
313
mES =,
314
CVaR =,
315
cVaR =,
316
ETL=,
317
mETL=,
318
ES = {
319
# We don't want to calculate these moments if we have an ES
320
# objective and are solving as an LP problem.
321
if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
322
if(!ROI){
323
switch(method,
324
sample = {
325
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
326
if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR)
327
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
328
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
329
},
330
boudt = {
331
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
332
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
333
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
334
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
335
},
336
black_litterman = {
337
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
338
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
339
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
340
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
341
},
342
meucci = {
343
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
344
if(is.null(momentargs$sigma)) momentargs$sigma = meucci.model$sigma
345
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
346
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
347
}
348
) # end nested switch on method
349
}
350
} # end switch on es, mES, CVaR, cVaR, ETL, mETL, ES
351
) # end switch on objectives
352
}
353
}
354
return(momentargs)
355
}
356
357
garch.mm <- function(R,mu_ts, covlist,momentargs=list(),...) {
358
#momentargs<-list()
359
#momentargs$mu<-mu_ts[last(index(R)),]
360
momentargs$mu<-mu_ts[last(index(R)),]
361
362
momentargs$sigma<-covlist[as.character(last(index(R)))]
363
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
364
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
365
return(momentargs)
366
}
367
368
#' Portfolio Moments
369
#'
370
#' Set portfolio moments for use by lower level optimization functions using
371
#' a statistical factor model based on the work of Kris Boudt.
372
#'
373
#' @note If any of the objectives in the \code{portfolio} object have
374
#' \code{clean} as an argument, the cleaned returns are used to fit the model.
375
#'
376
#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
377
#' asset returns
378
#' @param portfolio an object of type \code{portfolio} specifying the
379
#' constraints and objectives for the optimization, see
380
#' \code{\link{portfolio.spec}}
381
#' @param momentargs list containing arguments to be passed down to lower level
382
#' functions, default NULL
383
#' @param k number of factors used for fitting statistical factor model
384
#' @param \dots any other passthru parameters
385
portfolio.moments.boudt <- function(R, portfolio, momentargs=NULL, k=1, ...){
386
387
# Fit the statistical factor model
388
# If any of the objectives have clean as an argument, we fit the factor
389
# model with cleaned returns. Is this the desired behavior we want?
390
clean <- unlist(lapply(portfolio$objectives, function(x) x$arguments$clean))
391
if(!is.null(clean)){
392
if(length(unique(clean)) > 1){
393
warning(paste("Multiple methods detected for cleaning returns, default to use clean =", clean[1]))
394
}
395
# This sets R as the cleaned returns for the rest of the function
396
# This is proably fine since the only other place R is used is for the
397
# mu estimate
398
R <- Return.clean(R, method=clean[1])
399
}
400
fit <- statistical.factor.model(R=R, k=k)
401
402
if(!hasArg(momentargs) | is.null(momentargs)) momentargs<-list()
403
if(is.null(portfolio$objectives)) {
404
warning("no objectives specified in portfolio")
405
} else {
406
for (objective in portfolio$objectives){
407
switch(objective$name,
408
mean = {
409
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1)
410
},
411
var =,
412
sd =,
413
StdDev = {
414
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1)
415
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
416
},
417
mVaR =,
418
VaR = ,
419
CSM = {
420
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1)
421
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
422
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
423
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
424
},
425
es =,
426
mES =,
427
CVaR =,
428
cVaR =,
429
ETL=,
430
mETL=,
431
ES = {
432
# We don't want to calculate these moments if we have an ES
433
# objective and are solving as an LP problem.
434
if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
435
if(!ROI){
436
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1)
437
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
438
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
439
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
440
}
441
}
442
) # end switch on objectives
443
}
444
}
445
return(momentargs)
446
}
447
448
#' Portfolio Moments
449
#'
450
#' Set portfolio moments for use by lower level optimization functions using
451
#' a basic Black Litterman model.
452
#'
453
#' @note If any of the objectives in the \code{portfolio} object have
454
#' \code{clean} as an argument, the cleaned returns are used to fit the model.
455
#'
456
#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
457
#' asset returns
458
#' @param portfolio an object of type \code{portfolio} specifying the
459
#' constraints and objectives for the optimization, see
460
#' \code{\link{portfolio.spec}}
461
#' @param momentargs list containing arguments to be passed down to lower level
462
#' functions, default NULL
463
#' @param P a K x N pick matrix representing views
464
#' @param Mu vector of length N of the prior expected values. The sample mean
465
#' is used if \code{Mu=NULL}.
466
#' @param Sigma an N x N matrix of the prior covariance matrix. The sample
467
#' covariance is used if \code{Sigma=NULL}.
468
#' @param \dots any other passthru parameters
469
portfolio.moments.bl <- function(R, portfolio, momentargs=NULL, P, Mu=NULL, Sigma=NULL, ...){
470
471
472
# If any of the objectives have clean as an argument, we fit the factor
473
# model with cleaned returns. Is this the desired behavior we want?
474
clean <- unlist(lapply(portfolio$objectives, function(x) x$arguments$clean))
475
if(!is.null(clean)){
476
if(length(unique(clean)) > 1){
477
warning(paste("Multiple methods detected for cleaning returns, default to use clean =", clean[1]))
478
}
479
# This sets R as the cleaned returns for the rest of the function
480
# This is probably fine since the only other place R is used is for the
481
# mu estimate
482
R <- Return.clean(R, method=clean[1])
483
}
484
485
# Compute the Black Litterman estimates
486
B <- black.litterman(R=R, P=P, Mu=Mu, Sigma=Sigma)
487
488
if(!hasArg(momentargs) | is.null(momentargs)) momentargs<-list()
489
if(is.null(portfolio$objectives)) {
490
warning("no objectives specified in portfolio")
491
} else {
492
for (objective in portfolio$objectives){
493
switch(objective$name,
494
mean = {
495
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
496
},
497
var =,
498
sd =,
499
StdDev = {
500
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
501
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
502
},
503
mVaR =,
504
VaR = ,
505
CSM = {
506
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
507
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
508
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
509
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
510
},
511
es =,
512
mES =,
513
CVaR =,
514
cVaR =,
515
ETL=,
516
mETL=,
517
ES = {
518
# We don't want to calculate these moments if we have an ES
519
# objective and are solving as an LP problem.
520
if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
521
if(!ROI){
522
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
523
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
524
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
525
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
526
}
527
}
528
) # end switch on objectives
529
}
530
}
531
return(momentargs)
532
}
533
534
535
###############################################################################
536
# $Id$
537
###############################################################################
538
539