' Program that generates tables with decomposition of Fama's beta (FOR TABLES 2 AND 3 IN THE ARTICLE).
' Independent variables are FD, OF and ExpError.

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

' *****************************************
' Routine for creating horizon-dependent flow
subroutine createflows(string %Flow, string %Vol)
pageselect data

' Currencies
for !j = 1 to 3

' Create macro-variable to identify currency
%Ccy = names(1,!j)

' Forward OF
'series temp_of{%Ccy}01 = @movsum((tmpof{%Flow}{%Ccy}(1)/@stdev(tmpof{%Flow}{%Ccy})),1)*({%Vol}{%Ccy}01^2/@mean({%Vol}{%Ccy}01^2))/100
'series temp_of{%Ccy}03 = @movsum((tmpof{%Flow}{%Ccy}(3)/@stdev(tmpof{%Flow}{%Ccy})),3)*({%Vol}{%Ccy}03^2/@mean({%Vol}{%Ccy}03^2))/100
'series temp_of{%Ccy}06 = @movsum((tmpof{%Flow}{%Ccy}(6)/@stdev(tmpof{%Flow}{%Ccy})),6)*({%Vol}{%Ccy}06^2/@mean({%Vol}{%Ccy}06^2))/100
'series temp_of{%Ccy}12 = @movsum((tmpof{%Flow}{%Ccy}(12)/@stdev(tmpof{%Flow}{%Ccy})),12)*({%Vol}{%Ccy}12^2/@mean({%Vol}{%Ccy}12^2))/100

' Lag OF (org formulation)
series temp_of{%Ccy}01 = @movsum((tmpof{%Flow}{%Ccy}/@stdev(tmpof{%Flow}{%Ccy})),1)*({%Vol}{%Ccy}01(-1)^2/@mean({%Vol}{%Ccy}01^2))/100
series temp_of{%Ccy}03 = @movsum((tmpof{%Flow}{%Ccy}/@stdev(tmpof{%Flow}{%Ccy})),3)*({%Vol}{%Ccy}03(-3)^2/@mean({%Vol}{%Ccy}03^2))/100
series temp_of{%Ccy}06 = @movsum((tmpof{%Flow}{%Ccy}/@stdev(tmpof{%Flow}{%Ccy})),6)*({%Vol}{%Ccy}06(-6)^2/@mean({%Vol}{%Ccy}06^2))/100
series temp_of{%Ccy}12 = @movsum((tmpof{%Flow}{%Ccy}/@stdev(tmpof{%Flow}{%Ccy})),12)*({%Vol}{%Ccy}12(-12)^2/@mean({%Vol}{%Ccy}12^2))/100

' No vol-adjustment
'series temp_of{%Ccy}01 = @movsum((of{%Flow}{%Ccy}/@stdev(of{%Flow}{%Ccy})),1)
'series temp_of{%Ccy}03 = @movsum((of{%Flow}{%Ccy}/@stdev(of{%Flow}{%Ccy})),3)
'series temp_of{%Ccy}06 = @movsum((of{%Flow}{%Ccy}/@stdev(of{%Flow}{%Ccy})),6)
'series temp_of{%Ccy}12 = @movsum((of{%Flow}{%Ccy}/@stdev(of{%Flow}{%Ccy})),12)

next
endsub
' ****************************************

' ------------------------------------------------------------------------------------------
' A. Define routines
subroutine systabledecomp(string %TNum, string %Var, string %Flow)
pageselect data

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

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"


' Currencies
for !i = 1 to 3

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

system tempsys{%Var}{%Ccy}{%Htext}

' Definition of system
tempsys{%Var}{%Ccy}{%Htext}.append {%Var}{%Ccy}{%Htext} = (1+c(1)+c(2))*tmpfd{%Ccy}{%Htext} + c(11) @ tmpfd{%Ccy}{%Htext} 'temp_of{%Ccy}{%Htext}(-L)
tempsys{%Var}{%Ccy}{%Htext}.append temp_of{%Ccy}{%Htext} = c(1)*tmpfd{%Ccy}{%Htext} + c(21) @ tmpfd{%Ccy}{%Htext}  'temp_of{%Ccy}{%Htext}(-L)
tempsys{%Var}{%Ccy}{%Htext}.append tmpexpe{%Ccy}{%Htext} = c(2)*tmpfd{%Ccy}{%Htext} + c(31) @ tmpfd{%Ccy}{%Htext}  'temp_of{%Ccy}{%Htext}(-L)

' Estimation
tempsys{%Var}{%Ccy}{%Htext}.gmm(b=a) 'Andrews

' Defines starting-row in table. So when we run the procedure over again we can select
' where to start in the table so that we don't overwrite results
'!i = r

' J-stat
' Degrees of freedom are: Number of orthogonality restrictions minus number of parameters
scalar temp_overid = tempsys{%Var}{%Ccy}{%Htext}.@jstat
scalar temp_overidp=1-@cchisq(tempsys{%Var}{%Ccy}{%Htext}.@jstat,1)


