/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Bivariate Decomposition of inflation and unumployment

Mode: Unrestricted Model in Table 1&2

This program generates Table 1&2 in Lee and Nelson, "Expectation Horizon 
and the Phillips Curve: The Solution to an Empirical Puzzle"
(forthcoming, Journal of Applied Econometrics).

written by Jaejoon Lee   Feb. 28. 2004 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

new;
library  optmum, pgraph;
optset;
_opgtol = 10^(-3);

format /m1 /rd 9,6;

load data[182,2] = c:\lee-nelson-data.txt;

@ "data" = 1957:Q1 - 2002:Q2  @

ggdata=data[1:182,.];	
					    
inf_gdpd=ggdata[.,1];   @inflation series@ 
lhur = ggdata[.,2];     @unemployment series@

yy = lhur~inf_gdpd;  

t=rows(yy); @ 182(57:1-2002:2)@

@========================================================================@
@ Maximum Likelihood Estimation @
@ NOTE: different computer will result in different nemerical estimates for standard errors@
 
START=21;  @ 5 yrs later from the beginning yr @
           @ to eliminate the diffuse prior in Kalman filter @

/*  @ # of parameter: 8 + 8 = 16 @

@ parameters in Trend-cycle specification @
pr1= SV_UN;      @Standard deviation of unemployment trend@
pr2= SE_UN;      @Standard deviation of unemployment cycle@
pr3= SV_INF;     @Standard deviation of inflation trend@
pr4= SE_INF;     @Standard deviation of inflation cycle@
pr5 = Cov_ve_u;  @covariance between SV_UN and SE_Un@
pr6 = Cov_ve_i;  @covariance between SV_INF and SE_INF@
pr7 = Cov_v_ui;  @covariance between SV_UN and SV_INF@
pr8 = Cov_e_ui;  @covariance between SE_UN and SE_INF@

@ parameters in VAR transition equation @
pr9= rho1;     @ see Equation (13) in the paper @
pr10= rho2;    @ need some restrictions to be stationary cycle in TRANS @   
pr11 = a1;
pr12 = a2;
pr13= b1;
pr14 = b2;
pr15 = psi1;
pr16 = psi2;
*/

/* To find the initial value */

@ 1st trial w/ 0.1tol: { 10 5 0 0 0 0 0 -0.5 0 0 0 0 }; @
@xout from 1st trial: prmtr_in= { 10.000013  4.815353  4.177025  1.522620  0.000243  0.913787  0.002061 -0.250573  6.589845  6.589845 -2.034686  1.133728  0.139804 -0.109499 -0.203804 -0.161293 };@
@xout from 2nd with .001tol: prmtr_in = { 10.000013  4.815240  4.177026  1.522609 -0.000211  0.913219  0.002294 -0.250415  6.589872  6.589872 -2.036096  1.132750  0.139742 -0.109578 -0.202367 -0.161036  };@
@prmtr_in = { 9.000013  4.815353  4.177025  1.522620  0.000243  0.913787  0.002061 -0.250573  6.589845  6.589845 -2.034686  1.133728  0.139804 -0.109499 -0.203804 -0.161293 };@

@final input@
prmtr_in = {9.000013  4.815240  4.177026  1.522609 -0.000211  0.913219  0.002294 -0.250415  6.589872  6.589872 -2.036096  1.132750  0.139742 -0.109578 -0.202367 -0.161035 };
prmtr_in = prmtr_in';


 {xout,fout,gout,cout}= optmum(&lik_fcn,PRMTR_IN);
                       @ prmtr estimates, -log lik value, Grandient, code@

prm_fnl=trans(xout);

"Calculating Hessian..... Please be patient!!!!";
                  hessn0=hessp(&lik_fcn,xout);
                  cov0=inv(hessn0);

grdn_fnl=gradfd(&TRANS,xout);
cov=grdn_fnl*cov0*grdn_fnl';
SD =sqrt(diag(cov));    @Standard errors of the estimated coefficients@
                        @different computer will result in slightly different estimates for SEs@ 

format /m1 /rd 9,6;
output file=c:\Lee&Nelson_ur.out reset;

" ";;

"==FINAL OUTPUT================================================";
"initial values of prmtr(xout) is";
xout';
"==============================================================";
"Unrestricted VAR in Table 1";
"likelihood value is ";; -fout;
"code";;cout;
"Estimated parameters are:";
prm_fnl';
"Standard errors of parameters are:";
sd';
"==============================================================";


