*
* @GNewbold actual f1 f2 start end
* Granger-Newbold forecast comparison test
*
* Parameters:
*   actual     series of actual data
*   f1         first set of forecasts
*   f2         second set of forecasts
*   start end  range to analyze (common range of f1, f2 and actual by default)
*
* Options:
*   TITLE=title for report ["Granger-Newbold Forecast Comparison Test"]
*   [PRINT]/NOPRINT
*
* Reference:
*   Granger and Newbold(1973), "Some Comments on the Evaluation of Economic
*   Forecasts", Applied Economics, vol. 5, pp. 35-47.
*
* Variables Defined:
*   %CDSTAT  Test statistic for f1=f2 vs f2 better than f1. Flipping the
*      sign gives the test statistic for f2=f1 vs f1 better than f2.
*
* Revision Schedule:
*   01/2004 Written by Tom Doan, Estima
*   06/2010 Modernized code, added TITLE and PRINT options
*
procedure GNewbold actual f1 f2 start end
type   series  actual f1 f2
type   integer start  end
*
option string  title
option switch  print     1
*
local integer	startl endl
local series 	x z
local real 		gnstat sign12 sign21
local string 	stitle
local report   greport
*
*  Figure out the range if not supplied
*
inquire(reglist) startl<<start endl<<end
# actual f1 f2
*
* Compute the difference and the sum of the forecast errors
*
set x startl endl = f2-f1
set z startl endl = 2*actual-f1-f2
cmom(nocenter,corrs)
# x z
compute gnstat=sqrt((%nobs-1)/(1-%cmom(1,2)^2))*%cmom(1,2)
compute sign12=.5*%ttest(gnstat,%nobs-1)
if gnstat>0.0
   compute sign12=1-sign12
compute sign21=1-sign12
compute %cdstat=gnstat

if .not.print
   return

if %defined(title)
   compute stitle=title
else
   compute stitle="Granger-Newbold Forecast Comparison Test"

report(use=greport,action=define,title=stitle)
report(use=greport,atrow=1,atcol=1,span) stitle
report(use=greport,atrow=2,atcol=1,span) $
   "Forecasts of "+%l(actual)+" over "+%datelabel(startl)+" to "+%datelabel(endl)
*
report(use=greport,atrow=4,atcol=1,align=center) "Forecast" "Test Stat" "P(GN>x)"
report(use=greport,atrow=5,atcol=1) %l(f1)	  %cdstat   sign21
report(use=greport,atrow=6,atcol=1) %l(f2)     -%cdstat  sign12
report(use=greport,action=format,picture="*.####",align=decimal,atcol=2,tocol=2,atrow=5)
report(use=greport,action=format,picture="#.#####",atcol=3,tocol=3)
report(use=greport,action=show)
end

