LOAD data[51,4]=mkte.txt;
r = data[.,1];
de = data[.,2];
e = data[.,3];
ep = data[.,4];
lhs=r~de~e~ep;
/******************************* parameters **************************************************************/
q_nw = 2;
Ku = 20;
/*******************************************************************************************************************/
/******************************************* ANALYSIS**************************************/
rho = 1/(1+exp(meanc(ep)));
"rho ="; rho;
{T,N,R2,AR2,B,BETA,T_BETA,P_BETA,AVCOVB_NW,VCOV_EHAT}=VAR_REST(lhs,ep,q_nw);
"number of observations"; T;
"number of variables in the VAR"; N;
"R2 for VAR equations"; R2;
"adjusted R2 for VAR equations"; AR2;
"VAR coefficients"; B;
"VAR coefficients"; BETA;
"t-statistics for VAR coefficients"; T_BETA;
"p-values for VAR coefficients"; P_BETA;
"var-cov matrix for vAR coefficients"; AVCOVB_NW;
avcovb=AVCOVB_NW[2 4 6 8,2 4 6 8];
"var-cov matrix for vAR slope coefficients"; AVCOVB;
"var-cov matrix for vAR residuals"; VCOV_EHAT;
b_r=beta[1,2];
b_de=beta[2,2];
b_e=beta[3,2];
phi=beta[4,2];
"b_r"; b_r;
"b_de"; b_de;
"b_e"; b_e;
"phi"; phi;
b_ri = 1-rho*phi+b_e+b_de;
b_dei = -1+rho*phi+b_r-b_e;
b_ei = -1+rho*phi+b_r-b_de;
phi_i = (1+b_e-b_r+b_de)/rho;
"implied b_r"; b_ri;
"implied b_de"; b_dei;
"implied b_e"; b_ei;
"implied phi"; phi_i;
jacob = 0~1~1~(-rho)|1~0~(-1)~rho|1~(-1)~0~rho|(-1/rho)~(1/rho)~(1/rho)~0;
varcov = jacob*avcovb*jacob';
"variance-covariance matrix for the implied coefficients"; varcov;
tstat = (b_ri|b_dei|b_ei|phi_i)./sqrt(diag(varcov));
"t-statistics for implied coefficients"; tstat;
"---------------------------K-year decompositions-----------------------------------";
b_lrk = zeros(Ku,1);
b_ldek = zeros(Ku,1);
b_lek = zeros(Ku,1);
b_lepk = zeros(Ku,1);
identk = zeros(Ku,1);
t_lrk = zeros(Ku,1);
t_ldek = zeros(Ku,1);
t_lek = zeros(Ku,1);
t_lepk = zeros(Ku,1);
b_lrkd = zeros(Ku,1);
b_ldekd = zeros(Ku,1);
b_lekd = zeros(Ku,1);
b_lepkd = zeros(Ku,1);
t_lrkd = zeros(Ku,1);
t_ldekd = zeros(Ku,1);
t_lekd = zeros(Ku,1);
t_lepkd = zeros(Ku,1);
identkd = zeros(Ku,1);
K=1;
do while K<=Ku;
/* implied VAR(1) estimates */
b_lr = b_r*(1-(rho^K)*(phi^K))/(1-rho*phi);
b_lde = (1-rho)*b_de*(1-(rho^K)*(phi^K))/(1-rho*phi);
b_le = b_e*(1-(rho^K)*(phi^K))/(1-rho*phi);
b_lep = (rho^K)*(phi^K);
ident = b_lr-b_lde-b_le+b_lep;
aux1 = -K*b_r*(rho^K)*(phi^(K-1))*(1-rho*phi)+rho*b_r*(1-(rho^K)*(phi^K));
aux2 = -K*b_de*(rho^K)*(phi^(K-1))*(1-rho*phi)*(1-rho)+rho*b_e*(1-(rho^K)*(phi^K))*(1-rho);
aux3 = -K*b_e*(rho^K)*(phi^(K-1))*(1-rho*phi)+rho*b_e*(1-(rho^K)*(phi^K));
aux4 = (1-rho*phi)^2;
aux_br = aux1/aux4;
aux_bde = aux2/aux4;
aux_be = aux3/aux4;
jacob = ((1-(rho^K)*(phi^K))/(1-rho*phi))~0~0~aux_br|0~(1-rho)*((1-(rho^K)*(phi^K))/(1-rho*phi))~0~aux_bde|0~0~((1-(rho^K)*(phi^K))/(1-rho*phi))~aux_be|0~0~0~K*(rho^K)*(phi^(K-1));
varcov = jacob*avcovb*jacob';
tstat = (b_lr|b_lde|b_le|b_lep)./sqrt(diag(varcov));
b_lrk[K,1] = b_lr;
b_ldek[K,1] = b_lde;
b_lek[K,1] = b_le;
b_lepk[K,1] = b_lep;
identk[K,1] = ident;
t_lrk[K,1] = tstat[1,1];
t_ldek[K,1] = tstat[2,1];
t_lek[K,1] = tstat[3,1];
t_lepk[K,1] = tstat[4,1];
/* direct estimates from long-horizon regressions */
sumre = zeros(rows(lhs),1);
j = 1;
do while j<=K;
auxr = (rho^(j-1))*shiftr(lhs[.,1]',-j,0)';
sumre = sumre+auxr;
j = j+1;
endo;
sumre = trimr(sumre,0,K);
sumde = zeros(rows(lhs),1);
j = 1;
do while j<=K;
auxde = (rho^(j-1))*shiftr(lhs[.,2]',-j,0)';
sumde = sumde+auxde;
j = j+1;
endo;
sumde = (1-rho)*trimr(sumde,0,K);
sume = zeros(rows(lhs),1);
j = 1;
do while j<=K;
auxe = (rho^(j-1))*shiftr(lhs[.,3]',-j,0)';
sume = sume+auxe;
j = j+1;
endo;
sume = trimr(sume,0,K);
epk = lagn(ep,-K);
epk = (rho^K)*trimr(epk,0,K);
ep1 = trimr(ep,0,K);
{t,b,ehat,seb_w,seb_nw,tstatb_w,tstatb_nw,pvalues_w,pvalues_nw,r2,ar2} = olsgmm(sumre,ep1,K+1,1);
b_lrkd[K,1] = b[2,1];
t_lrkd[K,1] = tstatb_nw[2,1];
{t,b,ehat,seb_w,seb_nw,tstatb_w,tstatb_nw,pvalues_w,pvalues_nw,r2,ar2} = olsgmm(sume,ep1,K+1,1);
b_lekd[K,1] = b[2,1];
t_lekd[K,1] = tstatb_nw[2,1];
{t,b,ehat,seb_w,seb_nw,tstatb_w,tstatb_nw,pvalues_w,pvalues_nw,r2,ar2} = olsgmm(sumde,ep1,K+1,1);
b_ldekd[K,1] = b[2,1];
t_ldekd[K,1] = tstatb_nw[2,1];
{t,b,ehat,seb_w,seb_nw,tstatb_w,tstatb_nw,pvalues_w,pvalues_nw,r2,ar2} = olsgmm(epk,ep1,K+1,1);
b_lepkd[K,1] = b[2,1];
t_lepkd[K,1] = tstatb_nw[2,1];
identkd[K,1] = b_lrkd[K,1]-b_lekd[K,1]-b_ldekd[K,1]+b_lepkd[K,1];
K=K+1;
endo;
auxk = seqa(1,1,Ku);
"identity sum"; auxk~identk;
"long-run coefficients"; auxk~b_lrk~b_ldek~b_lek~b_lepk;
"t-stats for long-run coefficients"; auxk~t_lrk~t_ldek~t_lek~t_lepk;
"identity sum"; auxk~identkd;
"direct long-run coefficients"; auxk~b_lrkd~b_ldekd~b_lekd~b_lepkd;
"t-stats for direct long-run coefficients"; auxk~t_lrkd~t_ldekd~t_lekd~t_lepkd;
3 Answers
0
In order to be of much help, I think we will need to see the VAR_REST procedure. Could you post the VAR_REST procedure?
0
/******************************************************************************************************************************************/
PROC(10)=VAR_REST(YY,XX,Q_NW); /* Restricted VAR */
LOCAL T,N,X,K,Y,Z,W_I,D,B,EHAT,EPSLON,AUX1,AUX2,R2,AR2,G,S_W,S_NW,J,GAMMA_J,AVCOVB_NW,SEB_NW,TSTATB_NW,PVALUES_NW,
BETA,T_BETA,P_BETA,VCOV_EHAT;
/* Definition of variables */
YY=LAGN(YY,-1);
YY=TRIMR(YY,0,1);
XX=TRIMR(XX,0,1);
T=ROWS(YY);/* number of observations*/
X=ONES(T,1)~XX; /* Number of forecasting variables (including a constant) */
K=COLS(X);
N=COLS(YY); /* N is the number of variables in the VAR */
/* Calculations */
Y=RESHAPE(YY',1,N*T)';
Z=EYE(N).*.X; /* data matrix of the regressors (NTxNK) */
W_I=EYE(N*K); /* weighting matrix for first stage GMM (NKxNK) */
D=(1/T).*(EYE(N).*.(X'X)); /* D matrix; equal to dgT(b)/db (NKxNK) */
B=(EYE(N).*.(INVPD(X'X)*X'))*Y; /* Simplified formula (applied to this case) for GMM estimator (NKx1) */
EHAT=Y-Z*B; /* stacked vector with residuals (NTx1) */
EPSLON=RESHAPE(EHAT,N,T); /* Auxiliar matrix. Transforms the vector of residuals into a matrix,
where each column corresponds to the t th observation, t =1,...,T (NxT) */
VCOV_EHAT=VCXS(EPSLON');
AUX1=(EPSLON'-MEANC(EPSLON')')'(EPSLON'-MEANC(EPSLON')');
AUX2=(YY-MEANC(YY)')'(YY-MEANC(YY)');
R2=1-DIAG(AUX1)./DIAG(AUX2);
AR2=ONES(N,1)-(ONES(N,1)-R2).*((T-1)/(T-K));
G=(EPSLON')*~X;
S_W=(1/T).*G'G;
/******************************************************************************************
Newey-West standard errors
******************************************************************************************/
S_NW=ZEROS(N*K,N*K);
J=1;
DO WHILE J<=Q_NW-1;
GAMMA_J=(1/T).*G[1:T-J,.]'G[1+J:T,.]+(1/T).*G[1+J:T,.]'G[1:T-J,.];
S_NW=S_NW+(1-J/Q_NW).*GAMMA_J;
J=J+1;
ENDO;
S_NW=S_W+S_NW;
AVCOVB_NW=(1/T).*(INVPD(D)*S_NW*INVPD(D));
SEB_NW=SQRT(DIAG(AVCOVB_NW));
TSTATB_NW=B./SEB_NW;
PVALUES_NW=2*CDFNC(ABS(TSTATB_NW));
BETA=RESHAPE(B,N,K);
T_BETA=RESHAPE(TSTATB_NW,N,K);
P_BETA=RESHAPE(PVALUES_NW,N,K);
RETP(T,N,R2,AR2,B,BETA,T_BETA,P_BETA,AVCOVB_NW,VCOV_EHAT);
ENDP;
0
One more procedure appears to be missing, olsgmm. Can you provide that?
Your Answer
3 Answers
In order to be of much help, I think we will need to see the VAR_REST procedure. Could you post the VAR_REST procedure?
/******************************************************************************************************************************************/
PROC(10)=VAR_REST(YY,XX,Q_NW); /* Restricted VAR */
LOCAL T,N,X,K,Y,Z,W_I,D,B,EHAT,EPSLON,AUX1,AUX2,R2,AR2,G,S_W,S_NW,J,GAMMA_J,AVCOVB_NW,SEB_NW,TSTATB_NW,PVALUES_NW,
BETA,T_BETA,P_BETA,VCOV_EHAT;
/* Definition of variables */
YY=LAGN(YY,-1);
YY=TRIMR(YY,0,1);
XX=TRIMR(XX,0,1);
T=ROWS(YY);/* number of observations*/
X=ONES(T,1)~XX; /* Number of forecasting variables (including a constant) */
K=COLS(X);
N=COLS(YY); /* N is the number of variables in the VAR */
/* Calculations */
Y=RESHAPE(YY',1,N*T)';
Z=EYE(N).*.X; /* data matrix of the regressors (NTxNK) */
W_I=EYE(N*K); /* weighting matrix for first stage GMM (NKxNK) */
D=(1/T).*(EYE(N).*.(X'X)); /* D matrix; equal to dgT(b)/db (NKxNK) */
B=(EYE(N).*.(INVPD(X'X)*X'))*Y; /* Simplified formula (applied to this case) for GMM estimator (NKx1) */
EHAT=Y-Z*B; /* stacked vector with residuals (NTx1) */
EPSLON=RESHAPE(EHAT,N,T); /* Auxiliar matrix. Transforms the vector of residuals into a matrix,
where each column corresponds to the t th observation, t =1,...,T (NxT) */
VCOV_EHAT=VCXS(EPSLON');
AUX1=(EPSLON'-MEANC(EPSLON')')'(EPSLON'-MEANC(EPSLON')');
AUX2=(YY-MEANC(YY)')'(YY-MEANC(YY)');
R2=1-DIAG(AUX1)./DIAG(AUX2);
AR2=ONES(N,1)-(ONES(N,1)-R2).*((T-1)/(T-K));
G=(EPSLON')*~X;
S_W=(1/T).*G'G;
/******************************************************************************************
Newey-West standard errors
******************************************************************************************/
S_NW=ZEROS(N*K,N*K);
J=1;
DO WHILE J<=Q_NW-1;
GAMMA_J=(1/T).*G[1:T-J,.]'G[1+J:T,.]+(1/T).*G[1+J:T,.]'G[1:T-J,.];
S_NW=S_NW+(1-J/Q_NW).*GAMMA_J;
J=J+1;
ENDO;
S_NW=S_W+S_NW;
AVCOVB_NW=(1/T).*(INVPD(D)*S_NW*INVPD(D));
SEB_NW=SQRT(DIAG(AVCOVB_NW));
TSTATB_NW=B./SEB_NW;
PVALUES_NW=2*CDFNC(ABS(TSTATB_NW));
BETA=RESHAPE(B,N,K);
T_BETA=RESHAPE(TSTATB_NW,N,K);
P_BETA=RESHAPE(PVALUES_NW,N,K);
RETP(T,N,R2,AR2,B,BETA,T_BETA,P_BETA,AVCOVB_NW,VCOV_EHAT);
ENDP;
One more procedure appears to be missing, olsgmm. Can you provide that?