Path: blob/master/sandbox/symposium2013/results.HFindexes.R
1433 views
# Presentation of results from optimization scripts run prior to this script12op <- par(no.readonly=TRUE)34# --------------------------------------------------------------------5# Plot Ex Ante scatter of RP and ONLY Equal Weight portfolio in StdDev space6# --------------------------------------------------------------------7# Done8CairoPDF(file=paste(resultsdir, dataname, "-RP-EqWgt-MeanSD-ExAnte.pdf", sep=""), height=5.5, width=9)9par(mar=c(5, 4, 1, 1) + 0.1) #c(bottom, left, top, right)10# Calculate chart bounds to unify with the charts below11xlim.StdDev=c(min(c(xtract[,"StdDev"], buoys.portfmeas[,"StdDev"])), max(c(xtract[,"StdDev"], buoys.portfmeas[,"StdDev"])))12ylim.mean=c(min(c(xtract[,"mean"], buoys.portfmeas[,"Mean"])), max(c(xtract[,"mean"], buoys.portfmeas[,"Mean"])))1314plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante Std Dev", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.6, xlim=xlim.StdDev, ylim=ylim.mean) # leave cloud darkgray for this slide15grid(col = "darkgray")16abline(h = 0, col = "darkgray")17# Overplot the equal weight portfolio18points(buoys.portfmeas[8,"StdDev"],buoys.portfmeas[8,"Mean"], col=wb13color[4], pch=16, cex=1.5) # watch the order in portfmeas19axis(1, cex.axis = 0.8, col = "darkgray")20axis(2, cex.axis = 0.8, col = "darkgray")21box(col = "darkgray")22legend("bottomright",legend=results.names[8], col=wb13color[4], pch=16, ncol=1, border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)23par(op)24dev.off()2526# --------------------------------------------------------------------27# Plot Ex Ante scatter of RP and ASSET portfolios in StdDev space28# --------------------------------------------------------------------29# @TODO: add the assets to this chart30CairoPDF(file=paste(resultsdir, dataname, "-RP-Assets-MeanSD-ExAnte.pdf", sep=""), height=5.5, width=9)31xlim.StdDev.assets =c(min(c(xtract[,"StdDev"], assets.portfmeas[,"StdDev"], 0)), max(c(xtract[,"StdDev"], assets.portfmeas[,"StdDev"],0.03)))32ylim.mean.assets =c(min(c(xtract[,"mean"], assets.portfmeas[,"Mean"], 0)), max(c(xtract[,"mean"], assets.portfmeas[,"Mean"])))33par(mar=c(5, 4, 1, 1) + 0.1) #c(bottom, left, top, right)34# Revise the chart bounds to include the asssets35plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante mETL", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.3, xlim=xlim.StdDev.assets, ylim=ylim.mean.assets)36grid(col = "darkgray")37abline(h = 0, col = "darkgray")38abline(v = 0, col = "darkgray")39# Overplot the equal weight portfolio40points(buoys.portfmeas[8,"StdDev"],buoys.portfmeas[8,"Mean"], col=wb13color[4], pch=16, cex=1.5) # watch the order in portfmeas41text(x=buoys.portfmeas[8,"StdDev"], y=buoys.portfmeas[8,"Mean"], labels=rownames(buoys.portfmeas)[8], pos=4, cex=1)42points(assets.portfmeas[,"StdDev"],assets.portfmeas[,"Mean"], col=rich8equal, pch=18, cex=1.5) # watch the order in portfmeas43text(x=assets.portfmeas[,"StdDev"], y=assets.portfmeas[,"Mean"], labels=rownames(assets.portfmeas), pos=4, cex=1)44axis(1, cex.axis = 0.7, col = "darkgray")45axis(2, cex.axis = 0.7, col = "darkgray")46box(col = "darkgray")47#legend("right",legend=rownames(assets.portfmeas), col=rich8equal, pch=16, ncol=1, border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)48par(op)49dev.off()5051# --------------------------------------------------------------------52# Plot Ex Ante scatter of RP and BUOY portfolios in StdDev space53# --------------------------------------------------------------------54# Done55CairoPDF(file=paste(resultsdir, dataname, "-RP-BUOY-MeanSD-ExAnte.pdf", sep=""), height=5.5, width=9)56par(mar=c(5, 4, 1, 1) + 0.1) #c(bottom, left, top, right)57plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante Std Dev", ylab="Ex Ante Mean", col="gray", axes=FALSE, main="", cex=.6, xlim=xlim.StdDev, ylim=ylim.mean, cex.lab=1)58grid(col = "darkgray")59abline(h = 0, col = "darkgray")60# Overplot the buoy portfolios61points(buoys.portfmeas[,"StdDev"],buoys.portfmeas[,"Mean"], col=wb13color[c(3,9,13,6,7,11,8,4)], pch=16, cex=1.5) # watch the order in portfmeas62axis(1, cex.axis = 0.6, col = "darkgray")63axis(2, cex.axis = 0.6, col = "darkgray")64box(col = "darkgray")65legend("bottomright",legend=results.names, col=wb13color[c(3,9,13,6,7,11,8,4)], pch=16, ncol=1, border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)66par(op)67dev.off()6869# --------------------------------------------------------------------70# Plot Ex Ante scatter of RP and BUOY portfolios in mETL space71# --------------------------------------------------------------------72# Done73CairoPDF(file=paste(resultsdir, dataname, "-RP-BUOYS-mETL-ExAnte.pdf", sep=""), height=5.5, width=9)74par(mar=c(5, 4, 1, 1) + 0.1) #c(bottom, left, top, right)75xlim.ES=c(min(c(xtract[,"ES"], buoys.portfmeas[,"mETL"])), max(c(xtract[,"ES"], buoys.portfmeas[,"mETL"])))76plot(xtract[,"ES"],xtract[,"mean"], xlab="Ex Ante mETL", ylab="Ex Ante Mean", col="gray", axes=FALSE, main="", cex=.6, xlim=xlim.ES, ylim=ylim.mean, cex.lab=1)77grid(col = "darkgray")78# Overplot the buoy portfolios79points(buoys.portfmeas[,"mETL"],buoys.portfmeas[,"Mean"], col=wb13color[c(3,9,13,6,7,11,8,4)], pch=16, cex=1.5) # watch the order in portfmeas80axis(1, cex.axis = 0.6, col = "darkgray")81axis(2, cex.axis = 0.6, col = "darkgray")82box(col = "darkgray")83legend("bottomright",legend=results.names, col=wb13color[c(3,9,13,6,7,11,8,4)], pch=16, ncol=1, border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)84par(op)85dev.off()8687# --------------------------------------------------------------------88# Plot weights of Buoy portfolios89# --------------------------------------------------------------------90# Done91source('./R/chart.UnStackedBar.R')92# Wgts = extractWeights(buoys)93CairoPDF(file=paste(resultsdir, dataname, "-Weights-Buoys.png", sep=""), height=5.5, width=9)94chart.UnStackedBar(t(Wgts), colorset=wb13color[c(3,9,13,6,7,11,8,4)], equal.line=TRUE)95dev.off()9697# --------------------------------------------------------------------98# Plot contribution to risk of Buoy portfolios99# --------------------------------------------------------------------100# @TODO: revise for this result set101# @TODO: add contribution to risk to portfmeas102source('./R/chart.UnStackedBar.R')103CairoPDF(file=paste(resultsdir, dataname, "-mETL-Perc-Contrib-Buoys.pdf", sep=""), height=5.5, width=9)104chart.UnStackedBar(t(buoys.perc.es), colorset=wb13color[c(3,9,13,6,7,11,8,4)], equal.line=TRUE)105dev.off()106# Alternatively, use table function for ES107108# --------------------------------------------------------------------109# Plot cumulative contribution to risk of Buoy portfolios110# --------------------------------------------------------------------111cumRisk=NULL112for(i in 1:NROW(buoys.contrib.es)) {113y = cumsum(buoys.contrib.es[i,order(buoys.contrib.es[i,], decreasing=TRUE)])114cumRisk=rbind(cumRisk,y)115}116colnames(cumRisk)=c("Most",2:6,"Least")117rownames(cumRisk)= rownames(buoys.contrib.es)118119CairoPDF(file=paste(resultsdir, dataname, "-mETL-CumulPerc-Contrib-Buoys.pdf", sep=""), height=5.5, width=9)120par(mar=c(5, 4, 1, 4) + 0.1) #c(bottom, left, top, right)121plot(cumRisk[8,], ylim=c(0,max(cumRisk)), col=wb13color[8], type="l", lwd=2, axes=FALSE, main="", xlab="Rank of Contribution to Risk", ylab="Portfolio Risk")122grid(col = "darkgray")123abline(h = 0, col = "darkgray")124axis(1, cex.axis = 0.7, col = "darkgray")125axis(2, cex.axis = 0.7, col = "darkgray")126box(col = "darkgray")127for(i in 1:8) {128lines(cumRisk[i,], col=wb13color[c(3,9,13,6,7,11,8,4)][i], lwd=3)129# put the values of the rightmost dot on the plot; that's the portfolio risk130points(7, cumRisk[i,7], col = wb13color[c(3,9,13,6,7,11,8,4)][i], pch=20, cex=1)131mtext(paste(round(100*cumRisk[i,7],2),"%", sep=""), line=.5, side = 4, at=cumRisk[i,7], adj=0, las=2, cex = 0.9, col = wb13color[c(3,9,13,6,7,11,8,4)][i])132}133# Add legend134legend("bottomright",legend=rownames(cumRisk), col=wb13color[c(3,9,13,6,7,11,8,4)], pch=16, ncol=1, border.col="darkgray", y.intersp=1.2, cex=.9, lwd=3, inset=.02)135par(op)136dev.off()137138# --------------------------------------------------------------------139# Scatter chart with DE trail140# --------------------------------------------------------------------141CairoPDF(file=paste(resultsdir, dataname, "-DE-MeanSD-ExAnte.pdf", sep=""), height=5.5, width=9)142# chart in same coordinates as RP; will leave some of the dots outside the chart bounds143chart.RiskReward(RiskBudget.DE, risk.col="StdDev", return.col="mean", xlim=xlim.StdDev, ylim=ylim.mean, las=1)144par(op)145dev.off()146# convert -density 300 DE-MeanSD-ExAnte.pdf -quality 100 -sharpen 0x1.0 ../docs/symposium-slides-2013-figure/DE-MeanSD-ExAnte.png147148# --------------------------------------------------------------------149# Plot contribution of risk in EqWgt portfolio150# --------------------------------------------------------------------151CairoPDF(file=paste(resultsdir, dataname, "-Weights-Risk-Comparison.pdf", sep=""), height=5.5, width=9)152y=rbind(t(Wgts[8,]), t(buoys.perc.es[8,]), t(Wgts[6,]), t(buoys.perc.es[6,]))153rownames(y)=c("Weight","Risk", "Weight", "Risk")154# Break this into two charts155chart.UnStackedBar(y, rotate="horizontal", colorset=c(wb13color[4],wb13color[7]), las=1, density=c(-1,25,-1,25))156#chart.UnStackedBar(y, rotate="vertical", colorset=wb13color, equal.line=TRUE)157par(op)158dev.off()159160# For equal ES contribution161colorset=c(wb13color[4],wb13color[4],wb13color[11],wb13color[11])162w=rbind(t(Wgts[8,]), t(buoys.perc.es[8,]), t(Wgts[6,]), t(buoys.perc.es[6,]))163rownames(w)=c("Weight","% of Risk", "Weight", "% of Risk")164165# For equal Volatility contribution166colorset=c(wb13color[4],wb13color[4],wb13color[7],wb13color[7])167w=rbind(t(Wgts[8,]), t(buoys.perc.sd[8,]), t(Wgts[5,]), t(buoys.perc.sd[5,]))168rownames(w)=c("Weight","% of Volatility", "Weight", "% of Volatility")169170# Chart either of the above data sets171CairoPDF(file=paste(resultsdir, dataname, "-Weights-Risk-Comparison.pdf", sep=""), height=5.5, width=9)172par(oma = c(2,4,4,1), mar=c(1,0,.5,1)) # c(bottom, left, top, right)173layout(matrix(c(1:(NCOL(w))), nr = NCOL(w), byrow = FALSE), height=c(rep(1/7,7)))174for(i in 1:NCOL(w)){175if(i==NCOL(w)){176barplot(w[,i], col=colorset, horiz=FALSE, ylim=c(0,max(w)), axes=FALSE, names.arg=rownames(w), cex.names=1.5, density=c(-1,25,-1,25))177abline(h=0, col="darkgray")178abline(h=1/7, col="darkgray", lty=2)179axis(2, cex.axis = 0.7, col = "darkgray", las=1)180mtext(colnames(w)[i], side= 3, cex=1, adj=0)181}182else{183barplot(w[,i], col=colorset, horiz=FALSE, ylim=c(0,max(w)), axes=FALSE, names.arg="", ylab=colnames(w)[i], las=1, density=c(-1,25,-1,25))184abline(h=0, col="darkgray")185abline(h=1/7, col="darkgray", lty=2)186axis(2, cex.axis = 0.7, col = "darkgray", las=1)187mtext(colnames(w)[i], side= 3, cex=1, adj=0)188}189}190par(op)191dev.off()192193# --------------------------------------------------------------------194# Show CONCENTRATION of the RP portfolios195# --------------------------------------------------------------------196# Use HHI197198CairoPDF(file=paste(resultsdir, dataname, "-ConcPercESContrib.pdf", sep=""), height=5.5, width=9)199WB20 = c(colorpanel(1, "#008566","#E1E56D"), colorpanel(20, "#E1E56D", "#742414")[-1])200op <- par(no.readonly=TRUE)201layout(matrix(c(1,2)),height=c(4,1.25),width=1)202par(mar=c(5,4,1,2)+.1, cex=1) # c(bottom, left, top, right)203## Draw the Scatter chart of combined results204### Get the random portfolios from one of the result sets205x.hhi=apply(xtract[,10:16], FUN='HHI', MARGIN=1)206y=(x.hhi-min(x.hhi))/(max(x.hhi)-min(x.hhi)) # normalized HHI between 0 and 1207plot(xtract[order(y,decreasing=TRUE),"StdDev"],xtract[order(y,decreasing=TRUE),"mean"], xlab="Ex Ante StdDev", ylab="Ex Ante Mean", col=WB20[floor(y[order(y,decreasing=TRUE)]*19)+1], axes=FALSE, main="", cex=.5, pch=16)208grid(col = "darkgray")209# points(RND.objectives[1,2],RND.objectives[1,1], col="blue", pch=19, cex=1.5)210axis(1, cex.axis = 0.7, col = "darkgray")211axis(2, cex.axis = 0.7, col = "darkgray")212box(col = "darkgray")213214# Add legend to bottom panel215par(mar=c(5,5.5,1,3)+.1, cex=0.7)216## Create a histogramed legend for sequential colorsets217## this next bit of code is based on heatmap.2 in gplots package218x=x.hhi219scale01 <- function(x, low = min(x), high = max(x)) {220return((x - low)/(high - low))221}222breaks <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length = length(WB20)+1)223min.raw <- min(x, na.rm = TRUE)224max.raw <- max(x, na.rm = TRUE)225z <- seq(min.raw, max.raw, length = length(WB20))226image(z = matrix(z, ncol = 1), col = WB20, breaks = breaks, xaxt = "n", yaxt = "n")227par(usr = c(0, 1, 0, 1)) # needed to draw the histogram correctly228lv <- pretty(breaks)229xv <- scale01(as.numeric(lv), min.raw, max.raw)230axis(1, at = xv, labels=sprintf("%s%%", pretty(lv)))231h <- hist(x, plot = FALSE, breaks=breaks)232hx <- scale01(breaks, min(x), max(x))233hy <- c(h$counts, h$counts[length(h$counts)])234lines(hx, hy/max(hy)*.95, lwd = 2, type = "s", col = "blue")235axis(2, at = pretty(hy)/max(hy)*.95, pretty(hy))236title(ylab="Count")237title(xlab="Degree of Portfolio Concentration")238par(op)239dev.off()240241# --------------------------------------------------------------------242# Show CONCENTRATION of the RP portfolios in HHI space243# --------------------------------------------------------------------244CairoPDF(file=paste(resultsdir, dataname, "-ConcPercESContrib-HHI-wHull.pdf", sep=""), height=5.5, width=9)245WB20 = c(colorpanel(1, "#008566","#E1E56D"), colorpanel(20, "#E1E56D", "#742414")[-1])246op <- par(no.readonly=TRUE)247layout(matrix(c(1,2)),height=c(4,1.25),width=1)248par(mar=c(5,4,1,2)+.1, cex=1) # c(bottom, left, top, right)249seq.col = heat.colors(11)250## Draw the Scatter chart of combined results251### Get the random portfolios from one of the result sets252x.hhi=apply(xtract[,10:16], FUN='HHI', MARGIN=1)253y=(x.hhi-min(x.hhi))/(max(x.hhi)-min(x.hhi)) # normalized HHI between 0 and 1254plot(x.hhi[order(y,decreasing=TRUE)],xtract[order(y,decreasing=TRUE),"mean"], xlab="Degree of ex ante Risk Contribution", ylab="Ex Ante Mean", col=WB20[floor(y[order(y,decreasing=TRUE)]*19)+1], axes=FALSE, main="", cex=.5, pch=16)255grid(col = "darkgray")256# points(RND.objectives[1,2],RND.objectives[1,1], col="blue", pch=19, cex=1.5)257axis(1, cex.axis = 0.7, col = "darkgray")258axis(2, cex.axis = 0.7, col = "darkgray")259box(col = "darkgray")260261# HOWTO add a hull to this diagram262# Make a data.frame out of your vectors263dat <- data.frame(X = x.hhi[order(y,decreasing=TRUE)], Y = xtract[order(y,decreasing=TRUE),"mean"])264dat <- data.frame(X = x.hhi, Y = xtract[,"mean"])265# Compute the convex hull. This returns the index for the X and Y coordinates266c.hull <- chull(dat)267#Extract the coordinate points from the convex hull with the index.268z=dat[c.hull,]269# Plot the full hull270# with(dat, plot(X,Y))271# lines(z, col = "pink", lwd = 3)272273# Or just do the ascending hull in Y274z[,3] <- c(diff(as.numeric(z[,1])),z[1,1]-tail(z[,1],1)) # calculate whether the line segment is ascending in X275z[,4] <- c(tail(z[,2],1)-z[1,2],diff(as.numeric(z[,2]))) # calculate whether the line segment is ascending in Y276lines(z[z[,3]>0 & z[,4]>0,1:2], col = wb13color[1], lwd = 2, type="b")277z=cbind(z,c.hull)278# Here are the portfolios on the hull279hull.portfolios=z[which(z[,3]>0 & z[,4]>0),5]280281# Add legend to bottom panel282par(mar=c(5,5.5,1,3)+.1, cex=0.7)283## Create a histogramed legend for sequential colorsets284## this next bit of code is based on heatmap.2 in gplots package285x=x.hhi286scale01 <- function(x, low = min(x), high = max(x)) {287return((x - low)/(high - low))288}289breaks <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length = length(WB20)+1)290min.raw <- min(x, na.rm = TRUE)291max.raw <- max(x, na.rm = TRUE)292z <- seq(min.raw, max.raw, length = length(WB20))293image(z = matrix(z, ncol = 1), col = WB20, breaks = breaks, xaxt = "n", yaxt = "n")294par(usr = c(0, 1, 0, 1)) # needed to draw the histogram correctly295lv <- pretty(breaks)296xv <- scale01(as.numeric(lv), min.raw, max.raw)297axis(1, at = xv, labels=pretty(lv))298h <- hist(x, plot = FALSE, breaks=breaks)299hx <- scale01(breaks, min(x), max(x))300hy <- c(h$counts, h$counts[length(h$counts)])301lines(hx, hy/max(hy)*.95, lwd = 2, type = "s", col = wb13color[8])302axis(2, at = pretty(hy)/max(hy)*.95, pretty(hy))303title(ylab="Count")304title(xlab="Degree of Portfolio Concentration")305par(op)306dev.off()307308309# --------------------------------------------------------------------310# Show CONCENTRATION of the RP portfolios in STD DEV space WITH HULL311# --------------------------------------------------------------------312CairoPDF(file=paste(resultsdir, dataname, "-ConcPercESContrib-SD-wHull.pdf", sep=""), height=5.5, width=9)313WB20 = c(colorpanel(1, "#008566","#E1E56D"), colorpanel(20, "#E1E56D", "#742414")[-1])314op <- par(no.readonly=TRUE)315layout(matrix(c(1,2)),height=c(4,1.25),width=1)316par(mar=c(5,4,1,2)+.1, cex=1) # c(bottom, left, top, right)317plot(xtract[order(y,decreasing=TRUE),"StdDev"],xtract[order(y,decreasing=TRUE),"mean"], xlab="Ex Ante StdDev", ylab="Ex Ante Mean", col=WB20[floor(y[order(y,decreasing=TRUE)]*19)+1], axes=FALSE, main="", cex=.5, pch=16)318# points(xtract[hull.portfolios,"StdDev"], xtract[hull.portfolios,"mean"], col='blue')319lines(xtract[hull.portfolios,"StdDev"], xtract[hull.portfolios,"mean"], type="b", col=wb13color[1], lwd=2)320grid(col = "darkgray")321axis(1, cex.axis = 0.7, col = "darkgray")322axis(2, cex.axis = 0.7, col = "darkgray")323box(col = "darkgray")324325# Add legend to bottom panel326par(mar=c(5,5.5,1,3)+.1, cex=0.7)327## Create a histogramed legend for sequential colorsets328## this next bit of code is based on heatmap.2 in gplots package329x=x.hhi330scale01 <- function(x, low = min(x), high = max(x)) {331return((x - low)/(high - low))332}333breaks <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length = length(WB20)+1)334min.raw <- min(x, na.rm = TRUE)335max.raw <- max(x, na.rm = TRUE)336z <- seq(min.raw, max.raw, length = length(WB20))337image(z = matrix(z, ncol = 1), col = WB20, breaks = breaks, xaxt = "n", yaxt = "n")338par(usr = c(0, 1, 0, 1)) # needed to draw the histogram correctly339lv <- pretty(breaks)340xv <- scale01(as.numeric(lv), min.raw, max.raw)341axis(1, at = xv, labels=pretty(lv))342h <- hist(x, plot = FALSE, breaks=breaks)343hx <- scale01(breaks, min(x), max(x))344hy <- c(h$counts, h$counts[length(h$counts)])345lines(hx, hy/max(hy)*.95, lwd = 2, type = "s", col = wb13color[8])346axis(2, at = pretty(hy)/max(hy)*.95, pretty(hy))347title(ylab="Count")348title(xlab="Degree of Portfolio Concentration")349par(op)350dev.off()351352# --------------------------------------------------------------------353# Show CONCENTRATION of the RP portfolios in ETL space WITH HULL354# --------------------------------------------------------------------355CairoPDF(file=paste(resultsdir, dataname, "-ConcPercESContrib-mETL-wHull.pdf", sep=""), height=5.5, width=9)356WB20 = c(colorpanel(1, "#008566","#E1E56D"), colorpanel(20, "#E1E56D", "#742414")[-1])357op <- par(no.readonly=TRUE)358layout(matrix(c(1,2)),height=c(4,1.25),width=1)359par(mar=c(5,4,1,2)+.1, cex=1) # c(bottom, left, top, right)360plot(xtract[order(y,decreasing=TRUE),"ES"],xtract[order(y,decreasing=TRUE),"mean"], xlab="Ex Ante mETL", ylab="Ex Ante Mean", col=WB20[floor(y[order(y,decreasing=TRUE)]*19)+1], axes=FALSE, main="", cex=.5, pch=16, cex.lab=1.1)361points(xtract[hull.portfolios,"ES"], xtract[hull.portfolios,"mean"], col='blue')362lines(xtract[hull.portfolios,"ES"], xtract[hull.portfolios,"mean"], type="b", col=wb13color[1], lwd=2)363grid(col = "darkgray")364axis(1, cex.axis = .7, col = "darkgray")365axis(2, cex.axis = .7, col = "darkgray")366box(col = "darkgray")367# Add legend to bottom panel368par(mar=c(5,5.5,1,3)+.1, cex=0.7)369## Create a histogramed legend for sequential colorsets370## this next bit of code is based on heatmap.2 in gplots package371x=x.hhi372scale01 <- function(x, low = min(x), high = max(x)) {373return((x - low)/(high - low))374}375breaks <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length = length(WB20)+1)376min.raw <- min(x, na.rm = TRUE)377max.raw <- max(x, na.rm = TRUE)378z <- seq(min.raw, max.raw, length = length(WB20))379image(z = matrix(z, ncol = 1), col = WB20, breaks = breaks, xaxt = "n", yaxt = "n")380par(usr = c(0, 1, 0, 1)) # needed to draw the histogram correctly381lv <- pretty(breaks)382xv <- scale01(as.numeric(lv), min.raw, max.raw)383axis(1, at = xv, labels=pretty(lv))384h <- hist(x, plot = FALSE, breaks=breaks)385hx <- scale01(breaks, min(x), max(x))386hy <- c(h$counts, h$counts[length(h$counts)])387lines(hx, hy/max(hy)*.95, lwd = 2, type = "s", col = wb13color[8])388axis(2, at = pretty(hy)/max(hy)*.95, pretty(hy))389title(ylab="Count")390title(xlab="Degree of Portfolio Concentration")391par(op)392dev.off()393394395### APPENDIX SLIDES:396397# --------------------------------------------------------------------398# Show weights through time for MRC SD portfolio399# --------------------------------------------------------------------400print(load("results/MRCSD.DE.t-2013-10-17-historical.moments.rda"))401MRCSD.w = extractWeights(MRCSD.DE.t)402CairoPDF(file=paste(resultsdir, dataname, "-weights-SD.pdf", sep=""), height=5.5, width=9)403chart.UnStackedBar(MRCSD.w, rotate="horizontal", colorset=wb13color, space=0, las=3, cex.axis=0.7, cex.names=0.8)404dev.off()405406# --------------------------------------------------------------------407# Show percent contribution of MRC SD through time408# --------------------------------------------------------------------409# Extract perc contrib of mES from results object410x=NULL411for(i in 1:length(names(MRCSD.DE.t))) {412x = rbind(x,MRCSD.DE.t[[i]][["objective_measures"]]$StdDev$pct_contrib_StdDev)413}414x.xts = as.xts(x, order.by=as.POSIXct(names(MRCSD.DE.t)))415colnames(x.xts)=names(MRCmETL.DE.t[[1]][["objective_measures"]]$StdDev$pct_contrib_StdDev)416CairoPDF(file=paste(resultsdir, dataname, "-contribution-SD.pdf", sep=""), height=5.5, width=9)417chart.UnStackedBar(x.xts, rotate="horizontal", colorset=wb13color, space=0, las=3, cex.axis=0.7, cex.names=0.8)418dev.off()419420# --------------------------------------------------------------------421# Show weights through time for MRC mETL portfolio422# --------------------------------------------------------------------423print(load("results/MRCmETL.DE.t-2013-10-18-historical.moments.rda"))424MRCmETL.w = extractWeights(MRCmETL.DE.t)425CairoPDF(file=paste(resultsdir, dataname, "-weights-mETL.pdf", sep=""), height=5.5, width=9)426chart.UnStackedBar(MRCmETL.w, rotate="horizontal", colorset=wb13color, space=0, las=3, cex.axis=0.7, cex.names=0.8)427dev.off()428429# --------------------------------------------------------------------430# Show percent contribution of mETL through time431# --------------------------------------------------------------------432# Extract perc contrib of mES from results object433x=NULL434for(i in 1:length(names(MRCmETL.DE.t))) {435x = rbind(x,MRCmETL.DE.t[[i]][["objective_measures"]]$ES$pct_contrib_MES)436}437x.xts = as.xts(x, order.by=as.POSIXct(names(MRCmETL.DE.t)))438colnames(x.xts)=names(MRCmETL.DE.t[[1]][["objective_measures"]]$ES$pct_contrib_MES)439CairoPDF(file=paste(resultsdir, dataname, "-contribution-mETL.pdf", sep=""), height=5.5, width=9)440chart.UnStackedBar(x.xts, rotate="horizontal", colorset=wb13color, space=0, las=3, cex.axis=0.7, cex.names=0.8)441dev.off()442443# --------------------------------------------------------------------444# Show out-of-sample performance of buoy portfolios445# --------------------------------------------------------------------446EqWgt.opt$weights447dates=index(R[endpoints(R, on="years")])448EqWgt.w = xts(matrix(rep(1/NCOL(R),length(dates)*NCOL(R)), ncol=NCOL(R)), order.by=dates)449EqWgt.R = Return.rebalancing(R, EqWgt.w)450MRCSD.R = Return.rebalancing(R, MRCSD.w)451MRCmETL.R = Return.rebalancing(R, MRCmETL.w)452x.R = cbind(EqWgt.R, VolWgt.R, MRCSD.R, MRCmETL.R)453colnames(x.R)=c("Eq Wgt", "Vol Wgt", "MRC SD", "MRC mETL")454CairoPDF(file=paste(resultsdir, dataname, "-OOS-relative-performance.pdf", sep=""), height=5.5, width=9)455chart.RelativePerformance(x.R["2000::",2:4], x.R["2000::",1], colorset=wb13color[c(8,7,11)], lwd=3, legend.loc="bottomleft", main="Performance Relative to Equal Weight")456dev.off()457458table.RiskStats(x.R["2000::"], p=1-1/12)459460R.boudt=Return.clean(R, method="boudt")461# --------------------------------------------------------------------462# From Inception Mean of constituents463# --------------------------------------------------------------------464x.mean=apply.fromstart(R,FUN="mean")465x.mean=as.xts(x.mean)466CairoPDF(file=paste(resultsdir, dataname, "-from-inception-mean.pdf", sep=""), height=5.5, width=9)467chart.TimeSeries(x.mean["2000-01::",],legend.loc="topright", colorset=wb13color, pch="", lwd=3, main="From-Inception Mean")468dev.off()469470# --------------------------------------------------------------------471# From Inception Volatility of constituents472# --------------------------------------------------------------------473x.vol=apply.fromstart(R,FUN="StdDev")474x.vol=as.xts(x.vol)475CairoPDF(file=paste(resultsdir, dataname, "-from-inception-vol.pdf", sep=""), height=5.5, width=9)476chart.TimeSeries(x.vol["2000-01::",],legend.loc="bottomleft", colorset=wb13color, pch="", lwd=3, main="From-Inception Volatility")477dev.off()478479# --------------------------------------------------------------------480# From Inception Skewness of constituents481# --------------------------------------------------------------------482x.skew=apply.fromstart(R,FUN="skewness")483x.skew=as.xts(x.skew)484CairoPDF(file=paste(resultsdir, dataname, "-from-inception-skew.pdf", sep=""), height=5.5, width=9)485chart.TimeSeries(x.skew["2000-01::",],legend.loc="bottomleft", colorset=wb13color, pch="", lwd=3, main="From-Inception Skewness")486dev.off()487488# --------------------------------------------------------------------489# From Inception Kurtosis of constituents490# --------------------------------------------------------------------491x.kurt=apply.fromstart(R,FUN="kurtosis")492x.kurt=as.xts(x.kurt)493CairoPDF(file=paste(resultsdir, dataname, "-from-inception-kurt.pdf", sep=""), height=5.5, width=9)494chart.TimeSeries(x.kurt["2000-01::",],legend.loc="topleft", colorset=wb13color, pch="", lwd=3, main="From-Inception Kurtosis")495dev.off()496497