Impulse Response Functions

3,064 views
Skip to first unread message

Laura

unread,
Mar 6, 2012, 6:04:25 PM3/6/12
to tsdyn
Dear all,

I realize non-linear impulse response functions are still not
available in the package.

I wonder if you have any undocumented/work in progress version of the
generalized IRF function for the multivariate case that you could make
available with the usual disclaimers...

Do you think it makes any sense to compute linear impulse response
functions for each regime based on the TVAR coefficients, by just
assuming that the shock does not lead to a switch of regimes?

Best,

Laura





Matthieu Stigler

unread,
Mar 15, 2012, 4:21:03 PM3/15/12
to ts...@googlegroups.com, lau...@gmail.com
Dear Laura

Sorry for the late reply.

No, GIRF are still not in the package, I should remove this part from the documentation actually, is misleading.
Many people asked already, but I haven't found time for it. GIven however that there are already simulations functions, it should be quite straightforward to implement GIRFs.

You could always run linear IRFs with tools from packages urca/vars, but as you say correctly, such results will only hold for small shocks, small enough that the there is no regime switching. For big shocks, you will not be able to use IRFs but will need to implement the GIRFs.

Hope this helps

Matthieu






--
You received this message because you are subscribed to the Google Groups "tsdyn" group.
To post to this group, send email to ts...@googlegroups.com.
To unsubscribe from this group, send email to tsdyn+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/tsdyn?hl=en.


meenal.jav9

unread,
Jan 26, 2013, 11:30:32 AM1/26/13
to ts...@googlegroups.com, lau...@gmail.com
Dear all, 
So, Im guessing sergio's answer gives the Irf for small shocks? given they dont lead to a regime switch? Do you guys know how 
to obtain the GIRF, I would be really grateful if you guys could share any code or hints relating to that.  
Best regards, 
Meena

On Tuesday, 14 August 2012 19:59:43 UTC+5, Sergio René wrote:
Dear Laura, this answer might come a little bit late, nevertheless I thing that a  possible way to implement the IRF is by writing the companion matrix of the model, for that you would need either to retrieve the matrix of parameters from the TVAR, or you can enter it manually.  Consider the following code below:

#### Number of endogenous variables included in the VAR, entered manually so can change this number
ny<-2

#### Number of lags on the model, entered manually
p<-5


#### Entering the parameters matrix, this is done manually, at the moment I am not sure how to retrieve it automatically from the TVAR function
#### This is a random matrix which serves as an example, so you should enter the parameters obtained from the TVAR estimation

### First create the vector containing the parameters

LR_coef<-c(runif(ny*p, min=-0.95, max=0.85)) ##### these are the coefficients for the lower regime
UR_coef<-c(runif(ny*p, min=-0.95, max=0.55)) ##### these are the coefficients for the upper regime

LR_m<-matrix(LR_coef, nrow=ny, ncol=ny*p) ##### Now you have the matrix with the coefficients for the lower regime
UR_m<-matrix(UR_coef, nrow=ny, ncol=ny*p) ##### Now you have the matrix with the coefficients for the upper regime


#### Now you need to set up the companion matrix

diagonal<-diag(p*ny-ny)
zeros<-rep(0,(p*ny-ny)*ny)
zeros
step_1<-cbind(diagonal, matrix(zeros,nrow=p*ny-ny, ncol=ny))

LR<-rbind(LR_m,step_1) #### The companion matrix for the lower regime
UR<-rbind(UR_m,step_1) #### The companion matrix for the upper regime

#### Function for powering the matrix
matrix.power <- function(mat, n)
{
  # test if mat is a square matrix
  # treat n < 0 and n = 0 -- this is left as an exercise
  # trap non-integer n and return an error
  if (n == 1) return(mat)
  result <- diag(1, ncol(mat))
  while (n > 0) {
    if (n %% 2 != 0) {
      result <- result %*% mat
      n <- n - 1
    }
    mat <- mat %*% mat
    n <- n / 2
  }
  return(result)
}


