garchFit and garchSim

View: New views
7 Messages — Rating Filter:   Alert me  

garchFit and garchSim

by Eratothene :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi !

I am trying to fit garch(1,1) model to stock returns using fGarch.

m = garchFit(~garch(1,1), data = returns)


But next I need to do 100 simulations of future returns. I am trying to use
garchSim for that
I have extracted estimated alpha, beta and omega
        params = model@fit$matcoef[,1]
        mu = params[1]
        omega = params[2]
        alpha = params[3]
        beta = params[4]

and i trying to do future simulation of returns
        sim = mu + garchSim(list(alpha = alpha, beta = beta, omega = omega),
resample=???)
       
I see that there is a resample parameter in garchSim function, but can't
figure out how to use it, docs do not help.

Please help me, I am writing my MA theses and only one week is left till
submission and my supervisor can't help with that.


P.S.
Also I have done similar thing, but using arima model. Please, check it if I
am  doing everything right, because I  not sure about it.

forecast_arima = function (returns, n.ahead) {
    model = armaFit(~arma(10, 5), data=returns)
    c = coef(model)
    arma_mean = c["intercept"]
    arma_ar = c[1:10]
    arma_ma = c[11:15]

    m = matrix(NA, n.ahead, MAX_SIM)
                                               
    for(i in 1:MAX_SIM) {
        m[,i] = arma_mean + armaSim(list(ar = arma_ar, ma = arma_ma, d=0), n =
n.ahead, start.innov = as.vector(returns))
    }
    return(m)
}


Thank you for you expertise.

_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

Parent Message unknown Re: garchFit and garchSim

by michal miklovic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I guess you meant 'presample'. It is a matrix of starting values for the modeled series, h.t and innovations (with zero mean and unit variance). Have a look at the help page for garchSpec.
I think the arma simulation is OK if n.ahead = 1. I would say it is possible to simulate an arma process with garchSim when you set the parameters of the conditional distribution to zero but I have never done it myself.

Regards,

Michal




----- Original Message ----
From: Andrey Riabushenko <cdome@...>
To: r-sig-finance@...
Sent: Friday, May 16, 2008 3:35:17 PM
Subject: [R-SIG-Finance] garchFit and garchSim

Hi !

I am trying to fit garch(1,1) model to stock returns using fGarch.

m = garchFit(~garch(1,1), data = returns)


But next I need to do 100 simulations of future returns. I am trying to use
garchSim for that
I have extracted estimated alpha, beta and omega
    params = model@fit$matcoef[,1]
    mu = params[1]
    omega = params[2]
    alpha = params[3]
    beta = params[4]

and i trying to do future simulation of returns
    sim = mu + garchSim(list(alpha = alpha, beta = beta, omega = omega),
resample=???)
   
I see that there is a resample parameter in garchSim function, but can't
figure out how to use it, docs do not help.

Please help me, I am writing my MA theses and only one week is left till
submission and my supervisor can't help with that.


P.S.
Also I have done similar thing, but using arima model. Please, check it if I
am  doing everything right, because I  not sure about it.

forecast_arima = function (returns, n.ahead) {
    model = armaFit(~arma(10, 5), data=returns)
    c = coef(model)
    arma_mean = c["intercept"]
    arma_ar = c[1:10]
    arma_ma = c[11:15]

    m = matrix(NA, n.ahead, MAX_SIM)
                       
    for(i in 1:MAX_SIM) {
    m[,i] = arma_mean + armaSim(list(ar = arma_ar, ma = arma_ma, d=0), n =
n.ahead, start.innov = as.vector(returns))
    }
    return(m)
}


Thank you for you expertise.

_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.



     
        [[alternative HTML version deleted]]

_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

Re: garchFit and garchSim

by Eratothene :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, I meant presample indeed.

I see that it needs to be a thee column matrix, but I don't understand how to
get this columns from the times series of the returns that I have.

