CCCgarch.MM = function(R, momentargs = NULL , ... )
{
stopifnot("package:fGarch" %in% search() || requireNamespace("fGarch",quietly=TRUE))
if (!hasArg(momentargs) | is.null(momentargs))
momentargs <- list()
cAssets = ncol(R)
T = nrow(R)
if (!hasArg(mu)){
mu = apply(R, 2, "mean")
}else{ mu = match.call(expand.dots = TRUE)$mu }
R = R - matrix( rep(mu,T) , nrow = T , byrow = TRUE )
momentargs$mu = mu
S = nextS = c();
for( i in 1:cAssets ){
gout = fGarch::garchFit(formula ~ garch(1,1), data = R[,i],include.mean = F, cond.dist="QMLE", trace = FALSE )
if( as.vector(gout@fit$coef["alpha1"]) < 0.01 ){
sigmat = rep( sd( as.vector(R[,i])), length(R[,i]) ); nextSt = sd( as.vector(R[,i]))
}else{
sigmat = gout@sigma.t; nextSt = fGarch::predict(gout)[1,3]
}
S = cbind( S , sigmat); nextS = c(nextS,nextSt)
}
U = R/S;
if (!hasArg(clean)){
clean = match.call(expand.dots = TRUE)$clean
}else{ clean = NULL }
if(!is.null(clean)){
cleanU <- try(Return.clean(U, method = clean))
if (!inherits(cleanU, "try-error")) { U = cleanU }
}
Rcor = cor(U)
D = diag( nextS ,ncol=cAssets )
momentargs$sigma = D%*%Rcor%*%D
uncS = sqrt(diag( cov(U) ))
U = U*matrix( rep(nextS/uncS,T ) , ncol = cAssets , byrow = T )
momentargs$m3 = PerformanceAnalytics::M3.MM(U)
momentargs$m4 = PerformanceAnalytics::M4.MM(U)
return(momentargs)
}
set.portfolio.moments_v1 <- function(R, constraints, momentargs=NULL,...){
if(!hasArg(momentargs) | is.null(momentargs)) momentargs<-list()
if(is.null(constraints$objectives)) {
warning("no objectives specified in constraints")
} else {
lcl <- grep('garch', constraints)
if (!identical(lcl, integer(0))) {
for (objective in constraints[lcl]) {
objective = unlist(objective)
if( is.null( objective$garch ) ) next
if (objective$garch){
if (is.null(momentargs$mu)|is.null(momentargs$sigma)|is.null(momentargs$m3)|is.null(momentargs$m4))
{
momentargs = CCCgarch.MM(R,clean=objective$arguments.clean,...)
}
}
}
}
lcl<-grep('clean',constraints)
if(!identical(lcl,integer(0))) {
for (objective in constraints[lcl]){
objective = unlist(objective)
if (!is.null(objective$arguments.clean)){
if (is.null(momentargs$mu)|is.null(momentargs$sigma)|is.null(momentargs$m3)|is.null(momentargs$m4))
{
cleanR <- try(Return.clean(R, method = objective$arguments.clean,...))
if(!inherits(cleanR,"try-error")) {
momentargs$mu = matrix( as.vector(apply(cleanR,2,'mean')),ncol=1);
momentargs$sigma = cov(cleanR);
momentargs$m3 = PerformanceAnalytics::M3.MM(cleanR)
momentargs$m4 = PerformanceAnalytics::M4.MM(cleanR)
}
}
}
}
}
for (objective in constraints$objectives){
switch(objective$name,
sd =,
StdDev = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = cov(R, use='pairwise.complete.obs')
},
var =,
mVaR =,
VaR = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = cov(R)
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
},
es =,
mES =,
CVaR =,
cVaR =,
ES = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = cov(R)
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
}
)
}
}
return(momentargs)
}
set.portfolio.moments <- set.portfolio.moments_v2 <- function(R,
portfolio,
momentargs=NULL,
method=c("sample", "boudt", "black_litterman", "meucci"),
...){
if(!hasArg(momentargs) | is.null(momentargs)) momentargs <- list()
if(is.null(portfolio$objectives)) {
warning("no objectives specified in portfolio")
} else {
method <- match.arg(method)
clean <- unlist(lapply(portfolio$objectives, function(x) x$arguments$clean))
if(!is.null(clean)){
if(length(unique(clean)) > 1){
warning(paste("Multiple methods detected for cleaning returns, default to use clean =", clean[1]))
}
cleanR <- Return.clean(R, method=clean[1])
cleaned <- TRUE
} else {
cleaned <- FALSE
}
if(cleaned){
tmpR <- cleanR
} else {
tmpR <- R
}
switch(method,
boudt = {
if(hasArg(k)) k=match.call(expand.dots=TRUE)$k else k=1
fit <- statistical.factor.model(R=tmpR, k=k)
},
black_litterman = {
if(hasArg(P)) P=match.call(expand.dots=TRUE)$P else P=matrix(rep(1, ncol(R)), nrow=1)
if(hasArg(Mu)) Mu=match.call(expand.dots=TRUE)$Mu else Mu=NULL
if(hasArg(Sigma)) Sigma=match.call(expand.dots=TRUE)$Sigma else Sigma=NULL
if(hasArg(Views)) Views=match.call(expand.dots=TRUE)$Views else Views=NULL
B <- black.litterman(R=tmpR, P=P, Mu=Mu, Sigma=Sigma, Views=Views)
},
meucci = {
if(hasArg(posterior_p)) posterior_p=match.call(expand.dots=TRUE)$posterior_p else posterior_p=rep(1 / nrow(R), nrow(R))
meucci.model <- meucci.moments(R=tmpR, posterior_p=posterior_p)
}
)
lcl <- grep('garch', portfolio)
if (!identical(lcl, integer(0))) {
for (objective in portfolio[lcl]) {
objective = unlist(objective)
if( is.null( objective$garch ) ) next
if (objective$garch){
if (is.null(momentargs$mu)|is.null(momentargs$sigma)|is.null(momentargs$m3)|is.null(momentargs$m4))
{
momentargs = CCCgarch.MM(R,clean=objective$arguments.clean,...)
}
}
}
}
for (objective in portfolio$objectives){
if(!is.null(objective$arguments$clean)){
tmpR <- cleanR
} else {
tmpR <- R
}
switch(objective$name,
mean = {
switch(method,
sample =,
boudt = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean', na.rm=TRUE)), ncol=1)
},
black_litterman = {
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
},
meucci = {
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
}
)
},
var =,
sd =,
StdDev = {
switch(method,
sample = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean', na.rm=TRUE)), ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR, use='pairwise.complete.obs')
},
boudt = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean', na.rm=TRUE)), ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
},
black_litterman = {
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
},
meucci = {
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
if(is.null(momentargs$sigma)) momentargs$sigma = meucci.model$sigma
}
)
},
mVaR =,
VaR = ,
CSM = {
switch(method,
sample = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR)
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
},
boudt = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
},
black_litterman = {
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
},
meucci = {
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
if(is.null(momentargs$sigma)) momentargs$sigma = meucci.model$sigma
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
}
)
},
es =,
mES =,
CVaR =,
cVaR =,
ETL=,
mETL=,
ES = {
if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
if(!ROI){
switch(method,
sample = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = cov(tmpR)
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
},
boudt = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(tmpR, 2, 'mean')), ncol=1);
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
},
black_litterman = {
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
},
meucci = {
if(is.null(momentargs$mu)) momentargs$mu = meucci.model$mu
if(is.null(momentargs$sigma)) momentargs$sigma = meucci.model$sigma
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(tmpR)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(tmpR)
}
)
}
}
)
}
}
return(momentargs)
}
garch.mm <- function(R,mu_ts, covlist,momentargs=list(),...) {
momentargs$mu<-mu_ts[last(index(R)),]
momentargs$sigma<-covlist[as.character(last(index(R)))]
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
return(momentargs)
}
portfolio.moments.boudt <- function(R, portfolio, momentargs=NULL, k=1, ...){
clean <- unlist(lapply(portfolio$objectives, function(x) x$arguments$clean))
if(!is.null(clean)){
if(length(unique(clean)) > 1){
warning(paste("Multiple methods detected for cleaning returns, default to use clean =", clean[1]))
}
R <- Return.clean(R, method=clean[1])
}
fit <- statistical.factor.model(R=R, k=k)
if(!hasArg(momentargs) | is.null(momentargs)) momentargs<-list()
if(is.null(portfolio$objectives)) {
warning("no objectives specified in portfolio")
} else {
for (objective in portfolio$objectives){
switch(objective$name,
mean = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1)
},
var =,
sd =,
StdDev = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1)
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
},
mVaR =,
VaR = ,
CSM = {
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1)
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
},
es =,
mES =,
CVaR =,
cVaR =,
ETL=,
mETL=,
ES = {
if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
if(!ROI){
if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1)
if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
}
}
)
}
}
return(momentargs)
}
portfolio.moments.bl <- function(R, portfolio, momentargs=NULL, P, Mu=NULL, Sigma=NULL, ...){
clean <- unlist(lapply(portfolio$objectives, function(x) x$arguments$clean))
if(!is.null(clean)){
if(length(unique(clean)) > 1){
warning(paste("Multiple methods detected for cleaning returns, default to use clean =", clean[1]))
}
R <- Return.clean(R, method=clean[1])
}
B <- black.litterman(R=R, P=P, Mu=Mu, Sigma=Sigma)
if(!hasArg(momentargs) | is.null(momentargs)) momentargs<-list()
if(is.null(portfolio$objectives)) {
warning("no objectives specified in portfolio")
} else {
for (objective in portfolio$objectives){
switch(objective$name,
mean = {
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
},
var =,
sd =,
StdDev = {
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
},
mVaR =,
VaR = ,
CSM = {
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
},
es =,
mES =,
CVaR =,
cVaR =,
ETL=,
mETL=,
ES = {
if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
if(!ROI){
if(is.null(momentargs$mu)) momentargs$mu = B$BLMu
if(is.null(momentargs$sigma)) momentargs$sigma = B$BLSigma
if(is.null(momentargs$m3)) momentargs$m3 = PerformanceAnalytics::M3.MM(R)
if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics::M4.MM(R)
}
}
)
}
}
return(momentargs)
}