Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/R_interpretation/old/stackedweightriskcontributionplot.R
1433 views
1
#################################################################################
2
# Create stacked 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
# Choose data
14
15
datacase = "equitybondscommodity"# "intequitybonds" # equitybondscommodity
16
17
percriskcontribcriterion = "mES"
18
19
# specify the optimization criteria you want to analyze
20
# crit1 = "StdDev"; crit2="SR.StdDev"
21
crit1 = "mES"; crit2="SR.mES"
22
23
# specify the number of years used for the estimation
24
estyears = 4;
25
26
# specify the risk budget constraint
27
RBconstraint = 0.4;
28
29
# Load programs
30
31
source("R_interpretation/chart.StackedBar.R");
32
library(zoo); library(PerformanceAnalytics)
33
34
# number of risky assets
35
firstyear = 1976 ; firstquarter = 1; lastyear = 2009; lastquarter = 2;
36
cAssets = 4
37
38
39
# frequency of rebalancing: yearly of quarterly
40
frequency = "quarterly"
41
# Load portfolio weights:
42
criterion = paste(crit1,".",estyears,"yr",sep="")
43
weightsEW = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/EW.csv" , sep="") );
44
weightsconst.crit1 = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/", criterion , "-Inf",RBconstraint,".csv" , sep="") );
45
weightsInf.crit1 = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/",criterion , "-InfInf.csv" , sep="") );
46
weightsconst.crit1 = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/", criterion , "-Inf",RBconstraint,".csv" , sep="") );
47
weightsERisk = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/unconstrained/", criterion , 0.22,0.28,".csv" , sep="") );
48
weightsMinCVaR = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/unconstrained/", criterion , 0.22,0.28,".csv" , sep="") );
49
50
# Load percentage risk contributions:
51
52
riskcontribEW = read.csv( file = paste("riskcont/", percriskcontribcriterion,"/",datacase,"/",frequency,"/EW.csv" , sep="") );
53
criterion = paste("riskcont/",percriskcontribcriterion,"/",datacase,"/",frequency,"/",crit1,".",estyears,"yr",sep="")
54
riskcontribInf.crit1 = read.csv( file = paste( criterion , "-InfInf.csv" , sep="") );
55
riskcontribconst.crit1 = read.csv( file = paste( criterion , "-Inf",RBconstraint,".csv" , sep="") );
56
criterion = paste("riskcont/",percriskcontribcriterion,"/",datacase,"/",frequency,"/unconstrained/",crit1,".",estyears,"yr",sep="")
57
riskcontribERisk = read.csv( file = paste( criterion , 0.22,0.28,".csv" , sep="") );
58
riskcontribMinCVaR = read.csv( file = paste( criterion , -Inf,Inf,".csv" , sep="") );
59
60
# Is there cash in any of the portfolios due to infeasibility of the risk budget constraints?
61
62
if( any(weightsconst.crit1[,(1+cAssets)]!=0) | any(weightsERisk[,(1+cAssets)]!=0) ){
63
if(datacase=="intequitybonds"){ w.names = c( "US bond" , "MSCI Europe" , "MSCI Japan", "MSCI US" , "T-Bill" ) };
64
if(datacase=="equitybondscommodity"){ w.names = c( "US bond" , "S&P 500" , "MSCI EAFE" , "S&P GSCI", "T-Bill" ) };
65
colorset = gray( seq(0,(cAssets),1)/5 ) ; #c(gray.colors(11,start=0,end=1)
66
}else{
67
if(datacase=="intequitybonds"){ w.names = c( "US bond" , "MSCI Europe" , "MSCI Japan", "MSCI US" ) };
68
if(datacase=="equitybondscommodity"){ w.names = c( "US bond" , "S&P 500", "MSCI EAFE" , "S&P GSCI" ) };
69
weightsInf.crit1 = weightsInf.crit1[,1:cAssets];
70
weightsconst.crit1 = weightsconst.crit1[,1:cAssets];
71
weightsERisk = weightsERisk[,1:cAssets];
72
weightsMinCVaR = weightsMinCVaR[,1:cAssets];
73
colorset = gray( seq(0,(cAssets-1),1)/cAssets ) ; #c(gray.colors(11,start=0,end=1)
74
}
75
76
# Relabel rownames in the plots
77
78
if(frequency=="yearly"){
79
names = seq( (firstyear+estyears),lastyear+1,1)
80
}else{
81
names = paste(rep( seq( (firstyear+estyears),lastyear,1) , each=4),c("Q1","Q2","Q3","Q4"),sep="")
82
names = c( names , paste( lastyear+1, "Q1" , sep="" ) )
83
names = names[firstquarter:(length(names)-4+lastquarter)]
84
}
85
86
rownames(riskcontribEW) = rownames(riskcontribInf.crit1) = rownames(riskcontribconst.crit1) = rownames(riskcontribERisk) = rownames(riskcontribMinCVaR) = names;
87
88
#due to rounding, the sum of the risk contributions is sometimes 1 + epsilon: avoid this in plot
89
90
riskcontribEW = riskcontribEW/rowSums(riskcontribEW)
91
92
riskcontribInf.crit1 = riskcontribInf.crit1/rowSums(riskcontribInf.crit1);
93
riskcontribconst.crit1 = riskcontribconst.crit1/rowSums(riskcontribconst.crit1);
94
riskcontribERisk = riskcontribERisk/rowSums(riskcontribERisk)
95
riskcontribMinCVaR = riskcontribMinCVaR/rowSums(riskcontribMinCVaR)
96
97
# make weights versus risk allocation plot
98
99
postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontinf.eps",sep="") )
100
101
102
layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height=c(0.15,2,2*1.4,0.7), width=1)
103
par(mar=c(0.1,4,1,1.1) , cex.main=1)
104
plot.new()
105
title( main = "min StdDev" , cex.main=1.3 )
106
par(mar=c(0,4,2,1) , cex.main=1)
107
barplot(t(weightsInf.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="",
108
cex.axis=1, cex.lab=1, cex.main=1, axisnames=F )
109
110
par(mar=c(1,4,1,1.1))
111
chart.StackedBar2(riskcontribInf.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,
112
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.2,1.2))
113
par(mar=c(0.1,4,1,1.1))
114
plot.new()
115
legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)
116
117
par(mar=c(0.1,4,1,1.1) , cex.main=1)
118
plot.new()
119
title( main = "max Sharpe ratio", cex.main=1.3 )
120
par(mar=c(0,4,2,1.1) , cex.main=1)
121
barplot(t(weightsInf.crit2),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)
122
par(mar=c(1,4,1,1.1))
123
chart.StackedBar2(riskcontribInf.crit2,axisnames=T, colorset=colorset,space=0, cex.names=1,
124
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.2,1.2))
125
dev.off()
126
127
# make weights versus risk allocation plot
128
129
postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontconst.eps",sep="") )
130
131
layout( matrix( c(1,2,3,4,5,6,7,4), ncol = 2 ) , height=c(0.15,2,2*1.2,0.6), width=1)
132
par(mar=c(0.1,4,1,1.1) , cex.main=1)
133
plot.new()
134
title( main = "min StdDev" , cex.main=1.3 )
135
par(mar=c(0,4,2,1) , cex.main=1)
136
barplot(t(weightsconst.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)
137
138
par(mar=c(1,4,2,1.1))
139
chart.StackedBar2(riskcontribconst.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,
140
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL)
141
par(mar=c(0.1,4,1,1.1))
142
plot.new()
143
legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)
144
145
par(mar=c(0.1,4,1,1.1) , cex.main=1)
146
plot.new()
147
title( main = "max Sharpe ratio", cex.main=1.3 )
148
par(mar=c(0,4,2,1.1) , cex.main=1)
149
barplot(t(weightsconst.crit2),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)
150
par(mar=c(1,4,1,1.1))
151
chart.StackedBar2(riskcontribconst.crit2,axisnames=T, colorset=colorset,space=0, cex.names=1,
152
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL)
153
154
dev.off()
155
156
157
postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontconstminCVaR.eps",sep="") )
158
159
layout( 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)
160
par(mar=c(0.1,4,1,1.1) , cex.main=1)
161
plot.new()
162
title( main = "Investment weight constrained" , cex.main=1.3 )
163
par(mar=c(0,4,2,1) , cex.main=1)
164
barplot(t(weightsInf.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)
165
166
par(mar=c(1,4,2,1.1))
167
chart.StackedBar2(riskcontribInf.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,
168
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4))
169
par(mar=c(0.1,4,1,1.1))
170
plot.new()
171
legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)
172
173
par(mar=c(0.1,4,1,1.1) , cex.main=1)
174
plot.new()
175
title( main = "Risk budget constrained", cex.main=1.3 )
176
par(mar=c(0,4,2,1.1) , cex.main=1)
177
barplot(t(weightsconst.crit1),col=colorset,space=0, cex=.7, main = "Weight allocation", ylab="", cex.axis=1, cex.lab=1, cex.main=1, axisnames=F)
178
par(mar=c(1,4,1,1.1))
179
chart.StackedBar2(riskcontribconst.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,
180
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4) )
181
182
183
dev.off()
184
185
mar1 =c(0.1,l,1,1.1)
186
mar2 =c(0,l,2,1)
187
mar3 = c(1,l,2,1.1)
188
postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/stackedweightriskcontconstminCVaR.eps",sep="") )
189
l = 4
190
191
layout( 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)
192
193
par(mar=mar1 , cex.main=1)
194
plot.new()
195
title( main = "Equal-weighted" , cex.main=1.3 )
196
par(mar=mar2 , cex.main=1)
197
barplot(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 )
198
199
par(mar=mar3)
200
chart.StackedBar2(riskcontribEW,axisnames=T, colorset=colorset,space=0, cex.names=1.3,
201
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F)
202
203
par(mar=mar1)
204
205
plot.new()
206
legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)
207
208
plot.new()
209
title( main = "Min CVaR" , cex.main=1.3 )
210
par(mar=mar2 , cex.main=1)
211
barplot(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 )
212
213
par(mar=mar3)
214
chart.StackedBar2(riskcontribMinCVaR,axisnames=T, colorset=colorset,space=0, cex.names=1.3,
215
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F)
216
217
par(mar=mar1 , cex.main=1)
218
plot.new()
219
title( main = "Min CVaR, w <= 0.4" , cex.main=1.3 )
220
par(mar=c(0,l,2,1) , cex.main=1)
221
barplot(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)
222
223
par(mar=mar2)
224
chart.StackedBar2(riskcontribInf.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,
225
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F)
226
par(mar=mar3)
227
228
# plot.new()
229
#legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)
230
231
par(mar=mar1 , cex.main=1)
232
plot.new()
233
title( main = "Min CVaR, %Contrib CVaR <= 0.4", cex.main=1.3 )
234
par(mar=mar2 , cex.main=1)
235
barplot(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)
236
par(mar=mar3)
237
chart.StackedBar2(riskcontribconst.crit1,axisnames=T, colorset=colorset,space=0, cex.names=1,
238
main = "Risk allocation", ylab="",cex.axis=1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F )
239
240
241
par(mar=mar1 , cex.main=1)
242
plot.new()
243
title( main = "Equal %Contrib to CVaR", cex.main=1.3 )
244
par(mar=mar2 , cex.main=1)
245
barplot(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)
246
par(mar=mar3)
247
chart.StackedBar2(riskcontribERisk,axisnames=T, colorset=colorset,space=0, cex.names=1,
248
main = "Risk allocation", ylab="",cex.axis=1.1, cex.lab=1, cex.main=1,legend.loc = NULL, ylim=c(-0.4,1.4),border = F )
249
250
dev.off()
251
252
253
254
255
256
257