'***********
' Check if the two step procedure works
'***********

wfopen C:\Temp\usmacro3.dta @freq M @id @date(datem) @smpl @all

'3 equations to estimate the residuals for each of the endogenous regressors
equation eq1.ls  fedfunds  c  ip_growth(-1 to -2) fedfunds(-1 to -2) inflation(-1 to -2)
series fedres = resid
equation eq2.ls  ip_growth  c  fedfunds(-1 to -2) ip_growth(-1 to -2) inflation(-1 to -2)
series ipres = resid 
equation eq3.ls  inflation c  fedfunds(-1 to -2) ip_growth(-1 to -2) inflation(-1 to -2)
series inflres = resid 

'1 residual for the instrument
equation eq4.ls oil_inst c  fedfunds(-1 to -2) ip_growth(-1 to -2) inflation(-1 to -2)
series oilres = resid
 
'2SLS to estimate the impact coefficients
equation eq5.ls fedres c oilres
scalar fed = c(2)
equation eq6.ls ipres c oilres
scalar ip = c(2)
equation eq7.ls inflation c oilres
scalar inf = c(2)

'scale the impact coefficients  
scalar fed_impact = fed / inf
scalar ip_impact = ip / inf
scalar inf_impact = inf / inf

'estimate VAR
var var1.ls 1 2 inflation ip_growth fedfunds

'put the coefficient restrictions in the B matrix
var1.cleartext(svar)
var1.append(svar) @VEC(A) = 1, NA, NA, 0, 1, NA, 0, 0, 1
var1.append(svar) @VEC(B) = 1, -0.31198, 0.00461429119, 0, NA, 0, 0, 0, NA
var1.svar

'generate the IRF for the target shock only