#### Here starts the IRA

n<-35 ### This is the number of observations you want to have after the initial schock

LR_IRF<-numeric(length=n) ### This is the vector for storing your responses in the lower regime
UR_IRF<-numeric(length=n) ### This is the vector for storing your responses in the upper regime

d<-seq(1:n) ### The sequence parameter for the loop

#### Now the loop starts

for (i in seq(d))

{
### Obtain the exponential of the matrices
LR_n<-matrix.power(LR,i)
UR_n<-matrix.power(UR,i)
### [1,1] is for the effect of shock of the variable one on variable one
### if you are interested in the shock of variable two on one, then just replace [1,1] for [2,1]
### if you are interested in the shock of variable two on two, then write [2,2] and so on..

LR_IRF[i]=LR_n[1,1]
UR_IRF[i]=UR_n[1,1]
}
plot(ts(LR_IRF))
plot(ts(UR_IRF))
 

As far as I know, in the case of threshold models you have to report separately the short run effects for each regime, Just a remark, the function for powering the matrix is not from me, I found it in some example on the internet but cannot recall the author or the website where I found it,

Hope it helps!!!

Sergio René

Matthieu Stigler

unread,
Jan 28, 2013, 3:53:48 AM1/28/13
to ts...@googlegroups.com, meena...@gmail.com, Sergio René Araujo Enciso
Hi Meena

Indeed, there are no built-in functions in tsDyn for IRF nor GIRF. As you say correctly, the answer of Sergio is for shocks that do not lead to regime switching, which is by itself difficult to assess! (btw, Sergio, right now a little bit confused abt your code, should it not be based on the MA representation of the VAR? is this what you call the companion matrix?).

Now regarding GIRF, many people have asked for it indeed... it's long time I haven't looked at this paper, Meena, could you remind me of the procedure and steps to obtain these GIRF?

Thanks!

Mat


2013/1/26 meenal.jav9 <meena...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "tsdyn" group.
To post to this group, send email to ts...@googlegroups.com.
To unsubscribe from this group, send email to tsdyn+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

meenal.jav9

unread,
Feb 1, 2013, 12:42:31 PM2/1/13
to ts...@googlegroups.com, meena...@gmail.com, Sergio René Araujo Enciso
Hi!

Yes, of course. I think its the paper by Koop, Pesaran and Potter (1996) which states the procedure for 
obtaining the GIRF. They describe the process of computing the conditional expectations in the generalized
impulse function using montecarlo integration (pp. 135 of article). I have attached the article in this post. 

In their technique:

1. they pick a history and the shock; using observed values of time series for the history while randomly 
drawing the shocks. 

2. Then they use the model to obtain realizations that can be used inorder to form draws from 
joint distribution of Yt (where Yt is a random vector). 

3.Then they sample values of the disturbance terms for a given horizon. 

4. Using these sampled shocks they compute the realizations of Yt for the given horizon iterating on the non linear
model given the initial history. 

5. They repeat the steps 3-4  'n' times and then they use this compute the conditional expectations for (i). Yt conditional 
on vt (shock) and the history and (ii) Yt conditional on the history. 

6. The difference between the two conditional expectations will give the montecarlo estimate of the generalized
impulse. 

7. They repeat steps 1-6 a sufficient number of time for accurate estimation of the generalized impulse. 

Due to parameter uncertainty, it would be nice to have bootstrapped confidence intervals for the GIRFs.
The distribution of GI values for each horizon conditional on the same type of history and shock is built 
by simulating R samples of size T using estimated parameters and bootstraps for residuals. Then these 
samples are used to re-estimate the model and to re-calculate the GIs using the described algorithm 
(Galvao, 2003). 

I would be super grateful for any help. 

Regards, 
/Meena
Koop, Pesaran and Potter (1996).pdf
Galvao 2003.pdf

Matthieu Stigler

