' Article explaining Simultaneous Equation Models (SEM) applied to e-Views:
'	 http://davegiles.blogspot.com/2012/05/estimating-simulating-sem.html

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' A. SETTING UP EXOGENOUS AND SYSTEM EQUATIONS

' Open spread-sheet

wfopen "{' PLEASE INSERT HERE THE PATH TO THE FILE}\G4 dataset.xlsx" range=eViews

smpl @first+1 @last

' Setting up the system of equations with AR(1) components:
system _sys_3sls


_sys_3sls.append DE_LR = C(1) + C(2)*DE_GDP + C(3)*DE_INF + C(4)*DE_SR + C(5)*DE_LRS + [AR(1)=c(6)] @ JP_GDP JP_INF JP_SR UK_GDP UK_INF UK_SR US_GDP US_INF US_SR DE_SR(-1) DE_INF(-1) DE_GDP

_sys_3sls.append JP_LR = C(7) +C(8)* JP_GDP +C(9)*JP_INF + C(10)* JP_SR+C(11)* JP_LRS + [AR(1)=c(12)] @ DE_GDP DE_INF DE_SR UK_GDP UK_INF UK_SR US_GDP US_INF US_SR  JP_SR(-1) JP_GDP JP_INF(-1) 

_sys_3sls.append UK_LR =C(13) +C(14)*UK_GDP +C(15)*UK_INF+C(16)*UK_SR +C(17)*UK_LRS + [AR(1)=c(18)] @ DE_GDP DE_INF DE_SR JP_GDP JP_INF JP_SR  UK_GDP  US_GDP US_INF US_SR UK_SR(-1) UK_INF

_sys_3sls.append US_LR = C(19) +C(20)* US_GDP +C(21)*US_INF+C(22)* US_SR +C(23)*US_LRS  +[AR(1)=c(24)] @ DE_GDP DE_INF DE_SR JP_GDP JP_INF JP_SR UK_GDP UK_INF UK_SR US_GDP US_INF US_SR(-1)
 

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' ' A.1 {INPUT VARIABLES IN SCENARIO 1 to 3} ARIMA (p,q) models for endogenour variables
' First, we estimate the ARIMA models
' GERMANY
equation _f_degdp.LS  DE_GDP C AR(1)
equation _f_deinf.LS  DE_INF C DE_INF(-1)
equation _f_delrs.LS(DERIV=AA)  DE_LRS C AR(1) MA(1)
equation _f_desr.LS(DERIV=AA)  DE_SR C AR(1)  AR(2) MA(1)
' JAPAN
equation _f_jpgdp.LS  JP_GDP C AR(1)
equation _f_jpinf.LS  JP_INF C AR(1)
equation _f_jplrs.LS(DERIV=AA)  JP_LRS C AR(1) MA(1)
equation _f_jpsr.LS  JP_SR C AR(1) AR(2) MA(1)
'UK
equation _f_ukgdp.LS  UK_GDP C AR(1)
equation _f_ukinf.LS  UK_INF C AR(1)
equation _f_uklrs.LS(DERIV=AA)  UK_LRS C AR(1) MA(1)
equation _f_uksr.LS  UK_SR C AR(1) AR(2)
'US
equation _f_usgdp.LS  US_GDP C AR(1)
equation _f_usinf.LS  US_INF C AR(1)
equation _f_uslrs.LS  US_LRS C AR(1) AR(2)
equation _f_ussr.LS(DERIV=AA)  US_SR C AR(1) AR(2) MA(1)

' Second, we obtain forecast using model specification estimated above.

pagestruct(end=@last+24) ' The pagestruct command extend the sample size to include the period we want to forecasts, i.e. 24 months.

smpl @last-23 @last+24 

For  !i=1 to 3
_f_degdp.forecast de_gdp_{!i}
_f_deinf.forecast de_inf_{!i}
_f_delrs.forecast de_lrs_{!i}
_f_desr.forecast de_sr_{!i}

_f_jpgdp.forecast jp_gdp_{!i}
_f_jpinf.forecast jp_inf_{!i}
_f_jplrs.forecast jp_lrs_{!i}
_f_jpsr.forecast jp_sr_{!i}

_f_ukgdp.forecast uk_gdp_{!i}
_f_ukinf.forecast uk_inf_{!i}
_f_uklrs.forecast uk_lrs_{!i}
_f_uksr.forecast uk_sr_{!i}

_f_usgdp.forecast us_gdp_{!i}
_f_usinf.forecast us_inf_{!i}
_f_uslrs.forecast us_lrs_{!i}
_f_ussr.forecast us_sr_{!i}
next 

smpl @first+1 @last

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'A.2 {INPUT VARIABLES IN SCENARIO 4} 
' In this scenario, the user can choose to input the forecast of her choice.
' The tab "FcastInput" tab in the Excecl template is prepared for this. Select the variable to be tested (same for all four countries).
'For the other variable, the model takes the latest realised value for all the forecast period.

