' This Program scripted by yashar'
'Asking for Series Name'
%folder=@wfname
%indpi=""
%cpi=""
%ppi=""
%oilprice=""
%interest=""
%exrate=""
%imp=""
%ex=""
%reer=""
!result=@uiedit(%indpi,"Please Enter name of Industrial Production series ")
if !result=-1 then
	stop
endif
!result=@uiedit(%cpi,"Please Enter the name of CPI series or equivalent")
if !result=-1 then
stop
endif
!result=@uiedit(%ppi,"Please Enter the name of PPI series or equivalent")
if !result=-1 then
stop
endif
!result=@uiedit(%oilprice,"Please Enter the name of Oil Price series or equivalent")
if !result=-1 then
stop
endif
!result=@uiedit(%interest,"Please Enter the name of Interest series or equivalent")
if !result=-1 then
stop
endif
!result=@uiedit(%exrate,"Please Enter the name of Exchange Rate series or equlevalt")
if !result=-1 then
stop
endif
!result=@uiedit(%imp,"Please Enter the name of Import Price--- Without the number---")
if !result=-1 then
stop
endif
!result=@uiedit(%ex,"Please Enter the name of Export Price--- Without the number---")
if !result=-1 then
stop
endif
!result=@uiedit(%reer,"Please Enter the name of Real exchange rate")
if !result=-1 then
stop
endif
'The Number of Group Commodities'
scalar ng=0
!result=@uilist(ng,"How many groups of commodities are there ?","1 2 3")
if !result=-1 then
stop
endif
for !j=1 to ng step 1
rename {%ex}!j {%ex}!j_na {%imp}!j {%imp}!j_na
next j
'Get rid of NA and too big by moving average'
for !w=1 to ng step 1
series {%ex}!w={%ex}!w_na
series auxex= ({%ex}!w_na(-1)+{%ex}!w_na(-2)+{%ex}!w_na(1)+{%ex}!w_na(2))
series auxex2=auxex/4
{%ex}!w=@recode({%ex}!w_na=NA or {%ex}!w_na>10,auxex2,{%ex}!w_na)
series {%imp}!w={%imp}!w_na
series auximp= ({%imp}!w_na(-1)+{%imp}!w_na(-2)+{%imp}!w_na(1)+{%imp}!w_na(2))
series auximp2=auximp/4
{%imp}!w=@recode({%imp}!w_na=NA or {%imp}!w_na>10,auximp2,{%imp}!w_na)
next w

for !g=1 to ng step 1
'make a group'
group ge_{%ex}!g {%exrate} {%cpi} {%ppi} {%oilprice} {%interest} {%indpi} {%reer} {%ex}!g
'test for coint'
freeze(coint_{%ex}!g) ge_{%ex}!g.coint(d,4) ge_{%ex}!g
group ge_{%imp}!g {%exrate} {%cpi} {%ppi} {%oilprice} {%interest} {%indpi} {%reer} {%imp}!g
'test for coint'
freeze(coint_{%imp}!g) ge_{%imp}!g.coint(d,4) ge_{%imp}!g
next g

'--------------------------'

