@ =================================================================== @
@                         PROGRAM BY MORTEN RAVN                      @
@ =================================================================== @
 

@ DIMENSION OF CONTROL SPACE (NC), STATE AND COSTATE VECTORS (NS),
  EXOGENEOUS STATE VECTOR (NN) @

@ ORDERING OF VARIABLES: @
/* write here the ordering of the variables: first endogenous states,
   second exogenous states, third controls, fourth extra controls
   where extra controls are controls that do not enter the maximization
   procedure */

@ DIMENSION OF CONTROLS, STATES AND COSTATES, AND SHOCKS (EXOG. STATES) @
NC=;
NS=;
NN=;

@ FILL IN THE NAMES IN THE FOLLOWING VECTORS @
NAMEC=;   @ CONTROLS @
NAMEK=;   @ ENDOGENOUS STATES @
NAMEL=;   @ COSTATES @
NAMEE=;   @ EXOGENOUS STATES (SHOCKS) @
 

@ =================================================================== @
@                       ECONOMIC PARAMETER VALUES                     @
@ =================================================================== @

/* FILL IN WITH CHOICE OF PARAMETER VALUES */

@ ==================================================================== @
@                        STEADY STATE CALCULATIONS                     @
@ ==================================================================== @

/* 1. DERIVE GREAT RATIOS */

/* 2. DERIVE THE LEVELS OF THE VARIABLES */

/* 3. FILL IN THE STEADY STATE VALUES IN THE VECTOR SSVAL. THE
   ORDERING IS AS LISTED IN THE TOP OF THE PROGRAM */
SSVAL=;

@ ============================================================= @
@                     BASIC SYSTEM MATRICES                     @
@ ============================================================= @

@ MATRICES IN CONTROL SYSTEM: MCC*C(t)=MCS*S(t)+MCE*E(t) @

@ THE CONTROLS ARE: @
 

@ MCC LINKS CONTROLS TO CONTROLS @
MCC=ZEROS(NC,NC);
/* FILL THE MATRIX */
 

@ MCS LINKS CONTROLS TO ENDOGENOUS STATES AND COSTATES @
MCS=ZEROS(NC,NS*2);
/* FILL THE MATRIX */
 

@ MCE LINKS CONTROLS TO EXOGENOUS STATES @
MCE=ZEROS(NC,NN);
/* FILL THE MATRIX */
 

@ MATRICES IN STATE EQUATIONS
  MSS0*E(S(t+1))+MSS1*S(t)=MSC0*E(C(t+1))+MSC1*C(t)+MSE0*E(E(t+1))+MSE1*E(t)@
 

@ MSS0 LINKS STATES AND COSTATES TO CONTEMPORANEOUS STATES
  AND COSTATES @
MSS0=ZEROS(2*NS,2*NS);
/* FILL THE MATRIX */
 

@ MSS1 LINKS STATES AND COSTATES TO LAGGED STATES AND COSTATES @
MSS1=ZEROS(2*NS,2*NS);
/* FILL THE MATRIX */
 

@ MSC0 LINKS STATES AND COSTATES TO CONTEMPORANEOUS CONTROLS @
MSC0=ZEROS(2*NS,NC);
/* FILL THE MATRIX */
 

@ MSC0 LINKS STATES AND COSTATES TO LAGGED CONTROLS @
MSC1=ZEROS(2*NS,NC);
/* FILL THE MATRIX */
 

@ MSC0 LINKS STATES AND COSTATES TO CONTEMPORANEOUS EXOGENOUS STATES @
MSE0=ZEROS(2*NS,NN);
/* FILL THE MATRIX */

@ MSC0 LINKS STATES AND COSTATES TO LAGGED EXOGENOUS STATES @
MSE1=ZEROS(2*NS,NN);
/* FILL THE MATRIX */
 

@ =============================================================== @
@   RELATIONS LINKING FLOW VARIABLES (EXTRA CONTROLS) TO
              FUNDAMENTAL CONTROLS AND STATES                     @
@ =============================================================== @

@ THE ORDER OF VARIABLES IS: @

NX=;   @ DIMENSION OF EXTRA CONTROLS @
NAMEXC=; @ NAMES OF EXTRA CONTROLS @

