%%  An New ISLM with an interest rate shocks %% 
%%  By Ryo Kato May, 2003 
%%  This code is written by Ryo Kato (kato.13@osu.edu)
%%  You can use/rewrite this code without my permission.
%%  I will not be responsible for any damage/liability 
%%  induced by running this conde.

clear all;

%% ------------------- [1] Parameter proc  ------------------------
  sigma = 1.5;   % CRRA
  alpha = 0.5;   % Cob-Dag
  beta = 0.99;   
  a = 0.5 ; 
  b = 1.5 ;   
  phi = 0.8;     % AR(1) in tech


%% --------------------- [2] Steady State proc >> -----------------------
 % SS capital & ss labor
 xstar = 0;
 pistar = 0;
 istar = 0;
 vstar = 0;
 
  
%% --------------------------[2] MODEL proc-----------------------------%%  
   % Define endogenous vars  ('a' denotes t+1 values)
       syms  xt   pit  vt  xa   pia     va  ; 
   % Eliminate Taylor Rule  
       it = a*xt + b*pit +vt ;
   % Optimal Conditions  & state transition
       IS   = xa - sigma*(it - pia) - xt ;       % LS = LD 
       AS   = beta*pia + alpha*xt - pit 	  ;     % C-Euler
       tech    = va - phi*vt;        
       
       optcon  = [IS ; AS  ;  tech];           
%% ------------------  [3] Linearization proc  ------------------------%%     
  % Differentiation 
     xx = [xa  pia  va  xt  pit  vt] ; % define vars
     jopt = jacobian(optcon,xx);

   % Evaluate each derivative
     xt = xstar;     xa=xstar;
     pit = pistar;     pia = pistar;
     vt = vstar;   va = vstar;
     
   % Define Linear Coefficients  
     coef = eval(jopt);
   % In terms of % deviations from ss    
   % vo = [ xstar   pistar  vstar  ] ;
   % TW = [ vo ; vo  ; vo ] ;
 
    B =  [ -coef(:,1:3)  ] ;
    C =  [  coef(:,4:6)  ] ;
 % B[c(t+1)  l(t+1)  k(t+1)  z(t+1)] = C[c(t)  l(t)  k(t)  z(t)]
    A = inv(C)*B  %(Linearized reduced form )

%% =========== [4] Solution proc (Do NOT touch!!) ============== %%
%  EIGEN DECOMPOSITION
   [W V] = eig(A);
   Q = inv(W);
   W*V*Q
   theta = diag(V)
  % Extract stable vectors
      SQ = [];    jw = 1;
      for j = 1:length(theta)
      	if abs(theta(j)) >1.000000001
         	SQ(jw,:) = Q(j,:);
         	jw = jw+1;
      	end
      end
   % Extract unstable vectors
     UQ = [];   jjw = 1;
      for jj = 1:length(theta)
      	if abs(theta(jj))<0.9999999999
         	UQ(jjw,:) = Q(jj,:);
         	jjw = jjw+1;
      	end
      end
    % Extract stable roots 
      VLL = [];    jjjw = 1;
      for jjj = 1:length(theta)
      	if abs(theta(jjj)) >1.0000000001
            VLL(jjjw,:) = theta(jjj,:); 
            jjjw = jjjw+1;
      	end
      end
      
   % Show Eigen Vectors on U-S Roots  
     UQ;  % n x n+k
     SQ;  % k x n+k 
     
  % [3] ELIMINATING UNSTABLE VECTORS
      k = min(size(SQ));   % # of predetermined vars
      n = min(size(UQ));   % # of jump vars
      nk = [n k]
   % Stable V (eig mat)
   VL = inv(diag(VLL))
   
   % Elements in Q
    PA = UQ(1:n,1:n);    PB = UQ(1:n,n+1:n+k);
    PC = SQ(1:k,1:n);    PD = SQ(1:k,n+1:n+k);
    P = -inv(PA)*PB ; % X(t) = P*S(t)
    PE = PC*P+PD ;
   
   % SOLUTION
    PX = inv(PE)*VL*PE;
    AA = real(PX)   

    
%% ------------------ [5]  SIMULATION proc  ----------------- %%    
% [4] TIME&INITIAL VALUES 

     t = 24;			% Time span   
    
   % Initial Values 
   % state var + e
   S1 = [  0.1 ]' ;

    
% [5] SIMULATION 
      Ss = S1;
      S = zeros(t,k) ;  
        for i = 1:t
          q = AA*Ss  ;
   	      S(i,:) = q';
          Ss = S(i,:)';
     	end
      SY = [S1' ;S]  ; 
      X = (real(P)*SY')';
      
% [6] DRAWING FIGURES
   figure(1)
     subplot(2,1,1);
       plot( X );
       ylabel('X, PI')
       xlabel('time');
     subplot(2, 1,2);
       plot( SY );
       ylabel('SK')
       xlabel('time');
     