OUTPUT OFF;

DATAf = filter(xout);

output file = c:\Lee&Nelson_Out_ur.txt reset;
yy[start:t,1]~dataf[.,1:2]~yy[start:t,2]~dataf[.,3:4];
output off;
;
prm_fnl~sd;
"log(lik) = ";;-fout;
"a0 = ";;prm_fnl[8,1]/(prm_fnl[2,1]^2);

/* "Transformation of VAR estimation to SVAR representation" */

grdn_sva=gradfd(&TR_svar,prm_fnl);
cov_sva=grdn_sva*cov*grdn_sva';
SD_sva =sqrt(diag(cov_sva)); @Standard errors of the estimated SVAR coefficients@
prm_SVAR = tr_svar(prm_fnl);
tr_svar(prm_fnl)~sd_sva;

format /m1 /rd 9,6;
output file=c:\Lee&Nelson_ur2.out reset;

" ";;
"==============================================================";
"Unrestricted SVAR in Table 2";
"Estimated parameters are:";
prm_SVAR';
"Standard errors of parameters are:";
sd_sva';
"==============================================================";

OUTPUT OFF;

XY(SEQA(1,1,ROWS(DATAf)),YY[START:T,1]~DATAf[.,1]);
XY(SEQA(1,1,ROWS(DATAf)),DATAf[.,2]~dataf[.,4]~zeros(rows(dataf),1));
XY(SEQA(1,1,ROWS(DATAf)),YY[start:t,2]~DATAf[.,3]);




@ END OF MAIN PROGRAM @
@========================================================================@
@========================================================================@

PROC LIK_FCN(PRMTR1);

     LOCAL SV,SE,F,H,Q,R,BETA_LL,P_LL,BETA_TL,P_TL,BETA_TT,P_TT,vt,ft, VAL,
     LIK_MAT,J_ITER;

	local rho1,rho2,vecpll,prmtr,mu,SW,d0,d1,d2,d3, cov_ve;
	local a0, a1, a2, sv_inf, se_inf, sv_un, se_un;
	local cov_ve_u, cov_ve_i, cov_v_ui, cov_e_ui;
	local b1, b2, psi1, psi2;	 

prmtr = trans(prmtr1);

     SV_UN=PRMTR[1,1];  @s.e. of the random walk component@
     SE_UN=PRMTR[2,1];  @s.e. of the AR component@

	sv_inf = prmtr[3,1];
	se_inf = prmtr[4,1];

	cov_ve_u = prmtr[5,1];
	cov_ve_i =prmtr[6,1];
	cov_v_ui = prmtr[7,1];
	cov_e_ui = prmtr[8,1];

     rho1=prmtr[9,1];         @ persistence of ue cycle @
     rho2=prmtr[10,1];         @ persistence of ue cycle @

	a1 = prmtr[11,1];     @ trade-off btw inf & ue cycles @
	a2 = prmtr[12,1];

	b1 = prmtr[13,1];
	b2 = prmtr[14,1];

	psi1 = prmtr[15,1];
	psi2 = prmtr[16,1];

      F=(1~0~0~0~0~0)|
	(0~rho1~rho2~0~b1~b2)|
        (0~1~0~0~0~0)|
	(0~0~0~1~0~0)|
        (0~a1~a2~0~psi1~psi2)|
	(0~0~0~0~1~0);

     H=(1~1~0~0~0~0)|
       (0~0~0~1~1~0);

     Q= ZEROS(6,6);

		Q[1,1]=SV_un^2; 
		Q[2,2]=Se_un^2; 
		Q[1,2]=cov_ve_u;		Q[2,1]=cov_ve_u;
		Q[4,4]=sv_inf^2;
		Q[5,5]=se_inf^2;
		Q[4,5]=cov_ve_i;		Q[5,4] = cov_ve_i;
		Q[1,4] = cov_v_ui;		Q[4,1] = cov_v_ui;
		Q[2,5] = cov_e_ui;		Q[5,2] = cov_e_ui;

     R= zeros(2,2);	

     BETA_LL=zeros(6,1);
     P_LL=EYE(6)*100;

     LIK_MAT=ZEROS(T,1);

