//DEL NEGRO et al. 2015 Replication with both sticky and flexible price output in Dynare 

// DEL NEGRO, M., M. GIANNONI & F. SCHORFHEIDE 2015.: Inlfation in the Great Recession and New Keynesian Models,
// American Economic Journal: Macroeconomics
// 2015, 7(1): 168-196.

// This file simulates the dynamic response of the model to specific shocks

// Replication of IRF to monetary policy shock (one standard deviation)
// Replication of IRF to productivity shock (one standard deviation)
//------------------------------------------------------------------------------------------------------------------------
//1. Variable declaration
//------------------------------------------------------------------------------------------------------------------------

var c R pi L qk i Rktil rk kbar n y k u mc w wh z ztil mu sigw laf law rm g b pist

//% the flexble-price counterparts
c_f r_f L_f qk_f i_f rk_f y_f k_f u_f kbar_f w_f;

varexo psi_g psi_b psi_mu psi_z psi_laf psi_law psi_rm psi_sigw psi_pist;


//------------------------------------------------------------------------------------------------------------------------
// 2. Parameter declaration and calibration
//-------------------------------------------------------------------------------------------------------------------------

parameters alp ups zeta_p iota_p del Bigphi s2 h ppsi nu_l zeta_w iota_w bet bet_hat psi1 psi2 psi3 sigmac gam rho epsp epsw
    gstar rho_g rho_b rho_mu rho_z rho_laf rho_law rho_rm rho_sigw rho_pist sprd eta_gz eta_laf eta_law rkstar wl_c
    cstar wstar Lstar kstar kbarstar istar ystar zeta_spb gammstar vstar nstar zeta_nRk zeta_nR zeta_nsigw zeta_spsigw
    zeta_nqk zeta_nn;

alp = 0.179;
ups = 1;
zeta_p = 0.868;
iota_p = 0.226;
del = .025;
Bigphi = 0.1526;
s2 = 3.044;
h = 0.244;
ppsi = 0.188;
nu_l = 2.673;
zeta_w = 0.888;
iota_w = 0.419;
//law = 1.5;
//laf = [];
gam = 0.401;
sigmac = 1.316;
bet  = 1/(1+0.1331/100);
bet_hat = bet*exp((1-sigmac)*gam);
psi1 = 1.374;
psi2 = 0.018;
psi3 = 0.240;
rho = 0.675;
epsp = 10;
epsw = 10;

//Financial Frictions Parameters
zeta_spb = 0.0443;
gammstar = 0.990;
sprd = (1+1.9081/100)^(1/4);
vstar = 2.4708;
nstar = 2.4492;
zeta_nRk = 1.6938;
zeta_nR = 0.6930;
zeta_nsigw = 0.0043;
zeta_spsigw = 0.0276;
zeta_nqk = 0.0021;
zeta_nn = 0.9987;

//exogenous processes - level
gstar = .18;

//exogenous processes - autocorrelation

rho_g = 0.979;
rho_b = 0.944;
rho_mu = 0.644;
rho_z = 0.956;
rho_laf = 0.794;
rho_law = 0.661;
rho_rm = 0.067;

rho_sigw = 0.990;
rho_pist = 0.990;

//exogenous processes
eta_gz = 0.874;
eta_laf = 0.714;
eta_law = 0.572;

//Parameters (implicit) -- from steady state

rkstar = 0.0363884170545733;
wstar = (alp^(alp)*(1-alp)^(1-alp)*rkstar^(-alp)/Bigphi)^(1/(1-alp));
Lstar = 1;
kstar = (alp/(1-alp))*wstar*Lstar/rkstar;
kbarstar = 4.1654;
istar = 0.1204;
ystar = (kstar^alp)*(Lstar^(1-alp))/Bigphi;
cstar = 0.5725;

wl_c = 0.8081;  
  
//-----------------------------------------------------------------------------------------------------------------------
// 3. The model
//-----------------------------------------------------------------------------------------------------------------------


model (linear);

//consumption Euler equation
c = -(1-h*exp(-gam))/(sigmac*(1+h*exp(-gam)))*(R-pi(+1)+b)+(h*exp(-gam))/(1+h*exp(-gam))*(c(-1)-z)
    +(1/(1+h*exp(-gam)))*(c(+1)+z(+1))+(sigmac-1)*wl_c/(sigmac*(1+h*exp(-gam)))*(L-L(+1));

//investment Euler equation
qk = (s2*exp(2*gam)*(1+bet_hat))*(i-(1/(1+bet_hat))*(i(-1)-z)
     -(bet_hat/(1+bet_hat))*(i(+1)+z(+1))-mu);

//evolution of capital
kbar = (1-istar/kbarstar)*(kbar(-1)-z)+(istar/kbarstar)*i+istar*s2*exp(2*gam)*(1+bet_hat)*mu;

//capital utilization
k = u-z+kbar(-1);

//rate of utilization (rental rate of capital)
u = ((1-ppsi)/ppsi)*rk;

//price markup
mc = w+alp*L-alp*k;

//rental rate of capital
k = w-rk+L;

//aggregate production function
y = Bigphi*alp*k+Bigphi*(1-alp)*L+((Bigphi-1)/(1-alp))*ztil;

