CVaR with NIG- GARCH(1,1)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

CVaR with NIG- GARCH(1,1)

Alexandra Allexa


Hello,

My name is Alexandra and I have a very tight deadline for my MSc dissertation. My intention is to do a CVaR/ES using a NIG_GARCH model for estimating the volatility.

With the Alexios's help I did a part of the following code (Thank you very much for your help, Alexios Ghalanos!):

library(timeSeries)
library(timeDate)
library(Rcpp)
library(RcppArmadillo)
library(parallel)
library(chron)
library(Rsolnp)
library(truncnorm)
library(rugarch)
library(fGarch)
library(timeDate)
library(PerformanceAnalytics)
library(AER)
library(fGarch)

eur_all=timeSeries(eur_2001)
r_eur1=getReturns(eur_all)
rand_eur1=r_eur1*100

#-NIG_GARCH

#I. Specification Model and Fitting

spec1 = ugarchspec(variance.model = list(model = 'sGARCH',
                                         garchOrder = c(1,1)), mean.model = 
                     list(armaOrder = c(1,1), include.mean = TRUE),
                   distribution.model = "nig")



#Fit the model


tmp = ugarchroll(spec1, rand_eur1, forecast.length = 1500, refit.every = 50,
                 refit.window = 'moving', windows.size = 1500, solver ='hybrid', 
                 calculate.VaR = TRUE,VaR.alpha = c(0.01, 0.025, 0.05), keep.coef = TRUE)

if (!is.null(tmp@model$noncidx)) {
    
  tmp = resume(tmp, solver = "solnp", fit.control = list(scale = 1), solver.control = list(tol = 1e-07,
                                                                                           delta = 1e-06))
  
  if (!is.null(tmp@model$noncidx))
    
    fitlist1 = NA                 
} else {
  
  fitlist1 = as.data.frame(tmp, which = 'density')
  
  
}


# Defining NIG distribution

mu=fitlist1[, 'Mu']
sigma=fitlist1[, 'Sigma']
shape=fitlist1[, 'Shape']
skew=fitlist1[, 'Skew']
lambda =fitlist1[, 'Shape.GIG']

dist=ddist(distribution = "nig", y, mu = mu, sigma = sigma, lambda = lambda, skew = skew, 
           shape = shape)
pd=pdist(distribution = "nig", q, mu = mu, sigma = sigma, lambda = lambda, skew = skew, 
         shape = shape)
qst=qdist(distribution = "nig", p, mu = mu, sigma = sigma, lambda = lambda, skew = skew, 
          shape = shape)
r=rdist(distribution = "nig", n, mu = mu, sigma = sigma, lambda = lambda, skew = skew, 
        shape = shape)
fitdist(distribution = "nig", fitlist1, control=list())

distplot(distribution = "snig", skewbounds = NULL, shapebounds = NULL, 
         n.points = NULL)

#CVaR/ES

EURLOSS <- timeSeries(-1.0*rand_eur1,
                      char.vec = time(eur_all))

ESgarch <- function(rand_eur1, p = 0.99){
  sigma <-fitlist1[, 'Sigma']  
  df <- fitlist1[,"Shape"]
  ES <- sigma * (dist(qst(p, df), df)/(1 - p)) *
    ((df + (qst(p, df))^2)/(df - 1))
  return(ES)
}


from <- time(EURLOSS)[-c((nrow(EURLOSS) - 999) : nrow(EURLOSS))]
to <- time(EURLOSS)[-c(1:1000)]
EURSEES <- fapply(EURLOSS, from = from, to = to, FUN = ESgarch)
EURSEESL1 <- lag(EURSEES, k = 1)
res <- na.omit(cbind(EURSELOSS, EURSEESL1))
colnames(res) <- c("EURSELOSS", "ES99")
plot(res[, 2], col = "red", ylim = range(res),
     main = "EUR: NIG-GARCH(1,1) ES 99%",
     ylab = "percentages", xlab = "")
points(res[, 1], type = "p", cex = 0.2, pch = 19, col = "blue")
legend("topleft", legend = c("Loss", "ES"),
       col = c("blue", "pink"), lty = c(NA, 1), pch = c(19, NA))



My problem is how to define the NIG distribution for such a model?
How I set the parameters/ vectors: y,p,q and n?

In this situation, it is possible to use the function CVaR or ETL provided by the package {PerformanceAnalytics}? How?

