' Program: Reg-ProgIII_FD.prg (FOR TABLE 1 IN THE ARTICLE).
' Program that generates tables where FD is the independent	variable.
' Single equation procedures.

' Set path
' Program is supposed to reside in top-directory of UIP-project
%path = @runpath
cd %path

' MAIN ROUTINE:
' Arguments: End of file-name (%file), Table-name (%TNum) and Dependent variable (%Var)
subroutine regproc(string %file, string %TNum, string %Var)
' Open data-file
' Use relative path (relative to top-directory of UIP-project)
wfopen monthly\workfiles\month-dataiii_{%file}
pageselect data
smpl @first 2007m04

' Creates two tables to hold results. One for coeffs, and for R2 and unit-root tests.
table tab{%TNum}_regFD_{%Var}
table tab{%TNum}_statFD_{%Var}

tab{%TNum}_regFD_{%Var}(1,1) = "Currency"
tab{%TNum}_regFD_{%Var}(1,2) = "Horizon"
tab{%TNum}_statFD_{%Var}(1,1) = "Currency"
tab{%TNum}_statFD_{%Var}(1,2) = "Stat"
tab{%TNum}_statFD_{%Var}(1,3) = "Horizon"

table names
names(1,1) = "EUR"
names(1,2) = "JPY"
names(1,3) = "GBP"
names(2,1) = "USD/EUR"
names(2,2) = "USD/JPY"
names(2,3) = "USD/GBP"
names(3,1) = "01"
names(3,2) = "03"
names(3,3) = "06"
names(3,3) = "12"

' Declare equation object
'equation tempeq

' Currencies
for !i = 1 to 3

	' Create macro-variable to identify currency
    %Ccy = names(1,!i)
	%Ccyname = names(2,!i)
		
		' Maturities
		for !j = 1 to 4
		' Identify maturity	 H=horizon
		%Htext = names(3,!j)
		scalar L = @val(%Htext)

		equation tempeq{%Ccy}{%Htext}

		' Specification of regression
		' Edit this line for estimation method
		tempeq{%Ccy}{%Htext}.gmm(b=a) {%Var}{%Ccy}{%Htext} c fd{%Ccy}{%Htext} @ c fd{%Ccy}{%Htext}

		' Store Fama-coeffs in matrix.
		' j (horizon) is rows, i (currency) is columns
		if %Var = "RR" then
			M_fama(!j,!i) = tempeq{%Ccy}{%Htext}.@coefs(2)
		endif

		' Regression results:
		' Tab-index is row, column
		tab{%TNum}_regFD_{%Var}(2,!j+1) = @str(L)+" Month"			' Column-text: 1M etc.
		tab{%TNum}_regFD_{%Var}(2*!i+1,1) = @upper(%Ccyname)		' Row-text: EUR etc.
		tab{%TNum}_regFD_{%Var}(2*!i+1,!j+1) = tempeq{%Ccy}{%Htext}.@coefs(2)		' Coeff on var: e.g. beta_fd
		tab{%TNum}_regFD_{%Var}(2*!i+2,!j+1) = tempeq{%Ccy}{%Htext}.@tstats(2)	' t-stat below coeff.

' Creates residuals for testing for unit-root
tempeq{%Ccy}{%Htext}.makeresid tempres
tempres.uroot(kpss,save=tempurb)	'select 3
tempres.uroot(adf,save=tempur)		'select 4

' Wald-test:
' Test difference from UIP:
freeze(mode=overwrite,tempW) tempeq{%Ccy}{%Htext}.wald c(2)=1
%pwald = tempw(6,4)

		' R2 and unit-root tests
		tab{%TNum}_statFD_{%Var}(2,!j+2) = @str(L)+" Month"			' Column-text: 1M etc.
		tab{%TNum}_statFD_{%Var}(3*!i,1) = @upper(%Ccyname)		' Row-text: EUR etc.
		tab{%TNum}_statFD_{%Var}(3*!i,2) = "adj.R2"				' Row-label
		tab{%TNum}_statFD_{%Var}(3*!i,!j+2) = @str(tempeq{%Ccy}{%Htext}.@rbar2,"f.2")	' R2
		tab{%TNum}_statFD_{%Var}(3*!i+1,2) = "UR, p-value"			' Row-label
		tab{%TNum}_statFD_{%Var}(3*!i+1,!j+2) = @str(tempurb(3),"g.3")+"/"+@str(tempur(4),"f.2")			' Unit-root test
		tab{%TNum}_statFD_{%Var}(3*!i+2,2) = "UIP-test"				' Row-label
		tab{%TNum}_statFD_{%Var}(3*!i+2,!j+2) = %pwald				' Test of diff from 1

		next  ' End maturities
next	' End currencies

' Copy tables to Table-page, show them, and clean up temporary variables
copy(overwrite) Data\tab{%TNum}_regFD_{%Var} Tables\
copy(overwrite) Data\tab{%TNum}_statFD_{%Var} Tables\
delete tab{%TNum}_regFD_{%Var} tab{%TNum}_statFD_{%Var}
pageselect tables
pageselect data

smpl @all
wfsave monthly\workfiles\month-dataiii_{%file}
close month-dataiii_{%file}
endsub

' !!!!!!!!!!  EXECUTION !!!!!!!!!!
' Notice that sample ends in april and is set at top
'smpl @first 2007m04

' 2) Call procedure! First parameter is selecting file for estimation.

' Create USD-base
subroutine create_USD_base
for %ccy eur gbp jpy
	for %hor 01 03 06 12
		for %type RR RE
			series tmp{%type}{%ccy}{%hor} = {%type}{%ccy}{%hor}
			if %ccy = "eur" or %ccy= "gbp" then
				tmp{%type}{%ccy}{%hor} = -{%type}{%ccy}{%hor}
			endif
		next
	next
next
endsub

' Already done
'call create_USD_base

' Realized return (RR) (for Table 1)
	call regproc("Exnew","_1a","tmpRR")
' Expected return (RE) (for Table 1)
	call regproc("Exnew","_1b","tmpRE")
' Expectational errors (ExpE)
'call regproc("8","ExpE")
