/* We conduct an out-of-sample forecasting test of the efficiency of a simulated one-way causal stationary VAR in level with lag length equal to 2 (p=2). Here we specify that one-way causality runs from y2 to y1 and, thus, y2 is purely exogenous. The competitors are an incorrectly difference VAR and a levels VAR that naively assumes two-way casuality. As the sample size goes to infinity, the cost of this casuality naivity goes to zero. Here we set the forecast horizon at h = 1. 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.2y(t-1,2) + 0.1y(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.2 ] [ 0.0 0.5 ] [ 0.2 0.1 ] [ 0.0 0.2 ] and the Phi1 and Phi2 matrices are stacked vertically. */ Options nodate; ods graphics on; %macro main(rseed); proc iml; sig = 100*i(2); phi = {0.5 0.2, 0.0 0.5, 0.2 0.1, 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 differenced VAR through the hold-out sample data (observations 1 - 70) forecasting three steps ahead each time. The out-of-sample data set consists of observations 71 - 100. */ %macro difvar(num); proc varmax data=simul2(obs=&num) noprint; model y1 y2 / p=2 noint dify=(1); output out=result1 lead=1 back=0; run; data result1; set result1; difvarf1 = for1; difvarf2 = for2; if _n_ = &num + 1; keep difvarf1 difvarf2; proc append base=collect1 data=result1; %mend; %macro doit; %do ii=70 %to 99; %difvar(&ii); %end; %mend; %doit; /* 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=1 back=0; run; data result2; set result2; levelsvarf1 = for1; levelsvarf2 = for2; if _n_ = &num + 1; keep levelsvarf1 levelsvarf2; proc append base=collect2 data=result2; %mend; %macro doit2; %do ii=70 %to 99; %levelsvar(&ii); %end; %mend; %doit2; /* Roll the Restricted VAR (one-way causal) through the out-of-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=1 back=0 noprint; run; data result3; set result3; Rvarf1 = for1; Rvarf2 = for2; if _n_ = &num + 1; keep Rvarf1 Rvarf2; proc append base=collect3 data=result3; %mend; %macro doit3; %do ii=70 %to 99; %Rvar(&ii); %end; %mend; %doit3; data actual; set simul2; if 70 < _n_; actualy1 = y1; actualy2 = y2; keep actualy1 actualy2; data compare; merge actual collect1 collect2 collect3; keep actualy1 actualy2 difvarf1 difvarf2 levelsvarf1 levelsvarf2 Rvarf1 Rvarf2; data errors; set compare; difvare1 = actualy1 - difvarf1; difvare2 = actualy2 - difvarf2; levelsvare1 = actualy1 - levelsvarf1; levelsvare2 = actualy2 - levelsvarf2; Rvare1 = actualy1 - Rvarf1; Rvare2 = actualy2 - Rvarf2; absdifvare1 = abs(difvare1); absdifvare2 = abs(difvare2); abslevelsvare1 = abs(levelsvare1); abslevelsvare2 = abs(levelsvare2); absRvare1 = abs(Rvare1); absRvare2 = abs(Rvare2); difvare12 = difvare1**2.0; difvare22 = difvare2**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 absdifvare1 abslevelsvare1 absRvare1 absdifvare2 abslevelsvare2 absRvare2 difvare12 levelsvare12 Rvare12 difvare22 levelsvare22 Rvare22; output mean = maedifvar1 maelevelsvar1 maeRvar1 maedifvar2 maelevelsvar2 maeRvar2 msedifvar1 mselevelsvar1 mseRvar1 msedifvar2 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(546489+&i); %end; %mend; %doit4; /* Print the Forecasting Accuracies of the the Competing Methods */ proc print data=collect4; var maedifvar1 maelevelsvar1 maeRvar1 maedifvar2 maelevelsvar2 maeRvar2 msedifvar1 mselevelsvar1 mseRvar1 msedifvar2 mselevelsvar2 mseRvar2; title 'Forecasting Accuracy Measures of Competing Methods'; title2 'Forecast Horizon = 3'; run; data count; set collect4; if maedifvar1 < maelevelsvar1 and maedifvar1 < maeRvar1 then maedifvar1c = 1; else maedifvar1c = 0; if maelevelsvar1 < maedifvar1 and maelevelsvar1 < maeRvar1 then maelevelsvar1c = 1; else maelevelsvar1c = 0; if maeRvar1 < maedifvar1 and maeRvar1 < maelevelsvar1 then maeRvar1c = 1; else maeRvar1c = 0; if msedifvar1 < mselevelsvar1 and msedifvar1 < mseRvar1 then msedifvar1c = 1; else msedifvar1c = 0; if mselevelsvar1 < msedifvar1 and mselevelsvar1 < mseRvar1 then mselevelsvar1c = 1; else mselevelsvar1c = 0; if mseRvar1 < msedifvar1 and mseRvar1 < mselevelsvar1 then mseRvar1c = 1; else mseRvar1c = 0; if maedifvar2 < maelevelsvar2 and maedifvar2 < maeRvar2 then maedifvar2c = 1; else maedifvar2c = 0; if maelevelsvar2 < maedifvar2 and maelevelsvar2 < maeRvar2 then maelevelsvar2c = 1; else maelevelsvar2c = 0; if maeRvar2 < maedifvar2 and maeRvar2 < maelevelsvar2 then maeRvar2c = 1; else maeRvar2c = 0; if msedifvar2 < mselevelsvar2 and msedifvar2 < mseRvar2 then msedifvar2c = 1; else msedifvar2c = 0; if mselevelsvar2 < msedifvar2 and mselevelsvar2 < mseRvar2 then mselevelsvar2c = 1; else mselevelsvar2c = 0; if mseRvar2 < msedifvar2 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 maedifvar1c maelevelsvar1c maeRvar1c msedifvar1c mselevelsvar1c mseRvar1c maedifvar2c maelevelsvar2c maeRvar2c msedifvar2c mselevelsvar2c mseRvar2c; output mean = maedifvar1p maelevelsvar1p maeRvar1p msedifvar1p mselevelsvar1p mseRvar1p maedifvar2p maelevelsvar2p maeRvar2p msedifvar2p mselevelsvar2p mseRvar2p out = results; run; title 'Proportion of Wins for each method and given variable and measure'; title2 ' Forecast Horizon is h = 1'; proc print data = results; run;