Path: blob/master/sandbox/riskbudgetpaper(superseded)/illustration/CVaRvsVaRsst.R
1433 views
1# Code that produces the graph showing the difference between VaR and CVaR2# For a Skewed Student t distribution3#---------------------------------------------------------------------------45# skewedstudentt : sst67# a student has variance nu/(nu-2)8# ! standardized to have variance 1910# It has as a basis distribution the standardized student t1112dstd =13function(x, mean = 0, sd = 1, nu = 5)14{ # A function implemented by Diethelm Wuertz15# Compute the density for the standardized Student-t distribution.16# Density function can be found in Bollerslev (1987)1718if(nu!=Inf){19s = sqrt(nu/(nu-2))20z = (x - mean) / sd21result = dt(x = z*s, df = nu) * s / sd22}else{ result = dnorm(x)}23return(result)24}2526pstd =27function (q, mean = 0, sd = 1, nu = 5)28{ # A function implemented by Diethelm Wuertz29# Description: Compute the probability for the standardized Student-t distribution.3031if(nu!=Inf){32s = sqrt(nu/(nu-2))33z = (q - mean) / sd34result = pt(q = z*s, df = nu)35}else{ result = pnorm(q) }36return(result)37}3839qstd =40function (p, mean = 0, sd = 1, nu = 5)41{ # A function implemented by Diethelm Wuertz42# Description: compute the quantiles for the standardized Student-t distribution.4344if(nu!=Inf){45s = sqrt(nu/(nu-2))46result = qt(p = p, df = nu) * sd / s + mean47}else{ result = qnorm(p=p) }48return(result)49}5051m = function(nu)52{53m = gamma( (nu-1)/2 )*sqrt(nu-2)54m = m / ( sqrt(pi)*gamma(nu/2) )55return(m)56}57#converges to 0.8585960sstVAR=function(alpha,nu,xi) # computes VaR using the quantile function in Lambert and Laurent (2001), Giot and Laurent (2003)61{62# the quantile function depends on the value of alpha63nonstsstq = ifelse( alpha < 1/(1+xi^2) , ( 1/(xi) )*qstd( p= (alpha/2)*(1+xi^2) ,nu =nu ),64-(xi)*qstd( p= 0.5*(1-alpha)*(1+ 1/xi^2) , nu =nu ) )65# compute mean and standard deviation of non standardized skewed student66if(nu<150){67m = gamma( (nu-1)/2 )*sqrt(nu-2)68m = m / ( sqrt(pi)*gamma(nu/2) )}else{m=0.8}69m = m*( xi - 1/xi )70s = sqrt( xi^2 + (1/xi^2) - 1 - m^2 )71# compute skewed student t quantile72stsstq = (nonstsstq-m)/s73# VAR is the negative quantile74return(-stsstq)75}7677sstES=function(alpha,nu,xi) # computes ES as the integred VaR for a skewed student t78{79# compute mean and standard deviation of non standardized skewed student80if(nu<150){81m = gamma( (nu-1)/2 )*sqrt(nu-2)82m = m / ( sqrt(pi)*gamma(nu/2) )}else{m=0.8}83m = m*( xi - 1/xi )84s = sqrt( xi^2 + (1/xi^2) - 1 - m^2 )8586# the quantile function depends on the value of alpha8788# For alpha in [0,1/(1+xi^2)]:89vint = c()90for ( i in c(1:length(alpha)) )91{92# For alpha in 0,1/(1+xi^2)93sstVAR=function(alpha) # computes VaR using the quantile function in Lambert and Laurent (2001), Giot and Laurent (2003)94{95nonstsstq = ( 1/(xi) )*qstd( p= (alpha/2)*(1+xi^2) ,nu =nu )96stsstq = (nonstsstq-rep(m,length(alpha)))/s97return(-stsstq) # VAR is the negative quantile98}99int = integrate(sstVAR,lower=0,upper= min(alpha[i],1/(1+xi^2)) )$value100101# For alpha in [1/(1+xi^2),1]:102103sstVAR=function(alpha) # computes VaR using the quantile function in Lambert and Laurent (2001), Giot and Laurent (2003)104{105nonstsstq = -(xi)*qstd( p= 0.5*(1-alpha)*(1+ 1/xi^2) , df=nu )106stsstq = (nonstsstq-rep(m,length(alpha)))/s107return(-stsstq) # VAR is the negative quantile108}109int = int + ifelse( alpha[i] > 1/(1+xi^2) , integrate(sstVAR,lower=1/(1+xi^2),upper=alpha[i])$value ,0)110vint = c(vint,int)111}112ES = vint/alpha113return(ES)114}115116