% Basic RBC Model 
% Linearization in level
%
% 
%----------------------------------------------------------------
% 0. Housekeeping (close all graphic windows)
%----------------------------------------------------------------

close all;


%----------------------------------------------------------------
% 1. Defining variables & parameters;Calibration
%----------------------------------------------------------------

var ly lc lk li lh lw r lg theta;
varexo e_theta e_g;

parameters beta alpha gamma delta rho_theta rho_g;

beta    = 0.99;
alpha   = 0.3;
gamma   = 0.5;
delta   = 0.025;
rho_theta = 0.95;
rho_g   = 0.9;

%----------------------------------------------------------------
% 2. Model
%----------------------------------------------------------------

model; 

  %Production Function
  ly=theta+alpha*lk(-1)+(1-alpha)*lh;
  
  %Consumption Euler equation
   1=beta*exp(r(+1)+lc-lc(+1));

  %Leisure-Consumption Tradeoff
   lw-lc=(-gamma)*lh;

  %Capital Accumulation
  exp(lk-lk(-1)) =1-delta+exp(li-lk(-1));

  %Resource constraint
  exp(lc-ly)+exp(li-ly)+exp(lg-ly)=1;
  
  %Real Wage
   exp(lw)=(1-alpha)*exp(ly-lh) ;

  %Return to Capital
   exp(r)=alpha*exp(ly-lk(-1))+1-delta;

 %Technology Shock
  theta=rho_theta*theta(-1)+e_theta;

 %Government Spending Shock
  lg=rho_g*lg(-1)+e_g;

end;

%----------------------------------------------------------------
% 3. Initial Values & Shocks
%----------------------------------------------------------------

initval;
  ly = -1.4653;
  lc = -1.4758;
  lk = -2.3207;
  li = -6.0099;
  lh =  -1.0986;
  lw = -0.7234;
  r  = 0.0101;
  lg = 0; 
  theta = 0;
end;
steady;
check;

shocks;
var e_theta; periods 1:10;
values 0.01;
end;

%----------------------------------------------------------------
% 4. Computations
%----------------------------------------------------------------

simul(periods=200);

tt=1:202';

subplot(2,2,1);
plot(tt,oo_.endo_simul(5,:),tt,oo_.steady_state(5)*ones(1,202),'LineWidth',2);
title('r')