rugarch and copulas

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

rugarch and copulas

ludovic.theate
Hi everybody,

I recently discovered the rugarch package and I have some questions
about how I could use it for the model I am interested in.

In fact, we develop a currency model, using in a first step, some
garch models in order to fit a basket of currencies change rates. Then
we try to account the correlation between each of them using a copula.
A first version of this model has been developped using the fGarch
package.

Question 1 : If I have correctly understood the use of the fGarch
package, I have to fit the model to the data using the garchFit
method. Then in order to simulate some trajectories, I need to use the
garchSim method which needs a model specification created by the
garchSpec method (which requires to extract all the coefficients from
the fit object and to specify them inside the garchSpec function). So
there is no direct link between the fit method and the simulation one.
Did I miss something ? If not, I clearly prefer the spec >- fit ->
simulation workflow from rugarch.

Question 2 : After fitting the arma-garch models to the various
series, I compute a residulas matrix and I compute the rank of each of
them within the original series. Then I fit a student copula using the
fitCopula function. The next step is to simulate "correlated
trajectories". To do so, I simulate realisations of the student copula
using the rmvdc function, then I directly inject these innovations
inside the garchSim method (this is easy to do by modifying the
function, there is just a parameter to add and a little modification
inside the body to do.)

If I want to do the same job with the rugarch package, how should I
provide to the function the innovations which were previously
generated ?

Thanks for your help,

Ludo

_______________________________________________
[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: rugarch and copulas

alexios
Try the rmgarch package on r-forge (see in particular the cgarchfit function and related methods). The package's vignette has details on the implementation of each model.

-Alexios

Sent from my iPhone

On Oct 24, 2012, at 14:14, Ludovic Theate <[hidden email]> wrote:

> Hi everybody,
>
> I recently discovered the rugarch package and I have some questions
> about how I could use it for the model I am interested in.
>
> In fact, we develop a currency model, using in a first step, some
> garch models in order to fit a basket of currencies change rates. Then
> we try to account the correlation between each of them using a copula.
> A first version of this model has been developped using the fGarch
> package.
>
> Question 1 : If I have correctly understood the use of the fGarch
> package, I have to fit the model to the data using the garchFit
> method. Then in order to simulate some trajectories, I need to use the
> garchSim method which needs a model specification created by the
> garchSpec method (which requires to extract all the coefficients from
> the fit object and to specify them inside the garchSpec function). So
> there is no direct link between the fit method and the simulation one.
> Did I miss something ? If not, I clearly prefer the spec >- fit ->
> simulation workflow from rugarch.
>
> Question 2 : After fitting the arma-garch models to the various
> series, I compute a residulas matrix and I compute the rank of each of
> them within the original series. Then I fit a student copula using the
> fitCopula function. The next step is to simulate "correlated
> trajectories". To do so, I simulate realisations of the student copula
> using the rmvdc function, then I directly inject these innovations
> inside the garchSim method (this is easy to do by modifying the
> function, there is just a parameter to add and a little modification
> inside the body to do.)
>
> If I want to do the same job with the rugarch package, how should I
> provide to the function the innovations which were previously
> generated ?
>
> Thanks for your help,
>
> Ludo
>
> _______________________________________________
> [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: rugarch and copulas

ludovic.theate
Thanks for your answer, and sorry for the long delay.

OK, I am now giving a try to this cgarchfit function. However, as I can see, it works only (for the moment) with Gaussian and t-copulas. But I would like to have access to a more complete panoply of copulas models, including nested-archimedean copulas (even vines if time permits). So I am still interested to know how I could specify the innovations in the Garch prediction function of the rugarch package ?

Thanks for your help.
Reply | Threaded
Open this post in threaded view
|

Re: rugarch and copulas

alexios
1. Open the source package and see how the normal and student models are used in combination with GARCH...that should provide you with an idea of how to combine the two.
2. Read the vignette which contains additional details on the models with additional references.
3. Search this forum for an old reply to a similar question.

-Alexios

Sent from my iPhone

On Nov 8, 2012, at 13:43, "ludovic.theate" <[hidden email]> wrote:

> Thanks for your answer, and sorry for the long delay.
>
> OK, I am now giving a try to this cgarchfit function. However, as I can see,
> it works only (for the moment) with Gaussian and t-copulas. But I would like
> to have access to a more complete panoply of copulas models, including
> nested-archimedean copulas (even vines if time permits). So I am still
> interested to know how I could specify the innovations in the Garch
> prediction function of the rugarch package ?
>
> Thanks for your help.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/rugarch-and-copulas-tp4647300p4648880.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [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: rugarch and copulas

ludovic.theate
Alexios,

Thanks for your reply.

Unfortunately, I do not have so much time to spend on this problem and as I see, the rmgarch package has become quite complex. So I think I will continue for the moment using the fgarch package which is more accessible and would provide me an easier way to deal with other copulas than the elliptic one.

However, I will of course keep an eye on your package and I am looking forward to being able to use the cgarchsim function with other copulas, e.g. archimedean copulas.

Thanks again,

Regards,

Ludovic
Reply | Threaded
Open this post in threaded view
|

Re: rugarch and copulas

ludovic.theate
Hi Alexios,

Things are often clearer in the morning, and I think I changed a little bit my mind about what I said yesterday. I really do not like the fgarch package, since there is no well defined link between the fit and the simulation methods. I clearly prefer the architecture spec -> fit -> sim you use in rugarch.

So I decided to give another try, and I just would like to ask to you (and of course the other people following this list) if you could approve/amend the following approach :

1) I filter the data using an univariate rugarch model for each currency I am modelling.
2) I evaluate the standardized residuals using “residuals(fit)/sigma(fit)” as explained in a previous threat.
3) I transform them into uniform using either the ranks, or using a parametric approach you described in the thread I was talking about. Unfortunately, I am not sure I understand your explanation (“extracting any conditional higher moment parameters from the GARCH fitted object and pass those with the standardized residuals to the "pdist" function of rugarch indicating the distribution used”). Could you explain with a little more details please ?
4) With this matrix of uniform marginals, I am able to fit the copulas I am interested in (even nested, or vine ones), compare them, make tests, and choose the most appropriate one.
5) Using this copula, I can generate a random n_currencies x n_simulations x n_horizon matrix containing the innovations.
6) Then, if I’ve correctly understood, I can simply plug each row (for each currency) of this matrix inside the “ custom.dist = list(name = NA, distfit = NA) “ option of ugarchsim and simulate the trajectories. Is it correct ? If I made this for each currencies, I will get correlated sample trajectories.

