% "Econ 605 - Pset IV"
% Maximum likelihood estimation of parameters
%
% Code by Diego Vilán

%----------------------------------------------------------------
% 0. Housekeeping:
%----------------------------------------------------------------

close all;
clc;

%----------------------------------------------------------------
% 1. Define variables:
%----------------------------------------------------------------

var y, c, k, n, z, G, eta;
varexo eeta, ez, eg;

parameters P, beta, delta, theta, k_bar, y_bar, c_bar, n_bar, G_bar, eta_bar, rhoeta, rhoG, rhoz;


%----------------------------------------------------------------
% 2. Calibration - Deep parameters:
%----------------------------------------------------------------

theta = 0.36;
beta  = 0.99;
delta = 0.025;
n_bar = 0.3;
rhoz = 0.9;
rhoG = 0.9;
rhoeta = 0.9;


%----------------------------------------------------------------
% 3. Calibration - Parameters:
%----------------------------------------------------------------

P = (1/theta*(1/beta - (1-delta)))^(1/(theta-1));
k_bar = P*n_bar;
y_bar = k_bar^theta*n_bar^(1-theta);
G_bar = 0.17*y_bar;
c_bar = y_bar - delta*k_bar - G_bar;
eta_bar = (1-theta)*(1-n_bar)*(k_bar/n_bar)^theta/c_bar;


%----------------------------------------------------------------
% 4. The Model:
%----------------------------------------------------------------

model; 

//----------Equation 20----------//
1 = beta*(c/c(+1))*(theta*exp(z(-1))*(k/n(+1))^(theta-1)+(1-delta));

//----------Equation 21----------//
(1-theta)*exp(z(-1))*((k(-1)/n)^theta) = eta(-1)*c/(1-n);

//----------Equation 22----------//
y = c + k - (1-delta)*k(-1) + G(-1);

//----------Equation 23----------//
z = rhoz*z(-1) + ez;

//----------Equation 24----------//
G = (1-rhoG)*G_bar + rhoG*G(-1) + eg;

//----------Equation 25----------//
eta = (1-rhoeta)*eta_bar + rhoeta*eta(-1) + eeta;

//----------Equation 26----------//
y = exp(z(-1))*k(-1)^theta*n^(1-theta);

end;



%----------------------------------------------------------------
% 5. Computation:
%----------------------------------------------------------------

initval;
c = c_bar;
k = k_bar;
y = y_bar;
eta = eta_bar;
G = G_bar;
n = n_bar;
end;

steady;
check;



%----------------------------------------------------------------
% 6. Simulation Results:
%----------------------------------------------------------------

estimated_params;
   rhoeta, 0.5, 0.0001, 0.9999;
   rhoG, 0.5, 0.0001, 0.9999;
   rhoz, 0.5, 0.0001, 0.9999;
   stderr ez, 0.001, 0.0001, 0.2;
   stderr eg, 0.001, 0.0001, 0.2; 
   stderr eeta, 0.001, 0.0001, 0.2; 
end;


// Names of variables observed in the estimation:
// Note there are three shocks and three observables.
varobs n, c, y;

estimation(datafile=pset4,xls_sheet=Sheet1,xls_range = B1:D251, conf_sig = 0.95, first_obs=2, nobs=249, mode_check, mode_compute=1);

// NOTE: Arguments in the call to the Dynare command, estimation.
// datafile = pset4, calls the data xls file 
// conf_sig = xx, xx defines width of confidence intervals (e.g., xx = .95 means '95 percent confidence interval')
// first_obs = xx, xx is the first observation in the data vectors produced by datapullcgg, which is used in estimation (xx must be an actual number, not a pre-defined variable)
// nobs = yy, Dynare uses observations t=xx to t=yy-1 in estimation (yy must be an actual number)
// mode_check, activates a graph which plots the posterior mode against the criterion. This graph provides visual confirmation that the function of interest in fact has been optimized
// mode_compute=xx, xx selects one of several possible optimization routines to use in optimization