'Defining a subroutine for hegy'
subroutine hegy(series y)
Genr trend = @trend
Series D12Y = D(Y,0,12)
Series Y1 = Y + Y(-1) + Y(-2) + Y(-3) + Y(-4) + Y(-5) + Y(-6) + Y(-7) + Y(-8) + Y(-9) + Y(-10) + Y(-11)
Series Y2 = - Y + Y(-1) - Y(-2) + Y(-3) - Y(-4) + Y(-5) - Y(-6) + Y(-7) - Y(-8) + Y(-9) - Y(-10) + Y(-11)
Series Y3 = - Y(-1) + y(-3) - Y(-5) + Y(-7) - Y(-9) + Y(-11)
Series Y4 = - Y + Y(-2) - Y(-4) + Y(-6) - Y(-8) + Y(-10)
Series Y5 = 0.5*(- Y - Y(-1) + 2*Y(-2) - Y(-3) - Y(-4) + 2*Y(-5) - Y(-6) - Y(-7) + 2*Y(-8) - Y(-9) - Y(-10) + 2*Y(-11))
Series Y6 = (@sqrt(3)/2)*(Y - Y(-1) + Y(-3) - Y(-4) + Y(-6) - Y(-7) + Y(-9) - Y(-10))
Series Y7 = 0.5*(Y - Y(-1) - 2*Y(-2) - Y(-3) + Y(-4) + 2*Y(-5) + Y(-6) - Y(-7) - 2*Y(-8) - Y(-9) + Y(-10) + 2*Y(-11))
Series Y8 = (@sqrt(3)/2)*(Y - Y(-1) + Y(-3) + Y(-4) - Y(-6) - Y(-7) + Y(-9) + Y(-10))
Series Y9 = 0.5*(- @sqrt(3)*Y + Y(-1) - Y(-3) + @sqrt(3)*Y(-4) - 2*Y(-5) + @sqrt(3)*Y(-6) - Y(-7) + Y(-9) - @sqrt(3)*Y(-10) + 2*Y(-11))
Series Y10 = 0.5*(Y - @sqrt(3)*Y(-1) + 2*y(-2) - @sqrt(3)*y(-3) + y(-4) - y(-6) + @sqrt(3)*y(-7) - 2*y(-8) + @sqrt(3)*y(-9) - y(-10))
Series Y11 = 0.5*(@sqrt(3)*Y + Y(-1) - Y(-3) - @sqrt(3)*Y(-4) - 2*Y(-5) - @sqrt(3)*Y(-6) - Y(-7) + Y(-9) + @sqrt(3)*Y(-10) + 2*Y(-11)) 
Series Y12 = 0.5*(- Y - @sqrt(3)*Y(-1) - 2*Y(-2) - @sqrt(3)*Y(-3) - y(-4) + Y(-6) + @sqrt(3)*Y(-7) + 2*y(-8) + @sqrt(3)*Y(-9) + Y(-10))
'create seasonal dummies
for !i=1 to 11
series s!i = @seas(!i) - @seas(12)
next
EQUATION EQ.LS D12Y Y1(-1) Y2(-1) Y3(-1) Y4(-1) Y5(-1) Y6(-1) Y7(-1) Y8(-1) Y9(-1) Y10(-1) Y11(-1) Y12(-1) C TREND S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 D12Y(-1 to -12)
endsub
'-----------------------------------'
for %ser {%indpi} {%cpi} {%ppi} {%exrate} {%reer} {%oilprice} {%interest}
call hegy({%ser})
freeze(h_{%ser}) eq
'generate the test statistics that are not given in the equation output (Warning: critical values are different from those of EViews'. You should find true values for accurate hypothesis testing!)
freeze(pair1_{%ser}) eq.wald c(1)=0, c(2)=0
freeze(pair2_{%ser}) eq.wald c(3)=0, c(4)=0
freeze(pair3_{%ser}) eq.wald c(5)=0, c(6)=0
freeze(pair4_{%ser}) eq.wald c(7)=0, c(8)=0
freeze(pair5_{%ser}) eq.wald c(9)=0, c(10)=0
freeze(pair6_{%ser}) eq.wald c(11)=0, c(12)=0
freeze(cotest_{%ser}) eq.wald c(2)=0, c(3)=0, c(4)=0, c(5)=0, c(6)=0, c(7)=0, c(8)=0, c(9)=0, c(10)=0, c(11)=0, c(12)=0
freeze(cotestall_{%ser}) eq.wald c(1)=0, c(2)=0, c(3)=0, c(4)=0, c(5)=0, c(6)=0, c(7)=0, c(8)=0, c(9)=0, c(10)=0, c(11)=0, c(12)=0
	matrix(1,20) m_h_{%ser}
	m_h_{%ser}(1,1)=@round(1000*h_{%ser}(9,4))/1000
	m_h_{%ser}(1,2)=@round(1000*h_{%ser}(10,4))/1000
	
	m_h_{%ser}(1,3)=@round(1000*pair1_{%ser}(6,2))/1000
	m_h_{%ser}(1,4)=@round(1000*pair1_{%ser}(6,4))/1000
	
	m_h_{%ser}(1,5)=@round(1000*pair2_{%ser}(6,2))/1000
	m_h_{%ser}(1,6)=@round(1000*pair2_{%ser}(6,4))/1000
	
	m_h_{%ser}(1,7)=@round(1000*pair3_{%ser}(6,2))/1000
	m_h_{%ser}(1,8)=@round(1000*pair3_{%ser}(6,4))/1000
	
	m_h_{%ser}(1,9)=@round(1000*pair4_{%ser}(6,2))/1000
	m_h_{%ser}(1,10)=@round(1000*pair4_{%ser}(6,4))/1000
	
	m_h_{%ser}(1,11)=@round(1000*pair5_{%ser}(6,2))/1000
	m_h_{%ser}(1,12)=@round(1000*pair5_{%ser}(6,4))/1000
	
	m_h_{%ser}(1,13)=@round(1000*pair6_{%ser}(6,2))/1000
	m_h_{%ser}(1,14)=@round(1000*pair6_{%ser}(6,4))/1000
	
