Help with accumulate

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Help with accumulate

Unread post by Ozmando »

I am trying to compute the cumulative responses in a VAR set up. I have added at the bottom of the code a command:
accumulate impulses(i,j) 1 nsteps accumimp(i,j)
end do i
end do j


But it seems that I am not doing the right thing. Any help suggestions please?
This is the actual code:
Thanks

Code: Select all

*************  ESTIMATION OF PVAR FOR 18 COUNTRIES OVER 1970-2008 PERIOD ***********
environment  noecho

calendar(panelobs=39) 1970

allocate 18//2008:1

open data D:\temp\Rats\PVAR\crisis.xls

data(format=xls, org=columns)

*
*
COMPUTE LAGS=2            ;*Set number of logs
COMPUTE NVAR=5
COMPUTE NSTEP=11
COMPUTE NDRAWS=2000
COMPUTE N = 18            ;*Number of countries
compute YYY = 37          ;*Number of observations for each country
compute f1=0.05
compute f2=0.95
compute gg=1            ;*Position of Government Spending G
compute tt=2            ;*Position of Government Net Taxes NT
compute yy=3            ;*Position of Output Y
compute ratiogy=0.241   ;*Average Ratio G/Y
compute rationty=0.217   ;*Average Ratio NT/Y
*
*
dec vect[strings] ylabel(nvar+2)
compute ylabel(gg)= 'Exp'
compute ylabel(tt)= 'Inv'
compute ylabel(yy)= 'Crisis'
compute ylabel(4)= 'Tax '
compute ylabel(5)= 'Debt'
compute ylabel(6)= 'Budget'
*

*  this created country specific dummies and linear trend
*
SET LTREND = %PERIOD(T)
dec vector[series] dummies(N) time(YYY) trend(N) qtrend(N)
do i=1,N
   set dummies(i) = %indiv(t)==i
   set trend(i)    = (%indiv(t)==i)*ltrend
   set qtrend(i)    = (%indiv(t)==i)*ltrend*ltrend
end do i
*
* time dummy
*
do i=1,YYY
set time(i)    = (%period(t)==i)
end do i
*print / time
*
*   ggflq{1}ggflq{2}
*
*********************************************************************************
* !!!!!!!!!   change specification of the VAR !!!!!!!!!!!!!!!!!!!!!
**************************************************************************
system(model=varmodel)
variables g ntc y irl reer
lags 1 to lags
kfset xxx
det    dummies time
end(system)
estimate(noprint,outsigma=vmat,RESIDUALS=RESVAR)
*set gshock = resvar(1)
*
* print / resvar
*  The nonlinear parameters are put into a vector. This is not the
*  most convenient way to code this for estimation (as it's harder
*  to adjust the model), but it considerably cleans up the process
*  of making draws.
*
*  nfree is the number of free coefficients
*
**************************************************************************
* !!!!!!!!!   change short-run identifying restrictions !!!!!!!!!!!!!!!!!
**************************************************************************
*
compute nfree=10
dec vect ax(nfree)
nonlin ax
*
dec frml[rect] afrml
frml afrml =    ||   1.0       ,0.0    ,0.0    ,0.0     ,0.0     |$
                   -ax(1)     ,1.0    ,0.0     ,0.0      ,0.0     |$
                   -ax(10)   ,-ax(3) ,1.0    ,0.0      ,0.0     |$
                   -ax(4)    ,-ax(5) ,-ax(6) ,1.0      ,0.0     |$
                  -ax(7)    ,-ax(8) ,-ax(9) ,-ax(2)  ,1.0     ||
*
*
compute ax=%const(0.01)
*cvmodel(iters=20,method=simplex) vmat afrml
cvmodel(iters=300,method=bfgs,noprint) vmat afrml
*
declare rect sxx swish betaols betadraw
declare symm sigmad
*
declare rect sxx svtr swish betaols betadraw
declare symm sigmad
compute sxx    =%decomp(xxx)
compute svtr   =tr(%decomp(vmat))
compute betaols=%modelgetcoeffs(varmodel)
compute ncoef  =%rows(sxx)
compute wishdof=%nobs-ncoef
*
declare rect ranc(ncoef,nvar)
*
declare vect[rect] responses(ndraws)
declare rect[series] impulses(nvar,nvar)
*
infobox(action=define,progress,lower=1,upper=ndraws) 'Monte Carlo Integration'
do draws = 1,ndraws
   if %clock(draws,2)==1 {
      compute sigmad  =%mqform(%nobs*inv(%ranwishart(nvar,wishdof)),svtr)
      compute swish   =%decomp(sigmad)
      compute ranc    =%ran(1.0)
      compute betau   =sxx*ranc*tr(swish)
      compute betadraw=betaols+betau
   }
   else
      compute betadraw=betaols-betau
   compute %modelsetcoeffs(varmodel,betadraw)
   impulse(noprint,model=varmodel,decomp=swish,results=impulses) nvar nstep
*
*
   dim responses(draws)(nvar*nvar,nstep)
   ewise responses(draws)(i,j)=impulses((i-1)/nvar+1,%clock(i,nvar))(j)
   infobox(current=draws)