' Text box to be desplayed when running the Code:
%choice ="SR"
%list ="SR GDP INF LRS"
@uilist(%choice,"Please select the exogenous variable that you want to use for the scenario analysis",%list)
statusline %choice



If %choice = "SR" then
	group gr1 DE_SR  JP_SR UK_SR  US_SR 
	group gr2 DE_LRS DE_GDP  DE_INF  JP_GDP  JP_INF  JP_LRS   UK_GDP  UK_INF  UK_LRS  US_GDP  US_INF  US_LRS
else
If %choice = "GDP" then
	group gr1 DE_GDP  JP_GDP UK_GDP  US_GDP 
	group gr2 DE_LRS DE_INF  JP_INF  JP_LRS   UK_INF  UK_LRS  US_INF  US_LRS DE_SR  JP_SR UK_SR  US_SR 
else
If %choice = "INF" then
	group gr1 DE_INF  JP_INF UK_INF  US_INF 
	group gr2 DE_LRS DE_GDP  JP_GDP  JP_LRS   UK_GDP UK_LRS  US_GDP  US_LRS DE_SR  JP_SR UK_SR  US_SR 
else
If %choice = "LRS" then
	group gr1 DE_LRS JP_LRS UK_LRS  US_LRS
	group gr2 DE_GDP  DE_INF  JP_GDP  JP_INF UK_GDP  UK_INF US_GDP  US_INF  DE_SR  JP_SR UK_SR  US_SR 
Endif
Endif
Endif
Endif

' The following step construct the data series for scenario 4.
' GROUP 1 Variables
smpl @all		
For !i=1 to gr1.@count
	%name=gr1.@seriesname(!i) ' Obtaining the names ("strings") of the endogenous variables

	smpl @first @last-24 ' For the realised observation, scenario 4 and 5 are the same and equal to the realised values. 
	series {%name}_4 =gr1(!i)
	series {%name}_5 ={%name}_4
	
	smpl @last-23 @last+24 ' For the forecast period, the values are imported from Excel, "FcastInput" tab.
	import(mode=u) "{' PLEASE INSERT HERE THE PATH TO THE FILE}\G4 dataset.xlsx" range=FcastInput 	
	series {%name}_5 ={%name}_4
Next

smpl @all

' GROUP 2 Variables
smpl @all		
For !i=1 to gr2.@count
	%name=gr2.@seriesname(!i) ' Obtaining the names ("strings") of the endogenous variables
	
	smpl @first @last-24
	series {%name}_4 =gr2(!i)
	series {%name}_5 ={%name}_4
	
	smpl @last-23 @last+24 ' For the forecast period, we use the last realised value. 
	series {%name}_4 ={%name}_4(-1)
	series {%name}_5 ={%name}_4
Next
smpl @all
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'B. SOLVING THE SYSTEM AND CREATING MODEL.
' Solving the system with Three Stage Least Square:
 _sys_3sls.3sls

' Creating the model :
_sys_3sls.makemodel(_m_3sls)
_sys_3sls.output
freeze(coef_tbl) _sys_3sls.output

' Model statistics (optional)
'_sys_3sls.qstats ' residual autorcorrelation Portmanteau test
'_sys_3sls.cov ' residual covariance matrix
'_sys_3sls.correl(12, graph) ' residuals correlations

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'C. SOLVING THE MODEL

' 1. a FITTED VALUES: Stochastic Valuation with full sample values estimation.
smpl @first+1 @last-24
_m_3sls.scenario(n,a=_0) "Fitted Values" 
_m_3sls.stochastic
_m_3sls.solve(s=s,d=d) ' stochastic and dynamic options

'smpl @all
'_m_3sls.makegraph(s=s, a) grph_lr @endog ' a = include actuals, s= plots stochastics and std. deviation
'show grph_lr


'1.b Fitted Value Forecasts
smpl @first+1 @last+24
_m_3sls.scenario(n,a=_1) "Fitted Values & Forecasts" 
_m_3sls.override DE_LRS  DE_SR  DE_GDP  DE_INF  JP_GDP  JP_INF  JP_LRS  JP_SR  UK_GDP  UK_INF  UK_LRS  UK_SR  US_GDP  US_INF  US_LRS  US_SR ' Replaces the exogenoous variables with the extended variables that include the forecasts as estimated in  A.1 above
_m_3sls.stochastic
_m_3sls.solve(s=s,d=d)  ' stochastic and dynamic options

smpl @last-240 @last ' Charts showing 10 years of data points
'smpl @all 
_m_3sls.scenario "Fitted Values & Forecasts" 'ARMA forecasts
_m_3sls.makegraph(s=s, a) grph_Fitted @endog ' a = include actuals, s= plots stochastics and std. deviation
show grph_Fitted


