@ =================================================================== @
@                       PROGRAMME BY MORTEN RAVN                      @
@ =================================================================== @
@ =================================================================== @
@                      THE KING, PLOSSER AND REBELO
                            SOLUTION ALGORITHM                        @
@ =================================================================== @

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

@ ORDERING OF VARIABLES:
  C: C(T) N(T)
  S: K(T) LAMBDA(T)
  E: A(T) @

NAMEC="C"|"N";
NAMEK="K";
NAMEL="LAMBDA";
NAMEE="A";

NC=2; NS=1; NN=1;

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

@ FACTOR SHARES (SK,SN) AND THE ELASTICITY OF SUBSTITUTION (ZETAKN).
  WE USE COBB-DOUGLAS SPECIFICATION @

SN=0.58; SK=1-SN;
ZETAKN=1.00;
XIKK=-SN*ZETAKN; XIKN= SN*ZETAKN;
XINN=-SK*ZETAKN; XINK= SK*ZETAKN;

@ GROWTH RATE OF TECHNICAL CHANGE AND DEPRECIATION @

GAMMAX=1.004; DELTAK=0.025;

@ DISCOUNT FACTOR AND REAL INTEREST RATE @
RR=0.065/4;
BSTAR=GAMMAX/(1+RR);

@ STEADY STATE SHARE OF TIME TO MARKET ACTIVITIES @

NBAR=0.200;

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

@ - THE TRANSFORMED ECONOMY @

@ THE RATIO PHI AND CAP-OUTPUT RATIO ARE @

PHI=GAMMAX/(GAMMAX-(1-DELTAK));
KYRATIO=(BSTAR*SK)/(GAMMAX-BSTAR*(1-DELTAK));

@ STEADY STATE CONSUMPTION AND INVESTMENT SHARES @

SI=KYRATIO*GAMMAX/PHI; SC=1-SI;

ABAR=10;
YSS=(ABAR^(1/SN))*(KYRATIO^(SK/SN))*NBAR;
CSS=SC*YSS;

@ PARAMETERS OF PREFERENCES
  SIGMA IS THE ABSOULTE VALUE OF THE ELASTICITY OF MARGINAL UTILITY
  OF CONSUMPTION @

SIGMA=1;

@ THETA WE DETERMINE SO THAT IT IS CONSISTENT WITH THE STEADY
  STATE NUMBER OF HOURS WORKED @

THETA=((SC*NBAR)/(SN*(1-NBAR)))/(1+(SC*NBAR)/(SN*(1-NBAR)));

@ FROM WHICH WE CAN GET THE ELASTICITIES OF PREFERENCES @
@ WE HAVE TWO MAIN POSSIBILITIES:
         SIGMA=1 - ADDITIVELY SEP.
         SIGMA><1 - MULTIPLICATIVELY SEP. @

XICC=THETA*(1-SIGMA)-1; XILC=THETA*(1-SIGMA);

@ THE OTHER TWO ELASTICITIES ARE FREE SO LONG AS XICL=0 IF SIGMA=1 @

XICL=(1-THETA)*(1-SIGMA); XILL=(1-THETA)*(1-SIGMA)-1;

@ STEADY STATE ELASTICITIES OF MARGINAL PRODUCT OF CAPITAL @

ETAA=(GAMMAX-BSTAR*(1-DELTAK))/GAMMAX;
ETAK=-SN*ETAA;
ETAN=SN*ETAA;
ETAI=BSTAR*DELTAK*SI*YSS;

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

@ MATRICES IN CONTROL SYSTEM @
@ THE CONTROLS ARE: C(t) N(t) @

MCC=ZEROS(NC,NC);
MCC[1,1]=XICC; MCC[1,2]=-XICL*NBAR/(1-NBAR);
MCC[2,1]=XILC; MCC[2,2]=-XILL*NBAR/(1-NBAR)-XINN;

MCS=ZEROS(NC,NS*2);
MCS[1,1]=0;    MCS[1,2]=1;
MCS[2,1]=XINK; MCS[2,2]=1;

MCE=ZEROS(NC,NN);
MCE[1,1]=0;
MCE[2,1]=1;

@ MATRICES IN STATE EQUATIONS @

MSS0=ZEROS(2*NS,2*NS);
MSS0[1,1]=ETAK;   MSS0[1,2]=1;
MSS0[2,1]=SI*PHI; MSS0[2,2]=0;

MSS1=ZEROS(2*NS,2*NS);
MSS1[1,1]=0;              MSS1[1,2]=-1;
MSS1[2,1]=-SK-SI*(PHI-1); MSS1[2,2]=0;

MSC0=ZEROS(2*NS,NC);
MSC0[1,1]=0; MSC0[1,2]=-ETAN;
MSC0[2,1]=0; MSC0[2,2]=0;

MSC1=ZEROS(2*NS,NC);
MSC1[1,1]=0;   MSC1[1,2]=0;
MSC1[2,1]=-SC; MSC1[2,2]=SN;

MSE0=ZEROS(2*NS,NN);
MSE0[1,1]=-ETAA;
MSE0[2,1]=0;

MSE1=ZEROS(2*NS,NN);
MSE1[1,1]=0;
MSE1[2,1]=1;

@ =============================================================== @
@   RELATIONS LINKING EXTRA FLOW VARIABLES (OUTPUT, PRODUCTIVITY
         AND INVESTMENT) TO FUNDAMENTAL CONTROLS AND STATES       @
@ =============================================================== @

@ THE ORDER OF VARIABLES IS:
  Y(T) AP(T) I(T) @

NAMEXC="Y"|"AP"|"I";
NXF=3;

@ 1. EXTRA FLOWS ON CONTROLS [C N]' @
FVC=ZEROS(3,NC);
FVC[1,1]=0;      FVC[1,2]=SN;
FVC[2,1]=0;      FVC[2,2]=SN-1;
FVC[3,1]=-SC/SI; FVC[3,2]=SN/SI;

@ 2. EXTRA FLOWS ON STATES AND SHOCKS [K A]' @
FVKE=ZEROS(3,NS+NN);
FVKE[1,1]=SK;    FVKE[1,2]=1;
FVKE[2,1]=SK;    FVKE[2,2]=1;
FVKE[3,1]=SK/SI; FVKE[3,2]=1/SI;

@ EXTRA FLOWS ON CO-STATES [LAMBDA] @
FVL=ZEROS(3,NS);

@ =============================================================== @
@         FUNDAMENTAL STATE-COSTATE DIFFERENCE EQUATION           @
@ =============================================================== @

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));

@ =============================================================== @
@                    COMPUTATION OF STEADY STATE                  @
@ =============================================================== @

@ THE ORDERING OF VARIABLES (DESCRIBED IN MDR.X1) IS:
  K  A  L  C  N  Y  AP C  @
 

YSS=(ABAR^(1/SN))*(KYRATIO^(SK/SN))*NBAR;
CSS=SC*YSS;
ISS=SI*YSS;
KSS=ISS/(GAMMAX-(1-DELTAK));
APSS=YSS/NBAR;
ASS=ABAR;
NSS=NBAR;
LSS=1;   @ THIS VALUE IS WRONG BUT WE ARE NOT INTERESTED IN
           THE EVOLUTION OF SHADOW PRICES @

@ STEADY STATE VALUES IN SSVAL @
SSVAL=KSS|ASS|LSS|CSS|NSS|YSS|APSS|ISS;
NAME=NAMEK|NAMEE|NAMEL|NAMEC|NAMEXC;