freeze(m_h_{%ser}_m) m_h_{%ser}
m_h_{%ser}_m.save(t=csv) F:\ECU RA\2nd\result\htest\{%folder}_{%ser}
next 
'-------------------------'


'more should be added to here for hmatrix'

for !d=1 to ng step 1
call hegy({%imp}!d)
freeze(h_{%imp}!d) eq
'generate the test statistics that are not given in the equation output (Warning: critical values are different from those of EViews'. You should find true values for accurate hypothesis testing!)
freeze(pair1_{%imp}!d) eq.wald c(1)=0, c(2)=0
freeze(pair2_{%imp}!d) eq.wald c(3)=0, c(4)=0
freeze(pair3_{%imp}!d) eq.wald c(5)=0, c(6)=0
freeze(pair4_{%imp}!d) eq.wald c(7)=0, c(8)=0
freeze(pair5_{%imp}!d) eq.wald c(9)=0, c(10)=0
freeze(pair6_{%imp}!d) eq.wald c(11)=0, c(12)=0
freeze(cotest_{%imp}!d) eq.wald c(2)=0, c(3)=0, c(4)=0, c(5)=0, c(6)=0, c(7)=0, c(8)=0, c(9)=0, c(10)=0, c(11)=0, c(12)=0
freeze(cotestall_{%imp}!d) eq.wald c(1)=0, c(2)=0, c(3)=0, c(4)=0, c(5)=0, c(6)=0, c(7)=0, c(8)=0, c(9)=0, c(10)=0, c(11)=0, c(12)=0
next d
'-----------'
for !u=1 to ng step 1
call hegy({%ex}!u)
freeze(h_{%ex}!u) eq
'generate the test statistics that are not given in the equation output (Warning: critical values are different from those of EViews'. You should find true values for accurate hypothesis testing!)
freeze(pair1_{%ex}!u) eq.wald c(1)=0, c(2)=0
freeze(pair2_{%ex}!u) eq.wald c(3)=0, c(4)=0
freeze(pair3_{%ex}!u) eq.wald c(5)=0, c(6)=0
freeze(pair4_{%ex}!u) eq.wald c(7)=0, c(8)=0
freeze(pair5_{%ex}!u) eq.wald c(9)=0, c(10)=0
freeze(pair6_{%ex}!u) eq.wald c(11)=0, c(12)=0
freeze(cotest_{%ex}!u) eq.wald c(2)=0, c(3)=0, c(4)=0, c(5)=0, c(6)=0, c(7)=0, c(8)=0, c(9)=0, c(10)=0, c(11)=0, c(12)=0
freeze(cotestall_{%ex}!u) eq.wald c(1)=0, c(2)=0, c(3)=0, c(4)=0, c(5)=0, c(6)=0, c(7)=0, c(8)=0, c(9)=0, c(10)=0, c(11)=0, c(12)=0
next u
for %nu {%imp} {%ex}
for !lm=1 to ng step 1
	matrix(1,14) m_h_{%nu}!lm=na
	m_h_{%nu}!lm(1,1)=@round(1000*h_{%nu}!lm(9,4))/1000
	m_h_{%nu}!lm(1,2)=@round(1000*h_{%nu}!lm(10,4))/1000
	
	m_h_{%nu}!lm(1,3)=@round(1000*pair1_{%nu}!lm(6,2))/1000
	m_h_{%nu}!lm(1,4)=@round(1000*pair1_{%nu}!lm(6,4))/1000
	
	m_h_{%nu}!lm(1,5)=@round(1000*pair2_{%nu}!lm(6,2))/1000
	m_h_{%nu}!lm(1,6)=@round(1000*pair2_{%nu}!lm(6,4))/1000
	
	m_h_{%nu}!lm(1,7)=@round(1000*pair3_{%nu}!lm(6,2))/1000
	m_h_{%nu}!lm(1,8)=@round(1000*pair3_{%nu}!lm(6,4))/1000
	
	m_h_{%nu}!lm(1,9)=@round(1000*pair4_{%nu}!lm(6,2))/1000
	m_h_{%nu}!lm(1,10)=@round(1000*pair4_{%nu}!lm(6,4))/1000
	
	m_h_{%nu}!lm(1,11)=@round(1000*pair5_{%nu}!lm(6,2))/1000
	m_h_{%nu}!lm(1,12)=@round(1000*pair5_{%nu}!lm(6,4))/1000
	
	m_h_{%nu}!lm(1,13)=@round(1000*pair6_{%nu}!lm(6,2))/1000
	m_h_{%nu}!lm(1,14)=@round(1000*pair6_{%nu}!lm(6,4))/1000
	
	freeze(h2_{%nu}!lm) m_h_{%nu}!lm
	h2_{%nu}!lm.save(t=csv) F:\ECU RA\2nd\result\htest\{%folder}_{%nu}!lm