end do draws
*
infobox(action=remove)
*
**********************************************************************
* GRAPH
**********************************************************************
grparm(italics)  axislabelling 25
grparm(nobold,italics) matrixlabelling 30
grparm  header 30
*
spgraph(xpos=upper,vfields=3,hfields=2)
*
dec vect[series] upper(nvar) lower(nvar) resp(nvar)
*
*
smpl 1 ndraws
clear resp(gg)
do k=1,nstep
set work   1 ndraws = responses(t)((gg-1)*nvar+gg,k)
compute frac=%fractiles(work,||f1,f2||)
compute resp(gg)(k)=%avg(work)
end do k
compute ggg=resp(gg)(1)
*
do i=1,nvar
   compute minlower=maxupper=0.0
   smpl 1 ndraws
   clear lower(gg) upper(gg) resp(gg)
      do k=1,nstep
         set work   1 ndraws = responses(t)((i-1)*nvar+gg,k)
*print(nodates) / work
    compute frac=%fractiles(work,||f1,f2||)
         compute lower(gg)(k)=frac(1)/(ggg*ratiogy)
         compute upper(gg)(k)=frac(2)/(ggg*ratiogy)
         compute resp(gg)(k)=%avg(work)/(ggg*ratiogy)
end do k
compute maxupper=%max(maxupper,%maxvalue(upper(gg)))
compute minlower=%min(minlower,%minvalue(lower(gg)))
   smpl 1 nstep
display ylabel(i)
print / resp(gg) upper(gg) lower(gg)
   graph(ticks,vticks=5,patterns,min=minlower,max=maxupper,number=0,samesize,picture='*.#',hlabel='years after shock',header=''+ylabel(i)) 3
      # resp(gg) / 1
      # upper(gg) / 6
      # lower(gg) / 6
end do i
*
do i=6,6
   compute minlower=maxupper=0.0
   smpl 1 ndraws
   clear lower(gg) upper(gg) resp(gg)
      do k=1,nstep
         set work   1 ndraws = -ratiogy*responses(t)((gg-1)*nvar+gg,k)+rationty*(responses(t)((tt-1)*nvar+gg,k)+2.1*responses(t)((yy-1)*nvar+gg,k))-(ratiogy-rationty)*responses(t)((yy-1)*nvar+gg,k)
          compute frac=%fractiles(work,||f1,f2||)
         compute lower(gg)(k)=frac(1)/(ggg*ratiogy)
         compute upper(gg)(k)=frac(2)/(ggg*ratiogy)
         compute resp(gg)(k)=%avg(work)/(ggg*ratiogy)
      end do k
   compute maxupper=%max(maxupper,%maxvalue(upper(gg)))
   compute minlower=%min(minlower,%minvalue(lower(gg)))
   smpl 1 nstep
display ylabel(i)
print / resp(gg) upper(gg) lower(gg)
   graph(ticks,vticks=5,patterns,min=minlower,max=maxupper,number=0,samesize,picture='*.#',hlabel='years after shock',header=''+ylabel(i)) 3
      # resp(gg) / 1
      # upper(gg) / 6
      # lower(gg) / 6
end do i
*
*
spgraph(done)

**********************************************************************
* ACCUMULATE RESPONSE
**********************************************************************
accumulate impulses(i,j) 1 nsteps accumimp(i,j)
end do i
end do j
clear all
*
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Help with accumulate

Unread post by TomDoan »

I'm not sure what responses you're trying to accumulate, but you're missing the opening DO instructions for both the I and J loops.
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Re: Help with accumulate

Unread post by Ozmando »

TomDoan wrote:I'm not sure what responses you're trying to accumulate, but you're missing the opening DO instructions for both the I and J loops.
I am only interested on the the response of the first variable in the VAR and was trying to accumulate the response of the first variable (g) to shocks on the third (y)
Have added do i=6,6 but still not obtain what I wanted.
thanks
oz
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Help with accumulate

Unread post by TomDoan »

The response of the 1st variable to shocks in the 3rd is in impulses(1,3). You don't want/need a loop---just accumulate that one series.
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Re: Help with accumulate

Unread post by Ozmando »

TomDoan wrote:The response of the 1st variable to shocks in the 3rd is in impulses(1,3). You don't want/need a loop---just accumulate that one series.
Thanks.
Following your suggestion I have added the following line: accumulate impulses(1,3) 1 nstep accumimp(1,3)
But still seems that I am not doing something right? as there is no output.
Thanks,
oz
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Help with accumulate

Unread post by TomDoan »

That's right. ACCUMULATE, like other transformation instructions, does the calculation only. What you do with it is up to you.
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Re: Help with accumulate

Unread post by Ozmando »

TomDoan wrote:That's right. ACCUMULATE, like other transformation instructions, does the calculation only. What you do with it is up to you.
I have made further changes to the program as follows:

accumulate impulses(1,3) 1 nstep accumimp(1,3)
print / resp(gg) upper(gg) lower(gg)
display ylabel(i)
graph(ticks,vticks=5,min=minlower,max=maxupper,number=1,samesize,picture='*.#',hlabel='years after shock',header=''+ylabel(i)) 3