unread,
Feb 3, 2013, 4:08:13 PM2/3/13
to ts...@googlegroups.com, Meena Jav, Sergio René Araujo Enciso
Hi Meena

Thanks for the description of the algorithm, it is helpful indeed to write down each step separately. I must say I'm still a little bit confused reading your mail for the way one conditions only for history, and for history and shocks... But as long as you are not confused, that's fine!

So as far as I can see, I think some of the steps are trivial, such as sampling the shocks, which would be simply:
sample(residuals(yourModel))

that should make it for steps 1 and 3, no?
Now steps 2 and 4 can be obtained (quite simply) using the TVAR.sim function: this will allow you to simulate a TVAR given 1) a parameter matrix 2) starting values 3) innovations
So by using the for 1) coef(yourModel) 2) the values preceding your "history point" 3) the sampled innovations, you should be able to obtain the realizations you are looking for.

I suggest you try to fill the different steps, and post your preliminary answer and the questions that will come?

Best

Matthieu



2013/2/1 meenal.jav9 <meena...@gmail.com>
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.

Sergio René Araujo Enciso

unread,
Feb 10, 2013, 5:44:09 AM2/10/13
to Matthieu Stigler, ts...@googlegroups.com, Meena Jav
Dear Meena:

As Mathieu mentions the code I sent is assuming that the impulse do not lead to a change in the regime, nevertheless it can be useful to observe how the IRF behave in the different regimes, I do not recall what is usually reported on articles, the IRF for each regime or the GIRF, but can be helpful to revise it, at the end depends on what you want to analyze. 

Hi Matthieu, Hope everything is going well, sorry for the late reply, but recently I mode to Seville, hence I have been under a lot of pressure. The methodology I used is suggested in the book from Lütkepohl "New Introduction to Multiple Time Series Analysis", do not recall exactly the page, would like to check it, but my book is still in Germany, the reason why I used it was because it was more straight forward to program it in R. 

best, 

Sergio René

Alexander Haider

unread,
May 25, 2015, 12:15:02 AM5/25/15
to ts...@googlegroups.com, matthieu...@gmail.com, meena...@gmail.com

Hi,


I came across this thread while looking for GIRFs for the TVAR package. As far as I see they haven't been implemented yet. So I wrote my own functions which I wanted to share with you.

My approach is based on the appendix of Baum and Koester (2011, see attachment page 30) who build on Pesaran et al. (1996).

So far my two functions (GIRF() and the helper-function simTVAR() ) only work with an endogenous threshold variable. I want to extend it to make it work with an exogenous threshold variable as well. However, I have some problems with TVAR.sim() and an exogenous threshold variable (thus a working example of TVAR.sim() with an exogenous threshold variable would be appreciated).

The functions are in the attachment. The code is far from perfect and it takes some time running it (around 3min on my computer with the zerolyd dataset and default options). I mainly tested it with the “zerolyd” dataset (and a bigger VAR with 5 variables as well). So my code loads the tsDyn package, then there are the two functions and the example with the zerolyd dataset at the end. It should be straightforward.


The code runs without errors in both examples. However, I wanted to share it with you to get some feedback, because I'm not sure about two things:

1. the bootstrap residuals should correspond to their variables, right? For example in the zerolyd dataset, a bootstrap residual for long.rate should be a residual from long.rate and shouldn't be from short.rate?

2. For varcov in TVAR.sim(): I used the default value. As far as I can see the choice of varcov doesn't matter, because I bootstrap the residuals anyway. Varcov would make sense if I let TVAR.sim() choose the residuals according to some predefined variance-covariance matrix. Is this correct?


In the end I tested the code with two datasets and I hope that I got the idea of the papers right, but I would appreciate, if someone would give it a trial and play around a little bit and give me some feedback on it!


Best,

Alex

Baum_Koester.pdf
GIRF2.R

Bruno Spellanzon

unread,
Jul 12, 2016, 3:02:08 AM7/12/16
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Hey Alexander,