next lm
next
'----------------------------------'

'Matrix for Long Term Pattern'
matrix(7,7) lpat=na
lpat(1,2)=0
lpat(1,3)=0
lpat(1,4)=0
lpat(1,5)=0
lpat(1,6)=0
lpat(1,7)=0
lpat(2,3)=0
lpat(2,4)=0
lpat(2,5)=0
lpat(2,6)=0
lpat(2,7)=0
lpat(3,4)=0
lpat(3,5)=0
lpat(3,6)=0
lpat(3,7)=0
lpat(4,5)=0
lpat(4,6)=0
lpat(4,7)=0
lpat(5,6)=0
lpat(5,7)=0
lpat(6,7)=0
'-------------------------------------------------------------------------'
for !t=1 to ng step 1
'VAR Estimation for Import & Export'
var var{%imp}!t.ls 1 2  {%exrate} {%cpi} {%ppi} {%oilprice} {%interest} {%indpi} {%imp}!t @
freeze(SVAR_res_{%imp}!t) var{%imp}!t.svar(rtype=patlr,namelr=lpat)
var var{%ex}!t.ls 1 2  {%exrate} {%cpi} {%ppi} {%oilprice} {%interest} {%indpi} {%ex}!t @
freeze(SVAR_res_{%ex}!t) var{%ex}!t.svar(rtype=patlr,namelr=lpat)
	
	
		freeze(puls_{%imp}!t_{%ppi}_1) var{%imp}!t.impulse(1,m,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%imp}!t_{%cpi}_1) var{%imp}!t.impulse(1,m,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%imp}!t_1) var{%imp}!t.impulse(1,m,se=a) {%imp}!t @{%exrate}
		freeze(ex{%imp}!t_g_1) var{%imp}!t.impulse(1,m,se=a) {%exrate} @{%exrate}
		
		freeze(puls_{%ex}!t_{%ppi}_1) var{%ex}!t.impulse(1,m,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%ex}!t_{%cpi}_1) var{%ex}!t.impulse(1,m,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%ex}!t_1) var{%ex}!t.impulse(1,m,a,se=a) {%ex}!t @{%exrate}
		freeze(ex{%ex}!t_g_1) var{%ex}!t.impulse(1,m,a,se=a) {%exrate} @{%exrate}

		freeze(puls_{%imp}!t_{%ppi}_t1) var{%imp}!t.impulse(1,t,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%imp}!t_{%cpi}_t1) var{%imp}!t.impulse(1,t,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%imp}!t_t1) var{%imp}!t.impulse(1,t,se=a) {%imp}!t @{%exrate}
		freeze(ex{%imp}!t_t_t1) var{%imp}!t.impulse(1,t,se=a) {%exrate} @{%exrate}
		
		freeze(puls_{%ex}!t_{%ppi}_t1) var{%ex}!t.impulse(1,t,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%ex}!t_{%cpi}_t1) var{%ex}!t.impulse(1,t,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%ex}!t_t1) var{%ex}!t.impulse(1,t,a,se=a) {%ex}!t @{%exrate}
		freeze(ex{%ex}!t_t_t1) var{%ex}!t.impulse(1,t,a,se=a) {%exrate} @{%exrate}

		
	for !f=5 to 20 step 5
		freeze(puls_{%imp}!t_{%ppi}_!f) var{%imp}!t.impulse(!f,m,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%imp}!t_{%cpi}_!f) var{%imp}!t.impulse(!f,m,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%imp}!t_!f) var{%imp}!t.impulse(!f,m,a,se=a) {%imp}!t @{%exrate}
		freeze(ex{%imp}!t_g_!f) var{%imp}!t.impulse(!f,m,a,se=a) {%exrate} @{%exrate}
		
		freeze(puls_{%imp}!t_{%ppi}_t!f) var{%imp}!t.impulse(!f,t,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%imp}!t_{%cpi}_t!f) var{%imp}!t.impulse(!f,t,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%imp}!t_t!f) var{%imp}!t.impulse(!f,t,a,se=a) {%imp}!t @{%exrate}
		freeze(ex{%imp}!t_t_t!f) var{%imp}!t.impulse(!f,t,a,se=a) {%exrate} @{%exrate}
		
		freeze(puls_{%ex}!t_{%ppi}_!f) var{%ex}!t.impulse(!f,m,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%ex}!t_{%cpi}_!f) var{%ex}!t.impulse(!f,m,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%ex}!t_!f) var{%ex}!t.impulse(!f,m,a,se=a) {%ex}!t @{%exrate}
		freeze(ex{%ex}!t_g_!f) var{%ex}!t.impulse(!f,m,a,se=a) {%exrate} @{%exrate}
		
		freeze(puls_{%ex}!t_{%ppi}_t!f) var{%ex}!t.impulse(!f,t,a,se=a) {%ppi} @{%exrate}
		freeze(puls_{%ex}!t_{%cpi}_t!f) var{%ex}!t.impulse(!f,t,a,se=a) {%cpi} @{%exrate}
		freeze(puls_{%ex}!t_t!f) var{%ex}!t.impulse(!f,t,a,se=a) {%ex}!t @{%exrate}
		freeze(ex{%ex}!t_t_t!f) var{%ex}!t.impulse(!f,t,a,se=a) {%exrate} @{%exrate}
	next f

	freeze(VD_{%imp}!t_g) var{%imp}!t.decomp(m,20,se=mc)  {%cpi} {%ppi} {%exrate} {%interest} {%oilprice} {%imp}!t 
	freeze(VD_{%imp}!t_t) var{%imp}!t.decomp(t,20,se=mc)  {%cpi} {%ppi} {%exrate} {%interest} {%oilprice} {%imp}!t 

	freeze(VD_{%ex}!t_g) var{%ex}!t.decomp(m,20,se=mc)  {%cpi} {%ppi} {%exrate} {%interest} {%oilprice} {%ex}!t
	freeze(VD_{%ex}!t_t) var{%ex}!t.decomp(t,20,se=mc)  {%cpi} {%ppi} {%exrate} {%interest} {%oilprice} {%ex}!t
