/* ======================================================================== MODEL: Kim, Morley, and Piger's "Bounceback" model from Journal of Applied Econometrics, vol. 20, 291-309. AR(2) specification ========================================================================= */ new; cls; library optmum,PGRAPH; format /m1 /rd 9,5; load data[225,2]=gdp4703.txt; @1947.1 -- 2003.1; quarterly @ yy_d=100*(ln(data[2:225,2])-ln(data[1:224,2])); t0=rows(yy_d); /* 18: 1951:2 -- 1997:2 */ /* 22: 1952:2 -- 1997:2 */ /* >>>>>>>>>>>>>>>>>>>>>>>> Don't change the following >>>>>>>>>>> */ LAG_AR=4; @Maximum AR lag ===> currently we have an AR(4)@ NO_ST=9; @ NUMBER OF STATES TO BE CONSIDERED@ DMNSION=2^NO_ST; @ NUMBER OF CASES TO BE CONSIDERED@ st_mat=zeros(DMNSION,NO_ST); j=1; st8=0;do until st8>1; st7=0; do until st7>1; st6=0;do until st6>1; st5=0;do until st5>1; st4=0;do until st4>1; st3=0; do until st3>1; st2=0; do until st2>1; st1=0; do until st1>1; st=0; do until st>1; st_mat[j,.]=@st10~st9~@st8~st7~st6~st5~st4~st3~st2~st1~st; j=j+1; st=st+1;endo; st1=st1+1;endo; st2=st2+1;endo; st3=st3+1;endo; st4=st4+1;endo; st5=st5+1;endo; st6=st6+1;endo; st7=st7+1;endo; st8=st8+1;endo; yy=yy_d[8:t0,1]; @Always start in 1949:1, see beginning of section 4 on p. 295@ x_mat= yy_d[7:T0-1,1] ~ yy_d[6:T0-2,1]; @AR(2) case@ T=rows(yy); /* >>>>>>>>>>>>>>>>>>>>>>>> Don't change the above >>>>>>>>>>> */ @================= Choose "MSTAR" and "Option1" in the following =====@ MSTAR=6; @MSTAR= 6, 5, 4, 3, 2, or 1@ OPTION1=1; @1: phi(L)(y_t- mu_{s_t}) = omega*(S_{t-1}+...+S_{t-m})+e_t@ @2: phi(L)y_t = mu_{s_t})+ omega*(S_{t-1}+...+S_{t-m})+e_t@ num_phi = 0; PRMTR_IN={1.20657 3.15590 0.79619 0.8 2.0 0.3 0 0}; PRMTR_IN=PRMTR_IN'; @ Maximum Likelihood Estimation @ @==================================================@ {xout,fout,gout,cout}=optmum(&lik_fcn,PRMTR_in); PRM_FNL=TRANS(xout); @ Estimated coefficients, constrained@ k = rows(prmtr_in); AIC = (-2*((-1*fout)))/T + (2*k)/T; BIC = (-2*((-1*fout)))/T + (k*ln(T))/T; p11 = prm_fnl[1]; p00 = prm_fnl[2]; muboom = prm_fnl[4]; murec = prm_fnl[5]; cdrterm = prm_fnl[6]; lamda = -1 + p11 + p00; LRIMPACTHAM = (murec + mstar*cdrterm)*(lamda/(1-lamda)) + mstar*cdrterm; LRIMPACTtotal = (murec + mstar*cdrterm)*(1/(1-lamda)); output file=pr.out on; "==FINAL OUTPUT========================================================"; "This is the file for m = " mstar "and lag length = " num_phi; "initial values of prmtr is"; trans(prmtr_in)'; "=============================================================="; "code is--------";;cout; "likelihood value is ";; -1*fout; "Estimated parameters are:"; prm_fnl'; " "; xout'; output off; "Calculating Hessian..... Please be patient!!!!"; hout0=hessp(&lik_fcn,xout); hout=inv(hout0); grdn_fnl=gradfd(&TRANS,xout); Hsn_fnl=grdn_fnl*hout*grdn_fnl'; SD_fnl =sqrt(diag(Hsn_fnl)); @Standard errors of the estimated coefficients@ output on; "Standard errors of parameters are:"; sd_fnl'; "==============================================================="; "AIC = " AIC; "BIC = " BIC; "Long run impact - Hamilton: " LRIMPACTHAM; "Long run impact - Total: " LRIMPACTTOTAL; output off; {pr_tt0,pr_tl0}=FILTER(XOUT); @Pr[S_t=0|Y_t] and Pr[S_t=0|Y_{t-1}]@ smooth0=SMOOTH(pr_tt0,pr_tl0); @Pr[S_t=0|Y_T], Smoothed probabilities@ FLTR=(1-pr_tt0)~(1-pr_tl0)~(1-smooth0); format /m1 /rd 8,4; output file= pr.dta reset; FLTR; output off; xy(seqa(1,1,rows(pr_tt0)),1-pr_tt0); xy(seqa(1,1,rows(smooth0)),1-smooth0); end; @ END OF MAIN PROGRAM @ @========================================================================@ @========================================================================@ PROC LIK_FCN(PRMTR1); local prmtr, ppr,qpr,prob__0,prob__1,QQ, lik, j_iter, pr__0_l,pr__1_l, f_cast1, var_L,pr_vl, pr_val,likv,phi,PSIC,PSIX, vecp,st,st1,st2,st3,st4,ST5,ST6,ST7,ST8,ST9,ST10,ST11,ST12,ST13, pr_tr,pr_trf1,pr_trf,prob__t,prob__,pro_,pr_vl,j,psi1,psi2,var_c, delta0,DELTA1,MU0,MU1,st_k,st_k1,st_k2,st_k3,st_k4, f_cast1,f_cast2,PR_VL1,PR_VL2,pr_trf0, PR_TRF2,PR_TRF3,PR_TRF4,PR_TRF5,PR_TRF6,PR_TRF7,PR_TRF8,psic,psiL, TMPRY1,TMPRY2,SM_PRL,TMP_P0,SM_PR0,JJJ,MU_MAT,D_MAT,FLT_PRN, F1,F2,TMP00,TMP0,SM_PR0,SM_PR00,prob_dd,VAR,A,EN,omga1,omga2, omga3,omga4,cdr1,cdr2,cdr3,cdr4,cdr1_m,cdr2_m,cdr3_m,cdr4_m; PRMTR=TRANS(PRMTR1); /* DEFINE PARAMETERS */ PPR=PRMTR[1,1]; @Pr[St=1/St-1=1]@ QPR=PRMTR[2,1]; @Pr[St=0/St-1=0]@ VAR=PRMTR[3,1]^2; MU0=PRMTR[4,1]; @ recession, vs. boom@ MU1=PRMTR[5,1]; @ recession, vs. boom@ omga1=prmtr[6]; PHI= prmtr[7:8]; MU_MAT=ST_MAT*MU1 + (ONES(DMNSION,NO_ST))*MU0; /* A MATRIX OF TRANSITION PROBABILITIES */ PR_TR=(QPR~ (1-PPR))| ((1-QPR)~ PPR); /* INITIALIZING THE FILTER WITH STEADY-STATE PROBABILITIES */ A = (eye(2)-pr_tr)|ones(1,2); EN=(0|0|1); PROB__T = INV(A'A)*A'EN; @PR[S_t=0]|PR[S_t=1], 2x1 steady-state PROBABILITIES@ PR_TRF0=VEC(PR_TR); PR_TRF1=PR_TRF0|PR_TRF0; PR_TRF2=PR_TRF1|PR_TRF1; PR_TRF3=PR_TRF2|PR_TRF2; PR_TRF4=PR_TRF3|PR_TRF3; PR_TRF5=PR_TRF4|PR_TRF4; PR_TRF6=PR_TRF5|PR_TRF5; PR_TRF =PR_TRF6|PR_TRF6; PROB__T=VECR(PROB__T~PROB__T).*PR_TRF0; /*PR[S_{-10},S_{-11}|I_0) 4x1*/ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF1; /*PR[S_{-9},S_{10},S_{-11}|I_0) 8x1*/ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF2; /*PR[S_{-8},S_{-9},S_{-10},S_{-11}|I_0) 16x1 */ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF3; /*PR[S_{-7},...,S_{-11}|I_0) 32x1 */ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF4; /*PR[S_{-6},S_{-1},...,S_{-11}|I_0) 62x1 */ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF5; /*PR[S_{-5},S_{-4}|I_0) 4x1*/ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF6; /*PR[S_{-4},S_{-3},S_{-4}|I_0) 8x1*/ PROB__=VECR(PROB__T~PROB__T); /*2^11 x 1*/ clear pr_trf0,pr_trf1,pr_trf2,pr_trf3,pr_trf4,pr_trf5,pr_trf6@,pr_trf7,pr_trf8@; LIKV=0.0; J_ITER=1; DO UNTIL J_ITER>T; F_CAST1=(YY[J_ITER,1]-X_MAT[J_ITER,.]*PHI)*ONES(DMNSION,1) -(MU_MAT[.,9] - MU_MAT[.,8]*PHI[1,1] - MU_MAT[.,7]*PHI[2,1] ) -(omga1*(sumc(st_mat[.,9-mstar:8]')) - omga1*(sumc(st_mat[.,8-mstar:7]'))*PHI[1,1] - omga1*(sumc(st_mat[.,7-mstar:6]'))*PHI[2,1]); VAR_L=VAR*ONES(DMNSION,1); /* 2^6x1 */ PROB_DD=PR_TRF .* PROB__; /* Pr[S_t,S_{t-1},.....,S_{t-5} | I(t-1)]*/ PR_VL=(1./SQRT(2.*PI.*VAR_L)).*EXP(-0.5*F_CAST1.*F_CAST1./VAR_L).*PROB_DD; /* 2^6x1 */ /* Joint density of y_t,S_t,..,S_{t-5} given past information : */ clear f_cast1,var_l,prob_dd; PR_VAL=SUMC(PR_VL); /* f(y_t|I(t-1)), density of y_t given past information: This is weighted average of 2^6 conditional densities */ LIK=-1*LN(PR_VAL); PRO_=PR_VL/PR_VAL; /* Pr[S_t,S_{t-1},...,S_{t-5} | I(t-1),y_t]*/ /* Updating of prob. with new information, y_t */ PROB__T=PRO_[1:DMNSION/2,1]+PRO_[DMNSION/2+1:DMNSION,1]; /* Integrate out S_{t-4}: then you get Pr[S_t, S_{t-1},....,S_{t-4}| Y_t] */ /* 2^5x1*/ PROB__=VECR(PROB__T~PROB__T); /* 2^6x1 */ LIKV = LIKV+LIK; J_ITER = J_ITER+1; ENDO; RETP(LIKV); ENDP; @++++++++++++++++++++++++++++++++++++++++++++++++++++++++@ @++++++++++++++++++++++++++++++++++++++++++++++++++++++++@ PROC (2) = FILTER(PRMTR1); local prmtr, ppr,qpr,prob__0,prob__1,QQ, lik, j_iter, pr__0_l,pr__1_l, F_cast, var_L,pr_vl, pr_val,likv,phi,PSIC,PSIX, vecp,st,st1,st2,st3,st4,ST5,ST6,ST7,ST8,ST9,ST10,ST11,ST12,ST13, pr_tr,pr_trf1,pr_trf,prob__t,prob__,pro_,pr_vl,j,psi1,psi2,var_c, delta0,DELTA1,MU0,MU1,st_k,st_k1,st_k2,st_k3,st_k4, f_cast1,f_cast2,PR_VL1,PR_VL2,pr_trf0, PR_TRF2,PR_TRF3,PR_TRF4,PR_TRF5,PR_TRF6,PR_TRF7,PR_TRF8,psic,psiL,PR_STL0,PR_STT0, TMPRY1,TMPRY2,SM_PRL,TMP_P0,SM_PR0,JJJ,MU_MAT,D_MAT,FLT_PRN, F1,F2,TMP00,TMP0,SM_PR0,SM_PR00,prob_dd,VAR,TMP,OUT_MAT,A,EN,omga1,omga2, omga3,omga4,cdr1,cdr2,cdr3,cdr4,cdr1_m,cdr2_m,cdr3_m,cdr4_m; PRMTR=TRANS(PRMTR1); PPR=PRMTR[1,1]; @Pr[St=1/St-1=1]@ QPR=PRMTR[2,1]; @Pr[St=0/St-1=0]@ VAR=PRMTR[3,1]^2; MU0=PRMTR[4,1]; @ recession, vs. boom@ MU1=PRMTR[5,1]; @ recession, vs. boom@ omga1=prmtr[6]; PHI= prmtr[7:8]; PR_TR=(QPR~ (1-PPR))| ((1-QPR)~ PPR); MU_MAT=ST_MAT*MU1 + (ONES(DMNSION,NO_ST))*MU0; /* FOR UNCONDITIONAL PROBABILITIES */ A = (eye(2)-pr_tr)|ones(1,2); EN=(0|0|1); PROB__T = INV(A'A)*A'EN; @PR[S_t=0]|PR[S_t=1], 2x1 UNCONDITIONAL PROBABILITIES@ PR_TRF0=VEC(PR_TR); PR_TRF1=PR_TRF0|PR_TRF0; PR_TRF2=PR_TRF1|PR_TRF1; PR_TRF3=PR_TRF2|PR_TRF2; PR_TRF4=PR_TRF3|PR_TRF3; PR_TRF5=PR_TRF4|PR_TRF4; PR_TRF6=PR_TRF5|PR_TRF5; PR_TRF =PR_TRF6|PR_TRF6; PROB__T=VECR(PROB__T~PROB__T).*PR_TRF0; /*PR[S_{-10},S_{-11}|I_0) 4x1*/ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF1; /*PR[S_{-9},S_{10},S_{-11}|I_0) 8x1*/ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF2; /*PR[S_{-8},S_{-9},S_{-10},S_{-11}|I_0) 16x1 */ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF3; /*PR[S_{-7},...,S_{-11}|I_0) 32x1 */ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF4; /*PR[S_{-6},S_{-1},...,S_{-11}|I_0) 62x1 */ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF5; /*PR[S_{-5},S_{-4}|I_0) 4x1*/ PROB__T=VECR(PROB__T~PROB__T).*PR_TRF6; /*PR[S_{-4},S_{-3},S_{-4}|I_0) 8x1*/ PROB__=VECR(PROB__T~PROB__T); /*2^11 x 1*/ clear pr_trf0,pr_trf1,pr_trf2,pr_trf3,pr_trf4,pr_trf5,pr_trf6@,pr_trf7,pr_trf8@; PR_STT0=ZEROS(T,1); @WILL SAVE Pr[S_t=0|Y_{t}@ PR_STL0=ZEROS(T,1); @WILL SAVE Pr[S_t=0|Y_{t-1}@ LIKV=0.0; J_ITER=1; DO UNTIL J_ITER>T; F_CAST1=(YY[J_ITER,1]-X_MAT[J_ITER,.]*PHI)*ONES(DMNSION,1) -(MU_MAT[.,9] - MU_MAT[.,8]*PHI[1,1] - MU_MAT[.,7]*PHI[2,1] ) -(omga1*(sumc(st_mat[.,9-mstar:8]')) - omga1*(sumc(st_mat[.,8-mstar:7]'))*PHI[1,1] - omga1*(sumc(st_mat[.,7-mstar:6]'))*PHI[2,1]); /* 2^6x1 */ VAR_L=VAR*ONES(DMNSION,1); PROB_DD=PR_TRF .* PROB__; @-----------------------------------------------------@ TMP=PROB_DD; TMP=TMP[1:256]+TMP[257:512]; TMP=TMP[1:128]+TMP[129:256]; TMP=TMP[1:64]+TMP[65:128]; TMP=TMP[1:32]+TMP[33:64]; TMP=TMP[1:16]+TMP[17:32]; TMP=TMP[1:8]+TMP[9:16]; TMP=TMP[1:4]+TMP[5:8]; TMP=TMP[1:2]+TMP[3:4]; PR_STL0[J_ITER,1]=TMP[1,1]; @Pr[S_t=0|Y_t]@ @------------------------------------------------------@ PR_VL=(1./SQRT(2.*PI.*VAR_L)).*EXP(-0.5*F_CAST1.*F_CAST1./VAR_L).*PROB_DD; @PR[S_t,S_{T-1},S_{T-2},...,S_{T-5},Y_t|Y_{t-1}]@ PR_VAL=SUMC(PR_VL); @f(y_t| Y_{t-1})@ LIK=-1*LN(PR_VAL); PRO_=PR_VL/PR_VAL; @Pr[S_t, S_{t-1},S_{t-2},...,S_{t-5} | Y_t]@ @-------------------------------------------------------@ TMP=PRO_; TMP=TMP[1:256]+TMP[257:512]; TMP=TMP[1:128]+TMP[129:256]; TMP=TMP[1:64]+TMP[65:128]; TMP=TMP[1:32]+TMP[33:64]; TMP=TMP[1:16]+TMP[17:32]; TMP=TMP[1:8]+TMP[9:16]; TMP=TMP[1:4]+TMP[5:8]; TMP=TMP[1:2]+TMP[3:4]; PR_STT0[J_ITER,1]=TMP[1,1]; @Pr[S_t=0|Y_t]@ @-------------------------------------------------------@ PROB__T=PRO_[1:DMNSION/2,1]+PRO_[DMNSION/2+1:DMNSION,1]; @Pr[S_t, S_{t-1},....,S_{t-4}| Y_t]@ PROB__=VECR(PROB__T~PROB__T); J_ITER = J_ITER+1; ENDO; RETP(PR_STT0,PR_STL0); ENDP; @======================================================================@ @======================================================================@ /* KIM'S SMOOTHING ALGORITHM (J. ECONOMETRICS, 1994) */ PROC SMOOTH(pr_tt0,pr_tl0); @pr_TT0 contains Pr[S_t|Y_t]@ @pr_TL0 contains Pr[S_t|Y_{t-1}]@ local ppr, qpr, pr_sm0,pr_sm1, j_iter,pr_sm00,pr_sm01,pr_sm10,pr_sm11, pr_tt1,pr_tl1; PPR=PRM_fnl[1,1]; @Pr[St=1/St-1=1]@ QPR=PRM_fnl[2,1]; @Pr[St=0/St-1=0]@ pr_tt1=1-pr_tt0; pr_tl1=1-pr_tl0; pr_sm0=pr_tt0; @ pr_sm0 will contain Pr[S_t|Y_T]@ pr_sm1=pr_tt1; j_iter=T-1; do until j_iter < 1; @The followings are P[S_t, S_t+1|Y_T] @ pr_sm00=pr_sm0[j_iter+1,1]*qpr* pr_tt0[j_iter,1]/ pr_tl0[j_iter+1,1]; pr_sm01=pr_sm1[j_iter+1,1]*(1-qpr)*pr_tt0[j_iter,1]/ pr_tl1[j_iter+1,1]; pr_sm10=pr_sm0[j_iter+1,1]*(1-ppr)*pr_tt1[j_iter,1]/ pr_tl0[j_iter+1,1]; pr_sm11=pr_sm1[j_iter+1,1]*ppr* pr_tt1[j_iter,1]/ pr_tl1[j_iter+1,1]; pr_sm0[j_iter,1]=pr_sm00+pr_sm01; pr_sm1[j_iter,1]=pr_sm10+pr_sm11; j_iter=j_iter -1; endo; RETP(pr_sm0); @This proc returns Pr[S_t=0|Y_T]@ endp; @++++++++++++++++++++++++++++++++++++++++++++++++++++++++@ PROC TRANS(c0); @ constraining values of reg. coeff.@ local c1,m,u,d1,d2,d3,d4,d5,d6; c1=c0; c1[1:2,.]=exp(c0[1:2,.])./ (1+exp(c0[1:2,.])); c1[5] = -c0[5]; retp(c1); endp;