'run rolling 

'check that an object exists
%type = @getthistype
if %type="NONE" then
	@uiprompt("No object found, please open an equation object")
	stop
endif

'check that _this object is an equation
if %type<>"EQUATION" then
	@uiprompt("Procedure can only be run from an equation object")
	stop
endif

'Rolling specification
%cmnd = _this.@command
%fore_end = "2011q1"    'latest available data
%est_end = "2004q4"     'estimation end
%equation = _this.@name    'equation name
%variable = ""         'forecasted variable
%obs_adj = "0"         'starting obs adjustment

!doGUI = 1     	    'show dialogs

if !doGUI = 1 then
	!result = @uidialog("edit",%fore_end,"Latest available data","edit",%est_end, "Estimation end","edit",%equation ,"Equation name","edit",%variable ,"Forecasted Variable","edit",%obs_adj,"Observation adjustment")',"colbreak","text","Storage options","check",!StoreCoefs,"Store Coefficients","check",!StoreSEs,"Store Std. Errors","check",!StorePs,"Store P-values","check",!StoreR2s,"Store R-squareds","check",!doGraphs,"Create Graphs","caption","Rolling Specification and Storage Options")
	if !result = -1 then
		stop
	endif
endif

'Generate percent change series for actuals
smpl @all
genr dlog_{%variable}=dlog({%variable})

'Set forecast sample
smpl {%est_end}+1 {%fore_end}
!fore = @obs({%variable})

'Set obs sample
smpl @first {%est_end}
!obs = @obs({%variable})

'*************  Fixing sart of sample adjustment  ***************
!obs_fix=@val(%obs_adj)

'Results table
table bt_{%equation}


'Creating results table
table errors
errors(1,1) = {%equation}.@varlist
errors(2,1) = "--In Sample--"
errors(3,1) = "RMSE"
errors(4,1) = "MAE"
errors(5,1) = "MAPE"
errors(6,1) = "ME"
errors(7,1) = "MPE"
errors(8,1) = "--Out Of Sample--"
errors(9,1) = "RMSE"
errors(10,1) = "MAE"
errors(11,1) = "MAPE"
errors(12,1) = "ME"
errors(13,1) = "MPE"
errors(14,1) = "====="
errors(15,1) = "Obs"
errors(16,1) = "Forecasts"

'--------------------------------------------------------------IN SAMPLE--------------------------------------------------------------------

'Estimate equation
smpl @first {%est_end}
_this.{%cmnd}
fit(d) {%variable}f

'Initializing variables that holds the errors
!rmse = 0
!mae = 0
!mape = 0
!me = 0
!mpe = 0

for !i= 1 to !obs-!obs_fix 
	bt_{%equation}(!i+!obs_fix,1) = {%variable}f(!i+!obs_fix)
next

'Adding up errors
for !i=1 to !obs-!obs_fix
	!rmse = !rmse + (bt_{%equation}(!i+!obs_fix,1) - dlog_{%variable}(!i+!obs_fix))^2
	!mae = !mae + @abs(bt_{%equation}(!i+!obs_fix,1) - dlog_{%variable}(!i+!obs_fix))
	!mape = !mape + @abs((bt_{%equation}(!i+!obs_fix,1) - dlog_{%variable}(!i+!obs_fix))/dlog_{%variable}(!i+!obs_fix))
	!me = !me + (bt_{%equation}(!i+!obs_fix,1) -dlog_{%variable}(!i+!obs_fix))
	!mpe = !mpe + (bt_{%equation}(!i+!obs_fix,1) - dlog_{%variable}(!i+!obs_fix))/dlog_{%variable}(!i+!obs_fix)
next
	
'Calcualting average errors
!rmse = @sqrt(!rmse/(!obs-!obs_fix))
!mae = !mae/(!obs-!obs_fix)
!mape = !mape/(!obs-!obs_fix)
!me = !me/(!obs-!obs_fix)
!mpe = !mpe/(!obs-!obs_fix)
errors(3,2) = !rmse
errors(4,2) = !mae
errors(5,2) = !mape
errors(6,2) = !me
errors(7,2) = !mpe

'Delete table
d bt_{%equation}

'--------------------------------------------------------------OUT OF SAMPLE--------------------------------------------------------------------

'Results table
table bt_{%equation}

'Forecasting out of sample
for !i=1 to !fore
	smpl @first {%est_end}+!i-1
	_this.{%cmnd}
	smpl {%est_end}+!i  {%est_end}+!i
	{%equation}.forecast(e,d) {%variable}f
	bt_{%equation}(!i,1) = {%variable}f(!obs+!i)
next

'Initializing variables that holds the errors
!rmse = 0
!mae = 0
!mape = 0
!me = 0
!mpe = 0

'Adding up errors
for !i=1 to !fore
	!rmse = !rmse + (bt_{%equation}(!i,1) - dlog_{%variable}(!obs+!i))^2
	!mae = !mae + @abs(bt_{%equation}(!i,1) - dlog_{%variable}(!obs+!i))
	!mape = !mape + @abs((bt_{%equation}(!i,1) - dlog_{%variable}(!obs+!i))/dlog_{%variable}(!obs+!i))
	!me = !me + (bt_{%equation}(!i,1) -dlog_{%variable}(!obs+!i))
	!mpe = !mpe + (bt_{%equation}(!i,1) - dlog_{%variable}(!obs+!i))/dlog_{%variable}(!obs+!i)
next

'Calcualting average errors
!rmse = @sqrt(!rmse/!fore)
!mae = !mae/(!fore)
!mape = !mape/(!fore)
!me = !me/(!fore)
!mpe = !mpe/(!fore)
errors(9,2) = !rmse
errors(10,2) = !mae
errors(11,2) = !mape
errors(12,2) = !me
errors(13,2) = !mpe
errors(15,2) = !obs-!obs_fix 
errors(16,2) = !fore
errors.setjust(@all) left
errors.setformat(B15:B16) f.0
show errors

'Delete table
d bt_{%equation}
smpl @all
_this.{%cmnd}
close {%equation}