next t

''smaple for impulse response '
for %esm {%imp} {%ex}
	for !q=1 to ng step 1
	matrix(5,4) hu_{%esm}!q
		hu_{%esm}!q(1,1)=1
		hu_{%esm}!q(1,2)=@round(1000*puls_{%esm}!q_{%ppi}_t1(4,2)/ex{%esm}!q_t_t1(4,2))/1000
		hu_{%esm}!q(1,3)=@round(1000*puls_{%esm}!q_{%cpi}_t1(4,2)/ex{%esm}!q_t_t1(4,2))/1000
		hu_{%esm}!q(1,4)=@round(1000*puls_{%esm}!q_t1(4,2)/ex{%esm}!q_t_t1(4,2))/1000
			
				hu_{%esm}!q(2,1)=5
				hu_{%esm}!q(2,2)=@round(1000*puls_{%esm}!q_{%ppi}_t5(12,2)/ex{%esm}!q_t_t5(12,2))/1000
				hu_{%esm}!q(2,3)=@round(1000*puls_{%esm}!q_{%cpi}_t5(12,2)/ex{%esm}!q_t_t5(12,2))/1000
				hu_{%esm}!q(2,4)=@round(1000*puls_{%esm}!q_t5(12,2)/ex{%esm}!q_t_t5(12,2))/1000
			
				hu_{%esm}!q(3,1)=10
				hu_{%esm}!q(3,2)=@round(1000*puls_{%esm}!q_{%ppi}_t10(22,2)/ex{%esm}!q_t_t10(22,2))/1000
				hu_{%esm}!q(3,3)=@round(1000*puls_{%esm}!q_{%cpi}_t10(22,2)/ex{%esm}!q_t_t10(22,2))/1000
				hu_{%esm}!q(3,4)=@round(1000*puls_{%esm}!q_t10(22,2)/ex{%esm}!q_t_t10(22,2))/1000
				
				
				hu_{%esm}!q(4,1)=15
				hu_{%esm}!q(4,2)=@round(1000*puls_{%esm}!q_{%ppi}_t15(32,2)/ex{%esm}!q_t_t15(32,2))/1000
				hu_{%esm}!q(4,3)=@round(1000*puls_{%esm}!q_{%cpi}_t15(32,2)/ex{%esm}!q_t_t15(32,2))/1000
				hu_{%esm}!q(4,4)=@round(1000*puls_{%esm}!q_t15(32,2)/ex{%esm}!q_t_t15(32,2))/1000
				
				
				hu_{%esm}!q(5,1)=20
				hu_{%esm}!q(5,2)=@round(1000*puls_{%esm}!q_{%ppi}_t20(42,2)/ex{%esm}!q_t_t20(42,2))/1000
				hu_{%esm}!q(5,3)=@round(1000*puls_{%esm}!q_{%cpi}_t20(42,2)/ex{%esm}!q_t_t20(42,2))/1000
				hu_{%esm}!q(5,4)=@round(1000*puls_{%esm}!q_t20(42,2)/ex{%esm}!q_t_t20(42,2))/1000
			
				freeze(uh_{%esm}!q) hu_{%esm}!q
				uh_{%esm}!q.save(t=csv) F:\ECU RA\2nd\result\{%folder}_{%esm}!q_imp
			next q
next


'Do Cleanin'
delete aux* 
delete ge_*
delete lpat
delete *_na
delete ng
delete y?
delete y??
delete s?
delete s??
delete eq