clear all

However, getting an error message:

## SX22. Expected Type SERIES[REAL], Got VECTOR[RECTANGULAR[REAL]] Instead
>>>>CUMULATE RESPONSES<<<<


Any guidance on what I am doing wrong? please?
oz
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Help with accumulate

Unread post by TomDoan »

That looks like you forgot a comment * on what looks like a comment line. Use Edit-Show Last Error to take you to the line that's the problem and fix it.
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Re: Help with accumulate

Unread post by Ozmando »

TomDoan wrote:That looks like you forgot a comment * on what looks like a comment line. Use Edit-Show Last Error to take you to the line that's the problem and fix it.
Thanks.
I have made further changes to the code as:

accumulate impulses(1,3) 1 nstep accumimp(1,3)
print / resp(gg) upper(gg) lower(gg)
display ylabel(i)
graph(ticks,vticks=5,min=minlower,max=maxupper,number=1,samesize,picture='*.#',hlabel='years after shock',header=''+ylabel(i))
# resp(gg) / 1


However, now getting the error code:

# SX11. Identifier ACCUMIMP is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>> 1 nstep accumimp(<<<<


edit-show last error suggests that the error is in the line: accumulate impulses(1,3) 1 nstep accumimp(1,3)

However, having checked the RATS Reference Manual this seems correct?
Thanks.
oz
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Help with accumulate

Unread post by TomDoan »

You're trying to copy a whole bunch of instructions which don't apply to what you're doing. What are you trying to do? You wanted to accumulate just one set of responses to one shock. And then what? You're pulling code out of an example which is designed to do a whole page full of graphs (with extra calculations designed to keep the scale uniform). If you're just doing one graph, you don't need any of that. You don't need an ACCUMIMP set of series (which hasn't been declared---that's the reason you're getting that message), because you just need one.
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Re: Help with accumulate

Unread post by Ozmando »

TomDoan wrote:You're trying to copy a whole bunch of instructions which don't apply to what you're doing. What are you trying to do? You wanted to accumulate just one set of responses to one shock. And then what? You're pulling code out of an example which is designed to do a whole page full of graphs (with extra calculations designed to keep the scale uniform). If you're just doing one graph, you don't need any of that. You don't need an ACCUMIMP set of series (which hasn't been declared---that's the reason you're getting that message), because you just need one.
All work now fine.
Thanks for your help and patience.
oz
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Re: Help with accumulate

Unread post by Ozmando »

Ozmando wrote:
TomDoan wrote:You're trying to copy a whole bunch of instructions which don't apply to what you're doing. What are you trying to do? You wanted to accumulate just one set of responses to one shock. And then what? You're pulling code out of an example which is designed to do a whole page full of graphs (with extra calculations designed to keep the scale uniform). If you're just doing one graph, you don't need any of that. You don't need an ACCUMIMP set of series (which hasn't been declared---that's the reason you're getting that message), because you just need one.
All work now fine.
Thanks for your help and patience.
oz
The code works; however it seems that accumulate is picking up something else and strangely the values (accumulated) do not change.
on the top I have the responses and the output i get from accumulating at the bottom.
Ran the code step by step by can't seem to figure out the issue though. Any guidance please?
oz


Response

response upper lower
3.225806452 3.300521393 3.146906591
2.679848609 2.803751045 2.557515688
2.591803658 2.703336387 2.483107696


Accumulated

response upper lower
0.052847284 0.19755747 -0.089959504
0.052847284 0.19755747 -0.089959504
0.052847284 0.19755747 -0.089959504
0.052847284 0.19755747 -0.089959504
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Help with accumulate

Unread post by TomDoan »

First, note that you're basing this on old programming. @MCPROCESSIRF and ]@MCGRAPHIRF both have ACCUM options that let you do the cumulated responses at the end and 80% of your program is basically just doing what @MCGRAPHIRF is designed to do. If you just save your impulse responses into a properly designed %%RESPONSES (rather than your RESPONSES) you can use those procedures.

As an alternative, you can also just insert the ACCUMULATE right after you generate the responses:

impulse(noprint,model=varmodel,decomp=swish,results=impulses) nvar nstep
accumulate impulses(1,3) 1 nstep

so all the further processing will use them. You can do that as long as you never need the unaccumulated responses.
Ozmando
Posts: 52
Joined: Sun Jul 15, 2012 4:04 pm

Re: Help with accumulate

Unread post by Ozmando »

TomDoan wrote:First, note that you're basing this on old programming. @MCPROCESSIRF and ]@MCGRAPHIRF both have ACCUM options that let you do the cumulated responses at the end and 80% of your program is basically just doing what @MCGRAPHIRF is designed to do. If you just save your impulse responses into a properly designed %%RESPONSES (rather than your RESPONSES) you can use those procedures.

As an alternative, you can also just insert the ACCUMULATE right after you generate the responses:

impulse(noprint,model=varmodel,decomp=swish,results=impulses) nvar nstep
accumulate impulses(1,3) 1 nstep

so all the further processing will use them. You can do that as long as you never need the unaccumulated responses.
Thanks for your help. All works fine now.
Appreciated
oz
Post Reply