Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/R_interpretation/old/stackedweightriskcontributionplot_Palermo.R
1433 views
1
#################################################################################
2
# Create stacked weights and risk contributions plot
3
#################################################################################
4
5
# ! Set your working directory (folder containing the subfolders R_allocation, R_interpretation, data, weights, etc)
6
7
setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs")
8
9
10
# Options:
11
################
12
13
# specify the number of years used for the estimation
14
estyears = 8;
15
16
# Load programs
17
18
source("R_interpretation/chart.StackedBar.R");
19
library(zoo); library(PerformanceAnalytics)
20
21
# number of risky assets
22
firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;
23
cAssets = 4
24
25
# "MinRisk_ReturnTarget" "EqualRisk"
26
# "MinRiskConc_ReturnTarget"
27
28
names = c( "MinRisk" , "MinRiskConc" ,"MinRisk_PositionLimit" ,
29
"MinRisk_RiskLimit" , "EqualWeight" ,
30
"MinRisk_ReturnTarget" , "MinRiskConc_ReturnTarget" , "MinRiskConc_PositionLimit" )
31
32
namelabels = c( "Min CVaR" , "Min CVaR Concentration" , "Min CVaR + Position Limit" ,
33
"Min CVaR + CVaR Alloc Limit" , "Equal Weight" ,
34
"Min CVaR + Return Target" , "Min CVaR Concentration + Return Target" , "Min CVaR Concentration + Position Limit" )
35
36
# frequency of rebalancing: yearly of quarterly
37
frequency = "quarterly"
38
# Load portfolio weights:
39
weightsS1 = read.csv( file = paste("weights/", names[1], ".csv" , sep="") );
40
weightsS2 = read.csv( file = paste("weights/", names[2], ".csv" , sep="") );
41
weightsS3 = read.csv( file = paste("weights/", names[3], ".csv" , sep="") );
42
weightsS4 = read.csv( file = paste("weights/", names[4], ".csv" , sep="") );
43
weightsS5 = read.csv( file = paste("weights/", names[5], ".csv" , sep="") );
44
weightsS6 = read.csv( file = paste("weights/", names[6], ".csv" , sep="") );
45
weightsS7 = read.csv( file = paste("weights/", names[7], ".csv" , sep="") );
46
weightsS8 = read.csv( file = paste("weights/", names[8], ".csv" , sep="") );
47
48
# Load percentage risk contributions:
49
riskcontS1 = read.csv( file = paste("riskcont/", names[1], ".csv" , sep="") );
50
riskcontS2 = read.csv( file = paste("riskcont/", names[2], ".csv" , sep="") );
51
riskcontS3 = read.csv( file = paste("riskcont/", names[3], ".csv" , sep="") );
52
riskcontS4 = read.csv( file = paste("riskcont/", names[4], ".csv" , sep="") );
53
riskcontS5 = read.csv( file = paste("riskcont/", names[5], ".csv" , sep="") );
54
riskcontS6 = read.csv( file = paste("riskcont/", names[6], ".csv" , sep="") );
55
riskcontS7 = read.csv( file = paste("riskcont/", names[7], ".csv" , sep="") );
56
riskcontS8 = read.csv( file = paste("riskcont/", names[8], ".csv" , sep="") );
57
58
# Relabel rownames in the plots
59
60
if(frequency=="yearly"){
61
names = seq( (firstyear+estyears),lastyear+1,1)
62
}else{
63
names = paste(rep( seq( (firstyear+estyears),lastyear,1) , each=4),c("Q1","Q2","Q3","Q4"),sep="")
64
names = c( names , paste( lastyear+1, "Q1" , sep="" ) )
65
names = names[firstquarter:(length(names)-4+lastquarter)]
66
}
67
68
69
rownames(weightsS1) = rownames(weightsS2) = rownames(weightsS3) = rownames(weightsS4) = names;
70
rownames(weightsS5) = rownames(weightsS6) = rownames(weightsS7) = rownames(weightsS8) = names;
71
72
rownames(riskcontS1) = rownames(riskcontS2) = rownames(riskcontS3) = rownames(riskcontS4) = names;
73
rownames(riskcontS5) = rownames(riskcontS6) = rownames(riskcontS7) = rownames(riskcontS8) = names;
74
75
76
colorset = gray( seq(0,(cAssets-1),1)/cAssets ) ;
77
#due to rounding, the sum of the risk contributions is sometimes 1 + epsilon: avoid this in plot
78
79
riskcontS1 = riskcontS1/rowSums(riskcontS1); riskcontS2 = riskcontS2/rowSums(riskcontS2);
80
riskcontS3 = riskcontS3/rowSums(riskcontS3); riskcontS4 = riskcontS4/rowSums(riskcontS4);
81
riskcontS5 = riskcontS5/rowSums(riskcontS5); riskcontS6 = riskcontS6/rowSums(riskcontS6);
82
riskcontS7 = riskcontS7/rowSums(riskcontS7); riskcontS8 = riskcontS8/rowSums(riskcontS8);
83
84
w.names = c( "US bond" , "S&P 500", "EAFE" , "GSCI" )
85
l = 2
86
mar1 =c(2,l,2,1.1)
87
mar2 =c(0,l,2,1)
88
mar3 = c(3,l+1,3,0.1)
89
mar4 = c(2,l+1,2,0.1)
90
91
# Stacked weights plot:
92
postscript('stackedweightsriskcont_benchmark.eps')
93
layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)
94
95
par(mar=mar3 , cex.main=1)
96
chart.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 )
97
chart.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 )
98
chart.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 )
99
100
par(mar=mar1 , cex.main=1)
101
plot.new()
102
legend("center",legend=w.names,fill=colorset,ncol=4)
103
104
105
106
par(mar=mar3 , cex.main=1)
107
chart.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 )
108
chart.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 )
109
chart.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 )
110
111
dev.off()
112
113
postscript('MinCVaR_alternatives.eps')
114
layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)
115
116
par(mar=mar3 , cex.main=1)
117
chart.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 )
118
chart.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 )
119
chart.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 )
120
121
par(mar=mar1 , cex.main=1)
122
plot.new()
123
legend("center",legend=w.names,fill=colorset,ncol=4)
124
par(mar=mar3 , cex.main=1)
125
126
chart.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 )
127
chart.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 )
128
chart.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 )
129
130
dev.off()
131
132
postscript('MinCVaR_alternatives.eps')
133
layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)
134
135
par(mar=mar3 , cex.main=1)
136
chart.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 )
137
chart.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 )
138
chart.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 )
139
140
par(mar=mar1 , cex.main=1)
141
plot.new()
142
legend("center",legend=w.names,fill=colorset,ncol=4)
143
par(mar=mar3 , cex.main=1)
144
145
chart.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 )
146
chart.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 )
147
chart.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 )
148
149
dev.off()
150
151
postscript('MinCVaRConcentration_alternatives.eps')
152
layout( matrix( c(1,2,3,4,5,3), ncol = 2 ) , height= c(1.5,1.5,0.7), width=1)
153
154
par(mar=mar3 , cex.main=1)
155
chart.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 )
156
chart.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 )
157
158
par(mar=mar1 , cex.main=1)
159
plot.new()
160
legend("center",legend=w.names,fill=colorset,ncol=4)
161
par(mar=mar3 , cex.main=1)
162
chart.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 )
163
chart.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 )
164
165
dev.off()
166
167
postscript('stackedweightsriskcont_benchmark.eps')
168
layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height= c(1.5,1.5,1.5,0.7), width=1)
169
170
par(mar=mar3 , cex.main=1)
171
chart.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 )
172
chart.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 )
173
chart.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 )
174
175
par(mar=mar1 , cex.main=1)
176
plot.new()
177
legend("center",legend=w.names,fill=colorset,ncol=4)
178
par(mar=mar3 , cex.main=1)
179
180
chart.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 )
181
chart.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 )
182
chart.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 )
183
184
dev.off()
185
186
# Stacked weights plot:
187
postscript('stackedweights_palermo.eps')
188
layout( matrix( c(1,2,3,4,5,3,6,7,3), ncol = 3 ) , height= c(1.5,1.5,0.7), width=1)
189
190
par(mar=mar3 , cex.main=1)
191
chart.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 )
192
193
chart.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 )
194
195
par(mar=mar1 , cex.main=1)
196
plot.new()
197
legend("center",legend=w.names,fill=colorset,ncol=4)
198
par(mar=mar3 , cex.main=1)
199
200
chart.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 )
201
chart.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 )
202
203
204
chart.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 )
205
chart.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 )
206
207
dev.off()
208
209
# Stacked percentage CVaR plot:
210
211
postscript('stackedpercCVaR_palermo.eps')
212
213
layout( matrix( c(1,2,3,4,5,3,6,7,3), ncol = 3 ) , height= c(1.5,1.5,0.7), width=1)
214
215
par(mar=mar3 , cex.main=1 )
216
chart.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 )
217
chart.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 )
218
219
par(mar=mar1 , cex.main=1)
220
plot.new()
221
legend("center",legend=w.names,fill=colorset,ncol=4)
222
par(mar=mar3 , cex.main=1)
223
224
225
chart.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 )
226
chart.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 )
227
chart.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 )
228
chart.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 )
229
230
dev.off()
231
232