' Program: Tab-descstat.prg (FOR TABLE WITH DESCRIPTIVE STATISTICS IN THE ARTCILE'S APPENDIX).
' Program that generates table with descriptive statistics for rates, flows and forecasts.


subroutine DescStat(string %var, string %type, string %cont, scalar !qlags, string %Ressheet, string %Datasheet)

' 0. TABLE
	' Open the page "Results"
	pageselect {%Ressheet}
	
	' Delete previous table if it exists
	if @isobject("tab_DescStat") and %cont = "n" then
		delete tab_DescStat
	endif
	
	%Qtext = "Q("+@str(!qlags)+")"
	
	' Create table for holding descriptive statistics for returns
	table tab_DescStat
	
	' Some table headings
	'tab_DescStat(1,1) = "Currency"
	tab_DescStat(1,2) = "Mean"
	tab_DescStat(1,3) = "$\sigma$"
	tab_DescStat(1,4) = "$\rho(1)$"
	tab_DescStat(1,5) = "Skew."
	tab_DescStat(1,6) = "Kurt."
	tab_DescStat(1,7) = "p90"
	tab_DescStat(1,8) = "p10"
	tab_DescStat(1,9) = "Obs."
	tab_DescStat(1,10) = %Qtext
	tab_DescStat(1,11) = "p Q-stat"

' Control where to start in the table
if %cont = "n" then
	!r=2
endif
	!c=1

pageselect {%Datasheet}

series temp
if %type = "r" then
	if @pagefreq = "M" then
		!freq= 12
		else
		if @pagefreq = "D5" then
		!freq = 250
		else
		!freq = 1
		endif
	endif
temp = dlog({%var})*!freq*100
else
if %type = "e" then
	if @pagefreq = "M" then
		!freq= 12
		else
		if @pagefreq = "D5" then
		!freq = 250
		else
		!freq = 1
		endif
	endif
temp = {%var}*!freq*100
else
if %type = "of" then
	temp = {%var}/@stdev({%var})
	else
	temp = {%var}
	endif
endif
endif

	' Define control variables containing statistics
	!mean  = @mean(temp)
	!stdev = @stdev(temp)
	!scorr = @cor(temp,temp(-1))
	!kurto = @kurt(temp)
	!skew  = @skew(temp)
	!max   = @quantile(temp,.90)
	!min   = @quantile(temp,.10)
	!obs   = @obs(temp)
'		%Qtmp = @getnextname("_tmp")
'		freeze({%Qtmp}) temp.correl(12,d=0)
	!qstat = 0' @val({%Qtmp}(6+!qlags,6))
	!pqstat = 0' @val({%Qtmp}(6+!qlags,7))
		delete {%Qtmp}
	' Delete temporary variable
	delete temp
	pageselect {%Ressheet}
	' Fill in table holding descriptive statistics for returns
	tab_DescStat(!r,!c)   = @upper(%var)
	tab_DescStat(!r,!c+1) = @str(!mean,"f.2")
	tab_DescStat(!r,!c+2) = @str(!stdev,"f.2")
	tab_DescStat(!r,!c+3) = @str(!scorr,"f.2")
	tab_DescStat(!r,!c+4) = @str(!skew,"f.2")
	tab_DescStat(!r,!c+5) = @str(!kurto,"f.2")
	tab_DescStat(!r,!c+6) = @str(!max,"f.2")
	tab_DescStat(!r,!c+7) = @str(!min,"f.2")
	tab_DescStat(!r,!c+8) = @str(!obs,"f.0")
	tab_DescStat(!r,!c+9) = @str(!qstat,"f.2")
	tab_DescStat(!r,!c+10) = @str(!pqstat,"f.2")
	' Update control variable for use in table
	!r=!r+1

' End of subroutine
endsub

' subroutine DescStat(string %var, string %type, string %cont, string %Ressheet, string %Datasheet)

pageselect data
smpl @first 2007m04

!qlags = 1

'call DescStat("seur","r","n",!qlags,"Tables","Data")
'for %ccy jpy gbp
'	call DescStat("s"+%ccy,"r","y",!qlags,"Tables","Data")
'next
call DescStat("tmprreur01(-1)","e","n",!qlags,"Tables","Data")
for %ccy jpy gbp
	call DescStat("tmprr"+%ccy+"01(-1)","e","y",!qlags,"Tables","Data")
next
for %ccy eur jpy gbp
	call DescStat("tmpofn"+%ccy,"of","y",!qlags,"Tables","Data")
next
for %ccy eur jpy gbp
	call DescStat("temp_of"+%ccy+"01*100","i","y",!qlags,"Tables","Data")
next
for %ccy eur jpy gbp
	for %h 01 03 06 12
		call DescStat("iusd"+%h+"-i"+%ccy+%h,"i","y",!qlags,"Tables","Data")
'		call DescStat("fd"+%ccy+%h,"i","y","Tables","Data")
	next
next
for %ccy eur jpy gbp
	for %h 01 03 06 12
		call DescStat("iv"+%ccy+%h,"i","y",!qlags,"Tables","Data")
	next
next
for %ccy eur jpy gbp
	for %h 01 03 06 12
		call DescStat("sd"+%ccy+%h,"i","y",!qlags,"Tables","Data")
	next
next
for %ccy eur jpy gbp
	for %h 01 03 06 12
		call DescStat("tmpexpe"+%ccy+%h,"e","y",!qlags,"Tables","Data")
	next
next


