% ===================================================
% This code solves the basic RCK model
% We will solve the central planner problem, 
% ====================================================

% PREFACE: AS IN MATLAB
% As in Matlab this closes all previous figures (if any)
% shown on the screen
	close all;
% This clears the screen
	clc;
% This makes Matlab show a reduced number of decimal 
% positions
	format short;
% Delete previous text files (if any) containing the output
	delete('RCK_Ex.txt');
% Set where you want Matlab to start recording what 
% you want to be shown in the output file
	diary on;
% Name the output file
	diary('RCK_Ex.txt');
	
% =======================================================	
% FROM HERE ONWARDS, DYNARE CODE
% =======================================================

% Initial Steady State
display('Inital Steady State');
A = 1;
beta = 0.95;
delta = 0.02;
alpha = 0.25;
n = 0.0;
kss = ((alpha*A)/((1/beta) - (1 - delta)))^(1/(1 - alpha))
css = A*(kss^(alpha)) - kss*(n + delta)






% -------------------------------------------------------
% DECLARE ENDOGENOUS VARIABLES 
% WITHOUT TIME, AS IN THE STEADY STATE
% -------------------------------------------------------
% Number of variables: 5
    var  k,  c ; 

% -------------------------------------------------------
% LIST OF PARAMETERS 
% -------------------------------------------------------
  parameters beta, sigma, delta, alpha, n, A;
	beta = 0.95;
	sigma = 1.5;
	delta = 0.02;
	alpha = 0.25;
	n = 0.0;
	A = 1.5;

	
% Here we introuce the values of the variables in the 
% steady state  
kss = ((alpha*A)/((1/beta) - (1 - delta)))^(1/(1 - alpha));
css = A*(kss^(alpha)) - kss*(n + delta);
 

	
% -------------------------------------------------------
% MODEL DESCRIPTION
% -------------------------------------------------------
model;  
	 

	% i) Resource constraint
		A*(k(-1)^(alpha)) = k*(1 + n) - (1 - delta)*k(-1) + c;
	
	% ii) Euler equation
	    # aux = 1 - delta + alpha*A*(k^(alpha - 1));
		# u_1 = 1/c^(sigma);
		# u_2 = 1/c(+1)^(sigma);
		u_1 = beta*u_2*aux;
		
end;


% -------------------------------------------------------
% COMPUTING THE STEADY STATE
% -------------------------------------------------------
 
initval; 
	k = kss;
	c = css;
end;
steady (solve_algo = 0);



 
check;   


% -------------------------------------------------------
% COMPUTING THE DYNAMICS
% -------------------------------------------------------

% Where the economy starts: in our case kss. 
	initval; 
		% Initial Steady state
		k = kss;
	end;
	

% Here we ask Dynare to compute the simulated dynamics 
% for a time horizon of 90 periods
simul (stack_solve_algo=0, periods = 90);





 
	
	XY  = oo_.endo_simul';
	save RCK_Dynamics_Results   XY ;

 	


% -------------------------------------------------------
% GRAPHING THE RESULTS: 
% -------------------------------------------------------
vec_time = 0:1:95; % 95

 
	load RCK_Dynamics_Results ;
	A = 1;
	kss_0 = ((alpha*A)/((1/beta) - (1 - delta)))^(1/(1 - alpha)) 
	css_0 = A*(kss_0^(alpha)) - kss_0*(n + delta)
    vec_k = [kss_0*ones(11,1); XY(2:86,1)];
	vec_c = [css_0*ones(10,1); XY(1:86,2)];
	 
	
    aux_a = [vec_time', vec_k, vec_c]
	
	
	
	
 	

	figure;
	subplot(2,1,1);
	z = plot(vec_time, vec_k, 'b');
	set(z,'LineWidth',2.5);
	z = legend('Capital', 'Location', 'Best');
	set(z, 'FontSize', 12);
	vline(10);
	axis tight;
	ylabel('Stock of capital');
	
	 
		title('RCK Model: Unexpected change in TFP', 'FontSize', 12);
	 
	
	subplot(2,1,2);
	z = plot(vec_time, vec_c, 'r');
	set(z,'LineWidth',2.5);
	z = legend('Consumption', 'Location', 'Best');
	set(z, 'FontSize', 12);
	axis tight;
	vline(10);
	ylabel('Consumption');
	xlabel('Time', 'FontSize', 12);
	
	 
		print -depsc2 -tiff RCK_ProblemSet_1.eps;
	 

 
	
dynatype (RCK_Dynamics_OUTPUT_txt);
diary off;