//ressource constraint
y = g+cstar/ystar*c+istar/ystar*i+rkstar*kstar/ystar*u-(1/(1-alp))*ztil;

//Phillips curve
pi = ((1-zeta_p*bet_hat)*(1-zeta_p))/((1+iota_p*bet_hat)*zeta_p*((Bigphi-1)*epsp+1))*mc
    +iota_p*1/(1+iota_p*bet_hat)*pi(-1)+bet_hat*1/(1+iota_p*bet_hat)*pi(+1)+laf;

//evolution of wages
w = ((1-zeta_w*bet_hat)*(1-zeta_w))/((1+bet_hat)*zeta_w*((law-1)*epsw+1))*(wh-w)
    -(1+iota_w*bet_hat)*1/(1+bet_hat)*pi
    +(1/(1+bet_hat))*(w(-1)-z+iota_w*pi(-1))
    +(bet_hat/(1+bet_hat))*(w(+1)+z(+1)+pi(+1))+law;

//marginal rate of substitution
wh = (1/(1-h*exp(-gam)))*(c-h*exp(-gam)*c(-1)+h*exp(-gam)*z)+nu_l*L;

//monetary policy rule
R = rho*R(-1)+(1-rho)*psi1*(pi-pist)+(1-rho)*psi2*(y-y_f)+psi3*((y-y_f)-(y(-1)-y_f(-1)))+rm;

//Financial Frictions//
//return to capital
Rktil = pi+rkstar/(rkstar+1-del)*rk+(1-del)/(rkstar+1-del)*qk-qk(-1);
//spreads
Rktil(+1) = R+b+zeta_spb*(qk+kbar-n)+sigw;
//evolution of net worth
n = zeta_nRk*(Rktil-pi)-zeta_nR*(R(-1)-pi)+zeta_nqk*(qk(-1)+kbar(-1))+zeta_nn*n(-1)-zeta_nsigw/zeta_spsigw*sigw(-1)
    -gammstar*vstar/nstar*z;

//exogenous processes//

z = (1/(1-alp))*(rho_z-1)*ztil(-1)+(1/(1-alp))*psi_z;
ztil = rho_z*ztil(-1)+psi_z;

g = rho_g*g(-1)+psi_g+eta_gz*psi_z;

b = rho_b*b(-1)+psi_b;

mu = rho_mu*mu(-1)+psi_mu;

laf = rho_laf*laf(-1)+psi_laf-eta_laf*psi_laf(-1);

law = rho_law*law(-1)+psi_law-eta_law*psi_law(-1);

rm = rho_rm*rm(-1) + psi_rm;

sigw = rho_sigw*sigw(-1)+psi_sigw;

pist = rho_pist*pist(-1)+psi_pist;

// now the flexible-price equations//

c_f = -(1-h*exp(-gam))/(sigmac*(1+h*exp(-gam)))*(r_f+b)+(h*exp(-gam))/(1+h*exp(-gam))*(c_f(-1)-z)
    +(1/(1+h*exp(-gam)))*(c_f(+1)+z(+1))+(sigmac-1)*wl_c/(sigmac*(1+h*exp(-gam)))*(L_f-L_f(+1));

qk_f = (s2*exp(2*gam)*(1+bet_hat))*(i-(1/(1+bet_hat))*(i_f(-1)-z)-(bet_hat/(1+bet_hat))*(i_f(+1)+z(+1))-mu);

kbar_f = (1-istar/kbarstar)*(kbar_f(-1)-z)+(istar/kbarstar)*i_f+istar*s2*exp(2*gam)*(1+bet_hat)*mu;

k_f = u_f-z+kbar(-1);

u_f = ((1-ppsi)/ppsi)*rk_f;

w_f = - alp*L_f+alp*k_f;

k_f = w_f-rk_f+L_f;

y_f = Bigphi*alp*k_f+Bigphi*(1-alp)*L_f+((Bigphi-1)/(1-alp))*ztil;

y_f = g+cstar/ystar*c_f+istar/ystar*i_f+rkstar*kstar/ystar*u_f-(1/(1-alp))*ztil;

w_f = (1/(1-h*exp(-gam)))*(c_f-h*exp(-gam)*c_f(-1)+h*exp(-gam)*z)+nu_l*L_f;

//assume no financial frictions in the flex-price economy (i.e. arbitrage condition)

qk_f = rkstar/(rkstar+1-del)*rk_f(+1)+(1-del)/(rkstar+1-del)*qk_f(+1)-r_f+(sigmac*(1+h*exp(-gam)))/(1-h*exp(-gam))*b;

end;

//--------------------------------------------------------------------------------------------------------------------------
// 4. Steady state
//---------------------------------------------------------------------------------------------------------------------------

steady;
check;

//---------------------------------------------------------------------------------------------------------------------------
// 5. shocks
//---------------------------------------------------------------------------------------------------------------------------

shocks;

var psi_g; stderr 2.908;
var psi_b; stderr 0.038;
var psi_mu; stderr 0.503;
var psi_z; stderr 0.496;
var psi_laf; stderr 0.154;
var psi_law; stderr 0.257;
var psi_rm; stderr 0.292;

var psi_sigw; stderr 0.057;
var psi_pist; stderr 0.030;

end;

//write_latex_dynamic_model;
stoch_simul(irf=20) R pi L y; 

