chart.RiskBudget <- function(object, ...){
UseMethod("chart.RiskBudget")
}
chart.RiskBudget.optimize.portfolio <- function(object, ..., neighbors=NULL, risk.type="absolute", main="Risk Contribution", ylab="", xlab=NULL, cex.axis=0.8, cex.lab=0.8, element.color="darkgray", las=3, ylim=NULL){
if(!inherits(object, "optimize.portfolio")) stop("object must be of class optimize.portfolio")
portfolio <- object$portfolio
obj_class <- sapply(portfolio$objectives, function(x) class(x)[1])
if(!("risk_budget_objective" %in% obj_class)) print("no risk_budget_objective")
rb_idx <- which(obj_class == "risk_budget_objective")
if(length(rb_idx) > 1) message(paste(length(rb_idx), "risk_budget_objectives, generating multiple plots."))
contrib <- list()
pct_contrib <- list()
idx <- NULL
for(i in 1:length(object$objective_measures)){
if(length(object$objective_measures[[i]]) > 1){
contrib[[i]] <- object$objective_measures[[i]][2]
pct_contrib[[i]] <- object$objective_measures[[i]][3]
idx <- c(idx, i)
}
}
columnnames <- names(object$weights)
numassets <- length(columnnames)
if(is.null(xlab))
minmargin = 3
else
minmargin = 5
if(main=="") topmargin=1 else topmargin=4
if(las > 1) {
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
if(bottommargin > 10 ) {
bottommargin<-10
columnnames<-substr(columnnames,1,19)
}
}
else {
bottommargin = minmargin
}
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
if(risk.type == "absolute"){
for(ii in 1:length(idx)){
if(is.null(ylim)){
ylim <- range(contrib[[idx[ii]]][[1]])
ylim[1] <- min(0, ylim[1])
ylim[2] <- ylim[2] * 1.15
}
objname <- portfolio$objectives[[rb_idx[i]]]$name
plot(contrib[[idx[ii]]][[1]], type="n", axes=FALSE, xlab="", ylim=ylim, ylab=paste(objname, "Contribution", sep=" "), main=main, cex.lab=cex.lab, ...)
if(!is.null(neighbors)){
if(is.vector(neighbors)){
xtract <- extractStats(object)
riskcols <- grep(paste(objname, "contribution", sep="."), colnames(xtract))
if(length(riskcols) == 0) stop("Could not extract risk column")
if(length(neighbors) == 1){
orderx <- order(xtract[,"out"])
subsetx <- head(xtract[orderx,], n=neighbors)
for(i in 1:neighbors) points(subsetx[i, riskcols], type="b", col="lightblue")
} else {
subsetx <- xtract[neighbors, riskcols]
for(i in 1:length(neighbors)) points(subsetx[i,], type="b", col="lightblue")
}
}
if(is.matrix(neighbors) | is.data.frame(neighbors)){
nbriskcol <- grep(paste(objname, "contribution", sep="."), colnames(neighbors))
if(length(nbriskcol) == 0) stop(paste("must have '", objname,".contribution' as column name in neighbors",sep=""))
if(length(nbriskcol) != numassets) stop("number of 'contribution' columns must equal number of assets")
for(i in 1:nrow(neighbors)) points(as.numeric(neighbors[i, nbriskcol]), type="b", col="lightblue")
}
}
points(contrib[[idx[ii]]][[1]], type="b", ...)
axis(2, cex.axis = cex.axis, col = element.color)
axis(1, labels=columnnames, at=1:numassets, las=las, cex.axis = cex.axis, col = element.color)
box(col = element.color)
}
}
if(risk.type %in% c("percent", "percentage", "pct_contrib")){
for(ii in 1:length(rb_idx)){
min_prisk <- portfolio$objectives[[rb_idx[ii]]]$min_prisk
max_prisk <- portfolio$objectives[[rb_idx[ii]]]$max_prisk
if(is.null(ylim)){
ylim <- c(0, 1)
}
objname <- portfolio$objectives[[rb_idx[i]]]$name
plot(pct_contrib[[idx[ii]]][[1]], type="n", axes=FALSE, xlab='', ylim=ylim, ylab=paste(objname, " % Contribution", sep=" "), main=main, cex.lab=cex.lab, ...)
if(!is.null(min_prisk)){
points(min_prisk, type="b", col="darkgray", lty="solid", lwd=2, pch=24)
}
if(!is.null(max_prisk)){
points(max_prisk, type="b", col="darkgray", lty="solid", lwd=2, pch=25)
}
if(!is.null(neighbors)){
if(is.vector(neighbors)){
xtract <- extractStats(object)
if(risk.type == "absolute"){
riskcols <- grep(paste(objname, "contribution", sep="."), colnames(xtract))
} else if(risk.type %in% c("percent", "percentage", "pct_contrib")){
riskcols <- grep(paste(objname, "pct_contrib", sep="."), colnames(xtract))
}
if(length(riskcols) == 0) stop("Could not extract risk column")
if(length(neighbors) == 1){
orderx <- order(xtract[,"out"])
subsetx <- head(xtract[orderx,], n=neighbors)
for(i in 1:neighbors) points(subsetx[i, riskcols], type="b", col="lightblue")
} else {
subsetx <- xtract[neighbors, riskcols]
for(i in 1:length(neighbors)) points(subsetx[i,], type="b", col="lightblue")
}
}
if(is.matrix(neighbors) | is.data.frame(neighbors)){
nbriskcol <- grep(paste(objname, "pct_contrib", sep="."), colnames(neighbors))
if(length(nbriskcol) == 0) stop(paste("must have '", objname,".pct_contrib' as column name in neighbors",sep=""))
if(length(nbriskcol) != numassets) stop("number of 'pct_contrib' columns must equal number of assets")
for(i in 1:nrow(neighbors)) points(as.numeric(neighbors[i, nbriskcol]), type="b", col="lightblue")
}
}
points(pct_contrib[[idx[ii]]][[1]], type="b", ...)
axis(2, cex.axis = cex.axis, col = element.color)
axis(1, labels=columnnames, at=1:numassets, las=las, cex.axis = cex.axis, col = element.color)
box(col = element.color)
}
}
}
chart.RiskBudget.optimize.portfolio.rebalancing <- function(object, ..., match.col="ES", risk.type="absolute", regime=NULL, main="Risk Contribution"){
rebal.obj <- extractObjectiveMeasures(object)
if(inherits(object$portfolio, "regime.portfolios")){
if(is.null(regime)) regime=1
rebal.obj <- rebal.obj[[regime]]
}
if(risk.type == "absolute"){
rbcols <- grep(paste(match.col, "contribution", sep="."), colnames(rebal.obj))
if(length(rbcols) < 1) stop(paste("No ", match.col, ".contribution columns.", sep=""))
rbdata <- rebal.obj[, rbcols]
colnames(rbdata) <- gsub("^.*\\.", "", colnames(rbdata))
chart.StackedBar(w=rbdata, ylab=paste(match.col, "Contribution", sep=" "), main=main, ...)
}
if(risk.type %in% c("percent", "percentage", "pct_contrib")){
rbcols <- grep(paste(match.col, "pct_contrib", sep="."), colnames(rebal.obj))
if(length(rbcols) < 1) stop(paste("No ", match.col, ".pct_contrib columns.", sep=""))
rbdata <- rebal.obj[, rbcols]
colnames(rbdata) <- gsub("^.*\\.", "", colnames(rbdata))
chart.StackedBar(w=rbdata, ylab=paste(match.col, "% Contribution", sep=" "), main=main, ...)
}
}
chart.RiskBudget.opt.list <- function(object, ..., match.col="ES", risk.type="absolute", main="Risk Budget", plot.type="line", cex.axis=0.8, cex.lab=0.8, element.color="darkgray", las=3, ylim=NULL, colorset=NULL, legend.loc=NULL, cex.legend=0.8){
if(!inherits(object, "opt.list")) stop("object must be of class 'opt.list'")
if(plot.type %in% c("bar", "barplot")){
barplotRiskBudget(object=object, ...=..., match.col=match.col, risk.type=risk.type, main=main, ylim=ylim, cex.axis=cex.axis, cex.lab=cex.lab, element.color=element.color, las=las, colorset=colorset, legend.loc=legend.loc, cex.legend=cex.legend)
} else if(plot.type == "line"){
xtract <- extractObjectiveMeasures(object)
if(risk.type == "absolute"){
rbcols <- grep(paste(match.col, "contribution", sep="."), colnames(xtract))
dat <- na.omit(xtract[, rbcols])
if(ncol(dat) < 1) stop("No data to plot after na.omit")
opt_names <- rownames(dat)
colnames(dat) <- gsub("(.*)\\.", "", colnames(dat))
if(is.null(colorset)) colorset <- 1:nrow(dat)
columnnames <- colnames(dat)
numassets <- length(columnnames)
xlab <- NULL
if(is.null(xlab))
minmargin <- 3
else
minmargin <- 5
if(main=="") topmargin=1 else topmargin=4
if(las > 1) {
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
if(bottommargin > 10 ) {
bottommargin <- 10
columnnames<-substr(columnnames,1,19)
}
}
else {
bottommargin = minmargin
}
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
if(is.null(ylim)) ylim <- range(dat)
plot(dat[1,], type="n", ylim=ylim, xlab='', ylab=paste(match.col, "Contribution", sep=" "), main=main, cex.lab=cex.lab, axes=FALSE)
for(i in 1:nrow(dat)){
points(dat[i, ], type="b", col=colorset[i], ...)
}
axis(2, cex.axis=cex.axis, col=element.color)
axis(1, labels=columnnames, at=1:numassets, las=las, cex.axis=cex.axis, col=element.color)
box(col=element.color)
if(!is.null(legend.loc)) legend(legend.loc, legend=opt_names, col=colorset, lty=1, bty="n", cex=cex.legend)
}
if(risk.type %in% c("percent", "percentage", "pct_contrib")){
rbcols <- grep(paste(match.col, "pct_contrib", sep="."), colnames(xtract))
dat <- na.omit(xtract[, rbcols])
if(ncol(dat) < 1) stop("No data to plot after na.omit")
opt_names <- rownames(dat)
colnames(dat) <- gsub("(.*)\\.", "", colnames(dat))
if(is.null(colorset)) colorset <- 1:nrow(dat)
columnnames <- colnames(dat)
numassets <- length(columnnames)
xlab <- NULL
if(is.null(xlab))
minmargin <- 3
else
minmargin <- 5
if(main=="") topmargin=1 else topmargin=4
if(las > 1) {
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
if(bottommargin > 10 ) {
bottommargin <- 10
columnnames<-substr(columnnames,1,19)
}
}
else {
bottommargin = minmargin
}
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
if(is.null(ylim)) ylim <- range(dat)
plot(dat[1,], type="n", ylim=ylim, xlab='', ylab=paste(match.col, "% Contribution", sep=" "), main=main, cex.lab=cex.lab, axes=FALSE)
for(i in 1:nrow(dat)){
points(dat[i, ], type="b", col=colorset[i], ...)
}
axis(2, cex.axis=cex.axis, col=element.color)
axis(1, labels=columnnames, at=1:numassets, las=las, cex.axis=cex.axis, col=element.color)
box(col=element.color)
if(!is.null(legend.loc)) legend(legend.loc, legend=opt_names, col=colorset, lty=1, bty="n", cex=cex.legend)
}
}
}
barplotRiskBudget <- function(object, ..., match.col="ES", risk.type="absolute", main="Risk Budget", cex.axis=0.8, cex.lab=0.8, element.color="darkgray", las=3, colorset=NULL, legend.loc=NULL, cex.legend=0.8){
if(!inherits(object, "opt.list")) stop("object must be of class 'opt.list'")
xtract <- extractObjectiveMeasures(object)
if(risk.type == "absolute"){
rbcols <- grep(paste(match.col, "contribution", sep="."), colnames(xtract))
dat <- na.omit(xtract[, rbcols])
if(ncol(dat) < 1) stop("No data to plot after na.omit")
opt_names <- rownames(dat)
colnames(dat) <- gsub("(.*)\\.", "", colnames(dat))
columnnames <- colnames(dat)
numassets <- length(columnnames)
xlab <- NULL
if(is.null(xlab))
minmargin <- 3
else
minmargin <- 5
if(main=="") topmargin=1 else topmargin=4
if(las > 1) {
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
if(bottommargin > 10 ) {
bottommargin <- 10
columnnames<-substr(columnnames,1,19)
}
}
else {
bottommargin = minmargin
}
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
if(is.null(colorset)) colorset <- 1:nrow(dat)
barplot(dat, names.arg=columnnames, las=las, cex.names=cex.axis, xlab='', col=colorset, main=main, ylab=paste(match.col, "Contribution", sep=" "), cex.lab=cex.lab, cex.axis=cex.axis, beside=TRUE, ...)
box(col=element.color)
if(!is.null(legend.loc)) legend(legend.loc, legend=opt_names, fill=colorset, bty="n", cex=cex.legend)
}
if(risk.type %in% c("percent", "percentage", "pct_contrib")){
rbcols <- grep(paste(match.col, "pct_contrib", sep="."), colnames(xtract))
dat <- na.omit(xtract[, rbcols])
if(ncol(dat) < 1) stop("No data to plot after na.omit")
opt_names <- rownames(dat)
colnames(dat) <- gsub("(.*)\\.", "", colnames(dat))
columnnames <- colnames(dat)
numassets <- length(columnnames)
xlab <- NULL
if(is.null(xlab))
minmargin <- 3
else
minmargin <- 5
if(main=="") topmargin=1 else topmargin=4
if(las > 1) {
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
if(bottommargin > 10 ) {
bottommargin <- 10
columnnames<-substr(columnnames,1,19)
}
}
else {
bottommargin = minmargin
}
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
if(is.null(colorset)) colorset <- 1:nrow(dat)
barplot(dat, names.arg=columnnames, las=las, cex.names=cex.axis, col=colorset, main=main, ylab=paste(match.col, "% Contribution", sep=" "), cex.lab=cex.lab, cex.axis=cex.axis, beside=TRUE, ...)
box(col=element.color)
if(!is.null(legend.loc)) legend(legend.loc, legend=opt_names, fill=colorset, bty="n", cex=cex.legend)
}
}