Path: blob/master/sandbox/paper_analysis/R_interpretation/old/stackedweightriskcontributionplot.R
1433 views
#################################################################################1# Create stacked risk contributions plot2#################################################################################34# ! Set your working directory (folder containing the subfolders R_allocation, R_interpretation, data, weights, etc)56setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs")789# Options:10################1112# Choose data1314datacase = "equitybondscommodity"# "intequitybonds" # equitybondscommodity1516percriskcontribcriterion = "mES"1718# specify the optimization criteria you want to analyze19# crit1 = "StdDev"; crit2="SR.StdDev"20crit1 = "mES"; crit2="SR.mES"2122# specify the number of years used for the estimation23estyears = 4;2425# specify the risk budget constraint26RBconstraint = 0.4;2728# Load programs2930source("R_interpretation/chart.StackedBar.R");31library(zoo); library(PerformanceAnalytics)3233# number of risky assets34firstyear = 1976 ; firstquarter = 1; lastyear = 2009; lastquarter = 2;35cAssets = 4363738# frequency of rebalancing: yearly of quarterly39frequency = "quarterly"40# Load portfolio weights:41criterion = paste(crit1,".",estyears,"yr",sep="")42weightsEW = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/EW.csv" , sep="") );43weightsconst.crit1 = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/", criterion , "-Inf",RBconstraint,".csv" , sep="") );44weightsInf.crit1 = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/",criterion , "-InfInf.csv" , sep="") );45weightsconst.crit1 = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/", criterion , "-Inf",RBconstraint,".csv" , sep="") );46weightsERisk = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/unconstrained/", criterion , 0.22,0.28,".csv" , sep="") );47weightsMinCVaR = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/unconstrained/", criterion , 0.22,0.28,".csv" , sep="") );4849# Load percentage risk contributions:5051riskcontribEW = read.csv( file = paste("riskcont/", percriskcontribcriterion,"/",datacase,"/",frequency,"/EW.csv" , sep="") );52criterion = paste("riskcont/",percriskcontribcriterion,"/",datacase,"/",frequency,"/",crit1,".",estyears,"yr",sep="")53riskcontribInf.crit1 = read.csv( file = paste( criterion , "-InfInf.csv" , sep="") );54riskcontribconst.crit1 = read.csv( file = paste( criterion , "-Inf",RBconstraint,".csv" , sep="") );55criterion = paste("riskcont/",percriskcontribcriterion,"/",datacase,"/",frequency,"/unconstrained/",crit1,".",estyears,"yr",sep="")56riskcontribERisk = read.csv( file = paste( criterion , 0.22,0.28,".csv" , sep="") );57riskcontribMinCVaR = read.csv( file = paste( criterion , -Inf,Inf,".csv" , sep="") );5859# Is there cash in any of the portfolios due to infeasibility of the risk budget constraints?6061if( any(weightsconst.crit1[,(1+cAssets)]!=0) | any(weightsERisk[,(1+cAssets)]!=0) ){62if(datacase=="intequitybonds"){ w.names = c( "US bond" , "MSCI Europe" , "MSCI Japan", "MSCI US" , "T-Bill" ) };63if(datacase=="equitybondscommodity"){ w.names = c( "US bond" , "S&P 500" , "MSCI EAFE" , "S&P GSCI", "T-Bill" ) };64colorset = gray( seq(0,(cAssets),1)/5 ) ; #c(gray.colors(11,start=0,end=1)65}else{66if(datacase=="intequitybonds"){ w.names = c( "US bond" , "MSCI Europe" , "MSCI Japan", "MSCI US" ) };67if(datacase=="equitybondscommodity"){ w.names = c( "US bond" , "S&P 500", "MSCI EAFE" , "S&P GSCI" ) };68weightsInf.crit1 = weightsInf.crit1[,1:cAssets];69weightsconst.crit1 = weightsconst.crit1[,1:cAssets];70weightsERisk = weightsERisk[,1:cAssets];71weightsMinCVaR = weightsMinCVaR[,1:cAssets];72colorset = gray( seq(0,(cAssets-1),1)/cAssets ) ; #c(gray.colors(11,start=0,end=1)73}7475# Relabel rownames in the plots7677if(frequency=="yearly"){78names = seq( (firstyear+estyears),lastyear+1,1)79}else{80names = paste(rep( seq( (firstyear+estyears),lastyear,1) , each=4),c("Q1","Q2","Q3","Q4"),sep="")81names = c( names , paste( lastyear+1, "Q1" , sep="" ) )82names = names[firstquarter:(length(names)-4+lastquarter)]83}8485rownames(riskcontribEW) = rownames(riskcontribInf.crit1) = rownames(riskcontribconst.crit1) = rownames(riskcontribERisk) = rownames(riskcontribMinCVaR) = names;8687#due to rounding, the sum of the risk contributions is sometimes 1 + epsilon: avoid this in plot8889riskcontribEW = riskcontribEW/rowSums(riskcontribEW)9091riskcontribInf.crit1 = riskcontribInf.crit1/rowSums(riskcontribInf.crit1);92riskcontribconst.crit1 = riskcontribconst.crit1/rowSums(riskcontribconst.crit1);93riskcontribERisk = riskcontribERisk/rowSums(riskcontribERisk)94riskcontribMinCVaR = riskcontribMinCVaR/rowSums(riskcontribMinCVaR)9596# make weights versus risk allocation plot9798postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontinf.eps",sep="") )99100101layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height=c(0.15,2,2*1.4,0.7), width=1)102par(mar=c(0.1,4,1,1.1) , cex.main=1)103plot.new()104title( main = "min StdDev" , cex.main=1.3 )105par(mar=c(0,4,2,1) , cex.main=1)106barplot(t(weightsInf.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="",107cex.axis=1, cex.lab=1, cex.main=1, axisnames=F )108109par(mar=c(1,4,1,1.1))110chart.StackedBar2(riskcontribInf.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,111main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.2,1.2))112par(mar=c(0.1,4,1,1.1))113plot.new()114legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)115116par(mar=c(0.1,4,1,1.1) , cex.main=1)117plot.new()118title( main = "max Sharpe ratio", cex.main=1.3 )119par(mar=c(0,4,2,1.1) , cex.main=1)120barplot(t(weightsInf.crit2),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)121par(mar=c(1,4,1,1.1))122chart.StackedBar2(riskcontribInf.crit2,axisnames=T, colorset=colorset,space=0, cex.names=1,123main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.2,1.2))124dev.off()125126# make weights versus risk allocation plot127128postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontconst.eps",sep="") )129130layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height=c(0.15,2,2*1.2,0.6), width=1)131par(mar=c(0.1,4,1,1.1) , cex.main=1)132plot.new()133title( main = "min StdDev" , cex.main=1.3 )134par(mar=c(0,4,2,1) , cex.main=1)135barplot(t(weightsconst.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)136137par(mar=c(1,4,2,1.1))138chart.StackedBar2(riskcontribconst.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,139main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL)140par(mar=c(0.1,4,1,1.1))141plot.new()142legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)143144par(mar=c(0.1,4,1,1.1) , cex.main=1)145plot.new()146title( main = "max Sharpe ratio", cex.main=1.3 )147par(mar=c(0,4,2,1.1) , cex.main=1)148barplot(t(weightsconst.crit2),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)149par(mar=c(1,4,1,1.1))150chart.StackedBar2(riskcontribconst.crit2,axisnames=T, colorset=colorset,space=0, cex.names=1,151main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL)152153dev.off()154155156postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontconstminCVaR.eps",sep="") )157158layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height=c(0.15,1.6,1.6*2,0.6), width=1)159par(mar=c(0.1,4,1,1.1) , cex.main=1)160plot.new()161title( main = "Investment weight constrained" , cex.main=1.3 )162par(mar=c(0,4,2,1) , cex.main=1)163barplot(t(weightsInf.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)164165par(mar=c(1,4,2,1.1))166chart.StackedBar2(riskcontribInf.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,167main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4))168par(mar=c(0.1,4,1,1.1))169plot.new()170legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)171172par(mar=c(0.1,4,1,1.1) , cex.main=1)173plot.new()174title( main = "Risk budget constrained", cex.main=1.3 )175par(mar=c(0,4,2,1.1) , cex.main=1)176barplot(t(weightsconst.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)177par(mar=c(1,4,1,1.1))178chart.StackedBar2(riskcontribconst.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,179main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4) )180181182dev.off()183184mar1 =c(0.1,l,1,1.1)185mar2 =c(0,l,2,1)186mar3 = c(1,l,2,1.1)187postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontconstminCVaR.eps",sep="") )188l = 4189190layout( matrix( c(1,2,3,4,5,6,7,4,8,9,10,4,11,12,13,4,14,15,16,4), ncol = 5 ) , height= c(0.15,1.5,1.5*2,0.6), width=1)191192par(mar=mar1 , cex.main=1)193plot.new()194title( main = "Equal-weighted" , cex.main=1.3 )195par(mar=mar2 , cex.main=1)196barplot(t(weightsEW),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F,border = F )197198par(mar=mar3)199chart.StackedBar2(riskcontribEW,axisnames=T, colorset=colorset,space=0, cex.names=1.3,200main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F)201202par(mar=mar1)203204plot.new()205legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)206207plot.new()208title( main = "Min CVaR" , cex.main=1.3 )209par(mar=mar2 , cex.main=1)210barplot(t(weightsMinCVaR),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F,border = F )211212par(mar=mar3)213chart.StackedBar2(riskcontribMinCVaR,axisnames=T, colorset=colorset,space=0, cex.names=1.3,214main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F)215216par(mar=mar1 , cex.main=1)217plot.new()218title( main = "Min CVaR, w <= 0.4" , cex.main=1.3 )219par(mar=c(0,l,2,1) , cex.main=1)220barplot(t(weightsInf.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F,border = F)221222par(mar=mar2)223chart.StackedBar2(riskcontribInf.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,224main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F)225par(mar=mar3)226227# plot.new()228#legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)229230par(mar=mar1 , cex.main=1)231plot.new()232title( main = "Min CVaR, %Contrib CVaR <= 0.4", cex.main=1.3 )233par(mar=mar2 , cex.main=1)234barplot(t(weightsconst.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F,border =F)235par(mar=mar3)236chart.StackedBar2(riskcontribconst.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,237main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F )238239240par(mar=mar1 , cex.main=1)241plot.new()242title( main = "Equal %Contrib to CVaR", cex.main=1.3 )243par(mar=mar2 , cex.main=1)244barplot(t(weightsERisk),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F,border = F)245par(mar=mar3)246chart.StackedBar2(riskcontribERisk,axisnames=T, colorset=colorset,space=0, cex.names=1,247main = "Risk allocation", ylab="",cex.axis=1.1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F )248249dev.off()250251252253254255256257