'***************************************************************
'*     IMPULSE RESPONSE ON TRUNCATED MA	   *
'***************************************************************
PAGESELECT TIMESERIES 
coef(1000) C1
coef(1000) C2
coef(1000) C3
coef(1000) C4

!horz = 3+2
!reps = 1000
!year = 2007   
!start = 1981   

'set variables

%MODEL = "TB_SD2" 			'  EB_AT  	EB_ATHD   EB_ATLD
%LIST_YEARS = "1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007"'  
%LIST ="AUS AUT BEL CAN DNK ESP FRA GBR IRL ITA JPN PRT USA DEU "

'%LIST ="AUS AUT BEL CAN  DNK FRA GBR IRL JPN PRT USA DEU"  'ESP  ITA
!m=0
FOR %IDC  {%LIST}
		!m=!m+1
NEXT
IF !m>9 then 
		!m=9
ENDIF

smpl !start !year 

'create storage matrix
FOR %IDC  {%LIST}
		FOR %VAR {%VAR_to_PLOT} 
				matrix(!horz,!reps) S_{%VAR}_{%IDC}=NA
		NEXT 
NEXT

SCALAR A_FAIL=0
SCALAR A_COUNTER=0

rndseed(type=mt) 123456

'set the systems
%sys="sys_"+%VAR_to_PLOT+%MODEL
%sys2="sys_"+%VAR_to_PLOT+%MODEL+"2"

'generating the series
FOR %IDC  {%LIST}
		SMPL @ALL
		GENR IMF_TOT_UT_S{%idc} = IMF_TOT_UT{%idc}
		GENR IMF_TOT_ATT_S{%idc} = IMF_TOT_ATT{%idc}
		GENR IMF_TOT_ATT1_S{%idc} = IMF_TOT_ATT1{%idc}
		GENR IMF_TOT_ATT2_S{%idc} = IMF_TOT_ATT2{%idc}
		'GENR IMF_TOT_ATT3_S{%idc} = IMF_TOT_ATT3{%idc}
		'GENR IMF_TOT_ATT4_S{%idc} = IMF_TOT_ATT4{%idc}
		'GENR IMF_TOT_ATT5_S{%idc} = IMF_TOT_ATT5{%idc}
		GENR IMF_TB_S{%IDC} = IMF_TB{%idc}
		GENR IMF_EB_S{%IDC} = IMF_EB{%idc}
NE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IDC} + C1(20+!J)*IMF_TOT_ATT2_S{%IDC}*IMF_TB_S{%IDC} + C1(21+!J)*IMF_TOT_ATT2_S{%IDC}*IMF_EB_S{%IDC} +  C1(26+!J)*dummy_s1983+ C1(27+!J)*dummy_s1984+ C1(28+!J)*dummy_s1985+ C1(29+!J)*dummy_s1986+ C1(30+!J)*dummy_s1987+ C1(31+!J)*dummy_s1988+ C1(32+!J)*dummy_s1989+ C1(33+!J)*dummy_s1990+ C1(34+!J)*dummy_s1991+ C1(35+!J)*dummy_s1992+ C1(36+!J)*dummy_s1993+ C1(37+!J)*dummy_s1994+ C1(38+!J)*dummy_s1995+ C1(39+!J)*dummy_s1996+ C1(40+!J)*dummy_s1997+ C1(41+!J)*dummy_s1998+ C1(42+!J)*dummy_s1999+ C1(43+!J)*dummy_s2000+ C1(44+!J)*dummy_s2001+ C1(45+!J)*dummy_s2002+ C1(46+!J)*dummy_s2003+ C1(47+!J)*dummy_s2004+ C1(48+!J)*dummy_s2005 + C1(49+!J)*dummy_s2006 + C1(50+!J)*dummy_s2007 '+ C2(24+!J)*IMF_TOT_ATT4_S{%IDC}*IMF_TB_S{%IDC} + C(25+!J)*IMF_TOT_ATT4_S{%IDC}*IMF_EB_S{%IDC} '+ C(26+!J)*IMF_TOT_ATT5_S{%IDC}*IMF_TB_S{%IDC} + C(27+!J)*IMF_TOT_ATT5_S{%IDC}*IMF_EB_S{%IDC}    + C(24+!J)*dummy_s1981    C1(22+!J)*IMF_TOT_ATT3_S{%IDC}*IMF_TB_S{%IDC} + C1(23+!J)*IMF_TOT_ATT3_S{%IDC}*IMF_EB_S{%IDC} +
		!I=!I+50
		'!K=!K+7	
		'!J = !J +7 '***  	'if skipped, then panel restrictions are imposed on the GVAR  