' 2. OUT-OF-SAMPLE FORECASTS. The exogenous variables used are the ls estimates of the ARMA models computed above.
'2.a Stochastic forecasting 
smpl @last-23 @last+24
_m_3sls.scenario "actuals"
_m_3sls.scenario(n, a=_2) "Out-of-Sample Forecasts - ARMA inputs" ' Stochastic estimations
_m_3sls.override DE_LRS  DE_SR  DE_GDP  DE_INF  JP_GDP  JP_INF  JP_LRS  JP_SR  UK_GDP  UK_INF  UK_LRS  UK_SR  US_GDP  US_INF  US_LRS  US_SR
_m_3sls.solve(s=s,d=d) ' stochastic and dynamic options

smpl @last-240 @last ' Charts showing 10 years of data points
_m_3sls.makegraph(s=s, a) grph_OOS @endog ' a = include actuals, s= plots stochastics and std. deviation
show grph_OOS


'' 2.b Deterministic Forecasts. 
'' Generating override variables for scenario 2
'smpl @last-23 @last+24
'_m_3sls.scenario "actuals"
'_m_3sls.scenario(n,a=_3) "Out-of-Sample Forecasts - Deterministic" 
''_m_3sls.scenario(n, i="Out-of-Sample Forecasts") "Out-of-Sample Forecasts - Deterministic" ' Stochastic estimations
'_m_3sls.override DE_LRS  DE_SR  DE_GDP  DE_INF  JP_GDP  JP_INF  JP_LRS  JP_SR  UK_GDP  UK_INF  UK_LRS  UK_SR  US_GDP  US_INF  US_LRS  US_SR
'_m_3sls.solve(s=d,d=d) ' stochastic and dynamic options
'
'smpl @last-240 @last ' Charts showing 10 years of data points
'_m_3sls.makegraph(a) grph_Det @endog ' a = include actuals, s= plots stochastics and std. deviation
'show grph_Det



'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'3. SCENARIO ANALYSIS: Exogenous variables are the original data. The forecast for the exogenous variables are the last observations and "our" own expected values. 
'3.a. Fitted Values & Scenario Forecasts
smpl @first+1 @last
_m_3sls.scenario "Fitted Values" 
_m_3sls.scenario(n,a=_4)  "Scenario Input: SR" 
_m_3sls.override DE_LRS  DE_SR  DE_GDP  DE_INF  JP_GDP  JP_INF  JP_LRS  JP_SR  UK_GDP  UK_INF  UK_LRS  UK_SR  US_GDP  US_INF  US_LRS  US_SR
_m_3sls.stochastic
_m_3sls.solve(s=s,d=d) ' stochastic and dynamic options

smpl @last-240 @last ' Charts showing 10 years of data points
_m_3sls.makegraph(s=s, a) grph_scen @endog ' a = include actuals, s= plots stochastics and std. deviation
'_m_3sls.makegraph(s=s, a) grph_JP_lrscen jp_lr
show grph_scen

'3.b.
smpl @last-23 @last+24
_m_3sls.scenario "actuals"
_m_3sls.scenario(n, a=_5) "Out-of-Sample Forecasts - Scenario Inputs" ' Stochastic estimations
_m_3sls.override DE_LRS  DE_SR  DE_GDP  DE_INF  JP_GDP  JP_INF  JP_LRS  JP_SR  UK_GDP  UK_INF  UK_LRS  UK_SR  US_GDP  US_INF  US_LRS  US_SR
_m_3sls.solve(s=s,d=d) ' stochastic and dynamic options

smpl @last-240 @last ' Charts showing 10 years of data points
_m_3sls.makegraph(s=s, a) grph_scft @endog ' a = include actuals, s= plots stochastics and std. deviation
show grph_scft

group excel_series *_lr *lr_4m *4s
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'line de_lr_1m de_lr_4m
'line jp_lr_1m jp_lr_4m
'line uk_lr_1m uk_lr_4m
'line us_lr_1m us_lr_4m

'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Half Life

group gr3 de_lr jp_lr uk_lr us_lr 

smpl @all		
For !i=1 to gr3.@count
	%name=gr3.@seriesname(!i) ' Obtaining the names ("strings") of the endogenous variables
	equation ls{%name}.ls(COV=HAC) {%name} c {%name}(-1) 
	scalar LAMBDA_{%name}= c(2) ' Speed of Mean Reversion
	scalar mu_{%name}= c(1)/lambda_{%name} 'Mean
	scalar sigma_{%name}= @stdev(resid) ' Volatility
	scalar half_life__{%name} = log(0.5)/log(abs(lambda_{%name}))
Next


'' Transforming series to levels from logs (logs= 100*LN(1+lr/100), levels = (EXP(lr/100)-1)*100)
'series lev_de_lr_1 = (@exp(de_lr_1m/100)-1)*100
'series lev_jp_lr_1 = (@exp(jp_lr_1m/100)-1)*100


