Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/R_interpretation/stackedweightriskcontributionplot.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
#setwd("c:/Documents and Settings/n06054/Desktop/risk budget programs")
9
10
11
# Options:
12
################
13
14
# specify the number of years used for the estimation
15
estyears = 5;
16
Tstart = 1+(8-estyears)*4
17
CC = T
18
# Load programs
19
20
source("R_interpretation/chart.StackedBar.R");
21
library(zoo); library(PerformanceAnalytics)
22
23
# number of risky assets
24
firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;
25
cAssets = 4
26
27
# "MinRisk_ReturnTarget" "EqualRisk"
28
# "MinRiskConc_ReturnTarget"
29
30
names = c( "EqualWeight" , "MinRisk" , "MinRisk_PositionLimit" , "MinRisk_RiskLimit" ,
31
"MinRiskConc" , "MinRiskConc_PositionLimit", "EqualRisk" ,
32
"MinRisk_ReturnTarget", "MinRiskConc_ReturnTarget" )
33
if(CC){ names[1:length(names)] = paste( names[1:length(names)], "_CC", sep="") }
34
35
namelabels = c( "Equal-Weight" , "Min CVaR" , "Min CVaR + 40% Position Limit" , "Min CVaR + 40% CVaR Alloc Limit" ,
36
"Min CVaR Concentration" , "Min CVaR Concentration + 40% Position Limit", "Min CVaR + ERC constraint" , "Min CVaR + Return Target" , "Min CVaR Conc + Return Target" )
37
38
riskcrit = "mES"
39
40
# frequency of rebalancing: yearly of quarterly
41
frequency = "quarterly"
42
# Load portfolio weights:
43
weightsS1 = read.csv( file = paste("weights/", riskcrit , "/", names[1], ".csv" , sep="") );
44
weightsS2 = read.csv( file = paste("weights/", riskcrit , "/", names[2], ".csv" , sep="") );
45
weightsS3 = read.csv( file = paste("weights/", riskcrit , "/", names[3], ".csv" , sep="") );
46
weightsS4 = read.csv( file = paste("weights/", riskcrit , "/", names[4], ".csv" , sep="") );
47
weightsS5 = read.csv( file = paste("weights/", riskcrit , "/", names[5], ".csv" , sep="") );
48
weightsS6 = read.csv( file = paste("weights/", riskcrit , "/", names[6], ".csv" , sep="") );
49
weightsS7 = read.csv( file = paste("weights/", riskcrit , "/", names[7], ".csv" , sep="") );
50
51
# rownames(weightsS7) = weightsS7[,1] ;weightsS7 = weightsS7[,2:5]; colnames(weightsS7) = colnames(weightsS6)
52
# write.table( weightsS7 , file = paste("weights/", riskcrit , "/", names[7], ".csv" , sep="") ,
53
# append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")
54
55
56
# Load percentage risk contributions:
57
riskcontS1 = read.csv( file = paste("riskcont/", riskcrit , "/", names[1], ".csv" , sep="") );
58
riskcontS2 = read.csv( file = paste("riskcont/", riskcrit , "/", names[2], ".csv" , sep="") );
59
riskcontS3 = read.csv( file = paste("riskcont/", riskcrit , "/", names[3], ".csv" , sep="") );
60
riskcontS4 = read.csv( file = paste("riskcont/", riskcrit , "/", names[4], ".csv" , sep="") );
61
riskcontS5 = read.csv( file = paste("riskcont/", riskcrit , "/", names[5], ".csv" , sep="") );
62
riskcontS6 = read.csv( file = paste("riskcont/", riskcrit , "/", names[6], ".csv" , sep="") );
63
riskcontS7 = read.csv( file = paste("riskcont/", riskcrit , "/", names[7], ".csv" , sep="") );
64
65
#rownames(riskcontS7) = riskcontS7[,1] ;riskcontS7 = riskcontS7[,2:5]
66
# write.table( riskcontS7 , file = paste("riskcont/", riskcrit , "/", names[7], ".csv" , sep="") ,
67
# append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")
68
69
if(frequency=="yearly"){
70
rebal.names = seq( (firstyear+estyears),lastyear+1,1)
71
}else{
72
73
# Name labels using quarters:
74
rebal.names = paste(rep( seq( (firstyear+estyears),lastyear,1) , each=4),c("Q1","Q2","Q3","Q4"),sep="")
75
rebal.names = c( rebal.names , paste( lastyear+1, "Q1" , sep="" ) )
76
rebal.names = rebal.names[firstquarter:(length(rebal.names)-4+lastquarter)]
77
78
# Name labels using months:
79
nominalreturns = TRUE;
80
if(nominalreturns){ load(file="monthlyR.RData") }else{ load(file="monthlyR_real.RData") }
81
ep = endpoints(monthlyR,on='quarters')
82
# select those for estimation period
83
ep.start = ep[1:(length(ep)-estyears*4)]+1
84
from = time(monthlyR)[ep.start]
85
from = seq( as.Date(paste(firstyear,"-01-01",sep="")), as.Date(paste(lastyear-estyears,"-07-01",sep="")), by="3 month")
86
ep.end = ep[(1+estyears*4):length(ep)]
87
to = time(monthlyR)[ep.end]
88
rebal.names = as.yearmon(to+1)
89
90
91
}
92
93
94
rownames(weightsS1) = rownames(weightsS2) = rownames(weightsS3) = rownames(weightsS4) = rebal.names;
95
rownames(weightsS5) = rownames(weightsS6) = rownames(weightsS7) = rebal.names;
96
97
rownames(riskcontS1) = rownames(riskcontS2) = rownames(riskcontS3) = rownames(riskcontS4) = rebal.names;
98
rownames(riskcontS5) = rownames(riskcontS6) = rownames(riskcontS7) = rebal.names;
99
100
101
colorset = sort(gray( 1-(seq(1,cAssets,1)/(cAssets))^1.5 )) ;
102
#due to rounding, the sum of the risk contributions is sometimes 1 + epsilon: avoid this in plot
103
104
riskcontS1 = riskcontS1/rowSums(riskcontS1); riskcontS2 = riskcontS2/rowSums(riskcontS2);
105
riskcontS3 = riskcontS3/rowSums(riskcontS3); riskcontS4 = riskcontS4/rowSums(riskcontS4);
106
riskcontS5 = riskcontS5/rowSums(riskcontS5); riskcontS6 = riskcontS6/rowSums(riskcontS6);
107
riskcontS7 = riskcontS7/rowSums(riskcontS7);
108
109
Tend = nrow(weightsS1)
110
111
w.names = c( "US bond" , "S&P 500", "NAREIT" , "GSCI" )
112
l = 2
113
mar1 =c(2,l,2,1.1)
114
mar2 =c(0,l,2,1)
115
mar3 = c(3,l+1,3,0.1)
116
mar4 = c(2,l+1,2,0.1)
117
118
# Stacked weights plot:
119
if(!CC){
120
postscript('stackedweightsriskcont_benchmark.eps')
121
}else{
122
postscript('stackedweightsriskcont_benchmark_CC.eps')
123
}
124
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)
125
126
par(mar=mar3 , cex.main=1)
127
chart.StackedBar2(weightsS1[Tstart:Tend,],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 )
128
129
chart.StackedBar2(weightsS2[Tstart:Tend,],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 )
130
chart.StackedBar2(weightsS5[Tstart:Tend,],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 )
131
#chart.StackedBar2(weightsS7[Tstart:Tend,],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 )
132
133
par(mar=mar1 , cex.main=1)
134
plot.new()
135
legend("center",legend=w.names,col=colorset,lwd=6,ncol=4)
136
137
138
139
par(mar=mar3 , cex.main=1)
140
chart.StackedBar2(riskcontS1[Tstart:Tend,],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 )
141
chart.StackedBar2(riskcontS2[Tstart:Tend,],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 )
142
chart.StackedBar2(riskcontS5[Tstart:Tend,],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 )
143
#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 )
144
145
dev.off()
146
147
if(!CC){
148
postscript('MinCVaR_alternatives.eps')
149
}else{
150
postscript('MinCVaR_alternatives_CC.eps')
151
}
152
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)
153
154
par(mar=mar3 , cex.main=1)
155
chart.StackedBar2(weightsS3[Tstart:Tend,],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 )
156
chart.StackedBar2(weightsS4[Tstart:Tend,],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 )
157
chart.StackedBar2(weightsS6[Tstart:Tend,],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 )
158
159
par(mar=mar1 , cex.main=1)
160
plot.new()
161
legend("center",legend=w.names,col=colorset,lwd=6,ncol=4)
162
par(mar=mar3 , cex.main=1)
163
164
chart.StackedBar2(riskcontS3[Tstart:Tend,],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 )
165
chart.StackedBar2(riskcontS4[Tstart:Tend,],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 )
166
chart.StackedBar2(riskcontS6[Tstart:Tend,],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 )
167
168
dev.off()
169
170
171
#############################################################################################
172
# Plot the CVaR of the portfolios for each period, relatively to CVaR minimum CVaR portfolio
173
174
175
source("R_Allocation/Risk_budget_functions.R");
176
library(zoo); library(fGarch); library("PerformanceAnalytics");
177
178
# downside risk
179
alpha = alphariskbudget = 0.05;
180
CC=T
181
if(CC){ source( paste( getwd(),"/R_allocation/coskewkurtosis.R" ,sep="") ) }
182
183
184
# Load the data
185
186
nominalreturns = T;
187
if(nominalreturns){ load(file="monthlyR.RData") }else{ load(file="monthlyR_real.RData") }
188
R = monthlyR
189
cAssets = ncol(monthlyR)
190
191
# Define rebalancing periods:
192
193
ep = endpoints(monthlyR,on='quarters')
194
# select those for estimation period
195
ep.start = ep[1:(length(ep)-estyears*4)]+1
196
from = time(monthlyR)[ep.start]; from = seq( as.Date(paste(firstyear,"-01-01",sep="")), as.Date(paste(lastyear-estyears,"-07-01",sep="")), by="3 month")
197
ep.end = ep[(1+estyears*4):length(ep)]
198
to = time(monthlyR)[ep.end]
199
cPeriods = length(from);
200
201
# Loop where for each rebalancing period:
202
# - Compute total CVaR of portfolio
203
204
mCVaR = mMU = c()
205
206
for( per in c(1:cPeriods) ){
207
208
# At the end of each month, we compute the CVaR
209
enddates = na.omit(time(window( monthlyR , start = as.Date(to[per]) , end = as.Date(to[per]+90) ),on='months')[1:3])
210
211
for( enddate in enddates ){
212
213
# add a loop over the next months except when per = cPeriods or -1,-2
214
215
# Estimate GARCH model with data from inception
216
217
inception.R = window(R, start = as.Date(from[1]) , end = enddate );
218
219
# Estimate comoments of innovations with rolling estimation windows
220
in.sample.R = window(R, start = as.Date(from[per]) , end = as.Date(to[per]) );
221
in.sample.R = checkData(in.sample.R, method="matrix");
222
223
# Estimation of mean return
224
M = c();
225
library(TTR)
226
Tmean = 47 # monthly returns: 4 year exponentially weighted moving average
227
for( i in 1:cAssets ){
228
M = cbind( M , as.vector( EMA(x=inception.R[,i],n=Tmean) ) ) #2/(n+1)
229
}
230
M = zoo( M , order.by=time(inception.R) )
231
232
# Center returns (shift by one observations since M[t,] is rolling mean t-Tmean+1,...,t; otherwise lookahead bias)
233
inception.R.cent = inception.R;
234
ZZ = matrix( rep(as.vector( apply( inception.R[1:Tmean, ] , 2 , 'mean' )),Tmean),byrow=T,nrow=Tmean);
235
inception.R.cent[1:Tmean,] = inception.R[1:Tmean, ] - ZZ;
236
if( nrow(inception.R)>(Tmean+1) ){
237
A = M[Tmean:(nrow(inception.R)-1),];
238
A = zoo( A , order.by = time(inception.R[(Tmean+1):nrow(inception.R), ])) ; #shift dates; otherwise zoo poses problem
239
inception.R.cent[(Tmean+1):nrow(inception.R), ] = inception.R[(Tmean+1):nrow(inception.R), ] - A}
240
241
# Garch estimation
242
S = c();
243
for( i in 1:cAssets ){
244
gout = garchFit(formula ~ garch(1,1), data = inception.R.cent[,i],include.mean = F, cond.dist="QMLE", trace = FALSE )
245
if( as.vector(gout@fit$coef["alpha1"]) < 0.01 ){
246
sigmat = rep( sd( as.vector(inception.R.cent[,i])), length(inception.R.cent[,i]) );
247
}else{
248
sigmat = gout@sigma.t
249
}
250
S = cbind( S , sigmat)
251
}
252
S = zoo( S , order.by=time(inception.R.cent) )
253
254
# Estimate correlation, coskewness and cokurtosis matrix locally using cleaned innovation series in three year estimation window
255
selectU = window(inception.R.cent, start = as.Date(from[per]) , end = as.Date(to[per]) )
256
selectU = selectU/window(S, start = as.Date(from[per]) , end = as.Date(to[per]) );
257
selectU = clean.boudt2(selectU , alpha = 0.05 )[[1]];
258
Rcor = cor(selectU)
259
D = diag( as.vector(tail(S,n=1) ),ncol=cAssets )
260
sigma = D%*%Rcor%*%D
261
262
# we only need mean and conditional covariance matrix of last observation
263
mu = matrix(tail(M,n=1),ncol=1 ) ;
264
D = diag( as.vector(as.vector(tail(S,n=1) ) ),ncol=cAssets )
265
sigma = D%*%Rcor%*%D
266
in.sample.T = nrow(selectU);
267
# set volatility of all U to last observation, such that cov(rescaled U)=sigma
268
selectU = selectU*matrix( rep(as.vector(tail(S,n=1)),in.sample.T ) , ncol = cAssets , byrow = T )
269
if(CC){
270
M3 = coskewCC(selectU); M4 = cokurtCC(selectU);
271
}else{
272
M3 = PerformanceAnalytics:::M3.MM(selectU)
273
M4 = PerformanceAnalytics:::M4.MM(selectU)
274
}
275
CVaR_period = c( operPortMES(as.numeric(weightsS1[per,]),mu=mu,alpha=alphariskbudget,sigma=sigma,M3=M3,M4=M4)[[1]] ,
276
operPortMES(as.numeric(weightsS2[per,]),mu=mu,alpha=alphariskbudget,sigma=sigma,M3=M3,M4=M4)[[1]] ,
277
operPortMES(as.numeric(weightsS3[per,]),mu=mu,alpha=alphariskbudget,sigma=sigma,M3=M3,M4=M4)[[1]] ,
278
operPortMES(as.numeric(weightsS4[per,]),mu=mu,alpha=alphariskbudget,sigma=sigma,M3=M3,M4=M4)[[1]] ,
279
operPortMES(as.numeric(weightsS5[per,]),mu=mu,alpha=alphariskbudget,sigma=sigma,M3=M3,M4=M4)[[1]] ,
280
operPortMES(as.numeric(weightsS6[per,]),mu=mu,alpha=alphariskbudget,sigma=sigma,M3=M3,M4=M4)[[1]] ,
281
operPortMES(as.numeric(weightsS7[per,]),mu=mu,alpha=alphariskbudget,sigma=sigma,M3=M3,M4=M4)[[1]] )
282
283
mu_period = c( sum(as.numeric(weightsS1[per,])*mu) , sum(as.numeric(weightsS2[per,])*mu) ,
284
sum(as.numeric(weightsS3[per,])*mu) , sum(as.numeric(weightsS4[per,])*mu) ,
285
sum(as.numeric(weightsS5[per,])*mu) , sum(as.numeric(weightsS6[per,])*mu) ,
286
sum(as.numeric(weightsS7[per,])*mu) )
287
288
mCVaR = rbind( mCVaR , CVaR_period )
289
mMU = rbind( mMU , mu_period )
290
}
291
}
292
293
colnames(mCVaR) = colnames(mMU) = names[1:7]
294
mCVaR = xts( mCVaR ,
295
order.by = as.Date(time( window( monthlyR , start = as.Date(to[1]) , end = as.Date(to[cPeriods]) ))+1))
296
mMU = xts( mMU ,
297
order.by = as.Date(time( window( monthlyR , start = as.Date(to[1]) , end = as.Date(to[cPeriods]) ))+1))
298
299
head(mCVaR[,1:7],2)
300
#> head(mCVaR[,1:7],2)
301
# EqualWeight MinRisk MinRisk_PositionLimit MinRisk_RiskLimit MinRiskConc MinRiskConc_PositionLimit EqualRisk
302
#1984-01-01 0.0492 0.0320 0.0380 0.0333 0.0352 0.0388 0.0352
303
#1984-02-01 0.0490 0.0304 0.0374 0.0322 0.0344 0.0383 0.0344
304
305
306
if(CC){
307
save(mCVaR, file="mCVaR_CC.Rdata")
308
save(mMU, file="mMU_CC.Rdata")
309
}else{
310
save(mCVaR, file="mCVaR.Rdata")
311
save(mMU, file="mMU.Rdata")
312
}
313
314
head(mMU[,1:7],2)
315
#> head(mMU[,1:7],2)
316
# EqualWeight MinRisk MinRisk_PositionLimit MinRisk_RiskLimit MinRiskConc MinRiskConc_PositionLimit EqualRisk
317
#1984-01-01 0.009865862 0.00959764 0.01084905 0.009814883 0.009944563 0.01012198 0.009945766
318
#1984-02-01 0.010176409 0.01003861 0.01105078 0.010193260 0.010297694 0.01042968 0.010299787
319
320
321
###################################################
322
323
if(CC){
324
load( file="mCVaR_CC.Rdata")
325
load( file="mMU_CC.Rdata")
326
}else{
327
load( file="mCVaR.Rdata")
328
load( file="mMU.Rdata")
329
}
330
331
mMU = window(mMU , start=as.Date("1984-01-01") , end=tail(time(mMU),1) )
332
mCVaR = window(mCVaR , start=as.Date("1984-01-01") , end=tail(time(mCVaR),1) )
333
334
if(CC){
335
postscript(file="portfolioMeanCVaR_CC.eps")
336
}else{
337
postscript(file="portfolioMeanCVaR.eps")
338
}
339
par(mfrow=c(2,1),mar=c(3,2,3,2))
340
341
342
343
plot( mMU[,1]*12 , type = "l" , ylim=c(min(mMU),max(mMU))*12,col="darkgray", lwd=1.5 ,
344
main = "Expected annualized portfolio return" )
345
lines( mMU[,2]*12 , type = "l", col="black",lwd=2 , lty=3)
346
lines( mMU[,7]*12 , type = "l", col="darkgray",lwd=4)
347
lines( mMU[,5]*12 , type = "l", col="black", lwd=1.5)
348
349
legend("bottomleft", legend = c("Equal-Weight","Min CVaR Concentration","Min CVaR+ERC constraint","Min CVaR" ),
350
col=c("darkgray","black","darkgray","black"), lty=c("solid","solid","solid","dashed"), lwd=c(2,2,4,2) ,cex=0.7)
351
352
plot( mCVaR[,1] , type = "l" , ylim=c(0,max(mCVaR)),col="darkgray", lwd=1.5 , main = "Portfolio CVaR" )
353
lines( mCVaR[,2] , type = "l", col="black",lwd=1.5 , lty=3)
354
lines( mCVaR[,7] , type = "l", col="darkgray",lwd=4)
355
lines( mCVaR[,5] , type = "l", col="black", lwd=1.5)
356
dev.off()
357
358
# do not plot the last month such that it is fully comparable with out-of-sample plots
359
360
sel = c( 1 : (nrow(mCVaR)-1) );
361
362
363
# Bear periods
364
sp500 = window (monthlyR , start = from[1] , end = to[ length(to) ] )[,2]
365
bear = c(1:length(sp500))[sp500<mean(sp500)]
366
bear = c(1:length(sp500))[sp500<(-0.12)]
367
m.bear.dates = list();
368
i=1;
369
for( b in bear){
370
m.bear.dates[[i]] = c( b-0.5, b+0.5)
371
i = i + 1;
372
}
373
374
out = table.Drawdowns(sp500,top=10)
375
start.bear = out$From[out$Depth<(-0.12)]
376
end.bear = out$Trough[out$Depth<(-0.12)]
377
start.bear.index = c(1:length(sp500))[ time(sp500) ]
378
m.bear.dates = list()
379
v.bear.dates = c()
380
for( i in 1:length(start.bear) ){
381
m.bear.dates[[i]] = c( as.yearmon(start.bear[i]) , as.yearmon(end.bear[i]) )
382
v.bear.dates = c( v.bear.dates , seq(start.bear[i],end.bear[i],"days") )
383
}
384
v.bear.dates = as.Date( v.bear.dates )
385
386
387
if(CC){
388
postscript(file="portfolioCVaR_CC.eps")
389
}else{
390
postscript(file="portfolioCVaR.eps")
391
}
392
par(mfrow=c(2,1),mar=c(3,4,1,2))
393
394
chart.TimeSeries( mCVaR[sel,c(1,5,2)] , ylim=c(0,max(mCVaR)), ylab = "Portfolio CVaR" , main = "",
395
col=c("black","black","darkgray"), lty=c("dashed","solid","solid"), lwd=c(2,2,2,2) ,
396
auto.grid = TRUE, minor.ticks = FALSE ,
397
period.areas = m.bear.dates , period.color="lightgray",
398
date.format.in = "%Y-%m-%d",date.format = "%b %Y")
399
400
legend("topleft", legend = namelabels[c(1,5,2)],
401
col=c("black","black","darkgray"), lty=c("dashed","solid","solid"), lwd=c(2,2,2) ,cex=0.7)
402
403
chart.TimeSeries( mCVaR[sel,c(4,3,6)] , type = "l" , ylim=c(0,max(mCVaR)), ylab = "Portfolio CVaR" , main = "",
404
col=c("black","black","darkgray"), lty=c("dashed","solid","solid"), lwd=c(2,2,2,2) ,
405
auto.grid = TRUE, minor.ticks = FALSE ,
406
period.areas = m.bear.dates , period.color="lightgray",
407
date.format.in = "%Y-%m-%d",date.format = "%b %Y")
408
409
legend("topleft", legend = namelabels[c(4,3,6)],
410
col=c("black","black","darkgray"), lty=c("dashed","solid","solid"), lwd=c(2,2,2) ,cex=0.7)
411
412
413
dev.off()
414
415
416
417
418
419