NEXT

delete {%sys2}* 
system {%sys2} 
!J= 0
!I=0
!K=0
FOR %IDC {%LIST}
		{%sys2}.append {%VAR_to_PLOT}{%IDC}_0m =  C2(1+!I) + C2(2+!J)*IMF_TOT_UT_S{%IDC}*IMF_T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s1987+ C2(31+!J)*dummy_s1988+ C2(32+!J)*dummy_s1989+ C2(33+!J)*dummy_s1990+ C2(34+!J)*dummy_s1991+ C2(35+!J)*dummy_s1992+ C2(36+!J)*dummy_s1993+ C2(37+!J)*dummy_s1994+ C2(38+!J)*dummy_s1995+ C2(39+!J)*dummy_s1996+ C2(40+!J)*dummy_s1997+ C2(41+!J)*dummy_s1998+ C2(42+!J)*dummy_s1999+ C2(43+!J)*dummy_s2000+ C2(44+!J)*dummy_s2001+ C2(45+!J)*dummy_s2002+ C2(46+!J)*dummy_s2003+ C2(47+!J)*dummy_s2004+ C2(48+!J)*dummy_s2005 + C2(49+!J)*dummy_s2006 + C2(50+!J)*dummy_s2007'+ C2(24+!J)*IMF_TOT_ATT4_S{%IDC}*IMF_TB_S{%IDC} + C2(25+!J)*IMF_TOT_ATT4_S{%IDC}*IMF_EB_S{%IDC} '+ C2(26+!J)*IMF_TOT_ATT5_S{%IDC}*IMF_TB_S{%IDC} + C2(27+!J)*IMF_TOT_ATT5_S{%IDC}*IMF_EB_S{%IDC}     + C2(24+!J)*dummy_s1981    C2(22+!J)*IMF_TOT_ATT3_S{%IDC}*IMF_TB_S{%IDC} + C2(23+!J)*IMF_TOT_ATT3_S{%IDC}*IMF_EB_S{%IDC} + 
		!I=!I+50
		'!K=!K+7
		'!J = !J +7 '*** 'if skipped, then panel restrictions are imposed on the GVAR  
NEXT

smpl !start !year
{%sys}.sur 

delete resid1* resid2* resid3* resid4* resid6* resid7* resid8* resid9* resid0*

{%sys}.makeresids(n=gres)

FOR !i = 1 TO !m
		series resid{!i}=resid0{!i}
		delete resid0{!i}
NEXT

scalar temp_res = 0

FOR %IDC  {%LIST}
		FOR %VAR {%VAR_to_PLOT} 
				temp_res = temp_res + 1
				!p = temp_res
				genr res_{%VAR}_{%IDC} = resid{!p}
		NEXT
NEXT
delete temp_res* 

'********************************************************************************
'   BOOTSTRAP (SMALL SAMPLE CORRECTION)
'********************************************************************************
model model
model.merge {%sys}

FOR %IDC {%LIST}
 		FOR %VAR {%VAR_to_PLOT} 
  				model.addassign {%VAR}{%IDC} 
	 	NEXT
NEXT

'*******************************************************************************
FOR !j=1 TO !reps
		A_COUNTER = A_COUNTER + 1
