/* We conduct an out-of-sample forecasting test of the efficiency of a simulated two variable, independent (no causality) stationary VAR in levels with lag length equal to 2 (p=2). The competitors are the levels VAR and one-way causal VAR. As the sample size goes to infinity, the cost of this casuality naivity goes to zero. Here we set the forecast horizon at h = 3. The specification we are going to consider here is the following: (1) y(t,1) = phi(0,1) + 0.5y(t-1,1) + 0.2y(t-2,1) + 0.0y(t-1,2) + 0.0y(t-2,2) + error (2) y(t,2) = phi(0,2) + 0.0y(t-1,1) + 0.0y(t-2,1) + 0.5y(t-1,2) + 0.2y(t-2,2) + error In the Varmasim subroutine the Phi matrix is defined as follows. Phi = [ 0.5 0.0 ] [ 0.0 0.5 ] [ 0.2 0.0 ] [ 0.0 0.2 ] and the Phi1 and Phi2 matrices are stacked vertically. */ Options nodate; %macro main(rseed); proc iml; sig = 100*i(2); phi = {0.5 0.0, 0.0 0.5, 0.2 0.0, 0.0 0.2}; call varmasim(y,phi) sigma = sig n = 100 initial = 0 seed = &rseed; cn = {'y1' 'y2'}; create simul2 from y[colname=cn]; append from y; quit; data simul2; set simul2; t+1; /* Roll the two independent AR(2) models through the hold-out sample data (observations 71 - 100) forecasting three periods ahead each time. */ %macro Ar2(num); proc varmax data=simul2(obs=&num) noprint; model y1 y2 / p=2 noint; restrict AR(1,1,2)=0, AR(1,2,1)=0, AR(2,1,2)=0, AR(2,2,1)=0; output out=result1 lead=3 back=0 noprint; run; data result1; set result1; ar2f1 = for1; ar2f2 = for2; if _n_ = &num + 3; keep ar2f1 ar2f2; proc append base=collect1 data=result1; %mend; %macro doit1; %do ii=70 %to 97; %Ar2(&ii); %end; %mend; %doit1; /* Roll the levels VAR through the hold-out sample data (observations 71 - 100) forecasting three periods ahead each time. */ %macro levelsvar(num); proc varmax data=simul2(obs=&num) noprint; model y1 y2 / p=2 noint; output out=result2 lead=3 back=0; run; data result2; set result2; levelsvarf1 = for1; levelsvarf2 = for2; if _n_ = &num + 3; keep levelsvarf1 levelsvarf2; proc append base=collect2 data=result2; %mend; %macro doit2; %do ii=70 %to 97; %levelsvar(&ii); %end; %mend; %doit2; /* Roll the Restricted VAR (one-way causal) through the hold-out sample data (observations 71 - 100) forecasting three periods ahead each time. */ %macro Rvar(num); proc varmax data=simul2(obs=&num) noprint; model y1 y2 / p=2 noint; restrict AR(1,2,1)=0, AR(2,2,1)=0; output out=result3 lead=3 back=0 noprint; run; data result3; set result3; Rvarf1 = for1; Rvarf2 = for2; if _n_ = &num + 3; keep Rvarf1 Rvarf2; proc append base=collect3 data=result3; %mend; %macro doit3; %do ii=70 %to 97; %Rvar(&ii); %end; %mend; %doit3; data actual; set simul2; if 72 < _n_; actualy1 = y1; actualy2 = y2; keep actualy1 actualy2; data compare; merge actual collect1 collect2 collect3; keep actualy1 actualy2 Ar2f1 Ar2f2 levelsvarf1 levelsvarf2 Rvarf1 Rvarf2; data errors; set compare; Ar2e1 = actualy1 - Ar2f1; Ar2e2 = actualy2 - Ar2f2; levelsvare1 = actualy1 - levelsvarf1; levelsvare2 = actualy2 - levelsvarf2; Rvare1 = actualy1 - Rvarf1; Rvare2 = actualy2 - Rvarf2; absAr2e1 = abs(Ar2e1); absAr2e2 = abs(Ar2e2); abslevelsvare1 = abs(levelsvare1); abslevelsvare2 = abs(levelsvare2); absRvare1 = abs(Rvare1); absRvare2 = abs(Rvare2); Ar2e12 = Ar2e1**2.0; Ar2e22 = Ar2e2**2.0; levelsvare12 = levelsvare1**2.0; levelsvare22 = levelsvare2**2.0; Rvare12 = Rvare1**2.0; Rvare22 = Rvare2**2.0; /* Calculate the MAEs and MSEs of the Competing Models. */ proc univariate data=errors noprint; var absAr2e1 abslevelsvare1 absRvare1 absAr2e2 abslevelsvare2 absRvare2 Ar2e12 levelsvare12 Rvare12 Ar2e22 levelsvare22 Rvare22; output mean = maeAr21 maelevelsvar1 maeRvar1 maeAr22 maelevelsvar2 maeRvar2 mseAr21 mselevelsvar1 mseRvar1 mseAr22 mselevelsvar2 mseRvar2 out = results; run; proc append base=collect4 data=results; run; /* delete DATA SETS collect1, collect 2, and collect3 */ proc datasets lib=work mt=data nolist; delete collect1 collect2 collect3; run; %mend; /* end of main macro */ %macro doit4; %do i=1 %to 10; %main(4094312+&i); %end; %mend; %doit4; /* Print the Forecasting Accuracies of the the Competing Methods */ proc print data=collect4; var maeAr21 maelevelsvar1 maeRvar1 maeAr22 maelevelsvar2 maeRvar2 mseAr21 mselevelsvar1 mseRvar1 mseAr22 mselevelsvar2 mseRvar2; title 'Forecasting Accuracy Measures of Competing Methods'; title2 'Forecast Horizon = 3'; run; data count; set collect4; if maeAr21 < maelevelsvar1 and maeAr21 < maeRvar1 then maeAr21c = 1; else maeAr21c = 0; if maelevelsvar1 < maeAr21 and maelevelsvar1 < maeRvar1 then maelevelsvar1c = 1; else maelevelsvar1c = 0; if maeRvar1 < maeAr21 and maeRvar1 < maelevelsvar1 then maeRvar1c = 1; else maeRvar1c = 0; if mseAr21 < mselevelsvar1 and mseAr21 < mseRvar1 then mseAr21c = 1; else mseAr21c = 0; if mselevelsvar1 < mseAr21 and mselevelsvar1 < mseRvar1 then mselevelsvar1c = 1; else mselevelsvar1c = 0; if mseRvar1 < mseAr21 and mseRvar1 < mselevelsvar1 then mseRvar1c = 1; else mseRvar1c = 0; if maeAr22 < maelevelsvar2 and maeAr22 < maeRvar2 then maeAr22c = 1; else maeAr22c = 0; if maelevelsvar2 < maeAr22 and maelevelsvar2 < maeRvar2 then maelevelsvar2c = 1; else maelevelsvar2c = 0; if maeRvar2 < maeAr22 and maeRvar2 < maelevelsvar2 then maeRvar2c = 1; else maeRvar2c = 0; if mseAr22 < mselevelsvar2 and mseAr22 < mseRvar2 then mseAr22c = 1; else mseAr22c = 0; if mselevelsvar2 < mseAr22 and mselevelsvar2 < mseRvar2 then mselevelsvar2c = 1; else mselevelsvar2c = 0; if mseRvar2 < mseAr22 and mseRvar2 < mselevelsvar2 then mseRvar2c = 1; else mseRvar2c = 0; proc print data = count; run; /* Calculate the average performances of the Competing Models. */ proc univariate data=count noprint; var maeAr21c maelevelsvar1c maeRvar1c mseAr21c mselevelsvar1c mseRvar1c maeAr22c maelevelsvar2c maeRvar2c mseAr22c mselevelsvar2c mseRvar2c; output mean = maeAr21p maelevelsvar1p maeRvar1p mseAr21p mselevelsvar1p mseRvar1p maeAr22p maelevelsvar2p maeRvar2p mseAr22p mselevelsvar2p mseRvar2p out = results; run; title 'Proportion of Wins for each method and given variable and measure'; title2 'Forecast Horizon is h = 3'; proc print data = results; run;