Page 1 of 1

VARIRF - Impulse responses from VAR

PostPosted: Wed Jun 29, 2011 12:21 pm
by TomDoan
This procedure organizes the graphs of an impulse response fuction from an already estimated VAR. Note that this just computes the IRF at the estimated coefficients; for error bands, you need to use MONTEVAR or the combination of MCVARDODRAWS and MCGRAPHIRF.

varirf.src
Procedure file
(7.01 KiB) Downloaded 289 times


@VARIRF(model=VAR model,other options)

Options

MODEL=VAR model whose IRF's are to be calculated
FACTOR=Factor of the covariance matrix [Choleski of %SIGMA]
STEPS=number of forecast steps[48]

SHOCKLABELS=VECT[STRINGS] of labels for the shocks [dependent variables]
VARLABELS=VECT[STRING] of labels for the variables [dependent variables]

HEADER=' title for graph '
FOOTER=' footer for graph '
PAGE=[ALL]/ONE/BYSHOCK/BYVARIABLE
COLUMNS=# of columns on a page [depends upon number of graphs]

The PAGE option selects the layout of a single page of graphs.

  • PAGE=ALL (the default) does all responses to all shocks on a single page (shocks in columns, dependent variables in rows). Note that the panes get a bit small when you get more than four variables.
  • PAGE=ONE does one combination of shock and variable per page.
  • PAGE=BYSHOCK does a separate page for each shock, with all responses arranged in one or more columns.
  • PAGE=BYVARIABLE does a separate page for each variable, with all shocks arranged in one or more columns.

ERRORS/[NOERRORS] Produce a forecast error variance decomposition
ACCUMULATE=||vector of positions||. Indicates which variables need to be accumulated (integrated)

Examples
This is from the standard RATS example IMPULSES.RPF. It overrides the variable labels with more meaningful names, and does the graphs two ways: the first is organized by shocks, which will give six graph pages, each with six graphs (in a 3 x 2 layout) with each page having the responses of the six variables to a different shock. The second set is organized by the target variable; again six pages with six graphs, this time each page having the responses of a different variable to all six shocks. PAGE=ALL would create one graph page with a 6 x 6 set of graphs, while PAGE=ONE would give 36 separate graph pages.

Code: Select all
compute [vect[strings]] implabel=|| $
  "US Real GDP",$
  "Exchange Rate",$
  "Short Rate",$
  "M1",$
  "Canada Real GDP",$
  "CPI"||
@VARIRF(model=canmodel,steps=nsteps,$
  varlabels=implabel,page=byshocks)
@VARIRF(model=canmodel,steps=nsteps,$
  varlabels=implabel,page=byvariables)



This example is from a structural VAR, and overrides both the shock labels and the variable labels. Variables 2, 3 and 4 are in the VAR in differenced form, and their cumulated responses are generated.

Code: Select all
dec vect[strings] shocklabels varlabels
compute shocklabels=||"Output","Inflation","Commodity Price","Stock Price","Monetary Policy"||
compute varlabels=||"Output Gap","Annual CPI Inflation","Commodity Price","Stock Prices","Federal Funds Rate"||
@varirf(model=varstockp,steps=nsteps,factor=f,page=byshock,$
  shocks=shocklabels,varlabels=varlabels,accumulate=||2,3,4||)

Re: VARIRF - Impulse responses from VAR

PostPosted: Mon Aug 01, 2011 8:49 pm
by WALLE
Hello,

I am trying to estimate a 3 variable SVAR of the effects of oil price shocks on macroeconomic fluctuations as in "BJØRNLAND, H. C. (2000). The dynamic effects of aggregate demand, supply and oil price shocks - a comparative study. The Manchester School of Economic Studies, 68, pp. 578–607".

Identification is achieved by symmetry in the co-variance matrix which gives off six restrictions, one comes from assuming as in Blanchard and Quah that agg. demand has no LR effect on output however, real oil price shocks are allowed to affect output in the long run. The other two are zero short-run restrictions on oil prices which forces the contemporaneous effects of demand and supply shocks on real oil prices to be zero, allowing only oil price shocks will contemporaneously affect oil prices. However, after a period (one quarter), both demand and supply shocks are free to influence oil prices.

Following the RATS manual I came up with the following after the preliminary tests and all

Code: Select all
compute neqn = 3
compute nlags = 2
compute nsteps = 24
******************************************************************************************************
* ONE: SET UP VAR
*******************************************************************************************************
system(model=cansvar)
variables can_ur d_l_oilp_can d_l_can_gdp
lags 1 to nlags
det constant
end(system)