Do you think this kind of algorithm is correct ? Once again, thanks for your help.

Regards,

Ludovic
Reply | Threaded
Open this post in threaded view
|

Re: rugarch and copulas

alexios
Hi,

1. ok, you fit a GARCH model to each series (filter in rugarch means
you supply a pre-defined set of parameters without fitting).

2. Since version 1.0-12., you can use residuals(fit, standardize =
TRUE). R-forge has 1.0-13 which is more up to date.

3. You have 3 choices for the transformation:
a. Empirical (ranks) -> Genest 1985 (Pseudo-Likelihood),
b. Semi-Parametric -> Davison and Smith (1990) (e.g. spd package),
c. Parametric -> Joe (1987) (Inference-Functions-for-Margins).
The rmgarch vignette contains details on the methods and relevant
references.

 >args(pdist)
function(distribution = "norm", q, mu = 0, sigma = 1, lambda = -0.5,
     skew = 1, shape = 5)

Since you are passing (0,1) residuals, mu=0 and sigma = 1, but depending
on what conditional distribution you used to fit the univariate GARCH
models you will need to provide the additional parameters
e.g. from the "coef(fit)" you will have a shape and skew parameter if
you fitted a distribution which has these parameters.

You will likely also find the code below useful once you have
transformed your residuals into uniform variates:

# make tail adjustments in order to avoid problem with the quantile
# functions in optimization
if(any(UniformResiduals > 0.99999)){
ix = which(UniformResiduals > 0.99999)
UniformResiduals [ix] = 0.99999
}
if(any(UniformResiduals < .Machine$double.eps)){
ix = which(UniformResiduals < (1.5*.Machine$double.eps))
UniformResiduals [ix] = .Machine$double.eps
}


4./5 Correct

6. Incorrect. You first need to do a "reverse-transform" of the copula
simulated values by using the qdist (quantile) function. THEN you plug
in to the custom.dist function. These are now dependent (in the cross
section) standardized residuals.

Note that this topic was also covered here:
http://r.789695.n4.nabble.com/Copula-in-R-td928826.html