I can applied the same principle used in computing CVaR for CDD/CDaR?

Thank you in advance,

Alexandra Rautoiu
        [[alternative HTML version deleted]]


_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: CVaR with NIG- GARCH(1,1)

braverock
On 05/12/2013 05:20 PM, Alexandra Allexa wrote:

> My problem is how to define the NIG distribution for such a model?
> How I set the parameters/ vectors: y,p,q and n?
>
> In this situation, it is possible to use the function CVaR or ETL
> provided by the package {PerformanceAnalytics}? How?
>
> I can applied the same principle used in computing CVaR for
> CDD/CDaR?
>
> Thank you in advance,
>
> Alexandra Rautoiu

CVaR/ETL in PerformanceAnalytics don't have NIG distribution options.

I'm not sure if the NIG distribution has p,d,q,r functions as is common
in R for discrete distributions.

If you have the q(uantile) function, you should be able to adapt the
code from PerformanceAnalytics with relative ease, since it is only
required to know the quantile, and then integrate over the tail, to
compute the CVaR for a continuous distribution.

Regards,

Brian


--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: CVaR with NIG- GARCH(1,1)

alexios
In reply to this post by Alexandra Allexa
Hi Alexandra,

The conditional Expected Shortfall for the NIG (or any other
standardized rugarch distribution) using the rolling estimation/forecast
method is given below.

# using your example:
mu=fitlist1[, 'Mu']
sigma=fitlist1[, 'Sigma']
shape=fitlist1[, 'Shape']
skew=fitlist1[, 'Skew']

# for the 5% quantile (adjust 0.05 as appropriate)
f = function(x, mu, sigma, skew, shape) mu + qdist("nig", p=x, mu=0,
sigma=1, skew=skew, shape=shape)*sigma
for(i in 1:N)
{
ES[i] = integrate(f, 0, 0.05, mu = mu[i], sigma=sigma[i], shape =
shape[i], skew=skew[i])$value/0.05
}
# Also see the ESTest.

You do not need to load ALL those packages (you only need rugarch for
this example).

Regards,

Alexios

PS
# its "nig" NOT "snig" (you are confusing the name with that used in
# the fGarch package)...and yes, it is the same standardized
# distribution.
distplot(distribution = "nig", skewbounds = NULL, shapebounds = NULL,
          n.points = NULL)


On 12/05/2013 23:20, Alexandra Allexa wrote:

