*
*  Example IMPULSES.PRG
*  Manual Example 10.3
*
*  This program computes and graphs impulse response functions for
*  a VAR model.  The only lines below the ******* line in the middle
*  which need to be modified are those marked with  ;*<<<<<<<
*
cal 1981 1 4
allocate 2003:2
open data candata.rat
data(format=rats) / canrgdps canm1s cancd90d cancpinf canusxsr usargdps
*
log canrgdps
log canm1s
log cancpinf
log canusxsr
log usargdps
*
*************************************************************************
*
COMPUTE  NEQN   = 6                                               ;*<<<<<<
COMPUTE  NLAGS  = 5                                               ;*<<<<<<
COMPUTE  NSTEPS = 24                                              ;*<<<<<<
*
system(model=canmodel)
VARIABLES USARGDPS CANUSXSR CANCD90D CANM1S CANRGDPS CANCPINF     ;*<<<<<<<
lags 1 to nlags
det constant
end(system)
*
declare rect[series] impblk(neqn,neqn)
declare vect[series] scaled(neqn)
declare vect[strings] implabel(neqn)

*
*  To help create publication-quality graphs, this sets longer labels for
*  the series. These are used both in graph headers and key labels.
*

COMPUTE IMPLABEL=|| $                                        ;* <<<<<<
  'US Real GDP',$
  'Exchange Rate',$
  'Short Rate',$
  'M1',$
  'Canada Real GDP',$
  'CPI'||

estimate(noprint)
*
*   These apply to the GRAPH instructions which are coming up later
*
list ieqn = 1 to neqn
smpl 1 nsteps
*
*   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=canmodel,result=impblk,noprint) * nsteps * %sigma
*
*   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 '+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,$
     window='to_'+implabel(i)) neqn
  cards scaled(ieqn)
end do i
*
*   And this loop graphs the responses of a variable to all shocks.
*   These don’t have to be normalized.
*
do i=1,neqn
  compute header='Plot of responses of '+implabel(i)
  graph(header=header,key=below,klabels=implabel,number=0,window='of_'+implabel(i)) neqn
  cards impblk(i,ieqn)
end do i

