Generalized Impulse Responses: Feedback on user written function

947 views
Skip to first unread message

alexha...@gmail.com

unread,
May 25, 2015, 12:15:02 AM5/25/15
to ts...@googlegroups.com
Hi All,

My name is Alex and I'm a first year PhD student. I would like to use a TVAR model for a term paper, where I would also like to use GIRF.
As far as I know, they haven't been implemented in the package, therefore I wrote my own function based on Koop et al. (1996) and the appendix of Baum and Koester (2011) (see attachment, page 30 for the algorithm).

My code is far from perfect. It's pretty slow because of a for loop (I guess) which I'm planing to replace in the next step. Furthermore, I just tested it with the "zerolyd" dataset (therefore the code will only work with an endogenous threshold variable, I guess).

In writing the code I tried to follow the suggestions from this thread:
So I use sample() to bootstrap the residuals and then TVAR.sim() to simulate the two series (with and without the additional initial shock).

I know that there were some request for GIRF. Therefore I'm posting my code here in hope of some feedback to reassure myself of the correct working of my code. In the next step I would then try writing a better/faster and more general version of the function.
However, before doing so, I would like to make sure that I "get" the idea of a GIRF, because I'm no entirely sure about two things:
  • bootstrap samples: I assume that the samples have to be taken from the residuals of the corresponding variable and not from all residuals. For example: a bootstrap residual for long.rate has to come from the residuals of long.rate (and shouldn't be from a residual of short.rate) in the "zerolyd" dataset,
  • varcov in TVAR.sim(): I'm not using it (--> default of identity matrix). In my understanding, this shouldn't influence the behavior of the GIRF algorithm

I attached my code and the paper for the algorithm. The code is straightforward. The package is loaded and then there are two functions. GIRF() (the main function) which calls the second function: simTVAR() (which then uses TVAR.sim()).
At the end of the script, you'll find a small example with the "zerolyd" dataset.

Any feedback would be appreciated!!

Best,
Alex
Baum_Koester.pdf
GIRF.R

Alexander Haider

unread,
May 25, 2015, 12:20:40 AM5/25/15
to ts...@googlegroups.com
there is a updated version of the script in the "Impulse Response Functions" Thread.

Matthieu

unread,
May 25, 2015, 12:48:52 AM5/25/15
to ts...@googlegroups.com, alexha...@gmail.com
Dear Alexander

Thanksa  lot for sharing your function with the list, much appreciated! Unfortunately, I have myself my qualifying exams in less than one month, so won't be able to investigate the code (and the papers) any time soon!

Very quickly, on your specific questions:

-bootstrap residuals: yes, you should not mix the residuls. As I understand residual based boostrapping, you use the sample random draw for all variable, as if you were just resanmpling the index, so would rather look like:
resid <- sample(results$residuals, size=horizon, replace=TRUE)
no need in my opinion to to this specifically for each variable.
-varcov: you are right, this funciton is not relevant if you provide yourself the innovations, as you do
-external variable: unfortunately, this doesn't work so far, I should have actually not added the thVar argument at all. This is a function I am not really happy with, wanted to modify it, did it for the VAR.sim/VAR.boot in last version update, but so far did not have time to do it :-(

Good luck and thanks again for sharing your work!

Matthieu

Le 24. 05. 15 21:20, Alexander Haider a écrit :
--
You received this message because you are subscribed to the Google Groups "tsdyn" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tsdyn+un...@googlegroups.com.
To post to this group, send email to ts...@googlegroups.com.
Visit this group at http://groups.google.com/group/tsdyn.
For more options, visit https://groups.google.com/d/optout.

Alexander Haider

unread,
Jul 2, 2015, 9:18:47 PM7/2/15
to ts...@googlegroups.com
Hi, 

thanks for the feedback Matthieu. It was very helpful. I hope your qualifying exams went well! 

So, after some time I took another look at my function. Unfortunately there was a (really stupid) mistake in it. In my first version I shocked the residuals of the reduced form equation, which, of course, doesn't make much sense. I don't know how I could overlook that...
So I had to change my code. The residuals are now transformed into structural errors by the inverse of a Cholesky factorization (as suggested by Koop et al (1996)). After adding the initial shock , the errors are transformed back by the Cholesky factor and used in the simulation.

Furthermore, in the first version I bootstraped the residuals one by one. However, most authors argue that the shocks are jointly distributed. Therefore one should bootstrap vectors of size k, where k is the number of endog. variables. I also changed that.

Lastly I changed the algorithm a little bit: While some authors take each observations as a history (like I did in the first version), others take samples within the regimes (with replacement). I think the second approach is more useful, because the number of histories within each regime are equalized by this approach. in the old approach we could easily end up in a situation where one regime has only a few observations, which reduces the number of histories drastically. This also reduces the quality of the simulations. 

My new approach is based on Caggiano et al (see attachment), although they are not estimating a threshold VAR. But the GIRF algorithm shouldn't be affected by that.

If someone is interested in Generalized Impulse Response functions and wants to give me some feedback, I would appreciate it. Unfortunately I wasn't able to reproduce any examples yet (I want to try that soon).

The R-file in the attachment contains a simple example and three functions. Two of them are used for calculating the GIRF (the second function is just called by the first one internally). The last function plots the responses (I just wrote this one today. So it's very experimental).

Best,
Alex


wp2014n26.pdf
girf_sample.R

giovanna ciaffi

unread,
Oct 2, 2022, 10:05:52 AM10/2/22
to tsdyn
Hi Alex,
I am trying to estimate a TVAR with an external threshold using your example.
Specifically, picking up on Caggiano's 2015 analysis I would like to estimate a TVAR with an external threshold. While your example works with an internal threshold it seems to work, it does not with an external threshold.
In the TVAR.sim command, there is no possibility to estimate TVAR with an external threshold, whereas in the TVAR command this is possible.
I would therefore like to know if you think it is possible to adopt your procedure also with an external threshold. Would it be necessary to modify the functions you created in order to achieve this?

Thank you in advance for your help
Giovanna

alexha...@gmail.com

unread,
Oct 4, 2022, 2:40:41 PM10/4/22
to tsdyn
Dear Giovanna,

I never implemented the impulse response with an exogenous threshold variable. Therefore, you would have to rewrite the function if you want to use it with an exogenous threshold variable such that the simulation routine uses the external threshold.

Anyway, as far as I know my function is outdated and the package now includes a function named "irf" which should be used instead. I would take a look at that function.

Best,
Alex

alexha...@gmail.com

unread,
Oct 4, 2022, 6:32:53 PM10/4/22
to tsdyn
Hi, i was wrong before: you should have a look at function "GIRF" in the package

Matthieu S

unread,
Oct 5, 2022, 2:50:37 AM10/5/22
to ts...@googlegroups.com
Dear Alexander and Giovanna

Thanks for your answer. I had multiple people asking for regime-specific GIRF, but as I don't work on time series anymore, I am not very knowledgeable about those.

Could you share the definition of a regime-specific GRIF? I've opened a github issue to discuss this, would be thankful if we could follow-up there. But in a nutshell, copy-pasting the text:

First step is to find the definition of the regime-specific GIRF. I guess one needs to define first a "regime-specific trajectory". Is that a trajectory whose:

  • history originates in regime A,
  • history originates in A and the shock preserves the regime?
  • stays n.ahead in regime A?

Then, is the regime-specific GIRF history/shock-trajectory dependent? Or can be aggregated over the trajectories? Over shocks or histories?

Thanks!

Matthieu

--
You received this message because you are subscribed to the Google Groups "tsdyn" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tsdyn+un...@googlegroups.com.

giovanna ciaffi

unread,
Oct 6, 2022, 4:43:48 AM10/6/22
to tsdyn
Thank you for your email.
When I'm talking about a regime-specific GIRF I refer to the Baum and Koester, 2011algorithm. With the GIRF package, I am not able to obtain the GIRF conditional of the state.
I am not a good coder, but I am trying to learn. I think there is a problem with rewriting the function because, in TVAR.sim, which is used for simulation, it is not allowed to use an external threshold. But is this a problem just for the confidence interval or for the calculation of GIRF?
In the GIRF function what actually I should modify is:

 threshV <- which(res$model.specific$transCombin == 1) #which variable is used as threshold variable

Is this right?

I think that a definition of a regime-specific trajectory is a trajectory whose history originates in regime A.
Additionally, I think that regime-specific GIRF is history and shock-trajectory dependent.
I do not know if what I am saying is correct, but I think so.

Here is the link to the paper I am talking about, in the appendix page 30 for the algorithm
https://www.econstor.eu/bitstream/10419/44961/1/65618079X.pdf

Thank you,
Giovanna

Matthieu Stigler

unread,
Oct 6, 2022, 4:52:55 AM10/6/22
to tsdyn
Hi Giovanna

Thanks for your answer. SO there are really two points here:
- write code for regime-specific GIRF in tsdyn, independent of TVAR, setar etc: issue 45 https://github.com/MatthieuStigler/tsDyn/issues/45
- extend the function TVAR.sim for external threshold: issue 44, https://github.com/MatthieuStigler/tsDyn/issues/44

I would appreciate if we could move the conversation here to the github issues, hoping we make Alex on board.

Thanks also for the Baum and Koester link. Can you please highlight which steps on page 32 are general (hence already in tsDyn::GIRF) versus specific to the regime-specific GIRF?  Also, how do you intend to choose the epsilon_0 in your application?

Thanks
Reply all
Reply to author
Forward
0 new messages