>
>
> Hello,
>
> My name is Alexandra and I have a very tight deadline for my MSc dissertation. My intention is to do a CVaR/ES using a NIG_GARCH model for estimating the volatility.
>
> With the Alexios's help I did a part of the following code (Thank you very much for your help, Alexios Ghalanos!):
>
> library(timeSeries)
> library(timeDate)
> library(Rcpp)
> library(RcppArmadillo)
> library(parallel)
> library(chron)
> library(Rsolnp)
> library(truncnorm)
> library(rugarch)
> library(fGarch)
> library(timeDate)
> library(PerformanceAnalytics)
> library(AER)
> library(fGarch)
>
> eur_all=timeSeries(eur_2001)
> r_eur1=getReturns(eur_all)
> rand_eur1=r_eur1*100
>
> #-NIG_GARCH
>
> #I. Specification Model and Fitting
>
> spec1 = ugarchspec(variance.model = list(model = 'sGARCH',
>                                           garchOrder = c(1,1)), mean.model =
>                       list(armaOrder = c(1,1), include.mean = TRUE),
>                     distribution.model = "nig")
>
>
>
> #Fit the model
>
>
> tmp = ugarchroll(spec1, rand_eur1, forecast.length = 1500, refit.every = 50,
>                   refit.window = 'moving', windows.size = 1500, solver ='hybrid',
>                   calculate.VaR = TRUE,VaR.alpha = c(0.01, 0.025, 0.05), keep.coef = TRUE)
>
> if (!is.null(tmp@model$noncidx)) {
>
>    tmp = resume(tmp, solver = "solnp", fit.control = list(scale = 1), solver.control = list(tol = 1e-07,
>                                                                                             delta = 1e-06))
>
>    if (!is.null(tmp@model$noncidx))
>
>      fitlist1 = NA
> } else {
>
>    fitlist1 = as.data.frame(tmp, which = 'density')
>
>
> }
>
>
> # Defining NIG distribution
>
> mu=fitlist1[, 'Mu']
> sigma=fitlist1[, 'Sigma']
> shape=fitlist1[, 'Shape']
> skew=fitlist1[, 'Skew']
> lambda =fitlist1[, 'Shape.GIG']
>
> dist=ddist(distribution = "nig", y, mu = mu, sigma = sigma, lambda = lambda, skew = skew,
>             shape = shape)
> pd=pdist(distribution = "nig", q, mu = mu, sigma = sigma, lambda = lambda, skew = skew,
>           shape = shape)
> qst=qdist(distribution = "nig", p, mu = mu, sigma = sigma, lambda = lambda, skew = skew,
>            shape = shape)
> r=rdist(distribution = "nig", n, mu = mu, sigma = sigma, lambda = lambda, skew = skew,
>          shape = shape)
> fitdist(distribution = "nig", fitlist1, control=list())
>
> distplot(distribution = "snig", skewbounds = NULL, shapebounds = NULL,
>           n.points = NULL)
>
> #CVaR/ES
>
> EURLOSS <- timeSeries(-1.0*rand_eur1,
>                        char.vec = time(eur_all))
>
> ESgarch <- function(rand_eur1, p = 0.99){
>    sigma <-fitlist1[, 'Sigma']
>    df <- fitlist1[,"Shape"]
>    ES <- sigma * (dist(qst(p, df), df)/(1 - p)) *
>      ((df + (qst(p, df))^2)/(df - 1))
>    return(ES)
> }
>
>
> from <- time(EURLOSS)[-c((nrow(EURLOSS) - 999) : nrow(EURLOSS))]
> to <- time(EURLOSS)[-c(1:1000)]
> EURSEES <- fapply(EURLOSS, from = from, to = to, FUN = ESgarch)
> EURSEESL1 <- lag(EURSEES, k = 1)
> res <- na.omit(cbind(EURSELOSS, EURSEESL1))
> colnames(res) <- c("EURSELOSS", "ES99")
> plot(res[, 2], col = "red", ylim = range(res),
>       main = "EUR: NIG-GARCH(1,1) ES 99%",
>       ylab = "percentages", xlab = "")
> points(res[, 1], type = "p", cex = 0.2, pch = 19, col = "blue")
> legend("topleft", legend = c("Loss", "ES"),
>         col = c("blue", "pink"), lty = c(NA, 1), pch = c(19, NA))
>
>
>
> My problem is how to define the NIG distribution for such a model?
> How I set the parameters/ vectors: y,p,q and n?
>
> In this situation, it is possible to use the function CVaR or ETL provided by the package {PerformanceAnalytics}? How?
>
> I can applied the same principle used in computing CVaR for CDD/CDaR?
>
> Thank you in advance,
>
> Alexandra Rautoiu
> [[alternative HTML version deleted]]
>
>
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: CVaR with NIG- GARCH(1,1)

alexios
In reply to this post by braverock
Hi Brian,

All distributions in rugarch have p*,d*,q*,r* functions, but I have
unified the functions into the following functions:

pdist, ddist, qdist, rdist

They simply take as first argument the distribution (e.g. "nig",
"std","sstd", "norm", "snorm", "ghyp", "ged","sged","ghst"), and then
the standard inputs where mu is the mean, and sigma the standard
deviation (since the distributions in rugarch are standardized this way).

Best,

Alexios

On 12/05/2013 23:43, Brian G. Peterson wrote:

> On 05/12/2013 05:20 PM, Alexandra Allexa wrote:
>> My problem is how to define the NIG distribution for such a model?
>> How I set the parameters/ vectors: y,p,q and n?
>>
>> In this situation, it is possible to use the function CVaR or ETL
>> provided by the package {PerformanceAnalytics}? How?
>>
>> I can applied the same principle used in computing CVaR for
>> CDD/CDaR?
>>
>> Thank you in advance,
>>
>> Alexandra Rautoiu
>
> CVaR/ETL in PerformanceAnalytics don't have NIG distribution options.
>
> I'm not sure if the NIG distribution has p,d,q,r functions as is common
> in R for discrete distributions.
>
> If you have the q(uantile) function, you should be able to adapt the
> code from PerformanceAnalytics with relative ease, since it is only
> required to know the quantile, and then integrate over the tail, to
> compute the CVaR for a continuous distribution.
>
> Regards,
>
> Brian
>
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.