Path: blob/master/sandbox/paper_analysis/R_interpretation/old/stackedweightriskcontributionplot_Palermo.R
1433 views
#################################################################################1# Create stacked weights and 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# specify the number of years used for the estimation13estyears = 8;1415# Load programs1617source("R_interpretation/chart.StackedBar.R");18library(zoo); library(PerformanceAnalytics)1920# number of risky assets21firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;22cAssets = 42324# "MinRisk_ReturnTarget" "EqualRisk"25# "MinRiskConc_ReturnTarget"2627names = c( "MinRisk" , "MinRiskConc" ,"MinRisk_PositionLimit" ,28"MinRisk_RiskLimit" , "EqualWeight" ,29"MinRisk_ReturnTarget" , "MinRiskConc_ReturnTarget" , "MinRiskConc_PositionLimit" )3031namelabels = c( "Min CVaR" , "Min CVaR Concentration" , "Min CVaR + Position Limit" ,32"Min CVaR + CVaR Alloc Limit" , "Equal Weight" ,33"Min CVaR + Return Target" , "Min CVaR Concentration + Return Target" , "Min CVaR Concentration + Position Limit" )3435# frequency of rebalancing: yearly of quarterly36frequency = "quarterly"37# Load portfolio weights:38weightsS1 = read.csv( file = paste("weights/", names[1], ".csv" , sep="") );39weightsS2 = read.csv( file = paste("weights/", names[2], ".csv" , sep="") );40weightsS3 = read.csv( file = paste("weights/", names[3], ".csv" , sep="") );41weightsS4 = read.csv( file = paste("weights/", names[4], ".csv" , sep="") );42weightsS5 = read.csv( file = paste("weights/", names[5], ".csv" , sep="") );43weightsS6 = read.csv( file = paste("weights/", names[6], ".csv" , sep="") );44weightsS7 = read.csv( file = paste("weights/", names[7], ".csv" , sep="") );45weightsS8 = read.csv( file = paste("weights/", names[8], ".csv" , sep="") );4647# Load percentage risk contributions:48riskcontS1 = read.csv( file = paste("riskcont/", names[1], ".csv" , sep="") );49riskcontS2 = read.csv( file = paste("riskcont/", names[2], ".csv" , sep="") );50riskcontS3 = read.csv( file = paste("riskcont/", names[3], ".csv" , sep="") );51riskcontS4 = read.csv( file = paste("riskcont/", names[4], ".csv" , sep="") );52riskcontS5 = read.csv( file = paste("riskcont/", names[5], ".csv" , sep="") );53riskcontS6 = read.csv( file = paste("riskcont/", names[6], ".csv" , sep="") );54riskcontS7 = read.csv( file = paste("riskcont/", names[7], ".csv" , sep="") );55riskcontS8 = read.csv( file = paste("riskcont/", names[8], ".csv" , sep="") );5657# Relabel rownames in the plots5859if(frequency=="yearly"){60names = seq( (firstyear+estyears),lastyear+1,1)61}else{62names = paste(rep( seq( (firstyear+estyears),lastyear,1) , each=4),c("Q1","Q2","Q3","Q4"),sep="")63names = c( names , paste( lastyear+1, "Q1" , sep="" ) )64names = names[firstquarter:(length(names)-4+lastquarter)]65}666768rownames(weightsS1) = rownames(weightsS2) = rownames(weightsS3) = rownames(weightsS4) = names;69rownames(weightsS5) = rownames(weightsS6) = rownames(weightsS7) = rownames(weightsS8) = names;7071rownames(riskcontS1) = rownames(riskcontS2) = rownames(riskcontS3) = rownames(riskcontS4) = names;72rownames(riskcontS5) = rownames(riskcontS6) = rownames(riskcontS7) = rownames(riskcontS8) = names;737475colorset = gray( seq(0,(cAssets-1),1)/cAssets ) ;76#due to rounding, the sum of the risk contributions is sometimes 1 + epsilon: avoid this in plot7778riskcontS1 = riskcontS1/rowSums(riskcontS1); riskcontS2 = riskcontS2/rowSums(riskcontS2);79riskcontS3 = riskcontS3/rowSums(riskcontS3); riskcontS4 = riskcontS4/rowSums(riskcontS4);80riskcontS5 = riskcontS5/rowSums(riskcontS5); riskcontS6 = riskcontS6/rowSums(riskcontS6);81riskcontS7 = riskcontS7/rowSums(riskcontS7); riskcontS8 = riskcontS8/rowSums(riskcontS8);8283w.names = c( "US bond" , "S&P 500", "EAFE" , "GSCI" )84l = 285mar1 =c(2,l,2,1.1)86mar2 =c(0,l,2,1)87mar3 = c(3,l+1,3,0.1)88mar4 = c(2,l+1,2,0.1)8990# Stacked weights plot:91postscript('stackedweightsriskcont_benchmark.eps')92layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)9394par(mar=mar3 , cex.main=1)95chart.StackedBar2(weightsS5,col=colorset,space=0, main = namelabels[5], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T, legend.loc = NULL,ylim=c(0,1),border = F )96chart.StackedBar2(weightsS1,col=colorset,space=0, main = namelabels[1], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )97chart.StackedBar2(weightsS2,col=colorset,space=0, main = namelabels[2], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )9899par(mar=mar1 , cex.main=1)100plot.new()101legend("center",legend=w.names,fill=colorset,ncol=4)102103104105par(mar=mar3 , cex.main=1)106chart.StackedBar2(riskcontS5,col=colorset,space=0, main = namelabels[5], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )107chart.StackedBar2(riskcontS1,col=colorset,space=0, main = namelabels[1], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )108chart.StackedBar2(riskcontS2,col=colorset,space=0, main = namelabels[2], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )109110dev.off()111112postscript('MinCVaR_alternatives.eps')113layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)114115par(mar=mar3 , cex.main=1)116chart.StackedBar2(weightsS3,col=colorset,space=0, main = namelabels[3], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T, legend.loc = NULL,ylim=c(0,1),border = F )117chart.StackedBar2(weightsS4,col=colorset,space=0, main = namelabels[4], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )118chart.StackedBar2(weightsS6,col=colorset,space=0, main = namelabels[6], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )119120par(mar=mar1 , cex.main=1)121plot.new()122legend("center",legend=w.names,fill=colorset,ncol=4)123par(mar=mar3 , cex.main=1)124125chart.StackedBar2(riskcontS3,col=colorset,space=0, main = namelabels[3], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )126chart.StackedBar2(riskcontS4,col=colorset,space=0, main = namelabels[4], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )127chart.StackedBar2(riskcontS6,col=colorset,space=0, main = namelabels[6], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )128129dev.off()130131postscript('MinCVaR_alternatives.eps')132layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)133134par(mar=mar3 , cex.main=1)135chart.StackedBar2(weightsS3,col=colorset,space=0, main = namelabels[3], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T, legend.loc = NULL,ylim=c(0,1),border = F )136chart.StackedBar2(weightsS4,col=colorset,space=0, main = namelabels[4], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )137chart.StackedBar2(weightsS6,col=colorset,space=0, main = namelabels[6], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )138139par(mar=mar1 , cex.main=1)140plot.new()141legend("center",legend=w.names,fill=colorset,ncol=4)142par(mar=mar3 , cex.main=1)143144chart.StackedBar2(riskcontS3,col=colorset,space=0, main = namelabels[3], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )145chart.StackedBar2(riskcontS4,col=colorset,space=0, main = namelabels[4], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )146chart.StackedBar2(riskcontS6,col=colorset,space=0, main = namelabels[6], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )147148dev.off()149150postscript('MinCVaRConcentration_alternatives.eps')151layout( matrix( c(1,2,3,4,5,3), ncol = 2 ) , height= c(1.5,1.5,0.7), width=1)152153par(mar=mar3 , cex.main=1)154chart.StackedBar2(weightsS8,col=colorset,space=0, main = namelabels[8], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )155chart.StackedBar2(weightsS7,col=colorset,space=0, main = namelabels[7], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T, legend.loc = NULL,ylim=c(0,1),border = F )156157par(mar=mar1 , cex.main=1)158plot.new()159legend("center",legend=w.names,fill=colorset,ncol=4)160par(mar=mar3 , cex.main=1)161chart.StackedBar2(riskcontS8,col=colorset,space=0, main = namelabels[8], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )162chart.StackedBar2(riskcontS7,col=colorset,space=0, main = namelabels[7], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )163164dev.off()165166postscript('stackedweightsriskcont_benchmark.eps')167layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)168169par(mar=mar3 , cex.main=1)170chart.StackedBar2(weightsS5,col=colorset,space=0, main = namelabels[1], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T, legend.loc = NULL,ylim=c(0,1),border = F )171chart.StackedBar2(weightsS1,col=colorset,space=0, main = namelabels[1], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )172chart.StackedBar2(weightsS2,col=colorset,space=0, main = namelabels[2], ylab="Weight allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )173174par(mar=mar1 , cex.main=1)175plot.new()176legend("center",legend=w.names,fill=colorset,ncol=4)177par(mar=mar3 , cex.main=1)178179chart.StackedBar2(riskcontS5,col=colorset,space=0, main = namelabels[5], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )180chart.StackedBar2(riskcontS1,col=colorset,space=0, main = namelabels[1], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )181chart.StackedBar2(riskcontS2,col=colorset,space=0, main = namelabels[2], ylab="CVaR allocation", las=1, l=3.9, r=0, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )182183dev.off()184185# Stacked weights plot:186postscript('stackedweights_palermo.eps')187layout( matrix( c(1,2,3,4,5,3,6,7,3), ncol = 3 ) , height= c(1.5,1.5,0.7), width=1)188189par(mar=mar3 , cex.main=1)190chart.StackedBar2(weightsS1,col=colorset,space=0, main = namelabels[1] , ylab="", las=1, l=2.5, r=0.4, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T, legend.loc = NULL,ylim=c(0,1),border = F )191192chart.StackedBar2(weightsS2,col=colorset,space=0, main = namelabels[2], ylab="", las=1, l=2.5, r=0.4, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )193194par(mar=mar1 , cex.main=1)195plot.new()196legend("center",legend=w.names,fill=colorset,ncol=4)197par(mar=mar3 , cex.main=1)198199chart.StackedBar2(weightsS3,col=colorset,space=0, main = namelabels[3], ylab="", las=1, l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )200chart.StackedBar2(weightsS4,col=colorset,space=0, main = namelabels[4], ylab="", las=1, l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )201202203chart.StackedBar2(weightsS6,col=colorset,space=0, main = namelabels[6], ylab="", las=1,l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )204chart.StackedBar2(weightsS7,col=colorset,space=0, main = namelabels[7], ylab="", las=1,l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )205206dev.off()207208# Stacked percentage CVaR plot:209210postscript('stackedpercCVaR_palermo.eps')211212layout( matrix( c(1,2,3,4,5,3,6,7,3), ncol = 3 ) , height= c(1.5,1.5,0.7), width=1)213214par(mar=mar3 , cex.main=1 )215chart.StackedBar2(riskcontS1,col=colorset,space=0, main = namelabels[1] , ylab="", las=1, l=2.5, r=0.4, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T, legend.loc = NULL,ylim=c(0,1),border = F )216chart.StackedBar2(riskcontS5,col=colorset,space=0, main = namelabels[5], ylab="", las=1, l=2.5, r=0.4, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )217218par(mar=mar1 , cex.main=1)219plot.new()220legend("center",legend=w.names,fill=colorset,ncol=4)221par(mar=mar3 , cex.main=1)222223224chart.StackedBar2(riskcontS3,col=colorset,space=0, main = namelabels[3], ylab="", las=1, l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )225chart.StackedBar2(riskcontS4,col=colorset,space=0, main = namelabels[4], ylab="", las=1, l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )226chart.StackedBar2(riskcontS6,col=colorset,space=0, main = namelabels[6], ylab="", las=1,l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )227chart.StackedBar2(riskcontS7,col=colorset,space=0, main = namelabels[7], ylab="", las=1,l=2.4, r=0.5, cex.axis=1, cex.lab=1, cex.main=1, axisnames=T,legend.loc = NULL,ylim=c(0,1),border = F )228229dev.off()230231232