***********************************************************************************************
* TWO:ESTIMATE VAR
***********************************************************************************************
estimate(print,resids=varresidscan)
compute masums=inv(%varlagsums)
dec rect lr(3,3)
dec rect sr(3,3)
input sr
. . .
0 . 0
. . .
input lr
0 . .
. . .
. . .
@ShortAndLong(sr=sr,lr=lr,masum=masums) %sigma f
disp ###.### "Impact Responses" f
disp ###.### "Long run Responses" masums*f
@StructResids(factor=f) varresidscan 1970:01 2010:04 castrshocs

*********************************************************************************************************
*THREE: TO GENERATE IMPULSE RESPONSE FUNCTIONS
*********************************************************************************************************
dec vect[strings] shocklabels varlabels
compute shocklabels=||"Aggregate demand","Oil","Aggregate Supply"||
compute varlabels=||"Output","Oil Prices","Unemployment rate"||

dec vect[strings] shocklabels varlabels
@VARIRF(model=cansvar,steps=nsteps,factor=f,page=byshocks,$
  shocks=shocklabels,varlabels=varlabels)
@VARIRF(model=cansvar,steps=nsteps,factor=f,$
  shocks=shocklabels,varlabels=varlabels,page=byvariables)

**********************************************************************
*FOUR: IRF MATRICES
**********************************************************************
list ieqn = 1 to neqn
smpl 1 nsteps

declare rect[series] impblk(neqn,neqn)
declare vect[series] scaled(neqn)
declare vect[strings] implabel(neqn)
impulse(model=cansvar,result=impblk,noprint,$
  steps=nsteps,factor=f)

****************************************************************************************************************
*FIVE: GRAPHING RESPONSE OF ALL VARIABLES TO A SINGLE VARIABLE
****************************************************************************************************************
do i=1,neqn
  compute header="Plot of responses to "+implabel(i)
  do j=1,neqn
     set scaled(j) = (impblk(j,i))/sqrt(%sigma(j,j))
  end do j
  graph(header=header,key=below,klabels=implabel,number=0) neqn
  cards scaled(ieqn)
end do i

*******************************************************************************************************************
*SIX: GRAPHING RESPONSE OF A VARIABLE TO ALL SHOCKS
*******************************************************************************************************************
do i=1,neqn
  compute header="Plot of responses of "+implabel(i)
  graph(header=header,key=below,klabels=implabel,number=0) neqn
  cards impblk(i,ieqn)
end do i

*******************************************************************************************
*SEVEN: VARIANCE DECOMPOSITION
*******************************************************************************************
errors(factor=f,model=cansvar,steps=nsteps,window="CModel",$
   labels=||"Aggregate Demand","Oil","Aggregate Supply"||)


I have run the code however, I would like to ask the following questions:
1) Are my restrictions for identification as stated above correct with regards to my allocations of zeros in the LR and SR matrices?
2) My graphs are nameless when I try to carry out the procedure 5 & 6, is my procedure wrong and how do I rectify that?
3) In the structural example above, I see an accumulate = ||1,2,3|| what function does this perform?


Many thanks,

Re: VARIRF - Impulse responses from VAR

PostPosted: Tue Aug 02, 2011 11:26 am
by moderator
"1) Are my restrictions for identification as stated above correct with regards to my allocations of zeros in the LR and SR matrices?"

It looks as if you have your dependent variables in the wrong order in the VAR setup. Everything else seems to match with a variables instruction which reads:

variables d_l_can_gdp d_l_oilp_can can_ur

"2) My graphs are nameless when I try to carry out the procedure 5 & 6, is my procedure wrong and how do I rectify that?"

You've declared the variable IMPLABEL, but you haven't stored any values in it. Use a COMPUTE instruction of the form:

compute implabel = ||"first", "second","third"||

to assign the labels you want to use. If you just want to use the existing variable names, you can use COMPUTE or EWISE along with functions to extract those names from the series themselves or from the model. For example:

ewise implabel(i)=%modellabel(model,i)

"3) In the structural example above, I see an accumulate = ||1,2,3|| what function does this perform?"

I believe that's explained in the procedure comments shown above--it allows you to have the procedure accumulate (compute a running sum) of the selected variables. In your case, you would probably want accum=||1,2|| since those variables enter the VAR in differenced form.

Regards,
Tom Maycock
Estima

Re: VARIRF - Impulse responses from VAR

PostPosted: Tue Aug 02, 2011 10:22 pm
by WALLE
Thanks Tom,

I put in the code as advised below in step 3 and I obtained 6 graphs however when I tried to run steps 4,5 & 6, i get the following responses with no graphs:

##SX1. identifier IMPLABEL is already in use as a(n) RECTANGULAR [STRING]
subscripting error. Had array reference with(). Needed(INTEGER,INTEGER)
##SX27. Illegal combination of data types for operation
>>>>es of "+implabel(i)<<<<