J_ITER = 1;
DO UNTIL J_ITER>T;

      BETA_TL = F * BETA_LL ;
      P_TL = F * P_LL * F' + Q;

      vt=yy[j_iter,.]' - H * BETA_TL ; @prediction error@

      ft= H * P_TL * H' + R;       @variance of forecast error@


      BETA_TT= BETA_TL + P_TL * H' * inv(ft) * vt;
      P_TT= P_TL - P_TL * H' * inv(ft) * H * P_TL;

      LIK_MAT[J_ITER,1] =  0.5*(LN(2*pi*det(ft)) + vt'inv(ft)*vt);

      BETA_LL=BETA_TT;
      P_LL=P_TT;


J_ITER = J_ITER+1;
ENDO;

VAL = SUMC(LIK_MAT[START:T]);

RETP(VAL);
ENDP;


@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>@

PROC FILTER(PRMTR1);

     LOCAL SV,SE,F,H,Q,R,BETA_LL,P_LL,BETA_TL,P_TL,BETA_TT,P_TT,
     vt,ft, VAL, LIK_MAT,J_ITER;

	local rho1,rho2,vecpll,prmtr,mu,SW,BETA_MAT,d0,d1,d2,d3,sv_un,se_un,
     cov_ve;

	local a0, a1, a2, sv_inf, se_inf, sv_un, se_un;
	local cov_ve_u, cov_ve_i, cov_v_ui, cov_e_ui;
	local b1, b2, psi1, psi2;	 

prmtr = trans(prmtr1);

     SV_UN=PRMTR[1,1];  @s.e. of the random walk component@
     SE_UN=PRMTR[2,1];  @s.e. of the AR component@

	sv_inf = prmtr[3,1];
	se_inf = prmtr[4,1];

	cov_ve_u = prmtr[5,1];
	cov_ve_i =prmtr[6,1];
	cov_v_ui = prmtr[7,1];
	cov_e_ui = prmtr[8,1];

     rho1=prmtr[9,1];         @ persistence of ue cycle @
     rho2=prmtr[10,1];         @ persistence of ue cycle @

	a1 = prmtr[11,1];     @ trade-off btw inf & ue cycles @
	a2 = prmtr[12,1];

	b1 = prmtr[13,1];
	b2 = prmtr[14,1];

	psi1 = prmtr[15,1];
	psi2 = prmtr[16,1];

      F=(1~0~0~0~0~0)|
	(0~rho1~rho2~0~b1~b2)|
        (0~1~0~0~0~0)|
	(0~0~0~1~0~0)|
        (0~a1~a2~0~psi1~psi2)|
	(0~0~0~0~1~0);

     H=(1~1~0~0~0~0)|
       (0~0~0~1~1~0);

     Q= ZEROS(6,6);

		Q[1,1]=SV_un^2; 
		Q[2,2]=Se_un^2; 
		Q[1,2]=cov_ve_u;		Q[2,1]=cov_ve_u;
		Q[4,4]=sv_inf^2;
		Q[5,5]=se_inf^2;
		Q[4,5]=cov_ve_i;		Q[5,4] = cov_ve_i;
		Q[1,4] = cov_v_ui;		Q[4,1] = cov_v_ui;
		Q[2,5] = cov_e_ui;		Q[5,2] = cov_e_ui;
	 

     BETA_MAT=ZEROS(T,4);

     R= zeros(2,2);	

     BETA_LL=zeros(6,1);
     P_LL=EYE(6)*100;


J_ITER = 1;
DO UNTIL J_ITER>T;

      BETA_TL = F * BETA_LL ;
      P_TL = F * P_LL * F' + Q;

      vt=yy[j_iter,.]' - H * BETA_TL ; @prediction error@

      ft= H * P_TL * H' + R;       @variance of forecast error@

      BETA_TT= BETA_TL + P_TL * H' * inv(ft) * vt;
      P_TT= P_TL - P_TL * H' * inv(ft) * H * P_TL;


BETA_MAT[J_ITER,.]=BETA_TT[1,1]~BETA_TT[2,1]~BETA_TT[4,1]~beta_tt[5,1];

      BETA_LL=BETA_TT;
      P_LL=P_TT;


J_ITER = J_ITER+1;
ENDO;


RETP(BETA_MAT[START:T,.]);
ENDP;


@==========================================@
PROC TRANS(c0); @ constraining values of reg. coeff.@
         local c1,d1,d2, dd1, dd2, p, varcov;

         c1=c0;
         c1[1:4,.]=exp(c0[1:4,.]);

	@ stationary restriction for AR(2) UE_cyc coefficents cf. K&N p.15@
     d1=c0[9,1]./(1+abs(c0[9,1]));
     d2=c0[10,1]./(1+abs(c0[10,1]));

     c1[9,1]=d1+d2;
     c1[10,1]= -1* d1*d2 ;

	@ stationary restriction for AR(2) inf_cyc coefficents cf. K&N p.15@
     dd1=c0[15,1]./(1+abs(c0[15,1]));
     dd2=c0[16,1]./(1+abs(c0[16,1]));

     c1[15,1]=dd1+dd2;
     c1[16,1]= -1* dd1*dd2 ;
	
@==========================================@
/* constrain cov(n_t,e_t) to be pd using Cholesky factorization */

     p = zeros(6,6);
      p[1,1] = exp(-c0[1,1]);
      p[2,1] = c0[5,1];
      p[2,2] = exp(-c0[2,1]);

      p[4,4] = exp(-c0[3,1]);
      p[5,4] = c0[6,1];
      p[5,5] = exp(-c0[4,1]);	

      p[4,1] = c0[7,1];
      p[5,2] = c0[8,1];

      varcov = p'p;

      c1[1,1]=sqrt(varcov[1,1]);
      c1[2,1]=sqrt(varcov[2,2]);
      c1[3,1]=sqrt(varcov[4,4]);
      c1[4,1]=sqrt(varcov[5,5]);

      c1[5,1]=varcov[2,1];
      c1[6,1]=varcov[5,4];
      c1[7,1]=varcov[4,1];
      c1[8,1]=varcov[5,2]; 

retp(c1);
endp;

PROC Tr_SVAR(prm0);

local coef, cof_mat,sv_un, se_un, se_inf, sv_inf, cov_ve_u, cov_ve_i, cov_v_ui, cov_e_ui;
local rho1, rho2,a1, a2, b1, b2, psi1, psi2, b1_hat, b2_hat, se_hat;
local a0, a_0,a_1, a_2, se, sum_rho, sum_a, coef_, gamma;

@ define @
cof_mat = prm0;

SV_UN = cof_mat[1,1];
SE_UN= cof_mat[2,1];
SV_INF= cof_mat[3,1];
SE_INF= cof_mat[4,1];
Cov_ve_u= cof_mat[5,1];
Cov_ve_i= cof_mat[6,1];
Cov_v_ui= cof_mat[7,1];
Cov_e_ui= cof_mat[8,1];

rho1= cof_mat[9,1];
rho2= cof_mat[10,1];
a1= cof_mat[11,1];
a2= cof_mat[12,1];
b1= cof_mat[13,1];
b2= cof_mat[14,1];
psi1= cof_mat[15,1];
psi2= cof_mat[16,1];

B1_hat = (rho1~b1)|(a1~psi1);
B2_hat = (rho2~b2)|(a2~psi2);
SE_hat = (Se_Un^2~Cov_E_ui)|(Cov_E_ui~Se_inf^2);

A0 = Cov_e_ui/(SE_UN)^2;
A_0 =(1~0)|(-a0~1);

A_1 = A_0*B1_hat;
A_2= A_0*B2_hat;
SE = A_0*SE_hat*A_0';

@ coef_={SE_U SE_I rho1 rho2 a0 a1 a2 b1 b2 psi1 psi2 sum_rho sum_a};@
coef_ = zeros( 14,1);
coef_[1,1] = SE[1,1];
coef_[2,1] = SE[2,2];
coef_[3,1] =  rho1;
coef_[4,1] = rho2;
coef_[5,1] = a0;
coef_[6,1] = A_1[2,1]; @a1@
coef_[7,1] = A_2[2,1];  @a2@
coef_[8,1] = A_1[1,2];  @b1@
coef_[9,1] = A_2[1,2];  @b2@
coef_[10,1] = A_1[2,2];  @psi1@
coef_[11,1] = A_2[2,2];  @psi2@
coef_[12,1] = (rho1+rho2);
coef_[13,1] =  (a0+A_1[2,1]+A_2[2,1]);  @ Phillips slope in LR model @
coef_[14,1] = (1-rho1-rho2)*a0;  @ Phillips slope in 1-step fwd model@

retp(coef_);
endp;










