cal(m) 1999:1
all 2009:9

********************
*      DEFINITIONS
********************
compute LAGS = 6
compute NSTEP = 30
compute NVAR = 7
compute IPERIOD = 1
compute NDRAWS = 1000


open data "Input_w_manuf_99m1_09m9.xls"
data(format=xls,org=columns) 1999:1 2009:9 rpi_sa ppi_sa cpi_sa cbdiscr ffdr $
indy_excl_constr lindy_excl_constr_cy2 lindy_excl_constr_cy4 lindy_smdrop_cy2 $
lindy_smdrop_cy4 indy_lvldrop indy_lvldrop_cy2 indy_lvldrop_cy4 lindy_quadradetr indp_sa_notrad $
indp_sa_flow_dywk indp_sa_flow_wkwkd indp_sa_x11 indp_sa_tramo_seats indp_sa_lvldrop1 indp_sa_lvldrop2 $
indp_sa_lvl_dprs m1_sa_stock_30day crncy_sa_stock_30day neer_sa $
poil_rmb_sa_easter_8daybf poil_rmb_sa_easter_chris poil_usd_sa_easter_8d_bf $
poil_usd_sa_easter_chris palcm_usd_sa palcm_rmb_sa pnfcm_usd_sa pnfcm_rmb_sa_1 pnfcm_rmb_x11_sa

* Take logs
*set lindp =log(indp_sa_notrad)
*set lindy_excl_constr =log(indy_excl_constr)
*set lvdip = log(vdip)
*set lindy_lvldrop = log(indy_lvldrop)
set lrpi =log(rpi_sa)
set lcpi =log(cpi_sa)
set lppi =log(ppi_sa)
set lm1 =log(m1_sa_stock_30day)
*set lm2 =log(m2_sa_stock_30day)
set lcrncy =log(crncy_sa_stock_30day)
set lneer =log(neer_sa)
set lpoil =log(poil_usd_sa_easter_8d_bf)
set lpalcm =log(palcm_usd_sa)
set lpnfcm =log(pnfcm_usd_sa)

* Set intercept dummy for structural break
set d1 = t>=1998:1
set d2 = t>=2005:7

* Take 1st diff.
*set dlindp = lindp-lindp{1}
*set dlindy_excl_constr = lindy_excl_constr-lindy_excl_constr{1}
*set dlvdip = lvdip-lvdip{1}
*set dlindy_lvldrop = lindy_lvldrop-lindy_lvldrop{1}
set dlrpi = lrpi-lrpi{1}
set dlcpi = lcpi-lcpi{1}
set dlppi = lppi-lppi{1}
set dcbdiscr = cbdiscr-cbdiscr{1}
*set dlm2 = lm2-lm2{1}
set dlm1 = lm1-lm1{1}
set dlcrncy = lcrncy-lcrncy{1}
set dlneer = lneer-lneer{1}
set dlpoil = lpoil-lpoil{1}
set dlpalcm = lpalcm-lpalcm{1}
set dlpnfcm = lpnfcm-lpnfcm{1}
set dffdr = ffdr-ffdr{1}


* Define the variables in the system
set rgdp = lindy_quadradetr
set p_ppi = dlppi
set p_cpi = dlcpi
set p_rpi = dlrpi
set intr = dcbdiscr
set money = dlm1
set exchr = dlneer
set fyff = dffdr
set pcom = dlpalcm
set d2exchr = d2*exchr

**** Set up the sample range
DEC vector[interger] date1(2) date2(2)

compute date1 = ||1999:1,2002:1||
compute date2 = ||2009:9,2009:9||

dec frml[rect] ab
nonlin(parmset=kimroubi) a12 a21 a24 a25 a36 a43 a46 a56 $
a71 a72 a73 a74 a75 a76

* modified KR identification restriction *

frml ab =||1.0,a12,0.0,0.0,0.0,0.0,0.0|$
            a21,1.0,0.0,a24,a25,0.0,0.0|$
            0.0,0.0,1.0,0.0,0.0,a36,0.0|$
            0.0,0.0,a43,1.0,0.0,a46,0.0|$
            0.0,0.0,0.0,0.0,1.0,a56,0.0|$
            0.0,0.0,0.0,0.0,0.0,1.0,0.0|$
            a71,a72,a73,a74,a75,a76,1.0||

******************************************
*
*  Set up the system
*
dec vect[series] udraws(nvar) resids(nvar) resample(nvar)
dec vect[equation] eqsample(nvar) eqbase(nvar)
*
SYSTEM eqbase
variables money intr p_ppi p_rpi rgdp pcom exchr
lags 1 to lags
kfset xxx
det constant d2exchr{1 to lags}
End(system)

**** Estimate the VAR
   Estimate(sigma,cvout=v,print,ftests) date1(iperiod) $
     date2(iperiod) resids(1)
   compute xxx=%xx

***Structural VAR model restriction******

compute a12=-0.08615903,a21=22.71916072,a24=-0.93482506,a25=-2.67652275,$
a36=-0.03468131,a43=-0.20383820,a46=-0.00301133,a56=-0.10467822,a71=0.11729646,$
a72=-0.00857466,a73=0.80264590,a74=-0.25546364,a75=0.03659850,a76=0.03522544

*
*  Set up the parallel system for the resampled data
*
system eqsample
variables resample
lags 1 to lags
det constant
end(system)
*

do i=1,nvar
   set resample(i) = %eqndepvar(eqbase(i)){0}
end do i
*

list ieqn = 1 to 7
do draws=1,ndraws
   boot entries 2001:1 2009:9
   do i=1,nvar
      set udraws(i) = resids(i)(entries(t))
   end do i
   forecast(paths) nvar (2009:9)-(2001:1)+1 2001:1
   cards eqbase(ieqn) resample(ieqn)
   # udraws
   estimate(print,noftests,outsigma=vmat)
   cvmodel(parmset=kimroubi,print,factor=afactor) vmat ab
   end do draws