> Hi,
>
> I guess you meant 'presample'. It is a matrix of starting values for the
> modeled series, h.t and innovations (with zero mean and unit variance).
> Have a look at the help page for garchSpec. I think the arma simulation is
> OK if n.ahead = 1. I would say it is possible to simulate an arma process
> with garchSim when you set the parameters of the conditional distribution
> to zero but I have never done it myself.
>
> Regards,
>
> Michal
>
>
>
>
> ----- Original Message ----
> From: Andrey Riabushenko <cdome@...>
> To: r-sig-finance@...
> Sent: Friday, May 16, 2008 3:35:17 PM
> Subject: [R-SIG-Finance] garchFit and garchSim
>
> Hi !
>
> I am trying to fit garch(1,1) model to stock returns using fGarch.
>
> m = garchFit(~garch(1,1), data = returns)
>
>
> But next I need to do 100 simulations of future returns. I am trying to use
> garchSim for that
> I have extracted estimated alpha, beta and omega
>     params = model@fit$matcoef[,1]
>     mu = params[1]
>     omega = params[2]
>     alpha = params[3]
>     beta = params[4]
>
> and i trying to do future simulation of returns
>     sim = mu + garchSim(list(alpha = alpha, beta = beta, omega = omega),
> resample=???)
>
> I see that there is a resample parameter in garchSim function, but can't
> figure out how to use it, docs do not help.
>
> Please help me, I am writing my MA theses and only one week is left till
> submission and my supervisor can't help with that.
>
>
> P.S.
> Also I have done similar thing, but using arima model. Please, check it if
> I am  doing everything right, because I  not sure about it.
>
> forecast_arima = function (returns, n.ahead) {
>     model = armaFit(~arma(10, 5), data=returns)
>     c = coef(model)
>     arma_mean = c["intercept"]
>     arma_ar = c[1:10]
>     arma_ma = c[11:15]
>
>     m = matrix(NA, n.ahead, MAX_SIM)
>
>     for(i in 1:MAX_SIM) {
>     m[,i] = arma_mean + armaSim(list(ar = arma_ar, ma = arma_ma, d=0), n =
> n.ahead, start.innov = as.vector(returns))
>     }
>     return(m)
> }
>
>
> Thank you for you expertise.
>
> _______________________________________________
> R-SIG-Finance@... mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.

_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

Parent Message unknown Re: garchFit and garchSim

by Eratothene :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks a lot. I am making progress, but still some problems left
I am running

returns = as.vector(returns)
model = garchFit(~garch(1,1), data=returns)

params = model@fit$matcoef[,1]
mu = params[1]
omega = params[2]
alpha = params[3]
beta = params[4]

presample = cbind(returns, residuals(model, standardize =TRUE), model@...)

sim = mu + garchSim(list(alpha = alpha, beta = beta, omega = omega),
n=n.ahead, n.start = length(returns), presample=presample)

But what I get is
head(sim)
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

What it is wrong?


Also it is not clear why arima simulation is wrong  if n.ahead > 1, I need
more than 1 for my study?

> The modeled series is readily available - in your case it is 'returns'. h.t
> can be obtained by m@... and starting values for the innovations, i.e. the
> standardised residuals, can be obtained by residuals(m, standardize =
> TRUE).
>
> Hope this helps.
>
> Michal
>
>
>
>

_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

Parent Message unknown Re: garchFit and garchSim

by michal miklovic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

the correct order in presample is: innovations, h.t and returns. Next, I think it is necessary to first define a model by garchSpec and then use the definition to simulate it, have a look at the documentation. Btw, you can put 'mu' into the definition instead of adding it to the simulated series. I would set n.start = 0 in case of 'extending' an analysed series by simulation. n.start should be used when you simulate a series that is not 'appended' to another series.
Concerning the arma simulation, I had another look at it and now I understand what you are doing and I think it is OK. Sorry for the confusion.
Next time you post a question to mailing list, please indicate the version of R and packages you are using. It could be helpful because some packages get updated quite frequently.

Regards,

Michal




----- Original Message ----
From: Andrey Riabushenko <cdome@...>
To: michal miklovic <mmiklovic@...>; r-sig-finance@...
Sent: Saturday, May 17, 2008 11:33:16 AM
Subject: Re: [R-SIG-Finance] garchFit and garchSim

Thanks a lot. I am making progress, but still some problems left
I am running

