%===============================================================================
% Variables
%===============================================================================

% Exogenous
varexo n g;

% Endogenous
var Y K L C I r w;

% Predetermined
predetermined_variables K;

%===============================================================================
% Parameters
%===============================================================================

parameters h delta alpha beta gamma;

h     = 52*100;  % page 430
delta = 0.0556;  % page 433
alpha = 0.3590;  % page 434
beta  = 0.9752;  % page 440
gamma = 0.2846;  % page 440

%===============================================================================
% Detrended Model
%===============================================================================

model;
    Y = (K^alpha)*(L^(1-alpha));             % Production function
    w = (1-alpha)*Y/L;                       % Wage rate
    r = alpha*Y/K;                           % Interest rate
    C + I = Y;                               % Market clearing
    g*n*K(+1) = (1-delta)*K + I;             % Law of motion of capital
    g*n*C(+1)/C = beta*(1 - delta + r(+1));  % Euler equation
    w*(h - L) = ((1-gamma)/gamma)*C;         % Consumption-labor choice
end;

%===============================================================================
% Steady State
%===============================================================================

steady_state_model;
    r   = g*n/beta - 1 + delta;                                   % Interest rate
    K_Y = alpha/r;                                                % Capital-output ratio
    I_Y = (g*n - 1 + delta)*K_Y;                                  % Investment-output ratio
    C_Y = 1 - I_Y;                                                % Consumption-output ratio
    L   = (gamma*(1-alpha)*h)/(gamma*(1-alpha) + (1-gamma)*C_Y);  % Hours worked
    K_L = K_Y^(1/(1-alpha));                                      % Capital-labour ratio
    K   = L*K_L;                                                  % Capital
    Y   = (K^alpha)*(L^(1-alpha));                                % Output
    w   = (1-alpha)*Y/L;                                          % Wage rate
    I   = Y*I_Y;                                                  % Investment
    C   = Y*C_Y;                                                  % Consumption
end;

%===============================================================================
% Initial Values
%===============================================================================

initval;
    Y = 2505.30;
    K = 7783.13;
    L = 1328;
    C = 1829.67;
    I = 675.63;
    r = 0.1156;
    w = 1.21;
    g = 1.091;
    n = 1.004;
end;

%===============================================================================
% End Values
%===============================================================================

endval;
    Y = 2600;
    K = 7800;
    L = 1300;
    C = 1800;
    I = 700;
    r = 0.1170;
    w = 1.2009;
    g = 1.032;
    n = 1.003;
end;

%steady;
%check;

%===============================================================================
% Shocks
%===============================================================================

shocks;
    var n;
    periods 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27;
    values 1.00436545057686 1.00434647624961 1.00680061823802 1.0076757752533 1.00700792199878 1.00635400907716 1.00390859891762 1.00119796346211 1.00089739754711 1 1.0002988643156 1.00268897520167 1.00417163289631 1.00445103857567 1.00324963072378 1.00235571260306 1.00176263219741 1.00205278592375 1.00292654375183 1.00437700612781 1.00377687391052 1.00260492040521 1.00230946882217 1.00172811059908 1.00143760782059 1.00172265288544 1.00286615075953;

    var g;
    periods 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27;
    values 1.09116353402821 1.04365693135481 0.980502894724348 1.02599228267448 1.03407099140656 1.02931708219605 1.03480245899774 1.04175413808796 1.03397240147961 1.05002190841857 1.0571676371169 0.998642918237822 0.951392922319088 1.00538612741725 1.04917212942913 1.06636622089763 1.04684771634426 1.04576486661429 1.06565328518167 1.06273518087553 1.0253361097682 1.0577582844929 1.02458614847298 1.00932694101192 1.02411258518633 1.0404907427124 1.0282866528155;
end;

%===============================================================================
% Simulation
%===============================================================================

simul(periods=100);

%===============================================================================
% Figures
%===============================================================================

subplot(3, 3, 1), plot(oo_.endo_simul(1, 1:27)), title('Y')
subplot(3, 3, 2), plot(oo_.endo_simul(2, 1:27)), title('K')
subplot(3, 3, 3), plot(oo_.endo_simul(3, 1:27)), title('L')
subplot(3, 3, 4), plot(oo_.endo_simul(4, 1:27)), title('C')
subplot(3, 3, 5), plot(oo_.endo_simul(5, 1:27)), title('I')
subplot(3, 3, 6), plot(oo_.endo_simul(6, 1:27)), title('r')
subplot(3, 3, 7), plot(oo_.endo_simul(7, 1:27)), title('w')
