CLS;
FORMAT 4,0;
DD=DATE; TX=TIME;

@ ------------------------------------------------------- @
@      HERE WE SIMULATE THE MODEL                         @
@ ------------------------------------------------------- @

HX=HSEC;
"1: INITIALIZING";
LOCATE 1,50;"DATE";;DD[3,1];;DD[2,1];;DD[1,1];
LOCATE 2,50;"TIME";;TX[1,1];;TX[2,1];;TX[3,1];

@ ------------------------------------------------------- @
@            THIS INCLUDES A PROCEDURE WITH
             THE HP-FILTER - THIS PROGRAM CAN             @
@ ------------------------------------------------------- @
UU=0;
#INCLUDE C:\GAUSS1\procs\HP1;

NEXP=20;                           @ # REPLICATIONS       @

@ STACK @;
STACK=10;

@ ------------------------------------------------------- @
@           I SEED THE RANDOM NUMBER GENERATOR            @
@ ------------------------------------------------------- @
RNDSEED 7654321;

@ INITIALIZE MATRICES OF MOMENTS @
STD=ZEROS(34,NEXP);    @ STANDARD DEVIATIONS @
STDR=ZEROS(34,NEXP);   @ -------- ---------- RELATIVE TO Y @
AUC=ZEROS(34,NEXP);    @ AUTOCORRELATIONS @
COR=ZEROS(44,NEXP);    @ CORRELATIONS @

@ A VECTOR WITH THE VARIABLE NAMES @
VAR1={ NH NF A1 B2 ZH ZF GH GF KH KF YH YF
       A2 B1 IH IF VH VF CH CF PH PF XH XF IMH IMF
       APH APF q1 q2 qf1 qf2 NXH NXF };                     @ FOR STD,STDR,AU @
VAR2={ NH NF A1 B2 ZH ZF GH GF KH KF YH YF
       A2 B1 iH iF vH vF CH CF PH PF XH XF IMH IMF
       APH APF q1 q2 qf1 qf2 NXH NXF YY* CC* II* GG* ZZ*
       NN* XX* MM* PHNXH PFNXF };                              @ FOR COR @
@ CORRELATIONS ARE COMPUTED BETWEEN COR1[I,1] AND COR2[I,1] @
COR1=SEQA(1,1,34)|12|19|15
|7|5|1|23|25|21|22;
COR2=11|12|11|12|11|12|11|12|11|12|11|12|11|12|11|12|11|12|11|12|11|12|11|12|
     11|12|11|12|11|12|11|12|11|12|11|20|16|
8|6|2|24|26|33|34;

Z=SSVAL;
 

@ CHOOSE SHOCK CORRELATIONS (ORDERING IS ABOVE) @
COR12=0.258;
CORG=0;

@ CHOOSE STANDARD ERRORS OF SHOCKS @
SA1=0.00852; SA2=0.00852;
SG1=0.02; SG2=0.02;

@ USING THIS WE SET UP THE LOADING MATRIX FOR THE SHOCKS
  THIS IS GIVEN BY E WHERE E'=CHOLESKY DECOMP. OF
  THE VARIANCE COVARIANCE MATRIX OF THE SHOCKS @

IF SG1>0;
VCV=(SA1^2|COR12*SA1*SA2|0|0)~
             (COR12*SA1*SA2|SA2^2|0|0)~
             (0|0|SG1^2|0)~
             (0|0|0|SG2^2);
 

E1=CHOL(VCV)';
E=ZEROS(6,6);
E[1:4,1:4]=E1;
if corg>0.99;
e[1:3,1:4]=e1[1:3,1:4];
e[4,1:4]=e1[3,1:4];
endif;
ELSE;
VCV=(SA1^2|COR12*SA1*SA2)~
             (COR12*SA1*SA2|SA2^2);
E1=CHOL(VCV)';
E=ZEROS(6,6);
E[1:2,1:2]=E1;
ENDIF;

G=zeros(7,6);
g[2,1]=1;
g[3,2]=1;
G[4,3]=1;
G[5,4]=1;

FORMAT 10,10;

HOR=120;                       @ SPECIFY TIME HORISONT @
INIT=50;                       @ # PERIODS USED FOR INITIALIZING @

H1=HSEC;

XTT=zeros(6,INIT+HOR); UTT=zeros(6,INIT+HOR);
XUTT=zeros(rows(varux),INIT+HOR);
 

LOCATE 2,4;"SIMULATING ROUND NO.:";
IT=1;
DO UNTIL IT>NEXP/STACK;
DAT1=ZEROS(HOR,ROWS(STD));
IT1=1;
DO UNTIL IT1>STACK;

LOCATE 2,28;STACK*(IT-1)+IT1;
X=1|ln(ZSS|ZSS|GSS|GSS|KSS|KSS);

LL=E*RNDN(INIT+HOR+1,ROWS(E))';
 I=1;
 DO UNTIL I>HOR+INIT;
   L=LL[.,I+1];
   XX=AS*X+G*L;
   U=AU*X;
   XTT[.,I]=X[2:7,1];
   UTT[1:6,I]=U;
   xUTT[.,I]=axU*x;
   X=XX;
   I=I+1;
 ENDO;

XT=exp(XTT[.,INIT+1:INIT+HOR]);
UT=exp(UTT[.,INIT+1:INIT+HOR]);
xUT=exp(xUTT[.,INIT+1:INIT+HOR]);

nxht=(xut[13,.]'-xut[15,.]')./xut[1,.]';
nxft=(xut[14,.]'-xut[16,.]')./xut[2,.]';

DAT=(ln(ut[1:4,.])|ln(xt)|ln(xut)|nxht'|nxft')';
xxt=dat;
DAT1=DAT1~DAT;

IT1=IT1+1;
ENDO;

DAT=DAT1[.,ROWS(STD)+1:COLS(DAT1)];
UU=ZEROS(ROWS(DAT),COLS(DAT));
CALL HP1(DAT);

CLEAR DAT1;
CLEAR DAT;

@ ------------------------------------------------------- @
@   HERE WE CONPUTE THE MOMENTS OF THE FILTERED DATA      @
@ ------------------------------------------------------- @
IT1=1;
DO UNTIL IT1>STACK;
DAT=UU[.,ROWS(STD)*(IT1-1)+1:(ROWS(STD))*IT1];

@  STANDARD DEVIATIONS @
STD[.,STACK*(IT-1)+IT1]=100*STDC(DAT);

@  STANDARD DEVIATIONS RELATIVE TO Y @
J=1;
SY1=ZEROS(ROWS(STD),1);
DO UNTIL J>ROWS(STD);
SY1[J,1]=STD[COR2[J,1],STACK*(IT-1)+IT1];
J=J+1;
ENDO;
@SY1';
STD[11:12,STACK*(IT-1)+IT1];
WAIT;@
STDR[.,STACK*(IT-1)+IT1]=(STD[.,STACK*(IT-1)+IT1]./SY1);
 
 

@ AUTOCORRELATIONS @
X=DAT[2:HOR,.]; X1=DAT[1:HOR-1,.];
X2=X'*X1;
X3=SQRT(DIAG(X'*X))*(SQRT(DIAG(X1'*X1)))';
X4=X2./X3;
AUC[.,STACK*(IT-1)+IT1]=DIAG(X4);

@ CORRELATION MATRIX @
X=DAT'*DAT;
X1=SQRT(DIAG(X));
X2=X1*X1';
X3=X./X2;

J=1;
DO UNTIL J>ROWS(COR);
COR[J,STACK*(IT-1)+IT1]=X3[COR1[J,1],COR2[J,1]];
J=J+1;
ENDO;

IT1=IT1+1;
ENDO;
CLEAR DAT;

IT=IT+1;
ENDO;

STD1=(MEANC(STD'));
SSTD=STDC(STD');
STDR1=MEANC(STDR');
SSTDR=STDC(STDR');
AU1=MEANC(AUC');
SAU=STDC(AUC');
TCOR1=MEANC(COR');
SCOR=STDC(COR');
 
 

LOCATE 2,5; " TOTAL TIME";;(HSEC-TX)/100;

FORMAT /LDN 4,4;

@ ------------------------------------------------------- @
@               PRINTING THE RESULTS                      @
@ ------------------------------------------------------- @
OUTPUT FILE=STUD1.OUT;
OUTPUT ON;
DD=DATE; TX=TIME;
" ------------------------------------------------------- ";
"                SIMULATION RESULTS                       ";
" ------------------------------------------------------- ";
"DATE";;DD[3,1];;DD[2,1];;DD[1,1];
"TIME";;TX[1,1];;TX[2,1];;TX[3,1];
"NO. OF REPLICATIONS";;NEXP;
"STD. OF Z: ";;SA1;;"    STD. OF G";;SG1;
" STANDARD DEVIATIONS AND AUTOCORRELATIONS";
"X    S(X)    (Sd)  S(X)/S(Y) (Sd) AUTOCOR (Sd)";
J=1;
DO UNTIL J>ROWS(STD);
$VAR1[1,J];;STD1[J,1];;SSTD[J,1];;STDR1[J,1];;SSTDR[J,1];;AU1[J,1];;SAU[J,1];
J=J+1;
ENDO;
" CONTEMPORARY CORRELATION
"X    Y   COR(X,Y) (Sd) ";
J=1;
DO UNTIL J>ROWS(COR1);
$VAR1[1,COR1[J,1]];;$VAR1[1,COR2[J,1]];;TCOR1[J,1];;SCOR[J,1];
J=J+1;
ENDO;
OUTPUT OFF;
CLEAR ALL;