
close EMPlRICAL PROJECT_NWABISA.wf1 ' If such a workfile is open, it will be closed. (Useful if you wish to run this code multiple times and would like to avoid having opening multiple workfiles with the same name.)

wfcreate(wf=EMPIRICAL PROJECT_NWABISA, page = Quarterly) q 1980 2025

'  ************** Reading data and setting some parameters ************** 
read(b6,s=Sheet1) "C:\Users\P519987\Desktop\CORVINUS\RESEARCH\RESEARCH PAPER_OUTPUT GAP\DATA AND RESULTS\QGDP_HPF.xls" 7

'SA = South Africa
'CHI= China
'BRA=Brazil

'************generate log of GDP**************
genr L_QGDP_SA = log(QGDP_SA)
genr L_QGDP_CHI = log(QGDP_CHI)
genr L_QGDP_BRA= log(QGDP_BRA)


%first_available_observation = L_QGDP_SA.@first  ' this is a string, its value will be 1980Q1 in the case of QGDP_SA
%first_available_observation = L_QGDP_CHI.@first 
%first_available_observation = L_QGDP_BRA.@first 


%last_available_observation = L_QGDP_SA.@last  ' this is a string, its value will be 2020Q4
%last_available_observation = L_QGDP_CHI.@last  ' this is a string, its value will be 2020Q4
%last_available_observation = L_QGDP_BRA.@last  ' this is a string, its value will be 2020Q4

smpl %first_available_observation %last_available_observation



'----------------------------------------------------UNIVARIATE HP FILTER------------------------------------------------------------

hpf(lamba=1600) L_QGDP_SA L_QGDP_SA_hp @L_QGDP_SA_gap
'-------------------------------------------------------------------------------------------------------------------------------------------------------------------
'MULTIVARIATE HP FILTER - using South African data only
'Extend the model with Output-inflation relationship / augmented Philips-Curve
'------------------------------------------------------------------------------------------------------------------------------------------------------------------
'STATE-SPACE MODEL
'------------------------------------------------------------------------------------------------------------------------------------------------------------------
'HP filter smoothing parameter
scalar lambda=1600

'Create the sspace object
sspace hp_mod_SA

'State equation (trend)
hp_mod_SA.append@state trend11=c(1) + trend11(-1)+eta_trend11
hp_mod_SA.append@ename eta_trend11
hp_mod_SA.append@evar var(eta_trend11)=0

'State equation (gap)
hp_mod_SA.append@state gap11= c(1)*gap11(-1)+eta_gap11
hp_mod_SA.append@ename eta_gap11
hp_mod_SA.append@evar var(eta_gap11)=1/lambda

'Measurement equation
hp_mod_SA.append@signal L_QGDP_SA= trend11+ gap11 +eps
hp_mod_SA.append@ename eps
hp_mod_SA.append@evar var (esp)=1

'Extend the model with Output-inflation relationship
hp_mod_SA.append@signal Linfl_sa = c(1) +LInflex_sa+ c(2)*gap11+c(3)*gap11(-1)+eps_Linfl_sa  'The actual inflation rate p depends on inflation expectations (p^e) and the current and lagged output gap. 
hp_mod_SA.append@ename eps_Linfl_sa
hp_mod_SA.append@evar var (eps_Linfl_sa)=1


'Estimate model by maximum likelihood
hp_mod_SA.ml

'Create state estimates
hp_mod_SA.makestates(t=smooth)*f1
hp_mod_SA.makestates(t=pred)*f2
hp_mod_SA.makestates(t=filter)*f3

'Create gap estimate
series cycle1=L_QGDP_SA - trend11f1
series cycle2=L_QGDP_SA - trend11f2
series cycle3=L_QGDP_SA - trend11f3