returns = as.vector(returns)
model = garchFit(~garch(1,1), data=returns)

params = model@fit$matcoef[,1]
mu = params[1]
omega = params[2]
alpha = params[3]
beta = params[4]

presample = cbind(returns, residuals(model, standardize =TRUE), model@...)

sim = mu + garchSim(list(alpha = alpha, beta = beta, omega = omega),
n=n.ahead, n.start = length(returns), presample=presample)

But what I get is
head(sim)
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

What it is wrong?


Also it is not clear why arima simulation is wrong  if n.ahead > 1, I need
more than 1 for my study?

> The modeled series is readily available - in your case it is 'returns'. h.t
> can be obtained by m@... and starting values for the innovations, i.e. the
> standardised residuals, can be obtained by residuals(m, standardize =
> TRUE).
>
> Hope this helps.
>
> Michal
>
>
>
>



     
        [[alternative HTML version deleted]]

_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

Re: garchFit and garchSim

by Eratothene :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you Michal Miklovic, it is working now

Here is a working code

forecast_garch = function(returns, n.ahead) {
        returns = as.vector(returns)

        model = garchFit(~garch(1,1), data=returns)
        params = model@fit$matcoef[,1]
        mu = params[1]
        omega = params[2]
        alpha = params[3]
        beta = params[4]

        presample = cbind(residuals(model, standardize =TRUE), model@..., returns)
        spec = garchSpec(list(mu = mu, alpha = alpha, beta = beta, omega = omega),
presample=presample)

        m = matrix(NA, n.ahead, MAX_SIM)

        for(i in 1:MAX_SIM) {
                m[,i] = as.vector(garchSim(spec, n=n.ahead+1, n.start = 0))
        }
    return(m)
}


The small little thing is left. if n if equal to 10 the garchSim returns
series of length 9. That is way I have placed n = a.ahead + 1. Is this a bug?


I am using R-2.6.1 built from sources with ATLAS on FreeBSD 7.0, the version
of fGarch is 260.72 (also built from source)


Thank you once again.



В сообщении от Saturday 17 May 2008 13:42:49 вы написали:

> Hi,
>
> the correct order in presample is: innovations, h.t and returns. Next, I
> think it is necessary to first define a model by garchSpec and then use the
> definition to simulate it, have a look at the documentation. Btw, you can
> put 'mu' into the definition instead of adding it to the simulated series.
> I would set n.start = 0 in case of 'extending' an analysed series by
> simulation. n.start should be used when you simulate a series that is not
> 'appended' to another series. Concerning the arma simulation, I had another
> look at it and now I understand what you are doing and I think it is OK.
> Sorry for the confusion. Next time you post a question to mailing list,
> please indicate the version of R and packages you are using. It could be
> helpful because some packages get updated quite frequently.
>
> Regards,
>
> Michal
>
>
>
>
> ----- Original Message ----
> From: Andrey Riabushenko <cdome@...>
> To: michal miklovic <mmiklovic@...>; r-sig-finance@...
> Sent: Saturday, May 17, 2008 11:33:16 AM
> Subject: Re: [R-SIG-Finance] garchFit and garchSim
>
> Thanks a lot. I am making progress, but still some problems left
> I am running
>
> returns = as.vector(returns)
> model = garchFit(~garch(1,1), data=returns)
>
> params = model@fit$matcoef[,1]
> mu = params[1]
> omega = params[2]
> alpha = params[3]
> beta = params[4]
>
> presample = cbind(returns, residuals(model, standardize =TRUE), model@...)
>
> sim = mu + garchSim(list(alpha = alpha, beta = beta, omega = omega),
> n=n.ahead, n.start = length(returns), presample=presample)
>
> But what I get is
> head(sim)
> NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
>
> What it is wrong?
>
>
> Also it is not clear why arima simulation is wrong  if n.ahead > 1, I need
> more than 1 for my study?
>
> > The modeled series is readily available - in your case it is 'returns'.
> > h.t can be obtained by m@... and starting values for the innovations,
> > i.e. the standardised residuals, can be obtained by residuals(m,
> > standardize = TRUE).
> >
> > Hope this helps.
> >
> > Michal