'resampling residuals
smpl !start !year
delete g1*
group g1
FOR %IDC {%LIST}
FOR %VAR {%VAR_to_PLOT} 
g1.add res_{%VAR}_{%IDC}
next 
next    
smpl 1992 2007
scalar sigma=@var(res_{%VAR_to_PLOT}_deu)
smpl !start 1991
res_{%VAR_to_PLOT}_deu=(sigma^0.5)*nrnd     
smpl !start !year        
g1.resample(block = 2,dropna)
FOR %IDC {%LIST}
FOR %VAR {%VAR_to_PLOT} 
	series {%VAR}{%IDC}_a=res_{%VAR}_{%IDC}_b
	delete res_{%VAR}_{%IDC}_b
next 
next

	'	FOR %IDC {%LIST}
		'		FOR %VAR {%VAR_to_PLOT} 
			'			res_{%VAR}_{%IDC}.resample(dropna) {%VAR}{%IDC}_A
		'		NEXT
		'NEXT

		model.stochastic(i=b, d=T, v=t, r=100, s=!START+1 !YEAR, f=.2, b=.83,c=F)
		model.solveopt(s=m)
		'model.append assign @all  _0m 'to be activated only if sol=det) 
		smpl !START  !YEAR 
		model.solve

		smpl !START !YEAR 
		{%sys2}.sur 
		SMPL @ALL

		'***** SCENARIO
		model model_2
		model_2.merge {%sys2}
		model_2.append assign @all  _ALT
		model_2.scenario(n, a=_1) "Alternative"
		model_2.scenario "Alternative"

		FOR %YEAR {%LIST_YEARS}
				SMPL @ALL
				GENR DUMMY_S{%YEAR}_1 = DUMMY_S{%YEAR}
		NEXT
	
		FOR %IDC  {%LIST}
				SMPL @ALL
				GENR IMF_TOT_UT_S{%idc}_1 = IMF_TOT_UT_S{%idc}
				GENR IMF_TOT_ATT_S{%idc}_1 = IMF_TOT_ATT_S{%idc}
				GENR IMF_TOT_ATT1_S{%idc}_1 = IMF_TOT_ATT1_S{%idc}
				GENR IMF_TOT_ATT2_S{%idc}_1 = IMF_TOT_ATT2_S{%idc}
				'GENR IMF_TOT_ATT3_S{%idc}_1 = IMF_TOT_ATT3_S{%idc}
				'GENR IMF_TOT_ATT4_S{%idc}_1 = IMF_TOT_ATT4_S{%idc}
				'GENR IMF_TOT_ATT5_S{%idc}_1 = IMF_TOT_ATT5_S{%idc}
				GENR IMF_TB_S{%idc}_1 = IMF_TB_S{%idc}
				GENR IMF_EB_S{%idc}_1 = IMF_EB_S{%idc}
				smpl !YEAR+1 !YEAR+!horz
				IMF_TOT_UT_S{%idc}_1 =0
				IMF_TOT_UT_S{%idc} =0
				IMF_TOT_ATT_S{%idc}_1 = 0
				IMF_TOT_ATT_S{%idc} = 0
				IMF_TOT_ATT1_S{%idc}_1=0
				IMF_TOT_ATT1_S{%idc}=0
				IMF_TOT_ATT2_S{%idc}_1=0
				IMF_TOT_ATT2_S{%idc}=0
				'IMF_TOT_ATT3_S{%idc}_1=0
				'IMF_TOT_ATT3_S{%idc}=0
				IMF_EB_S{%idc}_1 = 0
				IMF_EB_S{%idc} = 0
				IMF_TB_S{%idc}_1 = 1
				IMF_TB_S{%idc} = 0
				smpl !YEAR+1 !YEAR+1
				IMF_TOT_UT_S{%idc}_1 =1
				IMF_TOT_ATT1_S{%idc}_1= eq_style{%IDC}_3.@coefs(2)*IMF_TOT_UT_S{%idc}_1
				IMF_TOT_ATT2_S{%idc}_1=eq_style{%IDC}_4.@coefs(2)*IMF_TOT_UT_S{%idc}_1
				smpl !YEAR+2 !YEAR+2
				IMF_TOT_ATT1_S{%idc}_1=IMF_TOT_ATT2_S{%idc}_1(-1)
                     IMF_TOT_ATT_S{%idc}_1=IMF_TOT_ATT1_S{%idc}_1(-1)
				smpl !YEAR+3 !YEAR+3
				IMF_TOT_ATT_S{%idc}_1=IMF_TOT_ATT1_S{%idc}_1(-1)
				smpl !YEAR+1 !YEAR+!horz
				model_2.override(m) IMF_TB_S{%idc} IMF_TOT_ATT_S{%idc} IMF_TOT_ATT1_S{%idc} IMF_TOT_ATT2_S{%idc} IMF_TOT_UT_S{%idc}
		NEXT		

		model_2.scenario(C) "Baseline"
		model_2.stochastic(i=b, v=t, r=100, s=!START+1 !YEAR, f=.1, b=.83,c=F)
		model_2.solveopt(s=m,d=d, A=T)
		smpl !YEAR+1 !YEAR+!horz

		model_2.solve

		'if cumulated IR are needed uncomment this
		FOR %IDC {%LIST}

		 	smpl !year+1 !year+1
			genr {%VAR}{%IDC}_0m_1_ALTM_c = {%VAR}{%IDC}_0m_1_ALTM
		 	smpl !year+2 !year+!horz
			series {%VAR}{%IDC}_0m_1_ALTM_c = {%VAR}{%IDC}_0m_1_ALTM_c(-1)+{%VAR}{%IDC}_0m_1_ALTM
		 	
		 	smpl !year+1 !year+!horz 
		 	FOR %VAR {%VAR_to_PLOT} 
	 			'stomNA( {%VAR}{%IDC}_0m_1_ALTM,temp) 'In the case of spread
				'activate if cumulated IR are needed
				stomNA( {%VAR}{%IDC}_0m_1_ALTM_c,temp) 
	 			colplace(S_{%VAR}_{%IDC},temp,!j)
	 		NEXT 
		NEXT