I am currently trying to use a TVAR model as well for Brazilian data.

I have tried to use your code but I think there might be a mistake.
From what I know shocks should eventually phase out, but I tried to used your code with the zeroyld data and, afterwards, with mine, but the opposite happens, that is, after 15 periods more or less, the fiscal policy shock into GDP makes the shock grows at faster rates...

I am not sure if my data is not rightly fixed, but the same goes when testing with the zeroyld.

I am really sorry I cannot help much because I do not understand about programming ;/ but I hope this could give you a light or something.

cheers

Bruno Spellanzon

unread,
Jul 20, 2016, 3:13:10 PM7/20/16
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Alexander and guys,

I tested again and it was my data that was weird... anyway, I guess your coding is fine Alexander!

about your questions

1) I'm not sure if I got it right, but you should bootstrap the residuals of the TVAR model, therefore, once you get the GIRFs for each state (lets call it "tight" and "normal") you should bootstrap the residuals of the "tight" and build the CI for the "tight".... afterwards, bootstrap the residuals of the "normal" and build the CI for it....

2) it makes sense, varcov should not matter in this case.

Now, I have a (probably stupid) question for you guys!!! 
I used your model and generated the GIRFs, but I need to see the values for each point in time to be able to construct my Fiscal Multipliers.

Is there a straightforward way? I was checking on the internet and saw some packages that "read" the graphic and using some reference points they can give you the points of the curve.

thanks!

Alexander Haider

unread,
Jul 20, 2016, 3:22:21 PM7/20/16
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Hi Bruno, 

sorry for the late response. Unfortunately the code posted here is not working correctly. I had to change a few things. See the link:


Anyways, it has been a while since I used the code (about one year now). I will take a look at it again at the end of next week and I will get back to you on how to get  the numeric values. Shouldn't be too difficult.

-Alex

Bruno Spellanzon

unread,
Jul 20, 2016, 3:35:09 PM7/20/16
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Thanks Alexander!

btw, I had already tested that other model, I just answered in the wrong thread hehehe!! 

but again thank you very much for your help!

Alexander Haider

unread,
Aug 16, 2016, 6:07:58 PM8/16/16
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Hey Bruno,

