Path: blob/master/sandbox/paper_analysis/illustration/Effect_Feasible_Space.R
1433 views
12setwd("C:\\Documents and Settings\\Administrator\\Desktop\\risk budget programs\\illustration")3# Bivariate normal example of the effect of constraints on percentage CVaR on the feasible space45showweightlines = T;6srt = 0 ;78CVaR = function( w , mu , sigma , alpha )9{10out = -t(w)%*%mu + sqrt( t(w)%*%sigma%*%w )*dnorm(qnorm(alpha))/alpha11return( out );12}1314CompCVaR = function( w , mu , sigma , alpha )15{16out = -mu + as.vector(sigma%*%w)*(1/sqrt( t(w)%*%sigma%*%w))*dnorm(qnorm(alpha))/alpha17return( w*out );18}1920N = 2; rho1 = -0.5 ; rho2 = 0; rho3 = 0.5; alpha = 0.0521mu1 = as.matrix( rep(0,N) ) ; mu2 = as.matrix( c(1, 0 ) ) ;22sigma1 = sigma2 = matrix ( rep(rho1,N^2) , ncol = N )23sigma3 = sigma4 = matrix ( rep(rho2,N^2) , ncol = N )24sigma5 = sigma6 = matrix ( rep(rho3,N^2) , ncol = N )25diag(sigma1) = diag(sigma2) = diag(sigma3) = diag(sigma4) = diag(sigma5) = diag(sigma6) =1;26sigma2[2,2] = sigma4[2,2] = sigma6[2,2] = 4;27sigma2[1,2]=sigma2[2,1]=2*sigma2[1,2];28sigma4[1,2]=sigma4[2,1]=2*sigma4[1,2];29sigma6[1,2]=sigma6[2,1]=2*sigma6[1,2];3031w = as.matrix( rep(0.5,2) )3233percCompCVaR = function( w = w , mu = mu , sigma = sigma , alpha = alpha ){34out1 = CompCVaR( w = w , mu = mu , sigma = sigma , alpha = alpha )35out2 = CVaR( w = w , mu = mu , sigma = sigma , alpha = alpha )36return(out1/as.numeric(out2))37}3839vw1 = seq(0,1,0.01)40constraint = rep(0,length(vw1));41i=0;42vpccvar1 = vpccvar2 = vpccvar3 = vpccvar4 = vpccvar5 = vpccvar6 = vpccvar7 = vpccvar8 = vpccvar9 = c();43for( w1 in vw1 )44{45w2 = 1-w1; i = i+1;46w = as.matrix( c(w1,w2) )47vpccvar1 = rbind( vpccvar1 , percCompCVaR( w = w , mu = mu1 , sigma = sigma1 , alpha = alpha )[1])48vpccvar2 = rbind( vpccvar2 , percCompCVaR( w = w , mu = mu2 , sigma = sigma1 , alpha = alpha )[1])49vpccvar3 = rbind( vpccvar3 , percCompCVaR( w = w , mu = mu1 , sigma = sigma2 , alpha = alpha )[1])50vpccvar4 = rbind( vpccvar4 , percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1])51vpccvar5 = rbind( vpccvar5 , percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1])52vpccvar6 = rbind( vpccvar6 , percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1])53vpccvar7 = rbind( vpccvar7 , percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1])54vpccvar8 = rbind( vpccvar8 , percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1])55vpccvar9 = rbind( vpccvar9 , percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1])56}5758# Plot 9 x 95960if(showweightlines){ postscript('sensitivity.eps') }else{ postscript('sensitivity_noweightlines.eps')}6162par( mfrow = c(3,3) , las = 1 , mar=c(2.2,2.5,2,1.5) , cex=1 , cex.main=1)6364# !!!! Setup 1: mu1, sigma165plot( vw1 , vpccvar1 , main = expression( mu[1]*"="*mu[2]*"=0 and "*sigma[1]*"="*sigma[2]*"=1") ,66type="l",ylab="" )67text( x = 0.1 , y = 0.95*max(vpccvar1) , labels=expression( rho*"="*-0.5) )68# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%69if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 )}70# Indicate ERC portfolio71objective = function( w1 ){72w = as.matrix( c(w1,1-w1) ); return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma1 , alpha = alpha )[1] ) }73h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root74print(ERC); print( 1/2 ) ;75text( x = ERC , y = 0.5 , labels="ERC" , srt = srt )76# uniroot: find value for which it is 0.4 and 0.677h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root78h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root79if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }8081# !!!! Setup 3: mu1, sigma282plot( vw1 , vpccvar3 , main = expression( mu[1]*"="*mu[2]*"=0 and "*sigma[1]*"=1, "*sigma[2]*"=2 "), type="l" )83text( x = 0.1 , y = 0.95*max(vpccvar3) , labels=expression( rho*"="*-0.5) )84if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }85# uniroot: find value for which it is 0.4 and 0.686objective = function( w1 ){87w = as.matrix( c(w1,1-w1) );88return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma2 , alpha = alpha )[1] )89}90h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root91print(ERC); print( 2/(1+2) ) ;92text( x = ERC , y = 0.5 , labels="ERC" )93# uniroot: find value for which it is 0.4 and 0.694h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root95h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root96if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }979899100101# !!!! Setup 2: mu2, sigma1102plot( vw1 , vpccvar2 , main = expression( mu[1]*"=1, "*mu[2]*"=0 and "*sigma[1]*"="*sigma[2]*"=1") , type="l" )103text( x = 0.1 , y = 0.95*max(vpccvar2) , labels=expression( rho*"="*-0.5) )104if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }105# uniroot: find value for which it is 0.4 and 0.6106objective = function( w1 ){107w = as.matrix( c(w1,1-w1) );108return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma1 , alpha = alpha )[1] )109}110h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root111text( x = ERC , y = 0.5 , labels="ERC" )112# uniroot: find value for which it is 0.4 and 0.6113h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root114h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root115if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }116117118#---------------------------------------------------------------------119120# !!!! Setup 4: mu1, sigma3121plot( vw1 , vpccvar4 , main = "" , type="l",ylab="" )122text( x = 0.1 , y = 0.95*max(vpccvar4) , labels=expression( rho*"="*0) )123# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%124if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }125# uniroot: find value for which it is 0.4 and 0.6126objective = function( w1 ){127w = as.matrix( c(w1,1-w1) );128return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )129}130h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root131text( x = ERC , y = 0.5 , labels="ERC" )132# uniroot: find value for which it is 0.4 and 0.6133h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root134h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root135if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }136137# !!!! Setup 6: mu1, sigma4138plot( vw1 , vpccvar6 , main = "", type="l" )139text( x = 0.1 , y = 0.95*max(vpccvar6) , labels=expression( rho*"="*0) )140if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }141# uniroot: find value for which it is 0.4 and 0.6142objective = function( w1 ){143w = as.matrix( c(w1,1-w1) );144return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1] )145}146h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root147text( x = ERC , y = 0.5 , labels="ERC" )148# uniroot: find value for which it is 0.4 and 0.6149h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root150h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root151if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }152153# !!!! Setup 5: mu2, sigma3154plot( vw1 , vpccvar5 , main = "" , type="l" )155text( x = 0.1 , y = 0.95*max(vpccvar5) , labels=expression( rho*"="*0) )156if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }157# uniroot: find value for which it is 0.4 and 0.6158objective = function( w1 ){159w = as.matrix( c(w1,1-w1) );160return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1] )161}162h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root163text( x = ERC , y = 0.5 , labels="ERC" )164# uniroot: find value for which it is 0.4 and 0.6165h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root166h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root167if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }168169170171#----172173# !!!! Setup 7: mu1, sigma5174plot( vw1 , vpccvar1 , main = expression( mu[1]*"="*mu[2]*"=0 and "*sigma[1]*"="*sigma[2]*"=1") ,175type="l",ylab="" )176text( x = 0.1 , y = 0.95*max(vpccvar1) , labels=expression( rho*"="*0.5) )177# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%178if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 )}179# Indicate ERC portfolio180objective = function( w1 ){181w = as.matrix( c(w1,1-w1) ); return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1] ) }182h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root183print(ERC); print( 1/2 ) ;184text( x = ERC , y = 0.5 , labels="ERC" , srt = srt )185# uniroot: find value for which it is 0.4 and 0.6186h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root187h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root188if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }189190191# !!!! Setup 9: mu1, sigma6192plot( vw1 , vpccvar9 , main = "" , type="l" )193text( x = 0.1 , y = 0.95*max(vpccvar9) , labels=expression( rho*"="*0.5) )194if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }195# uniroot: find value for which it is 0.4 and 0.6196objective = function( w1 ){197w = as.matrix( c(w1,1-w1) );198return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1] )199}200h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root201text( x = ERC , y = 0.5 , labels="ERC" )202# uniroot: find value for which it is 0.4 and 0.6203h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root204h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root205if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }206207208# !!!! Setup 8: mu2, sigma5209plot( vw1 , vpccvar8 , main = "", type="l" )210text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )211if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }212# uniroot: find value for which it is 0.4 and 0.6213objective = function( w1 ){214w = as.matrix( c(w1,1-w1) );215return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1] )216}217h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root218text( x = ERC , y = 0.5 , labels="ERC" )219# uniroot: find value for which it is 0.4 and 0.6220h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root221h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root222if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }223224225226#----------------------------------------------------------------------------------------227228229230dev.off()231232233234####################### Focus on effect of mu and sigma235236237238postscript('sensitivity_rho50.eps')239240par( mfrow = c(2,2) , las = 1 , mar=c(4.2,4.5,3,1.5) , cex=1.2 , cex.main=1.2)241242#---------------------------------------------------------------------243244showweightlines = TRUE;245246247# !!!! Setup 7: mu1, sigma5248plot( vw1 , vpccvar7 , main = expression( mu[1]*"="*mu[2]*"=0, "*sigma[1]*"="*sigma[2]*"=1 and "*rho*"=0.5") , type="l",249ylab="Perc CVaR asset 1", xlab="Weight asset 1" )250#text( x = 0.1 , y = 0.95*max(vpccvar7) , labels=expression( rho*"="*0.5) )251# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%252if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 )}253# Indicate ERC portfolio254objective = function( w1 ){255w = as.matrix( c(w1,1-w1) ); return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1] ) }256h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root257print(ERC); print( 1/2 ) ;258#text( x = ERC , y = 0.5 , labels="ERC" , srt = srt )259# uniroot: find value for which it is 0.4 and 0.6260h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root261h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root262if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }263264265# !!!! Setup 9: mu1, sigma6266plot( vw1 , vpccvar9 , main = expression( mu[1]*"="*mu[2]*"=0, "* sigma[1]*"=1, "*sigma[2]*"=2 and "*rho*"=0.5") , type="l" ,267ylab="Perc CVaR asset 1", xlab="Weight asset 1" )268#text( x = 0.1 , y = 0.95*max(vpccvar9) , labels=expression( rho*"="*0.5) )269if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }270# uniroot: find value for which it is 0.4 and 0.6271objective = function( w1 ){272w = as.matrix( c(w1,1-w1) );273return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1] )274}275#h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root276#text( x = ERC , y = 0.5 , labels="ERC" )277# uniroot: find value for which it is 0.4 and 0.6278h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root279h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root280if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }281282#par( mfrow = c(2,2) , las = 1 , mar=c(4,4.5,2,1.5) , cex=1.2 , cex.main=1.2)283284# !!!! Setup 8: mu2, sigma5285plot( vw1 , vpccvar8 , main = expression( mu[1]*"=1, "*mu[2]*"=0, "*sigma[1]*"="*sigma[2]*"=1 and "*rho*"=0.5"), type="l" ,286ylab="Perc CVaR asset 1 ", xlab = "Weight asset 1")287#text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )288if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }289# uniroot: find value for which it is 0.4 and 0.6290objective = function( w1 ){291w = as.matrix( c(w1,1-w1) );292return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1] )293}294h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root295#text( x = ERC , y = 0.5 , labels="ERC" )296# uniroot: find value for which it is 0.4 and 0.6297h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root298h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root299if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }300301302303# !!!! Setup 8: mu1, sigma3304plot( vw1 , vpccvar4 , main = expression(mu[1]*"="*mu[2]*"=0, "*sigma[1]*"="*sigma[2]*"=1 and "*rho*"=0"), type="l" ,305ylab="Perc CVaR asset 1 ", xlab = "Weight asset 1")306#text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )307if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }308# uniroot: find value for which it is 0.4 and 0.6309objective = function( w1 ){310w = as.matrix( c(w1,1-w1) );311return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )312}313h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root314#text( x = ERC , y = 0.5 , labels="ERC" )315# uniroot: find value for which it is 0.4 and 0.6316h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root317h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root318if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }319320321dev.off()322323324325326327328329330331332333334335336337338339340# Plot 1 x 3341342postscript('sensitivity_rho50.eps')343showweightlines = TRUE344par( mfrow = c(1,3) , las = 1 , mar=c(2.2,2.5,2,1.5) , cex=1 , cex.main=1)345346# !!!! Setup 7: mu1, sigma5347plot( vw1 , vpccvar7 , main = expression( mu[1]*"="*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") , type="l" )348text( x = 0.1 , y = 0.95*max(vpccvar7) , labels=expression( rho*"="*0.5) )349# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%350if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }351# uniroot: find value for which it is 0.4 and 0.6352objective = function( w1 ){353w = as.matrix( c(w1,1-w1) );354return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma5 , alpha = alpha )[1] )355}356h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root357text( x = ERC , y = 0.5 , labels="ERC" )358# uniroot: find value for which it is 0.4 and 0.6359h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root360h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root361if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;362abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }363364# !!!! Setup 9: mu1, sigma6365plot( vw1 , vpccvar9 , main = main = expression( mu[1]*"=1, "*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") , type="l" )366text( x = 0.1 , y = 0.95*max(vpccvar9) , labels=expression( rho*"="*0.5) )367if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }368# uniroot: find value for which it is 0.4 and 0.6369objective = function( w1 ){370w = as.matrix( c(w1,1-w1) );371return( h - percCompCVaR( w = w , mu = mu1 , sigma = sigma6 , alpha = alpha )[1] )372}373h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root374text( x = ERC , y = 0.5 , labels="ERC" )375# uniroot: find value for which it is 0.4 and 0.6376h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root377h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root378if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;379abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }380381382# !!!! Setup 8: mu2, sigma5383plot( vw1 , vpccvar8 , main = "", type="l" )384text( x = 0.1 , y = 0.95*max(vpccvar8) , labels=expression( rho*"="*0.5) )385if(showweightlines){ abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }386# uniroot: find value for which it is 0.4 and 0.6387objective = function( w1 ){388w = as.matrix( c(w1,1-w1) );389return( h - percCompCVaR( w = w , mu = mu2 , sigma = sigma5 , alpha = alpha )[1] )390}391h=0.5; ERC = uniroot( objective , lower = 0 , upper = 1)$root392text( x = ERC , y = 0.5 , labels="ERC" )393# uniroot: find value for which it is 0.4 and 0.6394h=0.4 ; v1 = uniroot( objective , lower = 0 , upper = 1)$root395h=0.6; v2 = uniroot( objective , lower = 0 , upper = 1)$root396if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;397abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }398399# !!!! Setup 4: mu1, sigma3400plot( vw1 , vpccvar4 , main = expression( mu[1]*"="*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") ,401type="l",ylab="Perc CVaR asset 1 \n in function of its weight" )402# %C[2]CVaR <= 60% implies %C[1]CVaR >= 40%403# uniroot: find value for which it is 0.4 and 0.6404objective = function( w1 ){405w = as.matrix( c(w1,1-w1) );406return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )407}408v1 = uniroot( objective , lower = 0 , upper = 1)$root409objective = function( w1 ){410w = as.matrix( c(w1,1-w1) );411return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma3 , alpha = alpha )[1] )412}413v2 = uniroot( objective , lower = 0 , upper = 1)$root414if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) ;415abline( h = 0.4 , lty = 3) ; abline( h = 0.6 , lty = 3 ) }416abline( h=0.5 , lty = 3 )417418# !!!! Setup 5: mu2, sigma3419plot( vw1 , vpccvar5 , main = expression( mu[1]*"=1, "*mu[2]*"=0, "*rho*"="*0*", "*sigma[1]*"="*sigma[2]*"=1") ,420type="l" , ylab = "Perc CVaR asset 1 \n in function of its weight" )421# uniroot: find value for which it is 0.4 and 0.6422objective = function( w1 ){423w = as.matrix( c(w1,1-w1) );424return( 0.5 - percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1] )425}426v1 = uniroot( objective , lower = 0 , upper = 1)$root427objective = function( w1 ){428w = as.matrix( c(w1,1-w1) );429return( 0.5 - percCompCVaR( w = w , mu = mu2 , sigma = sigma3 , alpha = alpha )[1] )430}431v2 = uniroot( objective , lower = 0 , upper = 1)$root432if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }433abline( h=0.5 , lty = 3 )434435# !!!! Setup 6: mu1, sigma4436plot( vw1 , vpccvar6 , main = expression( mu[1]*"="*mu[2]*"=0, "*rho*"=0, "* sigma[1]*"=1, "*sigma[2]*"=2 "),437type="l" , ylab = "Perc CVaR asset 1 \n in function of its weight" )438# uniroot: find value for which it is 0.4 and 0.6439objective = function( w1 ){440w = as.matrix( c(w1,1-w1) );441return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1] )442}443v1 = uniroot( objective , lower = 0 , upper = 1)$root444objective = function( w1 ){445w = as.matrix( c(w1,1-w1) );446return( 0.5 - percCompCVaR( w = w , mu = mu1 , sigma = sigma4 , alpha = alpha )[1] )447}448v2 = uniroot( objective , lower = 0 , upper = 1)$root449if(showweightlines){ abline( v = v1 , lty = 3) ; abline( v = v2 , lty = 3 ) }450abline( h=0.5 , lty = 3 )451452dev.off()453454455