NEXT

'************************************************************
' 	END OF THE BOOTSTRAP		  *
'************************************************************
'find bootstrap percentiles for each horizon
FOR %IDC {%LIST}
	FOR %VAR {%VAR_to_PLOT} 	
		matrix(!horz,3) rsp_{%VAR}_{%MODEL}_{%IDC}
		delete rtmp*
		rowvector rtmp
			FOR !i=1 TO !horz
				rtmp = @rowextract(S_{%VAR}_{%IDC},!i)
				rsp_{%VAR}_{%MODEL}_{%IDC}(!i,1) = @quantile(rtmp,0.84)
				rsp_{%VAR}_{%MODEL}_{%IDC}(!i,2) = @quantile(rtmp,0.50)
				rsp_{%VAR}_{%MODEL}_{%IDC}(!i,3) = @quantile(rtmp,0.16)
			NEXT
	NEXT
NEXT

FOR %IDC {%LIST}
		delete {%VAR_to_PLOT}{%IDC}_*
		delete {%VAR_to_PLOT}_{%IDC}_a*
NEXT
delete  s_* rtmp temp model* %sys2 resid* res* TOT_SHOCK_S* IMF_TOT_S* IMF_EB_S* IMF_TB_S* IMF_EB1_S* IMF_TB1_S* IMF_TOT_UT_S* IMF_TOT_ATT_S* IMF_TOT_ATT1_S* IMF_TOT_ATT2_S* IMF_TOT_ATT3_S* IMF_TOT_ATT4_S* IMF_TOT_ATT5_S* DUMMY_S* 'rsp*


