Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/R_interpretation/old/stackedweightriskcontributionplot_Oct.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 = 2009; lastquarter = 4;
23
cAssets = 4
24
25
names = c( "EqualWeight" , "EqualRisk" ,
26
"MinRisk" , "MinRisk_PositionLimit" , "MinRisk_RiskLimit" , "MinRisk_ReturnTarget",
27
"MinRiskConc" , "MinRiskConc_PositionLimit" , "MinRiskConc_ReturnTarget")
28
29
namelabels = c( "Equal Weight" , "Equal Risk" ,
30
"Min CVaR" , "Min CVaR + Position Limit" , "Min CVaR + CVaR Alloc Limit" , "Min CVaR + Return Target" ,
31
"Min CVaR Conc" , "Min CVaR Conc + Position Limit" , "Min CVaR Conc + Return Target" )
32
33
# frequency of rebalancing: yearly of quarterly
34
frequency = "quarterly"
35
# Load portfolio weights:
36
weightsS1 = read.csv( file = paste("weights/", names[1], ".csv" , sep="") );
37
weightsS2 = read.csv( file = paste("weights/", names[2], ".csv" , sep="") );
38
weightsS3 = read.csv( file = paste("weights/", names[3], ".csv" , sep="") );
39
weightsS4 = read.csv( file = paste("weights/", names[4], ".csv" , sep="") );
40
weightsS5 = read.csv( file = paste("weights/", names[5], ".csv" , sep="") );
41
weightsS6 = read.csv( file = paste("weights/", names[6], ".csv" , sep="") );
42
weightsS7 = read.csv( file = paste("weights/", names[7], ".csv" , sep="") );
43
weightsS8 = read.csv( file = paste("weights/", names[8], ".csv" , sep="") );
44
weightsS9 = read.csv( file = paste("weights/", names[9], ".csv" , sep="") );
45
46
47
# Load percentage risk contributions:
48
riskcontS1 = read.csv( file = paste("riskcont/", names[1], ".csv" , sep="") );
49
riskcontS2 = read.csv( file = paste("riskcont/", names[2], ".csv" , sep="") );
50
riskcontS3 = read.csv( file = paste("riskcont/", names[3], ".csv" , sep="") );
51
riskcontS4 = read.csv( file = paste("riskcont/", names[4], ".csv" , sep="") );
52
riskcontS5 = read.csv( file = paste("riskcont/", names[5], ".csv" , sep="") );
53
riskcontS6 = read.csv( file = paste("riskcont/", names[6], ".csv" , sep="") );
54
riskcontS7 = read.csv( file = paste("riskcont/", names[7], ".csv" , sep="") );
55
riskcontS8 = read.csv( file = paste("riskcont/", names[8], ".csv" , sep="") );
56
riskcontS9 = read.csv( file = paste("riskcont/", names[9], ".csv" , sep="") );
57
58
59
# Relabel rownames in the plots
60
61
if(frequency=="yearly"){
62
names = seq( (firstyear+estyears),lastyear+1,1)
63
}else{
64
names = paste(rep( seq( (firstyear+estyears),lastyear,1) , each=4),c("Q1","Q2","Q3","Q4"),sep="")
65
names = c( names , paste( lastyear+1, "Q1" , sep="" ) )
66
names = names[firstquarter:(length(names)-4+lastquarter)]
67
}
68
69
70
rownames(weightsS1) = rownames(weightsS2) = rownames(weightsS3) = rownames(weightsS4) = rownames(weightsS5) = names;
71
rownames(weightsS6) = rownames(weightsS7) = rownames(weightsS8) = rownames(weightsS9) = names;
72
rownames(riskcontS1) = rownames(riskcontS2) = rownames(riskcontS3) = rownames(riskcontS4) = rownames(riskcontS5) = names;
73
rownames(riskcontS6) = rownames(riskcontS7) = rownames(riskcontS8) = rownames(riskcontS9) = names;
74
75
colorset = gray( seq(0,(cAssets-1),1)/cAssets ) ;
76
#due to rounding, the sum of the risk contributions is sometimes 1 + epsilon: avoid this in plot
77
78
riskcontS1 = riskcontS1/rowSums(riskcontS1); riskcontS2 = riskcontS2/rowSums(riskcontS2);
79
riskcontS3 = riskcontS3/rowSums(riskcontS3); riskcontS4 = riskcontS4/rowSums(riskcontS4);
80
riskcontS5 = riskcontS5/rowSums(riskcontS5); riskcontS6 = riskcontS6/rowSums(riskcontS6);
81
riskcontS7 = riskcontS7/rowSums(riskcontS7); riskcontS8 = riskcontS8/rowSums(riskcontS8);
82
riskcontS9 = riskcontS9/rowSums(riskcontS9);
83
w.names = c( "US bond" , "S&P 500", "EAFE" , "GSCI" )
84
l = 2
85
mar1 =c(2,l,2,1.1)
86
mar2 =c(0,l,2,1)
87
mar3 = c(3,l+1,3,0.1)
88
89
# Stacked weights plot:
90
postscript('stackedweights.eps')
91
layout( matrix( c(1,2,3,4,5,6,7,4,8,9,10,4), ncol = 3 ) , height= c(1.5,1.5,1.5,0.7), width=1)
92
93
par(mar=mar3 , cex.main=1)
94
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 )
95
96
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 )
97
98
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 )
99
100
par(mar=mar1 , cex.main=1)
101
plot.new()
102
legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)
103
par(mar=mar3 , cex.main=1)
104
105
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 )
106
chart.StackedBar2(weightsS5,col=colorset,space=0, main = namelabels[5], 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 )
107
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 )
108
109
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 )
110
chart.StackedBar2(weightsS8,col=colorset,space=0, main = namelabels[8], 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 )
111
chart.StackedBar2(weightsS9,col=colorset,space=0, main = namelabels[9], 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 )
112
113
dev.off()
114
115
# Stacked percentage CVaR plot:
116
117
postscript('stackedpercCVaR.eps')
118
119
layout( matrix( c(1,2,3,4,5,6,7,4,8,9,10,4), ncol = 3 ) , height= c(1.5,1.5,1.5,0.7), width=1)
120
121
par(mar=mar3 , cex.main=1)
122
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 )
123
124
chart.StackedBar2(riskcontS2,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 )
125
126
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 )
127
128
par(mar=mar1 , cex.main=1)
129
plot.new()
130
legend("center",legend=w.names,fill=colorset,cex=1,ncol=4)
131
par(mar=mar3 , cex.main=1)
132
133
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 )
134
chart.StackedBar2(riskcontS5,col=colorset,space=0, main = namelabels[5], 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 )
135
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 )
136
137
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 )
138
chart.StackedBar2(riskcontS8,col=colorset,space=0, main = namelabels[8], 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 )
139
chart.StackedBar2(riskcontS9,col=colorset,space=0, main = namelabels[9], 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 )
140
141
dev.off()
142
143