''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Main program
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
open erp.wf1

'variables
scalar nofvars=8									'Number of "potential" forecasting variables (regressors)
scalar nofmodels=0
rowvector(nofvars) dummy
matrix(2^nofvars, nofvars) models
scalar nofmonths = 360								'Number of months in the out-of-sample period
scalar mwlength = 72                                'Length of moving window

matrix(nofmonths, 2^nofvars) AICvalues				'Matrix with AIC values for all models
matrix(nofmonths, 2^nofvars) BICvalues				'Matrix with BIC values for all models
matrix(nofmonths, 2^nofvars) AdjR2values			'Matrix with adj R2 values for all models
matrix(nofmonths, 2^nofvars) PCSvalues				'Matrix with PCS values for all models
matrix(nofmonths, 2^nofvars) ERPfcsts				'Matrix with one-step ahead out-of-sample forecasts for all models

vector(nofmonths) AICbest							'Vector with AIC values for the best model
vector(nofmonths) BICbest							'Vector with BIC values for the best model
vector(nofmonths) AdjR2best							'Vector with adj R2 values for the best model
vector(nofmonths) PCSbest							'Vector with PCS values for the best model

vector(nofmonths) AICbestmod						'Vector with number of best model according to AIC
vector(nofmonths) BICbestmod						'Vector with number of best model according to BIC
vector(nofmonths) AdjR2bestmod						'Vector with number of best model according to adj R2
vector(nofmonths) PCSbestmod						'Vector with number of best model according to PCS

series ERPfcstAIC	= NA							'Vector with one-step ahead out-of-sample forecasts for the best model according to AIC
series ERPfcstBIC	= NA							'Vector with one-step ahead out-of-sample forecasts for the best model according to BIC
series ERPfcstAdjR2 = NA							'Vector with one-step ahead out-of-sample forecasts for the best model according to adj R2
series ERPfcstPCS = NA								'Vector with one-step ahead out-of-sample forecasts for the best model according to PCS

series var1 = tb3m(-1)			                   	'define regressors that are to be selected
series var2 = yieldspr(-1)						
series var3 = defspr(-1)
series var4 = credspr(-1)
series var5 = empl(-2)
series var6 = ip(-2)
series var7 = m1(-2)
series var8 = ppif(-2)

'"""""""""""""""""""""""""""""""""""""""""""""""""""""
call Buildmodels(dummy, 0, nofvars)					'Build a matrix indicating which variables are to be included

for !month = 1 to nofmonths							'Loop over out-of-sample period
															
	scalar bestAIC = 1000							'Scalars to keep track of the best model so far in the recursion
	scalar bestBIC = 1000
	scalar bestAdjR2 = 0
	scalar bestPCS = 0

	scalar bestmodAIC = 1
	scalar bestmodBIC = 1
	scalar bestmodAdjR2 = 1
	scalar bestmodPCS = 1

	for !model = 1 to nofmodels						'Loop over models

		'Add selected variables to the regression
		rowvector(nofvars) selmod = @rowextract(models, !model)	

		for !i = 1 to nofvars	
			if selmod(!i) <> 0	then
			%regression = %regression + "var"+@str(!i) + " "
		endif
		next

		smpl @first+!month-1 @first+mwlength+!month-2			'Set sample to moving window
		equation temp.ls ERP C {%regression}
		temp.fit(f=na) ERPf

		AICvalues(!month,!model)=temp.@aic
		if temp.@aic < bestAIC then							
			bestAIC = temp.@aic
			bestmodAIC = !model
		endif
		BICvalues(!month,!model)=temp.@schwarz
		if temp.@schwarz < bestBIC then
			bestBIC = temp.@schwarz
			bestmodBIC = !model
		endif
		AdjR2values(!month,!model)=temp.@rbar2
		if temp.@rbar2 > bestAdjR2 then
			bestAdjR2 = temp.@rbar2
			bestmodAdjR2 = !model
		endif
		
		'Compute the in-sample PCS
		series corrsign=(ERPf*ERP>0)
		PCSvalues(!month,!model)=@MEAN(corrsign)
		if @MEAN(corrsign) > bestPCS then								
			bestPCS = @MEAN(corrsign)
			bestmodPCS = !model
		endif
		
		smpl @first+mwlength+!month-1 @first+mwlength+!month-1	'Set sample to next month for forecasting
		temp.fit ERPf
		ERPfcsts(!month,!model)=ERPf(mwlength+!month)

		%regression = ""

	next

	'Save the values of the selection criteria for the selected models
	AICbest(!month) = bestAIC
	BICbest(!month) = bestBIC
	AdjR2best(!month) = bestAdjR2
	PCSbest(!month) = bestPCS

	'Save the selected models according to the selection criteria
	AICbestmod(!month) = bestmodAIC
	BICbestmod(!month) = bestmodBIC
	AdjR2bestmod(!month) = bestmodAdjR2
	PCSbestmod(!month) = bestmodPCS

	'Save the forecasts for the selected models
	ERPfcstAIC(mwlength+!month)=ERPfcsts(!month,bestmodAIC)
	ERPfcstBIC(mwlength+!month) = ERPfcsts(!month,bestmodBIC)
	ERPfcstAdjR2(mwlength+!month) = ERPfcsts(!month,bestmodAdjR2)
	ERPfcstPCS(mwlength+!month) = ERPfcsts(!month,bestmodPCS)
next

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Subroutines
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
subroutine Buildmodels(rowvector model, scalar location, scalar sizeT)

	'check whether you're at the end of the tree, if not go either up (1) or down (0)
	if location < sizeT then											
		'first go up
		model(location+1) = location + 1								
		call Buildmodels(model, location+1, sizeT)
		
		'then go down
		model(location+1) = 0						
		call Buildmodels(model, location+1,sizeT)
	else
		'add a model to the matrix
		nofmodels = nofmodels + 1		
		rowplace(models, model, nofmodels) 
	endif

endsub
