'5&7 variable fiscal SVAR

'***********Variables****************
'Government spending			gs
'Tax								tax
'GDP								gdp
' the above are all log real per capita, deflated using CPI
'CPI inflation						cpi
'Real interest rates			ir
'linear time trend				lintt
'constant							cons

'***********Code****************

'create workfile
wfcreate(wf=untitled, page=annual) a 1980 2025

'number of lags
scalar nlags = 3

'dates
string first="1990"
string last="2017"

'pull in data from data_an1.wf1 file
wfopen C:\Users\ruwan\Desktop\requantifyingfiscalmultipliersinnewzealandtheevide\data_an1.wf1 
wfselect UNTITLED

'%series="lnrrexp lnrcexp	lnrrev lnrgdp cpi "
 %series="lnrtexp lnrrev lnrgdp cpi rir "
' Rename the series
	
%codes="gs tax gdp cpi rir"
for !n=1 to @wcount(%series)
	%s = @word(%series,!n)
	%c = @word(%codes,!n)
	
	copy DATA_AN1::{%s}
	rename {%s} {%c}
next

' create linear time trend
smpl {first} {first}
series lintt=1					
smpl {first}+1 {last}
lintt=lintt(-1)+1

'create constant
smpl {first} {last}
series cons=1
close DATA_AN1


'***********Identifying and running the 5 variable SVAR****************

	
'enter elasticities here
'naming convention: a = matrix a, t = net tax, g = government spending, y = GDP, p = inflation, i = interest rates, 21 and 12 refer to the matrix row and cell
scalar aty=1.20 		
scalar agy=0
scalar agp=-0.5	 
scalar atp=0.10
scalar agi=0
scalar ati=0
scalar a21=0
scalar b21 = na
scalar b12=0



'Here we fill matrices A and B 
	'Whenever you need to estimate a coefficient you write in the matrix NA.

matrix(5,5) pata				 'Matrix A
matrix(5,5) patb				 'Matrix B
pata.fill(by=r) 1, 0, -{agy}, -{agp}, -{agi}, {a21}, 1, -{aty}, -{atp}, -{ati}, na, na, 1, 0, 0, na, na, na, 1, 0, na, na, na, na, 1			'Fill A
patb.fill(by=r) na, {b12}, 0, 0, 0, {b21}, na, 0, 0, 0, 0, 0, na, 0, 0, 0, 0, 0, na, 0, 0, 0, 0, 0, na				 'Fill B
 

var  sgvar1.ls(n) 1 {nlags} gs tax gdp cpi rir @ cons lintt
''Creates a Table with the Svar results
 freeze(table1) sgvar1.svar(rtype=patsr,namea=pata,nameb=patb, f0=u)
 
  'These are the matrices obtained with the estimated VAR, which we need to obtain and manipulate 
 matrix mata1 = sgvar1.@svaramat
 matrix matb1 = sgvar1.@svarbmat
 
  'compute factorization matrix using the matrices A and B actually estimated (several parameters were estimated outside the VAR and imposed in matrix A).
	'** e= A-1B u 
  matrix fact1 = @inverse(mata1)*matb1
  
  'Here we get impulse responses (impulse) imposing the matrix we just computed (fact1) and create a figure (freeze)
 freeze(mode=overwrite,s_govt) sgvar1.impulse(20, imp=user, fname=fact1, se=mc,rep=400, matbys=m_govt)  gs tax gdp cpi rir @ gs
 freeze(mode=overwrite,s_ntax) sgvar1.impulse(20, imp=user, fname=fact1, se=mc,rep=400, matbys=m_ntax)  gs tax gdp cpi rir  @ tax