As to the correctness of this approach, I would say it is a valid
method, which adopts a 2/3 stage compromise rather than a full 1-stage
ML approach which would be computationally forbidding. In the rmgarch
package, things are done slightly differently since the focus is on
using DCC based modelling with elliptical copulas (although the non-DCC
approach is also allowed).

Hope that clarifies.

-Alexios

On 09/11/2012 10:29, ludovic.theate wrote:

> Hi Alexios,
>
> Things are often clearer in the morning, and I think I changed a little bit
> my mind about what I said yesterday. I really do not like the fgarch
> package, since there is no well defined link between the fit and the
> simulation methods. I clearly prefer the architecture spec -> fit -> sim you
> use in rugarch.
>
> So I decided to give another try, and I just would like to ask to you (and
> of course the other people following this list) if you could approve/amend
> the following approach :
>
> 1) I filter the data using an univariate rugarch model for each currency I
> am modelling.
> 2) I evaluate the standardized residuals using “residuals(fit)/sigma(fit)”
> as explained in a previous threat.
> 3) I transform them into uniform using either the ranks, or using a
> parametric approach you described in the thread I was talking about.
> Unfortunately, I am not sure I understand your explanation (“extracting any
> conditional higher moment parameters from the GARCH fitted object and pass
> those with the standardized residuals to the "pdist" function of rugarch
> indicating the distribution used”). Could you explain with a little more
> details please ?
> 4) With this matrix of uniform marginals, I am able to fit the copulas I am
> interested in (even nested, or vine ones), compare them, make tests, and
> choose the most appropriate one.
> 5) Using this copula, I can generate a random n_currencies x n_simulations x
> n_horizon matrix containing the innovations.
> 6) Then, if I’ve correctly understood, I can simply plug each row (for each
> currency) of this matrix inside the “ custom.dist = list(name = NA, distfit
> = NA) “ option of ugarchsim and simulate the trajectories. Is it correct ?
> If I made this for each currencies, I will get correlated sample
> trajectories.
>
> Do you think this kind of algorithm is correct ? Once again, thanks for your
> help.
>
> Regards,
>
> Ludovic
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/rugarch-and-copulas-tp4647300p4649023.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [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: rugarch and copulas

ludovic.theate
Hi Alexios,

It is really better than just clarification, your help is really precious and appreciated. Thanks.

Just a remark concerning point 5. It was just a lack of precision from my side, since in the actual version of the model, I am already using the command

copulaSpec <- mvdc(calibration$t.copula, calibration$models,sstdSpec)

to do what you call the "reverse-transform".

In a (near ?) future, I plan to try to understand all these multivariate models you are providing in your rmgarch package (especially the DCC ones), but for the moment, I will deal only with copula-garch models.

Once again, thank you very much, and have a nice weekend,

Ludovic
Reply | Threaded
Open this post in threaded view
|

Re: rugarch and copulas

ludovic.theate
Alexios,

Sorry but I've got a last problem.

I firstly simulated independant trajectories of my processes, and I find acceptable results.

Then I tried to simulate correlated trajectories using the following function (as we discussed last week) :

##########################
# Sim_Cop_Garch Function #
##########################


sim_cop_garch <- function(mfit,cop,nsim,horizon){
ncur = length(mfit)

result <- NULL
for(j in 1:nsim){

# Simulation of nsim random values of the copulas: these are uniform
#uni_res <- rCopula(horizon,cop)  # FIRST TRY
uni_res <- matrix(runif(horizon*ncur),ncol=ncur,nrow=horizon)  # SECOND TRY

# Inverse-transform : uniform values are transformed into appropriate values (e.g. norm, sstd,...)
innov = matrix(NA, ncol = ncur, nrow = horizon)
for(i in 1:ncur){
        gdist = mfit[[i]]@model$modeldesc$distribution
        lambda = ifelse(mfit[[i]]@model$modelinc[18]>0, mfit[[i]]@fit$ipars["ghlambda",1], 0)
        skew = ifelse(mfit[[i]]@model$modelinc[16]>0, mfit[[i]]@fit$ipars["skew",1],  0)
        shape = ifelse(mfit[[i]]@model$modelinc[17]>0, mfit[[i]]@fit$ipars["shape",1],  0)
        innov[,i] = qdist(gdist,uni_res[,i] , mu = 0, sigma = 1, lambda = lambda, skew = skew, shape = shape)}

# Simulation of the ARIMA-GARCH model using the previously generated innovations

one_simul <- matrix(NA,ncol=ncur,nrow=1)
for(i in 1:ncur){

test <- ugarchsim(mfit[[i]],n.sim=horizon,m.sim=1,
custom.dist=list(name="sample",distfit=matrix(innov[,i],ncol=1,nrow=horizon)))@simulation$seriesSim[horizon,1]

one_simul[,i] <- exp(diffinv(test))[2] }

result <- rbind(result,one_simul)
}
return(result)
}


The results I got for a Gaussian copula were clearly incorrect. So I modified the function to skip the copula-generated uniform random numbers (with the "first try" comment ) and I used the runif method instead. (with the "second try" comment)  Even when I do this, I find impossible results, so the problem doesn't come from the copula.

I think I have maybe misunderstood the behavior of the custom.dist option in the ugarchsim method. Could you please have a look and tell me if you see something wrong ? Thanks a lot.

Kind regards,

Ludovic
Reply | Threaded
Open this post in threaded view
|

Re: rugarch and copulas

alexios
This is not really a reproducible example...nevertheless, I am guessing
you are trying to generate the final point of the path at
simulation_time "horizon".
What you are forgetting is that the values in "simulation$seriesSim" are
returns, so doing "exp(diffinv(test))[2]" on the last value (horizon) is
meaningless. What you want (probably) is:

...@simulation$seriesSim[1:horizon,1]
one_simul[,i] <- tail(exp(diffinv(test)), 1)

-Alexios

On 21/11/2012 09:27, ludovic.theate wrote:

> Alexios,
>
> Sorry but I've got a last problem.
>
> I firstly simulated independant trajectories of my processes, and I find
> acceptable results.
>
> Then I tried to simulate correlated trajectories using the following
> function (as we discussed last week) :
>
> ##########################
> # Sim_Cop_Garch Function #
> ##########################
>
>
> sim_cop_garch <- function(mfit,cop,nsim,horizon){
> ncur = length(mfit)
>
> result <- NULL
> for(j in 1:nsim){
>
> # Simulation of nsim random values of the copulas: these are uniform
> #uni_res <- rCopula(horizon,cop)  # FIRST TRY
> uni_res <- matrix(runif(horizon*ncur),ncol=ncur,nrow=horizon)  # SECOND TRY
>
> # Inverse-transform : uniform values are transformed into appropriate values
> (e.g. norm, sstd,...)
> innov = matrix(NA, ncol = ncur, nrow = horizon)
> for(i in 1:ncur){
> gdist = mfit[[i]]@model$modeldesc$distribution
> lambda = ifelse(mfit[[i]]@model$modelinc[18]>0,
> mfit[[i]]@fit$ipars["ghlambda",1], 0)
> skew = ifelse(mfit[[i]]@model$modelinc[16]>0,
> mfit[[i]]@fit$ipars["skew",1],  0)
> shape = ifelse(mfit[[i]]@model$modelinc[17]>0,
> mfit[[i]]@fit$ipars["shape",1],  0)
> innov[,i] = qdist(gdist,uni_res[,i] , mu = 0, sigma = 1, lambda = lambda,
> skew = skew, shape = shape)}
>
> # Simulation of the ARIMA-GARCH model using the previously generated
> innovations
>
> one_simul <- matrix(NA,ncol=ncur,nrow=1)
> for(i in 1:ncur){
>
> test <- ugarchsim(mfit[[i]],n.sim=horizon,m.sim=1,
> custom.dist=list(name="sample",distfit=matrix(innov[,i],ncol=1,nrow=horizon)))@simulation$seriesSim[horizon,1]
>
> one_simul[,i] <- exp(diffinv(test))[2] }
>
> result <- rbind(result,one_simul)
> }
> return(result)
> }
>
>
> The results I got for a Gaussian copula were clearly incorrect. So I
> modified the function to skip the copula-generated uniform random numbers
> (with the "first try" comment ) and I used the runif method instead. (with
> the "second try" comment)  Even when I do this, I find impossible results,
> so the problem doesn't come from the copula.
>
> I think I have maybe misunderstood the behavior of the custom.dist option in
> the ugarchsim method. Could you please have a look and tell me if you see
> something wrong ? Thanks a lot.
>
> Kind regards,
>
> Ludovic
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/rugarch-and-copulas-tp4647300p4650268.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [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: rugarch and copulas

ludovic.theate
Yes. Clearly a stupid question for a stupid mistake. I made a try, and I get now correct results.

Thanks and have a nice day.

Ludovic