Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/riskbudgetpaper(superseded)/illustration/CVaRvsVaRsst.R
1433 views
1
2
# Code that produces the graph showing the difference between VaR and CVaR
3
# For a Skewed Student t distribution
4
#---------------------------------------------------------------------------
5
6
# skewedstudentt : sst
7
8
# a student has variance nu/(nu-2)
9
# ! standardized to have variance 1
10
11
# It has as a basis distribution the standardized student t
12
13
dstd =
14
function(x, mean = 0, sd = 1, nu = 5)
15
{ # A function implemented by Diethelm Wuertz
16
# Compute the density for the standardized Student-t distribution.
17
# Density function can be found in Bollerslev (1987)
18
19
if(nu!=Inf){
20
s = sqrt(nu/(nu-2))
21
z = (x - mean) / sd
22
result = dt(x = z*s, df = nu) * s / sd
23
}else{ result = dnorm(x)}
24
return(result)
25
}
26
27
pstd =
28
function (q, mean = 0, sd = 1, nu = 5)
29
{ # A function implemented by Diethelm Wuertz
30
# Description: Compute the probability for the standardized Student-t distribution.
31
32
if(nu!=Inf){
33
s = sqrt(nu/(nu-2))
34
z = (q - mean) / sd
35
result = pt(q = z*s, df = nu)
36
}else{ result = pnorm(q) }
37
return(result)
38
}
39
40
qstd =
41
function (p, mean = 0, sd = 1, nu = 5)
42
{ # A function implemented by Diethelm Wuertz
43
# Description: compute the quantiles for the standardized Student-t distribution.
44
45
if(nu!=Inf){
46
s = sqrt(nu/(nu-2))
47
result = qt(p = p, df = nu) * sd / s + mean
48
}else{ result = qnorm(p=p) }
49
return(result)
50
}
51
52
m = function(nu)
53
{
54
m = gamma( (nu-1)/2 )*sqrt(nu-2)
55
m = m / ( sqrt(pi)*gamma(nu/2) )
56
return(m)
57
}
58
#converges to 0.8
59
60
61
sstVAR=function(alpha,nu,xi) # computes VaR using the quantile function in Lambert and Laurent (2001), Giot and Laurent (2003)
62
{
63
# the quantile function depends on the value of alpha
64
nonstsstq = ifelse( alpha < 1/(1+xi^2) , ( 1/(xi) )*qstd( p= (alpha/2)*(1+xi^2) ,nu =nu ),
65
-(xi)*qstd( p= 0.5*(1-alpha)*(1+ 1/xi^2) , nu =nu ) )
66
# compute mean and standard deviation of non standardized skewed student
67
if(nu<150){
68
m = gamma( (nu-1)/2 )*sqrt(nu-2)
69
m = m / ( sqrt(pi)*gamma(nu/2) )}else{m=0.8}
70
m = m*( xi - 1/xi )
71
s = sqrt( xi^2 + (1/xi^2) - 1 - m^2 )
72
# compute skewed student t quantile
73
stsstq = (nonstsstq-m)/s
74
# VAR is the negative quantile
75
return(-stsstq)
76
}
77
78
sstES=function(alpha,nu,xi) # computes ES as the integred VaR for a skewed student t
79
{
80
# compute mean and standard deviation of non standardized skewed student
81
if(nu<150){
82
m = gamma( (nu-1)/2 )*sqrt(nu-2)
83
m = m / ( sqrt(pi)*gamma(nu/2) )}else{m=0.8}
84
m = m*( xi - 1/xi )
85
s = sqrt( xi^2 + (1/xi^2) - 1 - m^2 )
86
87
# the quantile function depends on the value of alpha
88
89
# For alpha in [0,1/(1+xi^2)]:
90
vint = c()
91
for ( i in c(1:length(alpha)) )
92
{
93
# For alpha in 0,1/(1+xi^2)
94
sstVAR=function(alpha) # computes VaR using the quantile function in Lambert and Laurent (2001), Giot and Laurent (2003)
95
{
96
nonstsstq = ( 1/(xi) )*qstd( p= (alpha/2)*(1+xi^2) ,nu =nu )
97
stsstq = (nonstsstq-rep(m,length(alpha)))/s
98
return(-stsstq) # VAR is the negative quantile
99
}
100
int = integrate(sstVAR,lower=0,upper= min(alpha[i],1/(1+xi^2)) )$value
101
102
# For alpha in [1/(1+xi^2),1]:
103
104
sstVAR=function(alpha) # computes VaR using the quantile function in Lambert and Laurent (2001), Giot and Laurent (2003)
105
{
106
nonstsstq = -(xi)*qstd( p= 0.5*(1-alpha)*(1+ 1/xi^2) , df=nu )
107
stsstq = (nonstsstq-rep(m,length(alpha)))/s
108
return(-stsstq) # VAR is the negative quantile
109
}
110
int = int + ifelse( alpha[i] > 1/(1+xi^2) , integrate(sstVAR,lower=1/(1+xi^2),upper=alpha[i])$value ,0)
111
vint = c(vint,int)
112
}
113
ES = vint/alpha
114
return(ES)
115
}
116