' Wald-test:
' 1) Test difference from Fama-coeff:
'!f = m_fama(!j,!i)
'freeze(mode=overwrite,tempW) tempsys{%Var}{%Ccy}{%Htext}.wald c(1)+c(2)=!f-1
' 2) Test difference from UIP:
freeze(mode=overwrite,tempW) tempsys{%Var}{%Ccy}{%Htext}.wald c(1)+c(2)=0
' 3) Test difference from 0:
'freeze(mode=overwrite,tempW) tempsys{%Var}{%Ccy}{%Htext}.wald c(1)+c(2)=-1
%pwald = tempw(6,4)

		' Tab-index is row, column
		tab{%TNum}_Sys_{%Var}{%Flow}(1,3*!i-1) = @upper(%Ccyname)		' Column-text: EUR etc.
		tab{%TNum}_Sys_{%Var}{%Flow}(2,3*!i-1) = "Beta"					' Column-text 
		tab{%TNum}_Sys_{%Var}{%Flow}(2,3*!i+0) = "OF"					' Column-text 
		tab{%TNum}_Sys_{%Var}{%Flow}(2,3*!i+1) = "ExpE"					' Column-text
		tab{%TNum}_Sys_{%Var}{%Flow}(2*!j+1,1) = @str(L)+" Month"			' Row-text: 1M etc.
		tab{%TNum}_Sys_{%Var}{%Flow}(2*!j+1,3*!i-1) = 1+tempsys{%Var}{%Ccy}{%Htext}.@coefs(1)+tempsys{%Var}{%Ccy}{%Htext}.@coefs(2)		' Implied beta
		tab{%TNum}_Sys_{%Var}{%Flow}(2*!j+2,3*!i-1) = %pwald								' beta test
		tab{%TNum}_Sys_{%Var}{%Flow}(2*!j+1,3*!i+0) = tempsys{%Var}{%Ccy}{%Htext}.@coefs(1)	' Coeff
		tab{%TNum}_Sys_{%Var}{%Flow}(2*!j+2,3*!i+0) = @str(tempsys{%Var}{%Ccy}{%Htext}.@tstats(1),"f.2")	' t-stat
		tab{%TNum}_Sys_{%Var}{%Flow}(2*!j+1,3*!i+1) = tempsys{%Var}{%Ccy}{%Htext}.@coefs(2)	' Coeff
		tab{%TNum}_Sys_{%Var}{%Flow}(2*!j+2,3*!i+1) = @str(tempsys{%Var}{%Ccy}{%Htext}.@tstats(2),"f.2")	' t-stat
		
tempsys{%Var}{%Ccy}{%Htext}.makeresids tempres1 tempres2 tempres3
'1) 
tempres1.uroot(pp,save=tempur1)	'select 4
tempres2.uroot(pp,save=tempur2)
tempres3.uroot(pp,save=tempur3)
'2) KPSS
tempres1.uroot(kpss,save=tempur1b)	'select 3
tempres2.uroot(kpss,save=tempur2b)
tempres3.uroot(kpss,save=tempur3b)

		' Tab-index is row, column
		tab{%TNum}_Sysstats_{%Var}{%Flow}(1,4*!i-2) = @upper(%Ccyname)			' Column-text: EUR etc.
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2,4*!i-2) = "J-stats"					' Column-text 
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2,4*!i-1) = "UIP"						' Column-text 
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2,4*!i+0) = "OF"						' Column-text 
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2,4*!i+1) = "ExpE"					' Column-text
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+1,1) = @str(L)+" Month, J/R2"	' Row-text: 1M etc.
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+2,1) = "J-p/UR (p)"				' Row-text: 1M etc.
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+1,4*!i-2) = temp_overid								' J-stat
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+2,4*!i-2) = temp_overidp								' J-stat, p
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+1,4*!i-1) = @str(tempsys{%Var}{%Ccy}{%Htext}.@rbar2(1),"f.2")	' R2 UIP
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+2,4*!i-1) = @str(tempur1b(3),"g.3")+"/"+@str(tempur1(3),"g.2")	' UR-test UIP
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+1,4*!i+0) = @str(tempsys{%Var}{%Ccy}{%Htext}.@rbar2(2),"f.2")	' R2 flow
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+2,4*!i+0) = @str(tempur2b(3),"g.3")+"/"+@str(tempur2(3),"g.2")	' UR-test flow
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+1,4*!i+1) = @str(tempsys{%Var}{%Ccy}{%Htext}.@rbar2(3),"f.2")	' R2 exp.error
		tab{%TNum}_Sysstats_{%Var}{%Flow}(2*!j+2,4*!i+1) = @str(tempur3b(3),"g.3")+"/"+@str(tempur3(3),"g.2")	' UR-test experror
		delete tempres* tempur*	temp_overid*

		next  ' End maturities
next	' End currencies

' Copy tables to table-sheet
' I have commented out the showing of the tables because it creates too many open windows.
copy(overwrite) Data\tab{%TNum}_Sys* Tables\
delete tab{%TNum}_Sys* tempres* tempur* 'tempsys*
pageselect tables

pageselect data
endsub

' ---- Overall routine
subroutine Datafile(string %file, string %TNum, string %Flow, string %Vol)
' Open data-file
' Use relative path (relative to top-directory of UIP-project)
wfopen monthly\workfiles\month-dataiii_{%file}

smpl @all
call createflows(%Flow,%Vol)

smpl @first 2007m04

call systabledecomp(%TNum,"tmpRR",%Flow)

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

'-----------------------------------------------------------------------------------------------------
' B. Call routines
' Arguments
'-----------------------------------------------------------------------------------------------------
'string %file      File "Exnew6" (alternatively: "OrgOld", "FBOld", "Exold", "Exnew", "5new", "5old"
'string %Tnum      Table-num, typically 6 and 8 with some additions to distinguish: Here vol-choice
'string %Flow	   "n" or "v"
'string %Vol	   "iv" or "sd"
' ----------------------------------------------------------------------------------------------------

call Datafile("Exnew","_2dr","n","iv")
