Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/illustration/Effect_Feasible_Space.R
1433 views
1
2
3
setwd("C:\\Documents and Settings\\Administrator\\Desktop\\risk budget programs\\illustration")
4
# Bivariate normal example of the effect of constraints on percentage CVaR on the feasible space
5
6
showweightlines = T;
7
srt = 0 ;
8
9
CVaR = function( w , mu , sigma , alpha )
10
{
11
out = -t(w)%*%mu + sqrt( t(w)%*%sigma%*%w )*dnorm(qnorm(alpha))/alpha
12
return( out );
13
}
14
15
CompCVaR = function( w , mu , sigma , alpha )
16
{
17
out = -mu + as.vector(sigma%*%w)*(1/sqrt( t(w)%*%sigma%*%w))*dnorm(qnorm(alpha))/alpha
18
return( w*out );
19
}
20
21
N = 2; rho1 = -0.5 ; rho2 = 0; rho3 = 0.5; alpha = 0.05
22
mu1 = as.matrix( rep(0,N) ) ; mu2 = as.matrix( c(1, 0 ) ) ;
23
sigma1 = sigma2 = matrix ( rep(rho1,N^2) , ncol = N )
24
sigma3 = sigma4 = matrix ( rep(rho2,N^2) , ncol = N )
25
sigma5 = sigma6 = matrix ( rep(rho3,N^2) , ncol = N )
26
diag(sigma1) = diag(sigma2) = diag(sigma3) = diag(sigma4) = diag(sigma5) = diag(sigma6) =1;
27
sigma2[2,2] = sigma4[2,2] = sigma6[2,2] = 4;
28
sigma2[1,2]=sigma2[2,1]=2*sigma2[1,2];
29
sigma4[1,2]=sigma4[2,1]=2*sigma4[1,2];
30
sigma6[1,2]=sigma6[2,1]=2*sigma6[1,2];
31
32
w = as.matrix( rep(0.5,2) )
33
34
percCompCVaR = function( w = w , mu = mu , sigma = sigma , alpha = alpha ){
35
out1 = CompCVaR( w = w , mu = mu , sigma = sigma , alpha = alpha )
36
out2 = CVaR( w = w , mu = mu , sigma = sigma , alpha = alpha )
37
return(out1/as.numeric(out2))
38
}
39
40
vw1 = seq(0,1,0.01)
41
constraint = rep(0,length(vw1));
42
i=0;
43
vpccvar1 = vpccvar2 = vpccvar3 = vpccvar4 = vpccvar5 = vpccvar6 = vpccvar7 = vpccvar8 = vpccvar9 = c();
44
for( w1 in vw1 )
45
{
46
w2 = 1-w1; i = i+1;
47
w = as.matrix( c(w1,w2) )
48
vpccvar1 = rbind( vpccvar1 , percCompCVaR( w = w , mu = mu1 , sigma = sigma1 , alpha = alpha )[1])
49
vpccvar2 = rbind( vpccvar2 , percCompCVaR( w = w , mu = mu2 , sigma = sigma1 , alpha = alpha )[1])
50
vpccvar3 = rbind( vpccvar3 , percCompCVaR( w = w , mu = mu1 , sigma = sigma2 , alpha = alpha )[1])
51
vpccvar4 = rbind( vpccvar4 , percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1])
52
vpccvar5 = rbind( vpccvar5 , percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1])
53
vpccvar6 = rbind( vpccvar6 , percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1])
54
vpccvar7 = rbind( vpccvar7 , percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1])
55
vpccvar8 = rbind( vpccvar8 , percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1])
56
vpccvar9 = rbind( vpccvar9 , percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1])
57
}
58
59
# Plot 9 x 9
60
61
if(showweightlines){ postscript('sensitivity.eps') }else{ postscript('sensitivity_noweightlines.eps')}
62
63
par( mfrow = c(3,3) , las = 1 , mar=c(2.2,2.5,2,1.5) , cex=1 , cex.main=1)
64
65
# !!!! Setup 1: mu1, sigma1
66
plot( vw1 , vpccvar1 , main = expression( mu[1]*"="*mu[2]*"=0 and "*sigma[1]*"="*sigma[2]*"=1") ,
67
type="l",ylab="" )
68
text( x = 0.1 , y = 0.95*max(vpccvar1) , labels=expression( rho*"="*-0.5) )
69
# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%
70
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 )}
71
# Indicate ERC portfolio
72
objective = function( w1 ){
73
w = as.matrix( c(w1,1-w1) ); return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma1 , alpha = alpha )[1] ) }
74
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
75
print(ERC); print( 1/2 ) ;
76
text( x = ERC , y = 0.5 , labels="ERC" , srt = srt )
77
# uniroot: find value for which it is 0.4 and 0.6
78
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
79
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
80
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
81
82
# !!!! Setup 3: mu1, sigma2
83
plot( vw1 , vpccvar3 , main = expression( mu[1]*"="*mu[2]*"=0 and "*sigma[1]*"=1, "*sigma[2]*"=2 "), type="l" )
84
text( x = 0.1 , y = 0.95*max(vpccvar3) , labels=expression( rho*"="*-0.5) )
85
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
86
# uniroot: find value for which it is 0.4 and 0.6
87
objective = function( w1 ){
88
w = as.matrix( c(w1,1-w1) );
89
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma2 , alpha = alpha )[1] )
90
}
91
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
92
print(ERC); print( 2/(1+2) ) ;
93
text( x = ERC , y = 0.5 , labels="ERC" )
94
# uniroot: find value for which it is 0.4 and 0.6
95
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
96
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
97
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
98
99
100
101
102
# !!!! Setup 2: mu2, sigma1
103
plot( vw1 , vpccvar2 , main = expression( mu[1]*"=1, "*mu[2]*"=0 and "*sigma[1]*"="*sigma[2]*"=1") , type="l" )
104
text( x = 0.1 , y = 0.95*max(vpccvar2) , labels=expression( rho*"="*-0.5) )
105
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
106
# uniroot: find value for which it is 0.4 and 0.6
107
objective = function( w1 ){
108
w = as.matrix( c(w1,1-w1) );
109
return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma1 , alpha = alpha )[1] )
110
}
111
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
112
text( x = ERC , y = 0.5 , labels="ERC" )
113
# uniroot: find value for which it is 0.4 and 0.6
114
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
115
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
116
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
117
118
119
#---------------------------------------------------------------------
120
121
# !!!! Setup 4: mu1, sigma3
122
plot( vw1 , vpccvar4 , main = "" , type="l",ylab="" )
123
text( x = 0.1 , y = 0.95*max(vpccvar4) , labels=expression( rho*"="*0) )
124
# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%
125
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
126
# uniroot: find value for which it is 0.4 and 0.6
127
objective = function( w1 ){
128
w = as.matrix( c(w1,1-w1) );
129
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )
130
}
131
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
132
text( x = ERC , y = 0.5 , labels="ERC" )
133
# uniroot: find value for which it is 0.4 and 0.6
134
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
135
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
136
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
137
138
# !!!! Setup 6: mu1, sigma4
139
plot( vw1 , vpccvar6 , main = "", type="l" )
140
text( x = 0.1 , y = 0.95*max(vpccvar6) , labels=expression( rho*"="*0) )
141
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
142
# uniroot: find value for which it is 0.4 and 0.6
143
objective = function( w1 ){
144
w = as.matrix( c(w1,1-w1) );
145
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1] )
146
}
147
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
148
text( x = ERC , y = 0.5 , labels="ERC" )
149
# uniroot: find value for which it is 0.4 and 0.6
150
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
151
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
152
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
153
154
# !!!! Setup 5: mu2, sigma3
155
plot( vw1 , vpccvar5 , main = "" , type="l" )
156
text( x = 0.1 , y = 0.95*max(vpccvar5) , labels=expression( rho*"="*0) )
157
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
158
# uniroot: find value for which it is 0.4 and 0.6
159
objective = function( w1 ){
160
w = as.matrix( c(w1,1-w1) );
161
return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1] )
162
}
163
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
164
text( x = ERC , y = 0.5 , labels="ERC" )
165
# uniroot: find value for which it is 0.4 and 0.6
166
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
167
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
168
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
169
170
171
172
#----
173
174
# !!!! Setup 7: mu1, sigma5
175
plot( vw1 , vpccvar1 , main = expression( mu[1]*"="*mu[2]*"=0 and "*sigma[1]*"="*sigma[2]*"=1") ,
176
type="l",ylab="" )
177
text( x = 0.1 , y = 0.95*max(vpccvar1) , labels=expression( rho*"="*0.5) )
178
# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%
179
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 )}
180
# Indicate ERC portfolio
181
objective = function( w1 ){
182
w = as.matrix( c(w1,1-w1) ); return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1] ) }
183
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
184
print(ERC); print( 1/2 ) ;
185
text( x = ERC , y = 0.5 , labels="ERC" , srt = srt )
186
# uniroot: find value for which it is 0.4 and 0.6
187
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
188
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
189
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
190
191
192
# !!!! Setup 9: mu1, sigma6
193
plot( vw1 , vpccvar9 , main = "" , type="l" )
194
text( x = 0.1 , y = 0.95*max(vpccvar9) , labels=expression( rho*"="*0.5) )
195
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
196
# uniroot: find value for which it is 0.4 and 0.6
197
objective = function( w1 ){
198
w = as.matrix( c(w1,1-w1) );
199
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1] )
200
}
201
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
202
text( x = ERC , y = 0.5 , labels="ERC" )
203
# uniroot: find value for which it is 0.4 and 0.6
204
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
205
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
206
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
207
208
209
# !!!! Setup 8: mu2, sigma5
210
plot( vw1 , vpccvar8 , main = "", type="l" )
211
text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )
212
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
213
# uniroot: find value for which it is 0.4 and 0.6
214
objective = function( w1 ){
215
w = as.matrix( c(w1,1-w1) );
216
return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1] )
217
}
218
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
219
text( x = ERC , y = 0.5 , labels="ERC" )
220
# uniroot: find value for which it is 0.4 and 0.6
221
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
222
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
223
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
224
225
226
227
#----------------------------------------------------------------------------------------
228
229
230
231
dev.off()
232
233
234
235
####################### Focus on effect of mu and sigma
236
237
238
239
postscript('sensitivity_rho50.eps')
240
241
par( mfrow = c(2,2) , las = 1 , mar=c(4.2,4.5,3,1.5) , cex=1.2 , cex.main=1.2)
242
243
#---------------------------------------------------------------------
244
245
showweightlines = TRUE;
246
247
248
# !!!! Setup 7: mu1, sigma5
249
plot( vw1 , vpccvar7 , main = expression( mu[1]*"="*mu[2]*"=0, "*sigma[1]*"="*sigma[2]*"=1 and "*rho*"=0.5") , type="l",
250
ylab="Perc CVaR asset 1", xlab="Weight asset 1" )
251
#text( x = 0.1 , y = 0.95*max(vpccvar7) , labels=expression( rho*"="*0.5) )
252
# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%
253
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 )}
254
# Indicate ERC portfolio
255
objective = function( w1 ){
256
w = as.matrix( c(w1,1-w1) ); return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1] ) }
257
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
258
print(ERC); print( 1/2 ) ;
259
#text( x = ERC , y = 0.5 , labels="ERC" , srt = srt )
260
# uniroot: find value for which it is 0.4 and 0.6
261
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
262
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
263
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
264
265
266
# !!!! Setup 9: mu1, sigma6
267
plot( vw1 , vpccvar9 , main = expression( mu[1]*"="*mu[2]*"=0, "* sigma[1]*"=1, "*sigma[2]*"=2 and "*rho*"=0.5") , type="l" ,
268
ylab="Perc CVaR asset 1", xlab="Weight asset 1" )
269
#text( x = 0.1 , y = 0.95*max(vpccvar9) , labels=expression( rho*"="*0.5) )
270
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
271
# uniroot: find value for which it is 0.4 and 0.6
272
objective = function( w1 ){
273
w = as.matrix( c(w1,1-w1) );
274
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1] )
275
}
276
#h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
277
#text( x = ERC , y = 0.5 , labels="ERC" )
278
# uniroot: find value for which it is 0.4 and 0.6
279
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
280
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
281
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
282
283
#par( mfrow = c(2,2) , las = 1 , mar=c(4,4.5,2,1.5) , cex=1.2 , cex.main=1.2)
284
285
# !!!! Setup 8: mu2, sigma5
286
plot( vw1 , vpccvar8 , main = expression( mu[1]*"=1, "*mu[2]*"=0, "*sigma[1]*"="*sigma[2]*"=1 and "*rho*"=0.5"), type="l" ,
287
ylab="Perc CVaR asset 1 ", xlab = "Weight asset 1")
288
#text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )
289
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
290
# uniroot: find value for which it is 0.4 and 0.6
291
objective = function( w1 ){
292
w = as.matrix( c(w1,1-w1) );
293
return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1] )
294
}
295
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
296
#text( x = ERC , y = 0.5 , labels="ERC" )
297
# uniroot: find value for which it is 0.4 and 0.6
298
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
299
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
300
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
301
302
303
304
# !!!! Setup 8: mu1, sigma3
305
plot( vw1 , vpccvar4 , main = expression(mu[1]*"="*mu[2]*"=0, "*sigma[1]*"="*sigma[2]*"=1 and "*rho*"=0"), type="l" ,
306
ylab="Perc CVaR asset 1 ", xlab = "Weight asset 1")
307
#text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )
308
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
309
# uniroot: find value for which it is 0.4 and 0.6
310
objective = function( w1 ){
311
w = as.matrix( c(w1,1-w1) );
312
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )
313
}
314
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
315
#text( x = ERC , y = 0.5 , labels="ERC" )
316
# uniroot: find value for which it is 0.4 and 0.6
317
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
318
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
319
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
320
321
322
dev.off()
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Plot 1 x 3
342
343
postscript('sensitivity_rho50.eps')
344
showweightlines = TRUE
345
par( mfrow = c(1,3) , las = 1 , mar=c(2.2,2.5,2,1.5) , cex=1 , cex.main=1)
346
347
# !!!! Setup 7: mu1, sigma5
348
plot( vw1 , vpccvar7 , main = expression( mu[1]*"="*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") , type="l" )
349
text( x = 0.1 , y = 0.95*max(vpccvar7) , labels=expression( rho*"="*0.5) )
350
# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%
351
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
352
# uniroot: find value for which it is 0.4 and 0.6
353
objective = function( w1 ){
354
w = as.matrix( c(w1,1-w1) );
355
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1] )
356
}
357
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
358
text( x = ERC , y = 0.5 , labels="ERC" )
359
# uniroot: find value for which it is 0.4 and 0.6
360
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
361
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
362
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;
363
abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
364
365
# !!!! Setup 9: mu1, sigma6
366
plot( vw1 , vpccvar9 , main = main = expression( mu[1]*"=1, "*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") , type="l" )
367
text( x = 0.1 , y = 0.95*max(vpccvar9) , labels=expression( rho*"="*0.5) )
368
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
369
# uniroot: find value for which it is 0.4 and 0.6
370
objective = function( w1 ){
371
w = as.matrix( c(w1,1-w1) );
372
return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1] )
373
}
374
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
375
text( x = ERC , y = 0.5 , labels="ERC" )
376
# uniroot: find value for which it is 0.4 and 0.6
377
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
378
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
379
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;
380
abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
381
382
383
# !!!! Setup 8: mu2, sigma5
384
plot( vw1 , vpccvar8 , main = "", type="l" )
385
text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )
386
if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
387
# uniroot: find value for which it is 0.4 and 0.6
388
objective = function( w1 ){
389
w = as.matrix( c(w1,1-w1) );
390
return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1] )
391
}
392
h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root
393
text( x = ERC , y = 0.5 , labels="ERC" )
394
# uniroot: find value for which it is 0.4 and 0.6
395
h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root
396
h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root
397
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;
398
abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
399
400
# !!!! Setup 4: mu1, sigma3
401
plot( vw1 , vpccvar4 , main = expression( mu[1]*"="*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") ,
402
type="l",ylab="Perc CVaR asset 1 \n in function of its weight" )
403
# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%
404
# uniroot: find value for which it is 0.4 and 0.6
405
objective = function( w1 ){
406
w = as.matrix( c(w1,1-w1) );
407
return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )
408
}
409
v1 = uniroot( objective , lower = 0 , upper = 1)$root
410
objective = function( w1 ){
411
w = as.matrix( c(w1,1-w1) );
412
return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )
413
}
414
v2 = uniroot( objective , lower = 0 , upper = 1)$root
415
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;
416
abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }
417
abline( h=0.5 , lty = 3 )
418
419
# !!!! Setup 5: mu2, sigma3
420
plot( vw1 , vpccvar5 , main = expression( mu[1]*"=1, "*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") ,
421
type="l" , ylab = "Perc CVaR asset 1 \n in function of its weight" )
422
# uniroot: find value for which it is 0.4 and 0.6
423
objective = function( w1 ){
424
w = as.matrix( c(w1,1-w1) );
425
return( 0.5 - percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1] )
426
}
427
v1 = uniroot( objective , lower = 0 , upper = 1)$root
428
objective = function( w1 ){
429
w = as.matrix( c(w1,1-w1) );
430
return( 0.5 - percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1] )
431
}
432
v2 = uniroot( objective , lower = 0 , upper = 1)$root
433
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
434
abline( h=0.5 , lty = 3 )
435
436
# !!!! Setup 6: mu1, sigma4
437
plot( vw1 , vpccvar6 , main = expression( mu[1]*"="*mu[2]*"=0, "*rho*"=0, "* sigma[1]*"=1, "*sigma[2]*"=2 "),
438
type="l" , ylab = "Perc CVaR asset 1 \n in function of its weight" )
439
# uniroot: find value for which it is 0.4 and 0.6
440
objective = function( w1 ){
441
w = as.matrix( c(w1,1-w1) );
442
return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1] )
443
}
444
v1 = uniroot( objective , lower = 0 , upper = 1)$root
445
objective = function( w1 ){
446
w = as.matrix( c(w1,1-w1) );
447
return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1] )
448
}
449
v2 = uniroot( objective , lower = 0 , upper = 1)$root
450
if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }
451
abline( h=0.5 , lty = 3 )
452
453
dev.off()
454
455