@ FVC LINKS EXTRA CONTROLS TO FUNDAMENTAL CONTROLS @
FVC=ZEROS(NX,NC);
/* FILL THE MATRIX */

@ FVC LINKS EXTRA CONTROLS TO ENDOGENOUS STATES AND EXOGENOUS STATES @
FVKE=ZEROS(NX,NS+NN);
/* FILL THE MATRIX */

@ FVL LINKS EXTRA CONTROLS TO COSTATES @
FVL=ZEROS(NX,NS);
/* FILL THE MATRIX */

@ =============================================================== @
@         FUNDAMENTAL STATE-COSTATE DIFFERENCE EQUATION           @
@             NOTHING SHOULD BE CHANGED FROM HERE ON              @
@ =============================================================== @

MSss0 = MSS0 - MSC0*(INV(MCC))*MCS;
MSss1 = MSS1 - MSC1*(INV(MCC))*MCS;
MSse0 = MSE0 + MSC0*(INV(MCC))*MCE;
MSse1 = MSE1 + MSC1*(INV(MCC))*MCE;

@ THE FUNDAMENTAL DIFFERENCE EQUATION IS PUT IN NORMAL FORM @

W = -(INV(MSss0))*MSss1;
R = (INV(MSss0))*MSse0;
Q = (INV(MSss0))*MSse1;

@ =============================================================== @
@ EIGENVECTOR-EIGENVALUE DECOMPOSITION OF STATE TRANSITION MATRIX @
@ =============================================================== @

@ FIRST WE FIND THE REAL PARTS OF THE EIGENVALUES (X1)
  AND EIGENVECTORS (X3) @

{X1,X2,X3,X4}=EIGRG2(W);

@ SECONDLY WE FIND THE INDICATOR (IND1) OF THE ORDER OF THE MAXIMUM
  ABSOLUTE EIGENVALUES (AMU) @

AMU=ABS(X1);
IND1=SORTIND(AMU);

@ THIRDLY WE ORDER THE COLUMNS OF THE EIGENVECTORS (X3) BY THIS
  INDICATOR RESULTING IN P @

P=ZEROS(2*NS,2*NS);
I=1;
DO UNTIL I>2*NS;
P[1:2*NS,I]=X3[1:2*NS,IND1[I,1]];
I=I+1;
ENDO;

@ FINALLY WE FORM A DIAGONAL MATRIX (MU) IN WHICH THE DIAGONAL HAVE
  THE EIGENVALUES IN ASCENDING ABSOLUTE VALUE, I.E. WE USE THE
  INDICATOR FUNCTION IND1 AGAIN @

MU=ZEROS(2*NS,2*NS);
I=1;
DO UNTIL I>2*NS;
MU[I,I]=X1[IND1[I,1],1];
I=I+1;
ENDO;

@ WE NOW HAVE P AND MU FOR WHICH WE KNOW THAT P*MU*P^-1=W (ALSO
  X3*DIAG(X1)*X3=W) @

@ =============================================================== @
@                    PARTITIONING THE MATRICES                    @
@ =============================================================== @

MU1=MU[1:NS,1:NS];
MU2=MU[NS+1:2*NS,NS+1:2*NS];

P11=P[1:NS,1:NS];
P12=P[1:NS,NS+1:2*NS];
P21=P[NS+1:2*NS,1:NS];
P22=P[NS+1:2*NS,NS+1:2*NS];

PS=INV(P);
PS11=PS[1:NS,1:NS];
PS12=PS[1:NS,NS+1:2*NS];
PS21=PS[NS+1:2*NS,1:NS];
PS22=PS[NS+1:2*NS,NS+1:2*NS];

RKE=R[1:NS,1:NN];
RLE=R[NS+1:2*NS,1:NN];
QKE=Q[1:NS,1:NN];
QLE=Q[NS+1:2*NS,1:NN];

@ =============================================================== @
@                       COMPOSITE EXPRESSIONS                     @
@ =============================================================== @

SP1=-(INV(MU2))*(PS21*RKE+PS22*RLE);
SP2=-(INV(MU2))*(PS21*QKE+PS22*QLE);

KLK=P11*MU1*(INV(P11));
KTL=(P11*MU1*PS12+P12*MU2*PS22)*(INV(PS22));
 

NAME=NAMEK|NAMEE|NAMEL|NAMEC|NAMEXC;