sorry for the late response, I forgot about it (must be the summer heat, I can't think straight ;-))
You can access the responses the following way (assuming that you saved the results of the GIRF function in the variable impulseResp):

impulseResp$girf[, , 1] will give you the values for the first regime 
impulseResp$girf[, , 2] will give you the values for the second regime

In general the structure is the following: 1dim (row): observation (e.g. first period), 2dim: variable (e.g. GDP or whatever variables you are using), 3dim: regime.
Example:

impulseResp$girf[1, 3, 2]: will give you the response in the first period of the third variable in the second regime

$girf will give you the mean of the simulations. You can also access the median of the simulations 

impulseResp$med[ , , ,]: same structure as $girf

Finally if you want to take a look at the structure of impulseResp:

str(impulseResp) will be useful

Cheers,
Alex

Bruno Spellanzon

unread,
Aug 23, 2016, 3:15:00 PM8/23/16
to ts...@googlegroups.com, matthieu...@gmail.com, meena...@gmail.com
Alexander,

sorry about the delayed answer!

Thank you very much for your help! I got all the data!

Btw, just a suggestion (I already changed it for me) with the graph lines.
If the people that are reading print the graphs in black and white, is practically impossible for them to know which line if red and which is blue.
In my case, I changed one of the lines to a dashed one! 

Anyway, thank you very much for ur help! 

cheers

bruno

--
You received this message because you are subscribed to a topic in the Google Groups "tsdyn" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tsdyn/XFF6lKOjqww/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tsdyn+unsubscribe@googlegroups.com.

To post to this group, send email to ts...@googlegroups.com.
Visit this group at https://groups.google.com/group/tsdyn.
For more options, visit https://groups.google.com/d/optout.



--
(11) 99656.4666
Skype: Bruno Spellanzon

Hayet ben haj hamida

unread,
Nov 4, 2016, 2:58:01 PM11/4/16
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Hello, 
I try to use the function GIRF2, but i have a problem 
an error message: Error in if (round(z2[i - thDelay], ndig) <= Thresh[1]) Yb[i, ] <- rowSums(cbind(BDown[,  : 
  missing value where TRUE/FALSE needed

please help me to resolve this problem

Best regards
To unsubscribe from this group and all its topics, send an email to tsdyn+un...@googlegroups.com.

To post to this group, send email to ts...@googlegroups.com.
Visit this group at https://groups.google.com/group/tsdyn.
For more options, visit https://groups.google.com/d/optout.

Hayet ben haj hamida

unread,
Nov 6, 2016, 9:18:44 PM11/6/16
to ts...@googlegroups.com
Hi
I can't resolve it, 
I have this message error: Error in solve.default(P) : 
  system is computationally singular: reciprocal condition number = 5.61938e-17
best regards

To unsubscribe from this group and all its topics, send an email to tsdyn+unsubscribe@googlegroups.com.

Alexander Haider

unread,
Nov 8, 2016, 2:50:29 PM11/8/16
to tsdyn
Hi,
The error message is new to me. I don't know on which line it happens. Can you attach a reproducible example? 

Hayet ben haj hamida

unread,
Nov 8, 2016, 3:52:11 PM11/8/16
to ts...@googlegroups.com
Hi; 
the message is following

Images intégrées 1

Thank you
Best regards

To unsubscribe from this group and all its topics, send an email to tsdyn+unsubscribe@googlegroups.com.

tecido...@gmail.com

unread,
Nov 22, 2016, 10:28:23 PM11/22/16
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Hi, 

GIRF2 do not work for TVAR(1)?

Only TVAR(>2) have worked for me...

Em segunda-feira, 25 de maio de 2015 01:15:02 UTC-3, Alexander Haider escreveu:
Message has been deleted

long...@gmail.com

unread,
Apr 7, 2017, 7:54:27 PM4/7/17
to tsdyn, matthieu...@gmail.com, meena...@gmail.com
Dear Alex,
Thank you for the function, it is a great help to me. However, I have a question: unlike linear IRF function  in "vars", the out put of your function, which shows the "responses", does not specify which are the "impulse" variables, i.e. the nature of the shocks? I may be missing something here.

Regards,

Long Vo

laura.b...@gmail.com

unread,
Aug 24, 2017, 5:24:06 PM8/24/17
to tsdyn, lau...@gmail.com
Does anyone have an example of GIRF for a STAR model ?

Hayet ben haj hamida

unread,
Aug 25, 2017, 4:31:35 AM8/25/17
to ts...@googlegroups.com
hi, dear Laura
Me too i  need the impulse function for the STAR model, but I have not found it yet
on the other hand, i suggest that it's a bad idea to use the impulse linear function for each regimes

Best regards
Hayet

--
You received this message because you are subscribed to a topic in the Google Groups "tsdyn" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tsdyn/XFF6lKOjqww/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tsdyn+unsubscribe@googlegroups.com.
To post to this group, send email to ts...@googlegroups.com.
Visit this group at https://groups.google.com/group/tsdyn.
For more options, visit https://groups.google.com/d/optout.

sultan...@gmail.com

unread,
Apr 9, 2018, 1:31:36 AM4/9/18
to tsdyn
Hi all,

I am wondering whether GIRF can be applied to DCC (GARCH/EGARCH) model in which shocks (positive or negative) affect conditional covariances. Is this possible with this code (since it is for TVAR) or any available packages in R?

Thanks in advance. 

Matthieu S

unread,
Apr 9, 2018, 1:32:52 AM4/9/18
to ts...@googlegroups.com
Hi

The function GIRF is specific to the TVAR model indeed. I am not awatre of GIRF for DCC-GARCH, so you would need to write your own function. 

Best,
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages