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

@ ------------------------------------------------------- @
@      THE THIRD PROGRAMME: SIMULATION OF 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];

@ ------------------------------------------------------- @
@         ORDERING OF VARIABLES                           @
@ ------------------------------------------------------- @

@  1   2   3
X  K   Z   G
   1   2
U  N   I  @

@ ------------------------------------------------------- @
@            WE INCLUDE A PROCEDURE WITH THE HP-FILTER    @
@ ------------------------------------------------------- @
UU=0;
#INCLUDE HP1;

NEXP=50;                           @ NUMBER OF REPLICATIONS @

@ STACK @;
STACK=50;

@ ------------------------------------------------------- @
@           SEEDING THE RANDOM NUMBER GENERATOR           @
@ ------------------------------------------------------- @
RNDSEED 7654321;

@ INITIALIZE MATRICES OF MOMENTS @
STD=ZEROS(9,NEXP);    @ STANDARD DEVIATION @
STDR=ZEROS(9,NEXP);   @ -------- ---------- RELATIVE TO Y @
AU=ZEROS(9,NEXP);     @ AUTOCORRELATIONER @
COR=ZEROS(10,NEXP);   @ CORRELATIONS @

@ VEKTOR MED VARIABLE NAVNE @
VAR1={ K Z G N I Y C APN R };           @ FOR STD,STDR,AU @
VAR2={ K Z G N I Y C APN R N-W };       @ FOR COR @

@ CORRELATIONS ARE COMPUTED COR1[I,1] BETWEEN COR2[I,1] @
COR1=1|2|3|4|5|6|7|8|9|4;
COR2=6|6|6|6|6|6|6|6|6|8;

Z=KSS|ZSS|GSS|NBAR|ISS;   @ A VECTOR WITH STEADY STATE VALUES @
 
 

LOADM FF; LOADM O;
G=zeros(4,2);
G[3,1]=1; G[4,2]=1;

FORMAT 10,10;

HOR=100;                       @ CHOOSE TIME HORIZON @

H1=HSEC;

XT=zeros(3,HOR); UT=zeros(2,HOR);

E=ZEROS(2,2);
E[1,1]=SIGMAZ;
E[2,2]=SIGMAG;

LOCATE 2,4;"SIMULATING DRAW 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|Z[1:3,1];

@ ------------------------------------------------------- @
@                      LET'S GO FOR IT                    @
@ ------------------------------------------------------- @
LL=E*RNDN(HOR+1,ROWS(E))';
 I=1;
 DO UNTIL I>HOR;
   L=LL[.,I+1];
   XX=O*X+G*L;
   U=-FF*X;
   XT[.,I]=X[2:4,1];
   UT[1:2,I]=U;
   X=XX;
   I=I+1;
 ENDO;

IVT=UT[2,.]';
NT=UT[1,.]';
ZT=XT[2,.]';
KT=XT[1,.]';
GOT=XT[3,.]';
YT=ZT.*(KT^SK).*(NT^SN);
CT=YT-GOT-IVT;
APNT=YT./NT;
WT=SN*ZT.*(KT^SK).*(NT^(SN-1));
RT=SK*ZT.*(KT^(SK-1)).*(NT^SN);

RDAT=KT~ZT~GOT~NT~IVT~YT~CT~APNT~RT;

DAT=LN(RDAT);

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;

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

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

@  STANDARD DEVIATIONS RELATIVE TO Y @
J=1;
SY=STD[6,STACK*(IT-1)+IT1];
SY=ONES(ROWS(STD),1)*SY;
STDR[.,STACK*(IT-1)+IT1]=STD[.,STACK*(IT-1)+IT1]./SY;

@ 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;
AU[.,STACK*(IT-1)+IT1]=DIAG(X4);

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

@ SELECT CORRELATIONS OF INTEREST @
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;

@  COMPUTE MEANS OF SIMULATED MOMENTS @
STD1=(MEANC(STD'));
SSTD=STDC(STD');
STDR1=MEANC(STDR');
SSTDR=STDC(STDR');
AU1=MEANC(AU');
SAU=STDC(AU');
COR1=MEANC(COR');
SCOR=STDC(COR');
 
 

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

FORMAT /LDN 4,4;

@ ------------------------------------------------------- @
@               PRINT OUT 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 DRAWS ";;NEXP;
"STD. OF Z:   ";;SIGMAZ;;"    STD. OF G";;SIGMAG;
" STANDARD DEVIATIONS AND AUTOCORRELATIONS";
"X    S(X)    (Sd)  S(X)/S(Y) (Sd) AUTOC (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;
" CONTEMPORANEOUS CORRELATION (WITH Y UNLESS OTHERWISE STATED)";
"X    COR(X,Y) (Sd) ";
J=1;
DO UNTIL J>ROWS(COR1);
$VAR2[1,J];;COR1[J,1];;SCOR[J,1];
J=J+1;
ENDO;
OUTPUT OFF;
CLEAR ALL;