How do I resolve this?

Many Thanks

Code: Select all
*********************************************************************************************************
*THREE: TO GENERATE IMPULSE RESPONSE FUNCTIONS
*********************************************************************************************************
dec vect[strings] shocklabels varlabels
compute shocklabels=||"Aggregate demand","Oil","Aggregate Supply"||
compute varlabels=||"Output","Oil Prices","Unemployment rate"||
[b]compute implabel=||"Aggregate demand","Oil","Aggregate Supply"||[/b]

dec vect[strings] shocklabels varlabels
@VARIRF(model=cansvar,steps=nsteps,factor=f,page=byshocks,$
  shocks=shocklabels,varlabels=varlabels)
@VARIRF(model=cansvar,steps=nsteps,factor=f,$
  shocks=shocklabels,varlabels=varlabels,page=byvariables)

**********************************************************************
*FOUR: IRF MATRICES
**********************************************************************
list ieqn = 1 to neqn
smpl 1 nsteps

declare rect[series] impblk(neqn,neqn)
declare vect[series] scaled(neqn)
declare vect[strings] implabel(neqn)
impulse(model=cansvar,result=impblk,noprint,$
  steps=nsteps,factor=f)
****************************************************************************************************************
*FIVE: GRAPHING RESPONSE OF ALL VARIABLES TO A SINGLE VARIABLE
****************************************************************************************************************
do i=1,neqn
  compute header="Plot of responses to "+implabel(i)
  do j=1,neqn
     set scaled(j) = (impblk(j,i))/sqrt(%sigma(j,j))
  end do j
  graph(header=header,key=below,klabels=implabel,number=0) neqn
  cards scaled(ieqn)
end do i
*******************************************************************************************************************
*SIX: GRAPHING RESPONSE OF A VARIABLE TO ALL SHOCKS
*******************************************************************************************************************
do i=1,neqn
  compute header="Plot of responses of "+implabel(i)
  graph(header=header,key=below,klabels=implabel,number=0) neqn
  cards impblk(i,ieqn)
end do i

Re: VARIRF - Impulse responses from VAR

PostPosted: Wed Aug 03, 2011 9:05 am
by moderator
In this first section, you aren't declaring the type of IMPLABEL:

dec vect[strings] shocklabels varlabels
compute shocklabels=||"Aggregate demand","Oil","Aggregate Supply"||
compute varlabels=||"Output","Oil Prices","Unemployment rate"||
compute implabel=||"Aggregate demand","Oil","Aggregate Supply"||

So, the COMPUTE defaults to defining it as the more general RECTANGULAR of LABELS, rather than a VECTOR of LABELS. Then, later on, you are trying to (re) define it as a VECTOR:

declare vect[strings] implabel(neqn)

Hence the error message.

Unless you had something else in mind, you should just add IMPLABEL to that first DECLARE instruction:

dec vect[strings] shocklabels varlabels implabel

You can then omit the later DECLARE.

Regards,
Tom Maycock
Estima

Re: VARIRF - Impulse responses from VAR

PostPosted: Sat Aug 13, 2011 4:40 pm
by WALLE
Thanks Tom. :D

Re: VARIRF - Impulse responses from VAR

PostPosted: Sun Jul 22, 2012 4:18 pm
by Snow
Dear Tom,
I'm trying to use this procedure to draw a 3 variables graph and it works very well. However, those lines are not distinct enough when I print out with a black-white printer. Is there any ways to make it look better? I've tried to difine those lines with "open XXX.txt and grparm (import=styles)" ,but this procedures did follow my command.Here is the code:
Code: Select all
*
* This organizes the graphs of an impulse response function from an already
* estimated VAR.
*
* @VARIRF(model=VAR model,other options)
*
* Options:
*    MODEL=VAR model whose IRF's are to be calculated
*    DECOMP=Factor of the covariance matrix [Choleski of %SIGMA]
*    STEPS=number of forecast steps[48]
*    BYSHOCK/[NOBYSHOCK] Produce a separate graph for each shock with responses to all
*    BYVARIABLE/[NOBYVARIABLE] Produce a separate graph for each variable showing its responses
*     to all shocks
*    ERRORS/[NOERRORS] Produce a forecast error variance decomposition
*    VLABELS=VECTOR[STRINGS] of labels for variables [labels of dependent variables]
*    LABELS=VECTOR[STRINGS] of labels for shocks [same as VLABELS]
*
* Revision Schedule:
*  07/05 Written by Tom Doan, Estima.
*
procedure VARIRF
*
option rect    decomp
option model   model
option integer steps      48
option switch  byshock    0
option switch  byvariable 0
option switch  scale      1
option switch  errors     0
option vect[strings] labels
option vect[strings] vlabels
*
local rect[series] impblk
local vect[series] scaled
local vect[strings] resplabels
local vect[strings] shocklabels
local integer ieqn neqn
local integer i j
local string header
local rect   factor
*
if .not.%defined(model) {
   display 'Syntax: @VARIRF(model=VAR model,lags=# of lags,...)'
   return
}
compute neqn=%modelsize(model)
dim impblk(neqn,neqn) scaled(neqn) shocklabels(neqn) resplabels(neqn)
*
* Use the vlabels if provided, else use the labels of the dependent
* variables
*
if %defined(vlabels)
   compute resplabels=vlabels
else
   ewise resplabels(i)=%l(%modeldepvars(model)(i))
*
* Use the labels if provided, otherwise label the shocks the same as
* the responses
*
if %defined(labels)
   compute shocklabels=labels
else
   compute shocklabels=resplabels
*
* Use the DECOMP provided, otherwise take the standard factorization
* of the %SIGMA matrix
*
if %defined(decomp)
   compute factor=decomp
else
   compute factor=%decomp(%sigma)
*
list ieqn = 1 to neqn
*
*   This computes the full set of impulse responses, which are in the series in
*   IMPBLK. IMPBLK(i,j) is the response of variable i to a shock in j.
*
impulse(model=model,result=impblk,noprint,decomp=factor,steps=steps)
*
*   This loop graphs the responses of a variable to all shocks.
*   These don抰 have to be normalized.
*
if byvariable
   do i=1,neqn
      compute header='Plot of responses of '+resplabels(i)
      graph(header=header,key=below,klabels=shocklabels,number=0,window='of_'+resplabels(i)) neqn
      cards impblk(i,ieqn) 1 steps
   end do i

if byshock
*
*   This loop plots the responses of all series to a single series. The response
*   of a series is normalized by dividing by its innovation variance. This
*   allows all the responses to a shock to be plotted on a single scale. Note that
*   these graphs get a bit hard to read with more than five or six variables.
*
*   As this program will generate a dozen graphs in a bunch, the WINDOW option
*   is used on the GRAPH instructions to give them descriptive labels in
*   the WINDOW menu.
*
   do i=1,neqn
      compute header='Plot of responses to '+shocklabels(i)
      do j=1,neqn
         set scaled(j) 1 steps = (impblk(j,i))/sqrt(%sigma(j,j))
      end do j
     open styles line.txt
     grparm (import=styles) 3
 graph(header=header,key=below,klabels=resplabels,number=0,$
         window='to_'+shocklabels(i)) neqn
      cards scaled(ieqn) 1 steps
    end do i
end VARIRF



You can see how i changed it and I also trid "patterns", it still hard to distinguish those 3 lines

Re: VARIRF - Impulse responses from VAR

PostPosted: Sun Jul 22, 2012 6:00 pm
by TomDoan
Don't change the procedure. Use it as is, but add the GRPARM to re-define the styles first. You have a unnecessary space in your GRPARM instruction before the options, so that won't work right. It should be set up as shown:

open styles line.txt
grparm(import=styles)

That does a global redefinition of the styles, so any graphs generated later will use those styles.

@varirf.....

Re: VARIRF - Impulse responses from VAR

PostPosted: Sun Jul 22, 2012 6:08 pm
by Snow
How to redefine it? In my rats or in this procedures? you mean difine it at the begining of my whole program? if the delete the space, there was always an error saying "This Instrucion does not have an option IMP". Maybe my version is too old.

Re: VARIRF - Impulse responses from VAR

PostPosted: Sun Jul 22, 2012 9:25 pm
by TomDoan
If you're getting that message, then you must have a typo in the instruction. At any rate, read section 3.16 in the RATS (v8) Introduction book and experiment with different line styles.

Re: VARIRF - Impulse responses from VAR

PostPosted: Sun Jul 22, 2012 11:40 pm
by Snow
I'm using Rats 6.2 right now, and I looked through my reference manual foud nothing about "import" instrudiction :shock:
Any other ways to draw a distinct impulse responses except GRPARM?

Re: VARIRF - Impulse responses from VAR

PostPosted: Tue Jul 24, 2012 9:25 am
by moderator
Version 6.2 is about 6 years old, and pre-dates the "style sheet" features, including the IMPORT option on GRPARM, so I'm not sure where you got the idea to include that in the first place?

In any case, one obvious solution is to upgrade to the current version. Otherwise, you would need to take advantages of the features available to you in 6.2 for selecting styles. That probably means using the "symbol choice" parameter on the supplementary cards of the GRAPH instruction to choose from the available patterns. See Chapter 3 in the RATS 6 User's Guide and the GRAPH section of the Reference Manual for details.

Regards,
Tom Maycock
Estima