_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.

Parent Message unknown Re: garchFit and garchSim

by michal miklovic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It is not a bug. It works fine for simulations which are not 'appended' to an existing series. But in your case, it leads to discarding one observation. I think it is possible to circumvent the problem by changing the source code as follows:
in GarchSimulation.R put # before the line "data = data[-(1:n.start),]" and recompile fGarch. I hope it works. If not, you have to find another workaround...

Michal



----- Original Message ----
From: Andrey Riabushenko <cdome@...>
To: michal miklovic <mmiklovic@...>; r-sig-finance@...
Sent: Saturday, May 17, 2008 2:32:10 PM
Subject: Re: [R-SIG-Finance] garchFit and garchSim

Thank you Michal Miklovic, it is working now

Here is a working code

forecast_garch = function(returns, n.ahead) {
    returns = as.vector(returns)

    model = garchFit(~garch(1,1), data=returns)
    params = model@fit$matcoef[,1]
    mu = params[1]
    omega = params[2]
    alpha = params[3]
    beta = params[4]

    presample = cbind(residuals(model, standardize =TRUE), model@..., returns)
    spec = garchSpec(list(mu = mu, alpha = alpha, beta = beta, omega = omega),
presample=presample)

    m = matrix(NA, n.ahead, MAX_SIM)

    for(i in 1:MAX_SIM) {
        m[,i] = as.vector(garchSim(spec, n=n.ahead+1, n.start = 0))
    }
    return(m)
}


The small little thing is left. if n if equal to 10 the garchSim returns
series of length 9. That is way I have placed n = a.ahead + 1. Is this a bug?


I am using R-2.6.1 built from sources with ATLAS on FreeBSD 7.0, the version
of fGarch is 260.72 (also built from source)


Thank you once again.



² áÞÞÑéÕÝØØ Þâ Saturday 17 May 2008 13:42:49 Òë ÝÐߨáÐÛØ:

> Hi,
>
> the correct order in presample is: innovations, h.t and returns. Next, I
> think it is necessary to first define a model by garchSpec and then use the
> definition to simulate it, have a look at the documentation. Btw, you can
> put 'mu' into the definition instead of adding it to the simulated series.
> I would set n.start = 0 in case of 'extending' an analysed series by
> simulation. n.start should be used when you simulate a series that is not
> 'appended' to another series. Concerning the arma simulation, I had another
> look at it and now I understand what you are doing and I think it is OK.
> Sorry for the confusion. Next time you post a question to mailing list,
> please indicate the version of R and packages you are using. It could be
> helpful because some packages get updated quite frequently.
>
> Regards,
>
> Michal
>
>
>
>
> ----- Original Message ----
> From: Andrey Riabushenko <cdome@...>
> To: michal miklovic <mmiklovic@...>; r-sig-finance@...
> Sent: Saturday, May 17, 2008 11:33:16 AM
> Subject: Re: [R-SIG-Finance] garchFit and garchSim
>
> Thanks a lot. I am making progress, but still some problems left
> I am running
>
> returns = as.vector(returns)
> model = garchFit(~garch(1,1), data=returns)
>
> params = model@fit$matcoef[,1]
> mu = params[1]
> omega = params[2]
> alpha = params[3]
> beta = params[4]
>
> presample = cbind(returns, residuals(model, standardize =TRUE), model@...)
>
> sim = mu + garchSim(list(alpha = alpha, beta = beta, omega = omega),
> n=n.ahead, n.start = length(returns), presample=presample)
>
> But what I get is
> head(sim)
> NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
>
> What it is wrong?
>
>
> Also it is not clear why arima simulation is wrong  if n.ahead > 1, I need
> more than 1 for my study?
>
> > The modeled series is readily available - in your case it is 'returns'.
> > h.t can be obtained by m@... and starting values for the innovations,
> > i.e. the standardised residuals, can be obtained by residuals(m,
> > standardize = TRUE).
> >
> > Hope this helps.
> >
> > Michal

     
        [[alternative HTML version deleted]]


_______________________________________________
R-SIG-Finance@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
LightInTheBox - Buy quality products at wholesale price